Create centroids from closed lines via temporary polys

Like it says on the tin. I wrote this script because I had thousands of features in a CAD drawing which were supposed to represent points, but were closed boxes made of lines instead.

# @date 26/06/2015
# @author Cindy Williams
# Creates centroids from closed lines by creating temporary
# polygons.
# For use as as a standalone script.
import arcpy
arcpy.env.workspace = r"C:\Some\Arb\Folder\work.gdb"
lyr_line = "ftr_line"
lyr_point = "ftr_point"
fields = ["SHAPE@", "Name"]
cursor_ins = arcpy.da.InsertCursor(lyr_point, fields)
with arcpy.da.SearchCursor(lyr_line, fields) as cursor:
for row in cursor:
poly = arcpy.Polygon(row[0].getPart())
centroid = arcpy.PointGeometry(poly.centroid)
cursor_ins.insertRow([centroid, row[1]])
del cursor_ins
print("Script complete.")

The script builds a polygon from the parts of the line, creates a centroid and inserts it into a point feature class.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.