如何使用ARCPY(python)读取形状文件的坐标

时间:2015-02-10 09:03:15

标签: python shapefile arcpy

我有一个目录中的形状文件列表,我试图使用arcpy,任何想法提取每个形状文件的坐标? 感谢

1 个答案:

答案 0 :(得分:4)

你的问题有点广泛,所以这个答案很普遍:)

如果您有ArcGIS 10.1+,则可以在shapefile中use an arcpy.da.SearchCursor to loop through features并提取几何体。

以下示例代码适用于积分。 Ref. the Esri documentation on reading geometries for lines or polygons(基本理论相同,实现有点复杂)以获取更多细节。要在同一个脚本中重复多个shapefile,只需在SearchCursor循环周围添加一个额外的for循环。

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))

对于10.0或更早版本,您需要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