AnsweredAssumed Answered

Help w/ py code (just a correction)

Question asked by rafaelruas on Nov 8, 2016
Latest reply on Nov 8, 2016 by dkwiens
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
 
# PARAMETERS
 
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)
 
 
# WORK
 
# 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 = arcpy.management.MakeFeatureLayer(in_points, "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, ...
arcpy.management.SelectLayerByAttribute(in_points_lyr, "NEW_SELECTION", w_clause)
 
# counstruct convex hull
hull = arcpy.management.MinimumBoundingGeometry(in_points_lyr, 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!!!

Outcomes