(一)arcpy开发&利用arcpy在arcgis中批量裁剪影像
最近有这么一个需求:我有一个面文件数据,该面文件shapefile有多条记录,具体如下图所示。现在需要遍历出图层中的每个面记录数据,然后作为范围面和一个大的tif影像裁剪。
我们知道,在arcgis中可以使用裁剪工具对影像裁剪。具体步骤,依次选择【Data Management Tools】、【Raster】、【Raster】、【Raster Processing】、【Clip】,最后弹出如下图所示的工具对话框。
从对话框中,我们可以看出,我们需要输入的tif影像,然后就是范围面数据,最后就是结果保存路径。现在需要做的工作是遍历每个一个范围面,然后取出范围值,拼接成裁剪函数需要的格式。根据每个面的名称不相同相应的保存到我们选择的路径。具体可以参考代码。总得来说代码相对简单,但是在其中遇到了问题,当加载工具到arcgis时,冒了一个大×,最后检查是没有设置编码的原因。好了下面来看一下源代码,以及打包代码,和实现的界面。
import arcpy
import os
def do(shpPathJHTB,importtif,resultPathTif):
with arcpy.da.SearchCursor(shpPathJHTB, ['FID','SHAPE@']) as cursor:
for row in cursor:
e = row[1].extent
ExtStr = "{} {} {} {}".format(e.XMin, e.YMin, e.XMax, e.YMax)
arcpy.Clip_management(in_raster=importtif, rectangle=ExtStr,
out_raster=os.path.join(resultPathTif,
str(row[0]) + ".tif"))
del cursor
打包代码:
# coding:gbk
import arcpy
from PLiangClip import do
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Toolbox"
self.alias = ""
# List of tool classes associated with this toolbox
self.tools = [Tool]
class Tool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "批量按照结合图表裁剪tif"
self.description = "批量按照结合图表裁剪tif"
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
shpPathJHTB = arcpy.Parameter(
displayName="结合图表shp面数据",
name="shpPathJHTB",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input"
)
tifPath = arcpy.Parameter(
displayName="tif影像",
name="tifPath",
datatype="DERasterDataset",
parameterType="Required",
direction="Input"
)
resultFolder = arcpy.Parameter(
displayName="最终处理结果",
name="resultFolder",
datatype="Folder",
parameterType="Required",
direction="Input"
)
#params = None
params = [shpPathJHTB, tifPath, resultFolder]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
shpPathJHTB= parameters[0].valueAsText
tifPath= parameters[1].valueAsText
resultFolder= parameters[2].valueAsText
# do(shpPathJHTB,tifPath,resultFolder)
return
实现结果界面:
更多内容,请关注公众号
转载自:https://blog.csdn.net/u010608964/article/details/83117010