1

I wrote the following hard coded script which asks a user to input a number of points (X,Y) to draw a Polygon feature in a feature class and the input type is float then append these inputs in a list, and to close the program (stop or finish sketch) user have to insert any STR value in the input, once the script runs the new parcel appear on the map after refresh (zoom in/out).

This script works well with Python window & Jupyter Notebook in ArcGIS Pro.

But I want to convert this script into a toolbox in ArcGIS Pro, so I created New Toolbox > Script, added parameter 0 as Feature class and parameter 1 as String (with multiple values), the tool doesn't work and give me error:

TypeError: cannot read geometry field. empty sequence.

I tried to change parameter 1 to different datatypes like Polygon/XY/Lon but all failed.

import arcpy
import os
## Following Parameters disabled so script can works well on any IDLE
## but when using Arctoolbox you have to enable this parameters and disabled any other Path
# 1 Input Parameters
#feature_class = arcpy.GetParameterAsText(0)
#one = arcpy.GetParameterAsText(1)
try:
 list2 = []
 while True:
 one=input()
 x, y = map(float, one.split(','))
 a = list2.append((x,y))
except:
 print(list2)
feature_class = "C:/Users/ProjectsName/Database.gdb/polygon"
# Write feature to new feature class
with arcpy.da.InsertCursor(feature_class, ['SHAPE@']) as cursor:
 cursor.insertRow([list2])
  • Also I created the following Script and it works well either Script or Arc Tool, but the input points are limited by the number of getparameter that mean I have to create 10 get parameters in script and the end user will have to use only 10 points not more/or less which doesn't make sense!

    import arcpy
    import os
    feature_class = arcpy.GetParameterAsText(0)
    x1 = arcpy.GetParameterAsText(1)
    y1 = arcpy.GetParameterAsText(2)
    x2 = arcpy.GetParameterAsText(3)
    y2 = arcpy.GetParameterAsText(4)
    x3 = arcpy.GetParameterAsText(5)
    y3 = arcpy.GetParameterAsText(6)
    coordinates = [(x1,y1),
     (x2,y2),
     (x3,y3)
     ]
    with arcpy.da.InsertCursor(feature_class, ['SHAPE@']) as cursor:
     cursor.insertRow([coordinates])
    
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Aug 11, 2021 at 13:45
1
  • Are you creating the geometry polygon array and inserting that, see this related Q/A gis.stackexchange.com/questions/180757/…. Also, I do not think you need the with loop on the cursor statement. Commented Aug 11, 2021 at 15:47

1 Answer 1

2

You need to set your second parameter to be a Multiple value as shown below:

Tool setup

This allows you to add multiple coordinates when running the tool, see example below:

Tool Interface

The code to utilise this would be:

import arcpy
import os
# Read parameters
feature_class = arcpy.GetParameterAsText(0)
coordlst = arcpy.GetParameterAsText(1) # This will be a string in format of x,y;,x1,y1;,x3,y3
# Reformat coordinate list into a list of tuples that can be inserted in the featureclass
poly = list()
lst = coordlst.split(";")
for pair in lst:
 x,y = map(float,pair.split(","))
 poly.append((x,y)) # creates a list of tuples
# Write feature to existing feature class
with arcpy.da.InsertCursor(feature_class, ['SHAPE@']) as cursor:
 cursor.insertRow([poly])
# Optional - refresh current map display to show new polygon by panning to extent
aprx = arcpy.mp.ArcGISProject("CURRENT")
mv = aprx.activeView
arr = arcpy.Array([arcpy.Point(*coords) for coords in poly])
geom = arcpy.Polygon(arr)
ext = geom.extent
mv.panToExtent(ext)

The key to the parameter is to understand what it generates which is a string formatted as x,y;,x1,y1;,x3,y3;x4,y4 etc..

answered Aug 11, 2021 at 16:54
1
  • Great! thanks, this works well, I just edited it to accept float type. Commented Aug 11, 2021 at 17:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.