Created
June 14, 2013 12:02
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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