使用 ARCPY 读取形状文件的坐标

icy*_*ypy 1 python shapefile arcpy

我在目录中有一个形状文件列表,我正在尝试使用arcpy.

有任何想法吗?谢谢。

Eri*_*ica 5

你的问题有点宽泛,所以这个答案很笼统:)

如果您有 ArcGIS 10.1+,那么您可以使用 arcpy.da.SearchCursor 来遍历shapefile 中的要素并提取几何。

下面的示例代码用于点。参考 有关读取线或多边形几何的 Esri 文档(基本理论相同,实现有点复杂)以获取更多详细信息。要在同一脚本中重复多个 shapefile,只需for在 SearchCursor 循环周围添加一个额外的循环。

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))
Run Code Online (Sandbox Code Playgroud)

对于 10.0 或更早版本,您需要使用 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
Run Code Online (Sandbox Code Playgroud)