繁体   English   中英

NetworkX:从shapefile向图形添加边

[英]NetworkX: add edges to a graph from a shapefile

给定以下测试shapefile ,该shapefile仅由折线组成:

在此处输入图片说明

我能够重现shapefile中表示的空间网络的节点:

import networkx as nx
import matplotlib.pyplot as plt

G=nx.read_shp('C:\Users\MyName\MyFolder\TEST.shp') #Read shapefile as graph
pos = {k: v for k,v in enumerate(G.nodes())} #Get the node positions based on their real coordinates
X=nx.Graph() #Empty graph
X.add_nodes_from(pos.keys()) #Add nodes preserving real coordinates
nx.draw_networkx_nodes(X,pos,node_size=100,node_color='r')
plt.xlim(450000, 470000)
plt.ylim(430000, 450000)

在此处输入图片说明

基本上,我使用了临时图G来提取最终作为图X一部分出现的节点的位置。 这似乎工作得很好。

我的问题:遵循使用G从shapefile提取信息的相同想法,如何绘制边缘?

如果我做这样的事情

X.add_edges_from(pos.keys())

然后我得到这个错误,指向上面的行:

TypeError: object of type 'int' has no len()

加上我的评论: nx.read_shp()包含边缘信息。 G具有看起来像(x,y)节点。 draw_networkx_*pos参数需要是一个字典,以节点作为键,并以(x,y)作为值。

import networkx as nx
import matplotlib.pyplot as plt

G=nx.read_shp('C:\Users\MyName\MyFolder\TEST.shp') #Read shapefile as graph
pos = {xy: xy for xy in G.nodes()}
nx.draw_networkx_nodes(G,pos,node_size=100,node_color='r')
nx.draw_networkx_edges(G,pos,edge_color='k')
plt.xlim(450000, 470000)
plt.ylim(430000, 450000)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM