ラインの分割点作成


Pythonを使えるようになる気がしねぇ。
適当にラインの距離で点を作成するツール。

# -*- coding: cp932 -*-

import os.path
import arcpy

# Python ツールボックスのツール動作のカスタマイズ
# http://resources.arcgis.com/ja/help/main/10.1/index.html#//00150000002m000000
class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "MyToolbox"
        self.alias = ""

        # ツールのリスト設定 ( クラス設定 )
        self.tools = [positionAlongLineTool]

# Difference
class positionAlongLineTool(object):
    def __init__(self):
        self.label = "ライン分割(指定間隔)"
        self.description = "ライン分割(指定間隔)"
        self.canRunInBackground = False

    def getParameterInfo(self):
        # Python ツールボックスでのパラメータの定義
        # http://resources.arcgis.com/ja/help/main/10.1/index.html#//001500000028000000
        # getParameterの型一覧
        # http://resources.arcgis.com/ja/help/main/10.1/index.html#//001500000035000000
        # パラメータリファレンス
        # http://resources.arcgis.com/ja/help/main/10.1/index.html#//018z00000063000000

        # レイヤ
        param0 = arcpy.Parameter(
        displayName="入力レイヤ",
        name="in_layer",
        datatype="GPFeatureLayer",
        parameterType="Required",
        direction="Input")
        param0.filter.list = ["Polyline"]

        param1 = arcpy.Parameter(
        displayName="分割距離",
        name="in_leng",
        datatype="GPLong",
        parameterType="Required",
        direction="Output")

        # 出力FeatureClass
        param2 = arcpy.Parameter(
        displayName="出力フィーチャクラス",
        name="outFeatures",
        datatype="DEFeatureClass",
        parameterType="Required",
        direction="Output")
        return [param0, param1, param2]

    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):
        # Python ツールボックスでのメッセージの書き込み
        # http://resources.arcgis.com/ja/help/main/10.1/index.html#//001500000036000000

        layer = parameters[0].value
        inLeng = parameters[1].value
        outFullName  = parameters[2].valueAsText

        messages.addMessage(layer.name)
        messages.addMessage(outFullName)

        outWs = os.path.dirname(outFullName)
        outFc = os.path.basename(outFullName)

        # messages.addMessage("WS:{0} / Name:{1}".format(outWs,outFc))
        has_m = "DISABLED"
        has_z = "DISABLED"
        spRef = None

        dataset = layer.dataSource
        spRef = arcpy.Describe(dataset).spatialReference
        geomType = "POINT" # arcpy.Describe(dataset).shapeType

        arcpy.CreateFeatureclass_management(outWs, outFc, geomType, None, has_m, has_z, spRef)
        arcpy.AddField_management(outFullName, "orgOid", "LONG", 10, "", "", "元のID", "NON_NULLABLE")
        oidCol = arcpy.Describe(layer).OIDFieldName
        outcols = [oidCol,"SHAPE@"]
        
        #if isinstance(extType , unicode):
        #    extType = extType.encode('cp932')
        #else:
        #    extType = str(extType)

        with arcpy.da.SearchCursor(layer, outcols) as cursor, arcpy.da.InsertCursor(outFullName, ["orgOid","SHAPE@"]) as ins:
            for row in cursor:
                
                geom = row[1]

                leng = geom.length
                addCnt = int(leng/inLeng)
                for cnt in range(0, addCnt):
                    messages.addMessage(str(inLeng*cnt))
                    pt = geom.positionAlongLine(leng - inLeng*cnt)
                    ins.insertRow([ row[0], pt ])
        return
カテゴリー: 開発 タグ: パーマリンク