# 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 = r'C:\temp\base.gdb\pts'
# 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 = r'c:\temp\results.txt'
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 = r'c:\temp\hull.shp'
# 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.")
# if you want to import the text file into ArcGIS, do
arcpy.management.CopyRows(out_results, r"c:\temp\results.dbf")
Hi!!!!
Could you help me again w/ the same code, please???
I have been using it w/ no problem since I up graded ArcGIS Desktop from 10.3 to 10.4.
Why that error???
Hi,
Hmmm, strange. A few things I can think of:
Make sure that in_points actually has the sort_field. For example shapefiles usually don't have OBJECTID.
Try to modify the expression to w_clause = '%s < %s' % (sort_field, i + 1)
I hope you'll find where the problem is.
F.
Shapefiles use FID and begin at 0... geodatabase is OBJECTID (do they begin at 1? can't remember)
Rather than hard-coding the name of the OID/FID (sort_field = 'OBJECTID'), you can get it dynamically by describing the data:
sort_field = arcpy.Describe(in_points).OIDFieldName