【发布时间】:2017-06-28 20:52:11
【问题描述】:
给定以下测试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()
【问题讨论】:
-
代码
X.add_edges_from(pos.keys())引发错误,因为pos.keys()是整数列表。在您的情况下,每个边都需要由 2 个ints 的元组指定(因为X中的每个节点都属于int类型)。更重要的是,nx.read_shp()已经生成了网络的边缘。我不明白你为什么要把那些扔掉...... -
我正在绘制
X,哪些节点是[0,1,2,...],所以X的边必须是[(0,1),(0,2),...]。您对nx.read_shp()的看法是正确的,但是如果您输入G.edges(),您会得到一个列表,其中代替(0,1),您会看到这两个点的坐标。所以我想我需要将G.edges()映射到pos。但我不知道该怎么做
标签: python networkx shapefile edges