def getNetworkGraph(segments,segmentlengths):
"""
Builds a networkx graph from the network file, inluding segment length taken from arcpy.
It selects the largest connected component of the network (to prevent errors from routing between unconnected parts)
"""
#generate the full network path for GDAL to be able to read the file
path =str(os.path.join(arcpy.env.workspace,segments))
print path
if arcpy.Exists(path):
g = nx.read_shp(path)
#This selects the largest connected component of the graph
sg = list(nx.connected_component_subgraphs(g.to_undirected()))[0]
print "graph size (excluding unconnected parts): "+str(len(g))
# Get the length for each road segment and append it as an attribute to the edges in the graph.
for n0, n1 in sg.edges_iter():
oid = sg[n0][n1]["OBJECTID"]
sg.edge[n0][n1]['length'] = segmentlengths[oid]
return sg
else:
print "network file not found on path: "+path
评论列表
文章目录