Monday 25 December 2017

Select the most distant vertex from polygon's centroid using ArcPy


I have a polygon and the polygon's centroid. I want to select the most distant vertex from centroid (picture: point A). I used arcpy.FeatureVerticesToPoints_management to create a feature class containing points generated from polygon, but I don't know what to do next. Could you help me?


Also I want to find intersection point (point B) of the outline of polygon and line created by centroid and the most distant point (line OA).



Example


I would be very grateful for any help.



Answer



Here's a pared down version of @crmackey's answer. The polygon layer is called 'POLY1', and should be the only thing you need to change to get an output point file of farthest vertices - it creates centroids on-the-fly:


>>> points = []
>>> with arcpy.da.SearchCursor("POLY1",['SHAPE@']) as cursor:
... for row in cursor:
... centroid = row[0].centroid
... dist = 0
... for part in row[0]:

... for pnt in part:
... cent_vert_dist = arcpy.PointGeometry(pnt).distanceTo(centroid)
... if cent_vert_dist > dist:
... dist = cent_vert_dist
... far_point = arcpy.PointGeometry(pnt)
... points.append(far_point)
...
>>> arcpy.CopyFeatures_management(points,'in_memory\points')

Back-tracking to the intersection point opposite the farthest vertex is possible, but will require some additional trigonometry that I'm not prepared to get into, atm.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...