1

Here is a link to an example cloud-optimized GeoTIFF:

https://copernicus-dem-30m.s3.amazonaws.com/Copernicus_DSM_COG_10_N08_00_W068_00_DEM/Copernicus_DSM_COG_10_N08_00_W068_00_DEM.tif

I assign this to a variable & open it using rasterio:

geotiffurl='https://copernicus-dem-30m.s3.amazonaws.com/Copernicus_DSM_COG_10_N08_00_W068_00_DEM/Copernicus_DSM_COG_10_N08_00_W068_00_DEM.tif'
src = rasterio.open(geotiffurl)

Show this raster:

plt.imshow(src_read)

enter image description here

If I am interested in a smaller rectangular subset of the raster, I can just do:

src_read = src.read(1,window=((3, 10), (0, 10)))

Plot it:

plt.imshow(src_read)

enter image description here

Using the window=((3, 10), (0, 10)) within src.read() makes the script much quicker, compared to reading all the pixels in and then discarding the parts I don't need.

Is there a way to read in a non-rectangular fraction of the GeoTIFF?

In my real-world problem, I am interested in areas described by polygons. I am looking for a solution where I can specify a polygon, read the pixels corresponding to that polygon, filling the rest with NaNs for example, so I still get a rectangular array out of it, but preferably faster than reading all the pixels within the bounding box of the polygon and then discarding the ones outside the polygon.

1
1

TIFF files are organized into stripes (one or more pixel rows) or into tiles. Stripe or tile is the smallest unit that can be read from the TIFF file. Both stripes and tiles are rectangular so the answer to your question could be No, what is read is always a rectangular area. But theoretically it could save time to use a tiled TIFF and read only the tiles that intersect a polygon, change unnecessary pixels from the tiles into NoData, and as the last step fill the final rectangle with NoData where no TIFF tiles were read at all. With a polygon that presents a long river flowing from North-East towards South-West that could make a big difference.

Rasterio may have some similar option than -crop_to_cutline in gdalwarp. I do not know if it tries to read as little data from GeoTIFF as possible but maybe it does. With typical polygons the advantage is not necessarily so great.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.