Create sparse raster with Rasterio. Is this possible?


Hi All -

I have benefited greatly from Rasterio - and first want to say thanks.

Second, I am trying to move bash GDAL code into rasterio and python. Is it possible to use rasterio rasterize to create a sparse raster?

In gdal_rasterize setting the SPARSE_OK=TRUE flag does this. Because rasterio rasterize creates a numpy array, (not a geotiff) I am assuming this may not be possible but wanted to ask. I also might imagine a way to use scipy sparse matrices to do something like this. I am doing this to save on memory requirements. 

here is the full call - any advice on how to replicate appreciated. 

gdal_rasterize \
test.shp \
-te $xmin $ymin $xmax $ymax \
-burn 1.0 -tr $res $res -ot Int16 \


Brendan Ward

Ben - I'm not sure I follow your intent here.

Is the idea to make the smallest possible geotiff after rasterizing all shapes in test.shp to pixels, where SPARSE_OK omits non-rasterized pixels where possible from the geotiff?

In rasterio, the best way to approach that would be to first do a windowed read of your data from raster that overlap the bounding box of all features in test.shp (see geometry_window(), then rasterize within that smaller window, then write it out to a new geotiff (note: it will have a different geotransform / width / height than the original).

If your shapes in test.shape are scattered around the raster with large areas of nodata in between, there's not really a way to handle that case aside from windowing out and writing each rasterized shape to a separate geotiff, then maybe making a VRT of the individual geotiffs it in GDAL to mosaic them back together.