How to open a remote file with GDAL in Python through a Flask application
Please try the follow code snippet:
from gzip import GzipFilefrom io import BytesIOimport urllib2from uuid import uuid4from gdalconst import GA_ReadOnlyimport gdaldef 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 Noneurl = '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.Refer to https://gist.github.com/jleinonen/5781308 for more info.