Skip to content

Instantly share code, notes, and snippets.

@jleinonen
Created June 14, 2013 12:02
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jleinonen/5781308 to your computer and use it in GitHub Desktop.
Save jleinonen/5781308 to your computer and use it in GitHub Desktop.
Use a GDAL memory-mapped file to open an image retrieved via HTTP directly as a NumPy array without saving to a temporary file.
from gzip import GzipFile
from io import BytesIO
import urllib2
from uuid import uuid4
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
def open_image(url):
image_data = open_http_query(url)
if not image_data:
return None
mmap_name = "/vsimem/"+uuid4().get_hex()
gdal.FileFromMemBuffer(mmap_name, image_data.read())
gdal_dataset = gdal.Open(mmap_name)
image = gdal_dataset.GetRasterBand(1).ReadAsArray()
gdal_dataset = None
gdal.Unlink(mmap_name)
return image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment