ARCGIS裁剪数据量比较大的栅格很容易卡死,故用gdal。
可批量裁剪多个shp的组合。
翻阅了一些博客,这么一个简单的功能写得很复杂。提供代码供有需要的各位参考。
from osgeo import gdal
input_raster = r"F:\JUNK\dem90.tif"
# or as an alternative if the input is already a gdal raster object you can use that gdal object
input_raster=gdal.Open(input_raster)
input_shape = r"F:\JUNK\gla-shp.shp" # or any other format
output_raster=r'F:\JUNK\test2.tif' #your output raster file
ds = gdal.Warp(output_raster,
input_raster,
format = 'GTiff',
cutlineDSName = input_shape, # or any other file format
cutlineWhere="FIELD = 'whatever'",# optionally you can filter your cutline (shapefile) based on attribute values
dstNodata = -9999) # select the no data value you like
ds=None #do other stuff with ds object, it is your cropped dataset. in this case we only close the dataset.