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])
    
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..

1
  • Great! thanks, this works well, I just edited it to accept float type.
    – Mo Ebra
    Aug 11 '21 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.