Reputation: 3202
I have a list of shape files in a directory and I am trying to extract the coordinates of each shape file using arcpy
.
Any ideas? Thanks.
Upvotes: 1
Views: 5503
Reputation: 2457
Your question is sort of broad, so this answer is rather general :)
If you have ArcGIS 10.1+, then you can use an arcpy.da.SearchCursor to loop through features in a shapefile and extract the geometry.
The example code below is for points. Ref. the Esri documentation on reading geometries for lines or polygons (the basic theory is identical, the implementation is a bit more complex) for more detail. To repeat for multiple shapefiles in the same script, just add an additional for
loop around the SearchCursor loop.
import arcpy
infc = ### your shapefile here
# Enter for loop for each feature
for row in arcpy.da.SearchCursor(infc, ["OID@", "SHAPE@"]):
# Print the current multipoint's ID
#
print("Feature {0}:".format(row[0]))
# For each point in the multipoint feature,
# print the x,y coordinates
for pnt in row[1]:
print("{0}, {1}".format(pnt.X, pnt.Y))
For 10.0 or earlier, you need to use an arcpy.SearchCursor.
import arcpy
infc = ### your shapefile here
# Identify the geometry field
desc = arcpy.Describe(infc)
shapefieldname = desc.ShapeFieldName
# Create search cursor
rows = arcpy.SearchCursor(infc)
# Enter for loop for each feature/row
for row in rows:
# Create the geometry object 'feat'
feat = row.getValue(shapefieldname)
pnt = feat.getPart()
# Print x,y coordinates of current point
print pnt.X, pnt.Y
Upvotes: 5