I'm using GDAL in Python to work with GeoTIFF rasters. I use the following code to extract small rectangular patches from the entire raster:
data_file = gdal.Open("path/to/raster.tiff")
data = data_file.ReadAsArray(xoffset, yoffset, xsize, ysize)
How could I change this code to extract rotated rectangular areas from the raster. For example, I would like to be able to extract data from the area shown in red below.
I'd like the red area to be resampled and rotated, so that I can access it as a simple numpy data array.
I created a solution to this by following this excellent post about how to implement affine transforms.
My solution works by:
ReadAsArray
to read a section of the full raster that fully contains the red area;p0
, p1
, p2
representing the top-left, top-right and bottom-left corners of the red area respectively in pixel coordinates;