(二)arcpy开发&arcpy中利用不规则矢量面在arcgis中批量裁剪影像

使用clip工具来裁剪图形一般是规则多边形,即一个范围裁剪是以最左点,最上点,最右点,最下点来确定范围线。如下图所示是一个范围线,该范围线是不规则的。

以及叠加上影像,按理来说应该是范围线内的部分。

下面我们来使用arcgis工具来裁剪一下影像。注意没有勾选【Use Input Features for Clipping Geometry(option)】

最后的裁决效果如下图所示。

而如果我们勾选了上面的选项,则最后的裁剪结果则是我们需要的。

而我现在需要做的工作是利用图幅结合表shapefile里面的数据,遍历其中每一条记录然后和影像进行裁剪,根据图幅分幅来说,矩形框并不是严格的长方形,而是梯形,因此我们这里需要使用多边形裁剪。按照网络的相关资料,可以使用ExtractByMask函数进行裁剪。但是在使用该函数的时候多次遇到拼接参数出现了问题,无奈只好使用了clip_Manager函数来裁剪。首先,在函数的开头遍历了所有的矢量面记录,然后使用矢量面去和影像进行裁剪,将影像按照FID作为裁剪后的名称保存。具体可以查看一下代码。

功能实现代码:


# coding:utf-8
import arcpy
import os
from arcpy.sa import *


def do(shpPathJHTB,importtif,resultPathTif):

     with  arcpy.da.SearchCursor(shpPathJHTB, ['FID', 'SHAPE@']) as cursor:
        for row in cursor:
         mask = row[1]
         extent = str(mask.extent.XMin) + " " + str(mask.extent.YMin) + " " + str(mask.extent.XMax) + " " + str(mask.extent.YMax)
         #outPath = outworkspace + "\\" + str(row.getValue(field) + outtype)
         outPath=os.path.join(resultPathTif,str(row[0]) + ".img");
         arcpy.Clip_management(importtif, extent, outPath, mask, "0", "ClippingGeometry")





 

打包代码:

# coding:gbk
import arcpy



#http://pro.arcgis.com/zh-cn/pro-app/arcpy/geoprocessing_and_python/defining-parameter-data-types-in-a-python-toolbox.htm
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 = "批量按照结合图表裁剪img"
        self.description = "批量按照结合图表裁剪img"
        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="img影像",
            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/83146136

You may also like...