I am working with a Search Cursor approach to calculate centroids within polygon features. You can see from the screenshot that the script calculates centroids within polygon features, however, some of the centroids are located outside of the polygon features (circled in red). Is there a way to force the centroids to be calculated within the polygons? It appears as if the Feature To Point (Data Management) tool allows for this, although I am unable to use an an Advanced license for this.
import arcpy
input_fc = "C:\\temp\\geodatabase.gdb\\featureclass"
output_fc = "C:\\temp\\geodatabase.gdb\\featureclass_centroids"
cursor = arcpy.da.SearchCursor(input_fc, "SHAPE@XY")
centroid_coords = []
for feature in cursor:
centroid_coords.append(feature[0])
point = arcpy.Point()
pointGeometryList = []
for pt in centroid_coords:
point.X = pt[0]
point.Y = pt[1]
pointGeometry = arcpy.PointGeometry(point)
pointGeometryList.append(pointGeometry)
arcpy.CopyFeatures_management(pointGeometryList, output_fc)