Select to view content in your preferred language

Migrate tool/script from ArcMap to ArcGIS Pro

5483
20
Jump to solution
03-11-2024 10:59 AM
TonyAlmeida
MVP Regular Contributor

I have a tool/script that performed exceptionally well in ArcMap both as a tool/script and as an Add-in. However, I've been attempting to convert it to ArcGIS Pro without success. This tool/script in ArcMap allowed users to calculate the distance between two points with just two clicks, essentially creating a polyline that conveyed the length for further calculations. It also generated a point and updated a field based on this calculation. Although I've attempted to replicate the parameters from ArcMap in Pro, the tool fails to function as expected. It doesn't allow me to click on the map or initiate any actions. How can I go about ensuring this script works in ArcGIS Pro?

The noticeable difference that I see in the parameters section is that script in pro doesn't have Schema.

ArcMap parameters:

Input - Feature Set-

Type - Required

Direction - Input

MultiValue - no

Schema - points to the Point layer

 

Script,

 

import arcpy
from arcpy import env
import pythonaddins
import os

fc = "Points"
arcpy.env.workspace = r"C:\Temp\PointsTest.mdb"
pointLayer = arcpy.env.workspace + os.sep + "Points" #target point feature class 

if arcpy.Exists ("line_test"): 
    arcpy.Delete_management ("line_test")

# Start an edit session. Must provide the worksapce.
edit = arcpy.da.Editor(arcpy.env.workspace)

# Edit session is started without an undo/redo stack for versioned data
#  (for second argument, use False for unversioned data)
edit.startEditing(True)

# Start an edit operation
edit.startOperation()

input = arcpy.GetParameterAsText(0)
Range = float(arcpy.GetParameterAsText(1))
# Create empty Point and Array objects
point = arcpy.Point()
array = arcpy.Array()

# A list that will hold each the Polyline object
featureList = []

rows = arcpy.SearchCursor(input)
for row in rows:
    geom = row.Shape
    point.X = geom.centroid.X
    point.Y = geom.centroid.Y
    # Add each point to the array
    array.add(point)    

# Create the polyline
polyline = arcpy.Polyline(array)
# Clear the array for future use
array.removeAll()
featureList.append(polyline)

del row, rows

# Copy the polyline to file geodatabase to create a Shape_Length field
arcpy.CopyFeatures_management(featureList, "line_test")
with arcpy.da.SearchCursor("line_test", ["SHAPE@","SHAPE@LENGTH"]) as cursor:
    for row in cursor:
        X = row[0].lastPoint.X
        Y = row[0].lastPoint.Y
        length = row[1] / 5.28
        print("Length:", length)

row_value = (length, (X, Y))
        
cursor = arcpy.da.InsertCursor(pointLayer, ("SiteNum", "SHAPE@XY"))
cursor.insertRow(row_value)

 

 

Add-in,

import arcpy
import pythonaddins

class PointTool(object):
    """Implementation for Point_addin.PointTool (Tool)"""
    def __init__(self):
        self.enabled = True
        self.cursor=3
        self.shape = "Line" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
        def onLine(self, line_geometry):
            
        fc = "Points"
        arcpy.env.workspace = r"C:\Temp\PointsTest.mdb"
        pointLayer = arcpy.env.workspace + os.sep + "Points" #target point feature class 

        if arcpy.Exists ("line_test"): 
            arcpy.Delete_management ("line_test")

        # Start an edit session. Must provide the worksapce.
        edit = arcpy.da.Editor(arcpy.env.workspace)

        # Edit session is started without an undo/redo stack for versioned data
        #  (for second argument, use False for unversioned data)
        edit.startEditing(True)

        # Start an edit operation
        edit.startOperation()

        input = arcpy.GetParameterAsText(0)
        Range = float(arcpy.GetParameterAsText(1))
        # Create empty Point and Array objects
        point = arcpy.Point()
        array = arcpy.Array()

        # A list that will hold each the Polyline object
        featureList = []

        rows = arcpy.SearchCursor(input)
        for row in rows:
            geom = row.Shape
            point.X = geom.centroid.X
            point.Y = geom.centroid.Y
            # Add each point to the array
            array.add(point)    

        # Create the polyline
        polyline = arcpy.Polyline(array)
        # Clear the array for future use
        array.removeAll()
        featureList.append(polyline)

        del row, rows

        # Copy the polyline to file geodatabase to create a Shape_Length field
        arcpy.CopyFeatures_management(featureList, "line_test")
        with arcpy.da.SearchCursor("line_test", ["SHAPE@","SHAPE@LENGTH"]) as cursor:
            for row in cursor:
                X = row[0].lastPoint.X
                Y = row[0].lastPoint.Y
                length = row[1] / 5.28
                print("Length:", length)

        row_value = (length, (X, Y))
                
        cursor = arcpy.da.InsertCursor(pointLayer, ("SiteNum", "SHAPE@XY"))
        cursor.insertRow(row_value)

 

 

 

20 Replies
ShabbirRaza
Regular Contributor

Good work

0 Kudos