from osgeo import gdal
import os
import shapefile
#要裁剪的原图
input_raster = r’raw.tif’
input_raster=gdal.Open(input_raster)

#shp文件所在的文件夹
path=r’D:/shp_file/’

#裁剪结果保存的文件夹
savepath=r’D:/result/’

#读取shp文件所在的文件夹
files= os.listdir(path)
for f in files: # 循环读取路径下的文件并筛选输出
if os.path.splitext(f)[1] == “.shp”:
name=os.path.splitext(f)[0]
input_shape=path+f
r = shapefile.Reader(input_shape)
output_raster=savepath+name+’.tif’
ds=gdal.Warp(output_raster,
input_raster,
format = ‘GTiff’,
outputBounds=r.bbox,
cutlineDSName = input_shape,
cutlineWhere=“FIELD = ‘whatever’”,
dstNodata = -1000)
ds=None
python gdal 基于shp文件裁剪geotif图并以最小外接矩形形式保存
刚开始写博客,不大会用,发现代码间的缩进没有了,那就直接再上传张图片吧,懒得改了。

相关文章:

  • 2021-09-01
  • 2022-01-23
  • 2021-08-17
  • 2021-10-31
  • 2021-11-01
  • 2022-02-25
  • 2022-12-23
猜你喜欢
  • 2021-05-31
  • 2021-12-13
  • 2022-12-23
  • 2021-07-30
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案