Order of polygon vertices returned by SearchCursor

979
9
10-19-2021 07:05 AM
ss2424
by
New Contributor II

ArcMap 10.8.1 /Python 2.7

I'm reading all the vertices of  polygon Shapefiles (some polygons are multipart crossing 180 and some are not perfect square/rectangle) using the code below. But the order of the vertices read in the code is different each time with every Shapefile, sometimes SW corner is returned first and sometimes NW. Is this true that the vertices will be traversed by SearchCursor in random order or am I missing something?

 

polygon_vertices = []
        for row in arcpy.da.SearchCursor(self.feature_class, ["OID@", 'SHAPE@']):
            vertices = [row[0]]
            try:
                for part in row[1]:
                    for point in part:
                        if point:
                            vertex = (point.X, point.Y)
                            if vertex not in vertices:
                                vertices.append(vertex)
                        else:
                            print('Inner ring')
                # check for 180 crossings and change NW, NE values
                if row[1].partCount > 1:
                    ordered_vertices = self.order_vertices(vertices, row[1].extent.XMin, row[1].extent.XMax)
                    polygon_vertices.append(ordered_vertices)
                else:
                    polygon_vertices.append(vertices)

 

 

For perfect square/rectangle polgon Shapefiles, I was able to get the vertices from Extent ( except for multiparts) but have to traverse the vertices for irregular polygons as shown below. ss2424_0-1634651883151.png

 

0 Kudos
9 Replies
JoeBorgione
MVP Emeritus

It's always been a mystery to me; I thought at one time it was by ascending value in ObjectID.  Seems like friend and colleague @JoshuaBixby provided an explanation at one time...

That should just about do it....
0 Kudos
ss2424
by
New Contributor II

Seems like Extent of polygons are always returned in one order but its not clear which order the vertices are read...is it possible to force it to read one particular vertex and then traverse clockwise?

0 Kudos
JoeBorgione
MVP Emeritus

Clockwise?  Beats me!

That should just about do it....
0 Kudos
ss2424
by
New Contributor II

@JoshuaBixby  I'm  searching for your post to find a response but no luck yet.. Can u help?

0 Kudos
JoeBorgione
MVP Emeritus

@JoshuaBixby has hundreds if not thousands of posts; it may have been a older discussion we had before this forum was in place...

That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Seeing you are working with multiple polygons in multiple shapefiles, it is best to start isolating the issue by isolating the data.  What happens if you take a single polygon from a single shapefile and run it many times, do you see different results?

ss2424
by
New Contributor II

I exported a single polygon from each of 2 different Shapefiles and I saw consistent  results. The firstPoint returned for a polygon in Shapefile 1 is SE corner while the other polygon from Shapefile 2 returned SW corner first.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Is that what you are perceiving to be the inconsistency, the fact that one polygon starts in the SE corner while the other starts in the SW corner?  The order of the vertices is determined when a shape is created, not when a shape is read, so the source shapefiles are not following a consistent rule with building polygons.

0 Kudos
ss2424
by
New Contributor II

Thanks for confirming..I modified the code to get the  extent of the polygon and then get the 4 corners from extent...though I'm seeing expected results I want to make sure I can go this way than getting the vertices using my code above(originally posted) or is there an advantage of one method over another?

0 Kudos