Answers:
有一个示例工具箱,其中包括将功能写入文本文件 python脚本,该脚本包括:
将要素坐标写入文本文件。
注意:
从技术上讲,“样本”工具箱中的工具已被弃用。它们仍与ArcGIS一起安装,因此您在10之前开发的任何现有脚本或模型工具都可以继续使用。
这是使用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
...
尝试使用空间技术的地理向导工具。它有几个免费工具可以满足您的需求。尝试获取多边形坐标。或多边形到点
以下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)
我只需要折线和多边形的x和y坐标。我使用了ToolBox-> Data Management工具-> Feature-> Feature to Point。这创建了一个点形状文件,然后使用同一功能菜单中的XY坐标生成XY坐标。然后,我从形状属性表中提取信息到Excel工作表中。这解决了我的问题,不确定是否要查找相同的内容。
这是在绝望的时候的解决方法:
该方法适用于小型数据集,但主要考虑的是OCR转换器的相关性/限制。请谨慎使用。