源起:博主在工作中需要进行地块裁剪,使用buffer来进行栅格的掩膜提取,遇到的问题是文件量巨大,手动一个个用shp进行掩膜切割根本不现实,况且也不够稳定,因此选择使用arcpy来进行掩膜提取的简单代码编写,亲测稳定可用,开源分享给需要的朋友们(python实现在最后)
官方教程:https://desktop.arcgis.com/zh-cn/arcmap/10.3/tools/spatial-analyst-toolbox/extract-by-mask.htm
python代码:
import arcpy
import os
if __name__ == '__main__':
try:
arcpy.env.overwriteOutput=True
arcpy.CheckOutExtension("Spatial")
inFile = r'切割的栅格文件路径'
outMaskData =r'切割之后储存的文件夹'
BasPath=arcpy.env.workspace =r'buffer文件路径\\'
featureclasses = arcpy.ListFeatureClasses()
for a_mxd in featureclasses:
try:
print a_mxd
mxd_path = os.path.join(BasPath, a_mxd)
arcpy.gp.ExtractByMask_sa(inFile, mxd_path, outMaskData+"\\"+a_mxd+".tif")
print mxd_path
except OSError:
pass
continue
except Exception as e:
print e.message