I have a GeoTIFF file and a shapefile. I want to clip the GeoTIFF file using the polygons defined in the shapefile in such a way that it produces a separate output for each polygon (which is different from the gdalwarp
usecase, which produces a single output by default).
Furthermore, I want to do this in an efficient manner. Iterating over each polygon and calling gdalwarp
will reload the GeoTIFF each and every time, which is something I want to avoid.
If some tool (or some options of gdalwarp
) exists to solve this use case, it would be very interesting to know.
EDIT: The piece of code that we are currently using:
def cut_region(fname, ids, output_folder, input_pic, input_shape):
photo_name = input_pic + fname + ".jp2"
shape_name = input_shape + fname + ".shp"
raster = gdal.Open(photo_name)
projection = raster.GetProjectionRef()
for id in ids:
output_name = output_folder + fname + '/' + str(id) + ".tif"
output = gdal.Warp(output_name,
raster,
format='GTiff',
cutlineDSName=shape_name,
cutlineSQL="SELECT * from " + fname + " where ID='" + str(id) + "'",
dstSRS = projection,
cropToCutline=True,
multithread=True,
dstAlpha=True
)