[GIS] In Python, reading a GDAL raster from memory instead of a file

gdalpostgispython

I'd like to analyze some raster data in Python using NumPy.

This data doesn't necessarily exist as a file on the filesystem, it could be the result of a query against PostGIS, or something transferred over the network.

Is it possible to get GDAL to read data from a buffer in memory, instead of requiring a filename?

I realize one solution could be to open up a named temp file, write the data to that, and pass it as a filename to GDAL, but that seems inefficient, and more likely to incur I/O bottlenecks. Additionally, in the case of PostGIS, it would be nice to read the native format directly, as opposed to converting it to some other file format and reading that.

Edit: to clear up a bit of confusion, just using the PostGIS driver directly won't work either, since the query could be for a raster analysis, rather than just data stored in the DB.

Best Answer

If you have your own bytes, and need a GDAL Raster object, just use the MEM driver. No files required.

from osgeo import gdal
import numpy as np
driver = gdal.GetDriverByName('MEM')
src_ds = driver.Create('', 100, 200, 1)
band = src_ds.GetRasterBand(1)
# Create random data array to put into the raster object
ar = np.random.randint(0, 255, (200, 100))
band.WriteArray(ar)