我受到@FelixIP的启发,但我想编写一个无需加入连接或创建额外文件的解决方案,因为我的网络非常庞大,有400K +个管道和500K +个节点。
几何网络的建立迫使节点的X,Y与管道末端重合。您可以在arcpy光标中使用形状标记访问这些位置并进行匹配。线的形状标记按绘制顺序返回顶点数组。在我的网络中,管道的绘制顺序经过大量质量检查,因为我们使用它来设置流向。因此,第一个顶点是管道的起点,而最后一个顶点是管道的终点。
参考:ASSETID =管道的ID,UNITID =管道开始的节点ID,UNITID2 =管道结束的节点ID。
nodes = "mergeNodes"
pipes = "SEWER_1"
nodeDict = {}
pipeDict = {}
#populate node dictionary with X,Y as the key and node ID as the value
for node in arcpy.da.SearchCursor(nodes, ["UNITID", "SHAPE@XY"]):
nodeDict[(node[1][0], node[1][1])] = node[0]
#populate pipe dictionary with pipe ID as the key and list of X,Y as values
#vertices populated in the order that the line was draw
#so that [0] is the first vertex and [-1] is the final vertex
for pipe in arcpy.da.SearchCursor(pipes, ["ASSETID", "SHAPE@"]):
for arrayOb in pipe[1]:
for point in arrayOb:
if pipe[0] in pipeDict:
pipeDict[pipe[0]].append((point.X, point.Y))
else:
pipeDict[pipe[0]] = [(point.X, point.Y)]
#populate UNITID with the first vertex of the line
#populate UNITID2 with the final vertex of the line
with arcpy.da.UpdateCursor(pipes, ["ASSETID", "UNITID", "UNITID2"]) as cur:
for pipe in cur:
if pipeDict[pipe[0]][0] in nodeDict:
pipe[1] = nodeDict[pipeDict[pipe[0]][0]]
if pipeDict[pipe[0]][-1] in nodeDict:
pipe[2] = nodeDict[pipeDict[pipe[0]][-1]]
cur.updateRow(pipe)