2

I want to have a string showing all vertices from all polygons in a shapefile. In my example below my code lists all vertices and the first column shows the polygon they are part of (I only have 2 in this example)

import arcpy
fc=r'D:\GIS Data\TOOLS\EV calc in Python\Data.gdb\PolyWGS84'

myList = []

with arcpy.da.SearchCursor(fc,['OID@','SHAPE@']) as cursor:
    for row in cursor:
        array1=row[1].getPart()
        for vertice in range(row[1].pointCount):
            pnt=array1.getObject(0).getObject(vertice)
            print row[0],pnt.X,pnt.Y
            myList.append(str(pnt.X) + " " + str(pnt.Y))

print ", ".join(myList)

This is the result

1 151.513429431 -33.568900991
1 151.313554706 -34.011480738
1 150.928082023 -33.975788823
1 150.906666874 -34.27560091
1 150.506917425 -34.211355462
1 150.692515384 -33.51893231
1 151.513429431 -33.568900991
2 151.72520146 -32.500522999
2 152.082120611 -33.242914833
2 151.196961117 -33.100147173
2 151.72520146 -32.500522999
151.513429431 -33.568900991, 151.313554706 -34.011480738, 150.928082023 -33.975788823, 150.906666874 -34.27560091, 150.506917425 -34.211355462, 150.692515384 -33.51893231, 151.513429431 -33.568900991, 151.72520146 -32.500522999, 152.082120611 -33.242914833, 151.196961117 -33.100147173, 151.72520146 -32.500522999

But what I want is separate strings for each polygon in the shapefile

151.513429431 -33.568900991, 151.313554706 -34.011480738, 150.928082023 -33.975788823, 150.906666874 -34.27560091, 150.506917425 -34.211355462, 150.692515384 -33.51893231, 151.513429431 -33.568900991
151.72520146 -32.500522999, 152.082120611 -33.242914833, 151.196961117 -33.100147173, 151.72520146 -32.500522999

How would you do this?

5

You have the general idea in place already -- just move your print of the list into the loop, and remove the print for each vertex.

import arcpy
fc=r'D:\GIS Data\TOOLS\EV calc in Python\Data.gdb\PolyWGS84'



with arcpy.da.SearchCursor(fc,['OID@','SHAPE@']) as cursor:
    for row in cursor:
        myList = []
        array1=row[1].getPart()
        for vertice in range(row[1].pointCount):
            pnt=array1.getObject(0).getObject(vertice)
            # print row[0],pnt.X,pnt.Y
            myList.append(str(pnt.X) + " " + str(pnt.Y))
        print row[0], ", ".join(myList)




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.