-
Notifications
You must be signed in to change notification settings - Fork 3
/
readShpCoordinates
82 lines (64 loc) · 2.73 KB
/
readShpCoordinates
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#-*- coding: utf-8 -*-
"""
Created on Wed May 22 14:00:59 2019
@author: Arlan
@Desc : 使用arcpy的令牌读取点几何,多点几何,读取折线和面几何的经纬度
"""
import os
import arcpy
import time
def readPoint(fc):
print("读取点几何开始运行")
flag = 1
#for row in arcpy.da.SearchCursor(fc, ["SHAPE@XY"]):
with arcpy.da.SearchCursor(fc,["SHAPE@XY"]) as cursor:
for row in cursor:
print("这是第" + str(flag) + "个点要素")
flag += 1
# 打印每个点要素的x,y坐标
x, y = row[0]
print("{}, {}".format(x, y))
def readmultiPoint(fc):
print("读取多点几何开始运行")
for row in arcpy.da.SearchCursor(infc, ["OID@", "SHAPE@"]):
# 打印当前多点要素的ID
print("Feature {}:".format(row[0]))
# 遍历每个多点的内部点
# 打印坐标
for pnt in row[1]:
print("{}, {}".format(pnt.X, pnt.Y))
def readPolygonAndPolyline(fc):
print("读取面或者线几何开始运行")
#for row in arcpy.da.SearchCursor(fc, ["OID@", "SHAPE@"]):
with arcpy.da.SearchCursor(fc, ["OID@", "SHAPE@"]) as cursor:
for row in cursor:
# 打印当前要素的ID(即属性列表的记录个数,比如一个面要素由十个多边形组成)
print("Feature {}:".format(row[0]))
partnum = 0
# 分解每个要素的部分(比如环,有内部和外部之分)
for part in row[1]:
#打印部分点的编号
print("Part {}:".format(partnum))
#分别打印每个点
for pnt in part:
if pnt:
# 打印当前点的经纬度
print("{}, {}".format(pnt.X, pnt.Y))
else:
# 如果pnt为空,则表示一个内部环
print("Interior Ring:")
partnum += 1
if __name__ == '__main__':
print("程序开始运行")
s_time=time.time()
datadir = r'E:\data\test' #文件根目录
pointFile = datadir + os.sep + 'point.shp'#点shp文件名
#multiPointFile = datadir + os.sep + 'xxx.shp'#多点几何shp文件
polylineFile = datadir + os.sep + 'line.shp'#折线几何shp文件
polygonFile = datadir + os.sep + 'polygon.shp'#面几何shp文件
readPoint(pointFile)#读取点几何数据
#readmultiPoint(multiPointFile)#读取多点几何数据
readPolygonAndPolyline(polylineFile)#读取面几何数据
readPolygonAndPolyline(polygonFile)#读取面几何数据
e_time=time.time()
print("程序共耗时: %.2f s!" % (e_time - s_time))