在ArcMap中提取多边形顶点的坐标?


25

在ArcMap 10中加载的要素类中,我有大约十二个多边形,全部在WGS 1984地理区域中。

如何轻松获得与该要素类中每个多边形的每个顶点关联的坐标?

理想情况下,我希望将它们以电子表格格式很好地制成表格。

Answers:



12

这适用于标准ArcGIS许可:

desc = arcpy.Describe(fcl)
shapefieldname = desc.ShapeFieldName

gebieden = arcpy.UpdateCursor(fcl)

for gebied in gebieden:
    polygoon = gebied.getValue(shapefieldname)
    for punten in polygoon:
        for punt in punten:
            print punt.X, punt.Y


4

这是使用da.SearchCursor的另一种方式:

import arcpy
fc=r'C:\TEST.gdb\polygons123'

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

得到的ObjectID,X和Y可以复制到Excel:

...
1 537505.894287 6731069.60889
1 537533.516296 6731078.20947
1 537555.316528 6731082.53589
1 537562.501892 6731085.47913
1 537589.395081 6731070.52991
1 537617.062683 6731058.29651
2 537379.569519 6729746.16272
2 537384.81311 6729746.06012
2 537396.085327 6729748.62311
2 537404.065674 6729752.75311
2 537425.145325 6729773.72931
2 537429.842102 6729777.07129
2 537442.971313 6729780.10651
2 537450.27533 6729780.51611
...

快速问题,“ array1.getObject(0).getObject(vertice)”部分发生了什么?
Rex

@Rex请参阅Array的帮助部分:pro.arcgis.com/en/pro-app/arcpy/classes/array.htm。我从数组中获取每个点/顶点:i67.tinypic.com/34gstig.jpg
BERA

太好了,这有助于我更好地理解它。那么,为什么在数组中存在一个数组,而不仅仅是点数组?第一个数组中还有其他内容吗?array1.getObject(1)给您什么?
Rex

这是因为您在“ row [1] .getPart()”中获得了所有零件,因此,您的第一个数组是多部分特征的不同部分。因此,如果您具有多部分功能,那么您将只有array1.getObject(0)以外的东西?
Rex


2

以下python脚本(需要ArcGIS 10.1或更高版本)arcpy.da用于将shapefile用作输入,并创建一个电子表格,其中包含.shp中每个多边形中每个顶点的条目(我相信它可以与较低级别的arcgis许可证一起使用) 。对象和序列ID将点绑定到特定多边形中的特定位置。

上一篇文章@PaulSmith的H / t:获取折线的所有点以突出显示工具中的explode_to_points选项arcpy.da.FeatureClassToNumPyArray

import os
import csv
import arcpy
from os import path
from arcpy import da
from arcpy import env

env.overwriteOutput = True
env.workspace = '/folder/containing/your/shp/here'

polygon_shp = path.join(env.workspace, 'your_shapefile_name.shp')
vertex_csv_path = 'your/csv/path/here/poly_vertex.csv'

def getPolygonCoordinates(fc):
    """For each polygon geometry in a shapefile get the sequence number and
    and coordinates of each vertex and tie it to the OID of its corresponding
    polygon"""

    vtx_dict = {}
    s_fields = ['OID@', 'Shape@XY']
    pt_array = da.FeatureClassToNumPyArray(polygon_shp, s_fields, 
        explode_to_points=True)

    for oid, xy in pt_array:
        xy_tup = tuple(xy)
        if oid not in vtx_dict:
            vtx_dict[oid] = [xy_tup]
        # this clause ensures that the first/last point which is listed
        # twice only appears in the list once
        elif xy_tup not in vtx_dict[oid]:
            vtx_dict[oid].append(xy_tup)


    vtx_sheet = []
    for oid, vtx_list in vtx_dict.iteritems():
        for i, vtx in enumerate(vtx_list):
            vtx_sheet.append((oid, i, vtx[0], vtx[1]))

    writeVerticesToCsv(vtx_sheet)

def writeVerticesToCsv(vtx_sheet):
    """Write polygon vertex information to csv"""

    header = (
        'oid',          'sequence_id', 
        'x_coordinate', 'y_coordinate')

    with open(vertex_csv_path, 'wb') as vtx_csv:
        vtx_writer = csv.writer(vtx_csv)
        vtx_writer.writerow(header)

        for row in vtx_sheet:
            vtx_writer.writerow(row)

getPolygonCoordinates(polygon_shp)

我还编写了一个脚本,专门满足以下要求:标记为该问题重复的多边形中插入顶点坐标,该代码如下:

import os
import arcpy
from os import path
from arcpy import da
from arcpy import env
from arcpy import management

env.overwriteOutput = True
env.workspace = '/folder/containing/your/shp/here'

