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)
Solved! Go to Solution.
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?
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.
Removing the layer would not ideal because I need that layer on the map.
I can't reproduce this. I'm working with Pro 3.2.2 and a File Geodatabase.
I will try it on a new project and report back.
Again, thank you so much for your help!
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?
@TonyAlmeida, yes, I can execute the tool multiple times. Try adding del cursor below the SearchCursor:
It's working now, thank you for the help!