在做最短路径等应用时,我们需要确定每条道路起点和终点的ID
在这里写了一份运用ArcPy来让线要素找到它的起点终点的脚本。
已有数据:
- 道路polyline
- 交叉点point
输出:
- [道路ID, 起点ID, 终点ID] 的列表
# encoding: utf-8
import arcpy
def ifClose (x1, y1, x2, y2) :
if abs(x1-x2) < 0.00001 and abs(y1-y2) < 0.00001 :
return True
else :
return False
pointPath = r"point_4326.shp"
def findNode (x, y) :
ptFields = arcpy.ListFields(pointPath)
ptFieldsArray = ['SHAPE@', u'FID']
for row in arcpy.da.SearchCursor(pointPath, ptFieldsArray):
if row[0] is None: continue
xp = row[0].firstPoint.X
yp = row[0].firstPoint.Y
if ifClose (x, y, xp, yp):
return row[1]
inFC = r"road_4326.shp"
theFields = arcpy.ListFields(inFC)
FieldsArray = ['SHAPE@', u'FID']
for row in arcpy.da.SearchCursor(inFC, FieldsArray):
xs = row[0].firstPoint.X
ys = row[0].firstPoint.Y
xe = row[0].lastPoint.X
ye = row[0].lastPoint.Y
lineId = row[1]
nodeSid = findNode(xs, ys)
nodeEid = findNode(xe, ye)
print [lineId, nodeSid, nodeEid]