Select to view content in your preferred language

Migrate tool/script from ArcMap to ArcGIS Pro

5389
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
TonyAlmeida
MVP Regular Contributor

I did have to add, Range = float(arcpy.GetParameterAsText(1)) and  included it in, length = row[0].length / 5.28 + Range.

I am not getting a different error, a lock error.

line 48, in <module>
cursor.insertRow(row_value)
RuntimeError: Cannot acquire a lock.

I am using "Point at end of Line" from the drop down and with"Points" in the input drop down window.

I click on the drawing tool to the right, then click points, and click on "selecting point at end of line", it automatically populates with "Scrip Input(points)" in the TOC, I change it to "Points". Is this correct?

 

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Try the attached tool.  First, update the gdb variable by right-clicking on the toolbox > Properties > Execution:

JakeSkinner_0-1710442949693.png

 

Next, double-click the Script tool.  Click the dropdown next the pencil and select Points.  Create two points and execute the tool.

0 Kudos
TonyAlmeida
MVP Regular Contributor

I am still getting the error, Cannot acquire a lock.

I have attached an image.

Would you like me to upload some test data samples?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Remove the CCAP feature class from your map and try executing the tool again.  I think this is placing a lock on the feature class, which is causing this error.

0 Kudos
TonyAlmeida
MVP Regular Contributor

Removing the layer would not ideal because I need that layer on the map.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

I can't reproduce this.  I'm working with Pro 3.2.2 and a File Geodatabase. 

0 Kudos
TonyAlmeida
MVP Regular Contributor

I will try it on a new project and report back.

Again, thank you so much for your help!

0 Kudos
TonyAlmeida
MVP Regular Contributor

Sorry for the delay. I did test it and it does work but for only the first time. If I need to run the script tool again I get the error - line 35, in <module>
RuntimeError: Cannot acquire a lock.

 

Are you able to run the script tool multiple times?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

@TonyAlmeida, yes, I can execute the tool multiple times.  Try adding del cursor below the SearchCursor:

JakeSkinner_0-1712053105334.png

 

0 Kudos
TonyAlmeida
MVP Regular Contributor

It's working now, thank you for the help!

0 Kudos