简体   繁体   中英

CSV of XY coordinates for multiple polygons

I have "borrowed" bits and piece of python code to help me do this but, I am having trouble getting exactly what I want. I am trying to get the XY coordinates of the vertices of several polygons. I want to be able to know which polygon each vertex is belongs to and want each on a single line. It would also be good to have the vertex ID. The following code gets me close but it writes a single line for the polygon then gives me each of the vertices on a separate line, and I don't know how to get the vertex IDs.

import arcpy, os, csv
from arcpy import env
fc = "Z:/VHF/MyShapefile.shp"
csv = open("Z:/VHF/UECAVerticesFinal.csv", "w")

#with arcpy.da.SearchCursor(fc, ("OID@", 'PARCELID','UECA', "SHAPE@X","SHAPE@Y")) as cursor:
with arcpy.da.SearchCursor(fc, ("OID@", 'PARCELID','UECA', "SHAPE@")) as cursor:

 for row in cursor:
#   partnum = 0 taken out because it writes the same vertex for every poly
#
    for part in row[3]:
#   for row in cursor:
#    print ("{0}, {1}, {2}, {3}".format(row[0], row[1], row[2], row[3]))
     csv.write("{0},{1},{2}\n".format(row[0],row[1], row[2]))
#     print ("{0},{1},{2}\n".format(row[0],row[1], row[2]))
    for vertex in part:
#     print(", , , {0},{1}\n".format(vertex.X, vertex.Y))
     csv.write(", , , {0},{1}\n".format(vertex.X, vertex.Y))
#   partnum +=1 see line 10
# 
csv.close()

(above code borrowed from Writing XY coordinates to CSV file using Python among others)

This code should do what you want, using the OBJECTID to identify each polygon:

 with arcpy.da.SearchCursor(fc, ["OID@", "SHAPE@"]) as cursor: for row in cursor: for part in row[1]: for vertex in part: csv.write("{0},{1},{2},{3}".format(row[0], vertex.ID, vertex.X, vertex.Y) 

As for getting vertex IDs, I have not been able to find documentation on that, and there may not be such a thing. Points are used to construct polygons, so do you mean point IDs? (These are included in my answer). See http://resources.arcgis.com/en/help/main/10.1/index.html#//018z0000006t000000 for the official explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM