简体   繁体   English

如何通过Flask应用程序在Python中使用GDAL打开远程文件

[英]How to open a remote file with GDAL in Python through a Flask application

So, I'm developing a Flask application which uses the GDAL library, where I want to stream a .tif file through an url. 因此,我正在开发一个使用GDAL库的Flask应用程序,我想在其中通过URL流式传输.tif文件。

Right now I have method that reads a .tif file using gdal.Open(filepath). 现在,我有使用gdal.Open(filepath)读取.tif文件的方法。 When run outside of the Flask environment (like in a Python console), it works fine by both specifying the filepath to a local file and a url. 在Flask环境之外运行时(例如在Python控制台中),通过指定本地文件的文件路径和url都可以正常工作。

from gdalconst import GA_ReadOnly
import gdal
filename = 'http://xxxxxxx.blob.core.windows.net/dsm/DSM_1km_6349_614.tif'
dataset = gdal.Open(filename, GA_ReadOnly )
if dataset is not None:
    print 'Driver: ', dataset.GetDriver().ShortName,'/', \
      dataset.GetDriver().LongName

However, when the following code is executed inside the Flask environement, I get the following message: ERROR 4: ` http://xxxxxxx.blob.core.windows.net/dsm/DSM_1km_6349_614.tif ' does not exist in the file system, and is not recognised as a supported dataset name. 但是,在Flask环境中执行以下代码时,出现以下消息:错误4:` http : //xxxxxxx.blob.core.windows.net/dsm/DSM_1km_6349_614.tif '在文件系统中不存在。 ,并且不能识别为受支持的数据集名称。

If I instead download the file to the local filesystem of the Flask app, and insert the path to the file, like this: 如果我改为将文件下载到Flask应用程序的本地文件系统,然后插入文件路径,如下所示:

block_blob_service = get_blobservice() #Initialize block service
block_blob_service.get_blob_to_path('dsm', blobname, filename) # Get blob to local filesystem, path to file saved in filename
dataset = gdal.Open(filename, GA_ReadOnly)

That works just fine... The thing is, since I'm requesting some big files (200 mb), I want to stream the files using the url instead of the local file reference. 那工作得很好...事情是,因为我要一些大文件(200 mb),所以我想使用url而不是本地文件引用来流式传输文件。

Does anyone have an idea of what could be causing this? 有谁知道这可能是什么原因? I also tried putting "/vsicurl_streaming/" in front of the url as suggested elsewhere. 我还尝试将“ / vsicurl_streaming /”放在URL的前面,如在其他地方建议的那样。

I'm using Python 2.7, 32-bit with GDAL 2.0.2 我正在使用32位Python 2.7和GDAL 2.0.2

Please try the follow code snippet: 请尝试以下代码片段:

from gzip import GzipFile
from io import BytesIO
import urllib2
from uuid import uuid4
from gdalconst import GA_ReadOnly
import gdal

def open_http_query(url):
    try:
        request = urllib2.Request(url, 
            headers={"Accept-Encoding": "gzip"})
        response = urllib2.urlopen(request, timeout=30)
        if response.info().get('Content-Encoding') == 'gzip':
            return GzipFile(fileobj=BytesIO(response.read()))
        else:
            return response
    except urllib2.URLError:
        return None


url = 'http://xxx.blob.core.windows.net/container/example.tif'
image_data = open_http_query(url)
mmap_name = "/vsimem/"+uuid4().get_hex()
gdal.FileFromMemBuffer(mmap_name, image_data.read())
dataset = gdal.Open(mmap_name)
if dataset is not None:
    print 'Driver: ', dataset.GetDriver().ShortName,'/', \
      dataset.GetDriver().LongName

Which use a GDAL memory-mapped file to open an image retrieved via HTTP directly as a NumPy array without saving to a temporary file. 它们使用GDAL内存映射文件打开通过HTTP直接作为NumPy数组检索的图像,而不保存到临时文件。 Refer to https://gist.github.com/jleinonen/5781308 for more info. 有关更多信息,请参阅https://gist.github.com/jleinonen/5781308

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM