Help w/ py code (just a correction)

11-08-2016 09:42 AM
New Contributor III
I need some help to correct a bug on this simple py code.

I have been using it w/ no problem since I up graded ArcGIS Desktop from 10.3 to 10.4.

# The code below adds one point at a time to a selection and
# calculates area of convex hull of the selection.
# Output is a text file (or table) with with two columns.
# First column holds number of points selected,
# second column holds total area of the hull.
# Provided you want to use all points in the feature class.
# I recommend you use square metres to get the area and do
# the conversion yourself if you need to. If square metres
# result in too large numbers, use square kilmetres.
import arcpy
in_points = arcpy.GetParameterAsText(0)
# column indicating in what order to add points to the curve
# should be integers from 1 to N by 1
sort_field = 'OBJECTID'
out_results = arcpy.GetParameterAsText(1)
out_delim = ","
# we need to store the hull temporarily;
# there are more ways how to deal with intermediate data, this is a simple one
arcpy.env.overwriteOutput = True
tmp_hull = arcpy.GetParameterAsText(2)
# write header for result file
with open(out_results, "w") as fl:
header = out_delim.join(("PT_COUNT", "M_SQ"))
fl.write(header + "\n")
# total number of points
n = int(arcpy.GetCount_management(in_points).getOutput(0))
# we need a feature layer in order to make quick selections
in_points_lyr =, "points").getOutput(0)
for i in range(n):
print i
# add another point to the selecition
w_clause = '"%s" < %s' % (sort_field, i + 1) # e.g. OBJECTID < 1, OBJECTID < 2, ..., "NEW_SELECTION", w_clause)
# counstruct convex hull
hull =, tmp_hull, "CONVEX_HULL", "ALL").getOutput(0)
# get the area of the convex hull
# (if you coordinate system units are metres)
ara = 0
with arcpy.da.SearchCursor(hull, ["SHAPE@AREA"]) as sc:
for row in sc:
ara += row[0] # should be just one row anyway
# write results for i into the result file
with open(out_results, "a") as fl:
result = out_delim.join((str(i + 1), str(ara)))
fl.write(result + "\n")
print("Script completed.")
That's the error message!!!
0 Kudos
6 Replies
MVP Esteemed Contributor

Rafael, you will have to format your code so it is readable


MVP Frequent Contributor

As Dan says, without proper formatting, it is very hard to tell.

But what is your select clause supposed to be doing?

# add another point to the selecition

w_clause = '"%s" < %s' % (sort_field, i + 1) # e.g. OBJECTID < 1, OBJECTID < 2, ..., "NEW_SELECTION", w_clause)

The other thing is, have you considered just running this as a plain python script before you turn it into a script tool, with all the extra that entails.

MVP Honored Contributor

Try using AddFieldDelimiters to handle the quotes:

>>> fc = 'points'
... sort_field = 'FID'
... w_clause = arcpy.AddFieldDelimiters(fc, sort_field) + ' < ' + str(5) # or str(i+1)
... arcpy.SelectLayerByAttribute_management(fc,"NEW_SELECTION",w_clause)‍‍‍‍
New Contributor III

Hi everyone.

The problem is the variable SORT FIELD = "OBJECTID" and the data had no OBJECTID field. It had OID field.

It's quite simple. I am a rookie in Python.

Next time, I will post the sample code as Dan Patterson and Neil Ayres suggested.

Thanks all!!!!

0 Kudos
MVP Esteemed Contributor


Shapefiles will have a FID and generally an OID field, the first is required, the 2nd is sometimes generated.

Geodatabase featureclasses will have the OBJECTID field.

MVP Honored Contributor

Glad you figured it out. If you want to save yourself this headache in the future, you can get the unique ID field dynamically like this:

>>> fc = 'myFeatureLayer'
... print arcpy.Describe(fc).OIDFieldName