赞
踩
由于经常用到单个矢量去批量裁剪多个栅格数据,这里记录以防总是去找程序文件。
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Created on Fri Jan 8 16:31:59 2021 """ import os # 对应自己的python包的安装地址,也是prj.db文件存放的位置,直接电脑搜吧,选择osgeo文件夹的那个路径就可以,我是以为自己糊涂多装了很多gdal吧,然后prj.db文件过多,代码就找不到哪个了,所以这里我加了一行代码来指出我的Prj.bd文件存在哪里。 os.environ['PROJ_LIB'] = r'E:\Users\Lib\site-packages\osgeo\data\proj' import time,os from osgeo import gdal import numba @numba.jit # 程序加速 def clip_batch(in_folder, out_folder, in_shape): files = os.listdir(in_folder) for file in files: if file[-4:] == '.tif': filename = os.path.join(in_folder,file) in_raster = gdal.Open(filename) out_raster = os.path.join(out_folder,file) ds = gdal.Warp(out_raster,in_raster,format = 'GTiff', cutlineDSName = in_shape, cropToCutline = True, creationOptions=["TILED=YES", "COMPRESS=LZW"], cutlineWhere = None, dstNodata = -999) ds=None if __name__=="__main__": start = time.perf_counter() # 开始时间 in_shape = r"E:\backup_1\small_area.shp" # 矢量范围 in_folder = r"H:\backup_4\backup_4" # 输入栅格路径 out_folder=r"I:\reclass_data" # 输出栅格路径 clip_batch(in_folder, out_folder, in_shape) end = time.perf_counter() # 结束时间 print('finish') print('Running time: %s Seconds'%(end-start))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。