Select to view content in your preferred language

Order polygon vertices clockwise ArcGIS Pro

414
3
07-27-2024 12:43 PM
MohamedHassan17
Emerging Contributor

I have a Python script that generates layout and I need to arrange the polygon vertices clockwise Like Image_1

MohamedHassan17_0-1722109022551.png

my script to extract polygon vertices not clockwise.

 



def extract_polygon_vertices(feature_layer😞
    results = []
    with arcpy.da.SearchCursor(feature_layer, ["SHAPE@"]) as cursor:
        for row in cursor:
            geom = row[0]
            if geom:
                vertices = [(pnt.X, pnt.Y) for part in geom.getPart() for pnt in part if pnt]
                vertices = order_vertices(vertices
                for i in range(len(vertices)):
                    if i > 0 and vertices[i] == vertices[i - 1]:
                        continue
                    pnt1 = vertices[i]
                    pnt2 = vertices[i - 1] if i > 0 else vertices[-1]
                    length = calculate_distance(pnt2, pnt1)
                    results.append((pnt1, pnt2, length))
            else:
                add_error_message("No geometry available.")
    del cursor
    return results
 
 

def order_vertices(vertices😞    
    """
    
    Order the vertices in a clockwise direction starting from the highest Y coordinate.
    """
    if not vertices:
        return []

    ordered_vertices = []
    # Get the highest Y coordinate
    highest_y = max(vertices, key=lambda vertex: vertex[1])
    highest_y_index = vertices.index(highest_y)
    # Check if multiple points have the same highest Y and choose the most left one (smallest X)
    top_points = [v for v in vertices if v[1] == highest_y[1]]
    start_point = min(top_points, key=lambda vertex: vertex[0])
    start_point_index = vertices.index(start_point)
    # Order the points starting from start_point to the end, then from the beginning to start_point
    ordered_vertices = vertices[start_point_index:] + vertices[:start_point_index]
    return ordered_vertices
 
 
Image_2 : the incorrect ouptut
MohamedHassan17_1-1722109390607.png

 

3 Replies
DanPatterson
MVP Esteemed Contributor

That is clockwise.


... sort of retired...
0 Kudos
MohamedHassan17
Emerging Contributor

okay, but my output not match the Image_1.
I need to arrange from the highest Y on the left then clockwise.

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

You want the point closest to the left-most top of the extent of the polygon, with

L - smallest X

R - largest X

B - smallest Y

T - largest Y

you want the point with the minimum distance to L,T 

 


... sort of retired...