polygon_shp = path.join(env.workspace, 'your_shapefile_name.shp')
file_gdb = 'your/file/gdb/path/here/temp.gdb'

def addVerticesAsAttributes(fc):
    """Add the x,y coordinates of vertices as attributes to corresponding 
    features.  The coordinates will be in the order the appear in the geometry"""

    polygon_copy = createGdbFcCopy(fc)

    vtx_dict = {}
    s_fields = ['OID@', 'Shape@XY']
    pt_array = da.FeatureClassToNumPyArray(polygon_copy, s_fields, 
        explode_to_points=True)

    for oid, xy in pt_array:
        xy_tup = tuple(xy)
        if oid not in vtx_dict:
            vtx_dict[oid] = [xy_tup]
        # this clause ensures that the first/last point which is listed
        # twice only appears in the list once
        elif xy_tup not in vtx_dict[oid]:
            vtx_dict[oid].append(xy_tup)

    # find that largest number of points that exist within a polygon to determine 
    # the number of fields that need to be added to the shapefile
    max_vertices = 0
    for vtx_list in vtx_dict.values():
        if len(vtx_list) > max_vertices:
            max_vertices = len(vtx_list)

    xy_fields = addXyFields(polygon_copy, max_vertices)

    u_fields = ['OID@'] + xy_fields
    with da.UpdateCursor(polygon_copy, u_fields) as cursor:
        oid_ix = cursor.fields.index('OID@')
        for row in cursor:
            xy_ix = oid_ix + 1
            for vtx in vtx_dict[row[oid_ix]]:
                for coord in vtx:
                    row[xy_ix] = coord
                    xy_ix += 1

            cursor.updateRow(row)

def createGdbFcCopy(fc):
    """Create copy of the input shapefile as a file geodatabase feature class,
    because a huge number of fields may be added to the fc this preferable to shp"""

    if not arcpy.Exists(file_gdb):
        management.CreateFileGDB(path.dirname(file_gdb), 
            path.basename(file_gdb))

    polygon_copy = path.join(file_gdb, 'polygon_shp_copy')
    management.CopyFeatures(polygon_shp, polygon_copy)
    return polygon_copy

def addXyFields(fc, vtx_count):
    """Add fields to the feature class that will hold the x, y coordinates for each
    vertex, the number of fields is twice the number of most vertices in any polygon"""

    field_list = []
    f_type = 'DOUBLE'
    for i in range(1, vtx_count+1):
        f_names = ['x{0}'.format(i), 'y{0}'.format(i)]
        for fn in f_names:
            management.AddField(fc, fn, f_type)

        field_list.extend(f_names)

    return field_list

addVerticesAsAttributes(polygon_shp)

第一个python脚本完成了hpy的要求!它运作非常快!谢谢格兰特·汉弗莱斯!
ArisA

1

因此,我尚未完成解决方案,但是看起来您可以使用此工具:

转换> JSON>功能到JSON。

这会将您的shapefile(在我的情况下为81个多边形)转换为JSON文件。您可以使用文本编辑器打开它,以查看确实列出了每个多边形的所有顶点。

此外,python标准库(导入json)将json对象视为字典。然后,您可以简单地遍历字典以将顶点值(以及所需的任何其他属性)写入csv文件。如果我可以使用,我会回去并发布解决方案。


0

我只需要折线和多边形的x和y坐标。我使用了ToolBox-> Data Management工具-> Feature-> Feature to Point。这创建了一个点形状文件,然后使用同一功能菜单中的XY坐标生成XY坐标。然后,我从形状属性表中提取信息到Excel工作表中。这解决了我的问题,不确定是否要查找相同的内容。


当问题要求每个折线/多边形中的每个顶点一个点(X,Y)时,这是否只会给您每个折线/多边形一个点(X,Y)?
PolyGeo

-2

这是在绝望的时候的解决方法:

  • 开始编辑要素类或shapefile
  • 选择多边形要素,然后右键单击以“编辑顶点”
  • 右键单击其中一个顶点,然后选择“草图属性”
  • 弹出窗口将显示列出的顶点坐标
  • 拍摄坐标列表的屏幕截图
  • 将屏幕截图粘贴到您喜欢的照片/图片编辑器中,并另存为jpeg / png / bmp等
  • Google“免费在线OCR”从结果中选择一个(有些比其他更好)
  • 上传您的坐标屏幕截图文件并进行转换
  • 选择您的输出文件类型(txt,Excel等)
  • 检查结果,因为某些OCR转换器是垃圾!!!
  • 使用Arcmap中的添加X,Y数据创建点数据集。

该方法适用于小型数据集,但主要考虑的是OCR转换器的相关性/限制。请谨慎使用。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.