用Arcpy读取shapefile
目录
本文主要从两个部分介绍:
1. shapefile文件属性表的读取
2. shapefile内每个元素的坐标信息读取
属性表读取
分这几步走:
1. arcpy.ListFields函数解析shp文档所在路径,并返回列名数组
2. arcpy.da.SearchCursor读取列名下的属性表内容
inFC = 'C:\Users\Wu823\Desktop\data_test\starbucks.shp;'
theFields = arcpy.ListFields(inFC)
FieldsArray = []
for Field in theFields:
FieldsArray.append(Field.aliasName)
for row in arcpy.da.SearchCursor(inFC, FieldsArray):
print row
坐标信息读取
坐标信息也是通过arcpy.da.SearchCursor函数来获取。和上面不同的是,列名需要换成几何令牌
ArcGIS Manual上面的解释内容如下:
- SHAPE@XY —A tuple of the feature’s centroid x,y coordinates.
- SHAPE@TRUECENTROID —A tuple of the feature’s true centroid x,y coordinates.
- SHAPE@X —A double of the feature’s x-coordinate.
- SHAPE@Y —A double of the feature’s y-coordinate.
- SHAPE@Z —A double of the feature’s z-coordinate.
- SHAPE@M —A double of the feature’s m-value.
- SHAPE@JSON — The esri JSON string representing the geometry.
- SHAPE@WKB —The well-known binary (WKB) representation for OGC geometry. It provides a portable representation of a geometry value as a contiguous stream of bytes.
- SHAPE@WKT —The well-known text (WKT) representation for OGC geometry. It provides a portable representation of a geometry value as a text string.
- SHAPE@ —A geometry object for the feature.
- SHAPE@AREA —A double of the feature’s area.
- SHAPE@LENGTH —A double of the feature’s length.
- OID@ —The value of the ObjectID field.
我这里主要用到SHAPE@XY和SHAPE@两个令牌
- SHAPE@XY:返回几何要素的中心坐标
- SHAPE@:返回一整个几何要素
第一个很好理解,就是个二元数组。
第二个,整个几何要素。
几何要素包含点线面等。根据个人实际操作经验,几何要素其实就是多维数组。点是一维,面和线是三维(点->部分->记录)。searchCursor的返回值也确实如此。
# A list of features and coordinate pairs
feature_info = [[[1, 2], [2, 4], [3, 7]],
[[6, 8], [5, 7], [7, 2], [9, 5]]]
for row in arcpy.da.SearchCursor(inFC, 'SHAPE@'):
for part in row:
for pnt in part:
print pnt.X
print pnt.Y
参考来源:http://pro.arcgis.com/en/pro-app/arcpy/get-started/reading-geometries.htm
转载自:https://blog.csdn.net/qyh666/article/details/52907122