I am attempting to make a script tool that takes polygon input, and outputs centroid point (within the polygon). This knowledge base article seems to be incorrect, in that the SHAPE@XY token returns the center of gravity for the polygon, not the centroid within the polygon. Likewise, the centroid property in the Polygon class, used in the code below, returns the center of gravity rather than centroid within polygon. Any ideas?
# import libraries
import arcpy, os
# set input/output parameters
polyFC = arcpy.GetParameterAsText(0)
outCentroids = arcpy.GetParameterAsText(1)
# set overwrite environment
arcpy.env.overwriteOutput = True
# if the output file does not exist, create it. Add "ORIG_ID" field.
if not arcpy.Exists(outCentroids):
arcpy.CreateFeatureclass_management(os.path.dirname(outCentroids),
os.path.basename(outCentroids),
"POINT",
polyFC,
"",
"",
polyFC)
arcpy.AddField_management(outCentroids,'ORIG_ID', 'LONG')
# collect all of the polygon centroids into an array
pointArray = []
for row in arcpy.da.SearchCursor(polyFC, ["SHAPE@",'*','OID@']):
rowArray = []
rowArray.append(row[0].centroid)
for field in range(1,len(row)):
rowArray.append(row[field])
pointArray.append(rowArray)
del row
# write the centroids to the output file
cursor = arcpy.da.InsertCursor(outCentroids, ["SHAPE@",'*'])
for point in pointArray:
arcpy.AddMessage(point)
cursor.insertRow(point)
del cursor
Example output below:
