我试图比较有序的街道网络和随机生成的街道网络。在
首先,我用NetworkX创建了一个随机图,每个节点都有一个随机位置。我想把这个导出到一个shapefile,这样我就可以在ArcGIS中使用它了。我查看了NetworkX documentattion,很高兴看到他们有一个write\u shp()方法。他们的文件提到:"Nodes and edges are expected to have a Well Known Binary (Wkb) or
Well Known Text (Wkt) key in order to generate geometries. Also
acceptable are nodes with a numeric tuple key (x,y)."
我决定将每个节点的位置存储为(x,y)元组,而不是将WKT作为节点的属性。在
以下是我使用的代码:g=nx.fast_gnp_random_graph(15, 0.25)
#Relabel Nodes
mapping=dict(zip(g.nodes(),"ABCDEFGHIJKLMNO"))
g=nx.relabel_nodes(g, mapping)
radius=100000 #100 Km
for d in g.nodes_iter(data=True): #I know this is a round about way to do this, but I might need node attributes later
#Generate point location
t = random.random() * (math.pi * math.pi)
r = radius * math.sqrt(random.random())
x = r * math.cos(t)
y = r * math.sin(t)
co_od=(x,y)
nx.set_node_attributes(g, 'loc', {d[0]: co_od})
nx.write_shp(g, './shp/trialAgainShp')
我得到以下错误跟踪:
^{pr2}$
但是,当我打开文件夹“trialAgainShp”时,我确实看到用名称“node”创建的shapefile,但是它们是空的。在
我不知道我哪里出错了
编辑:
我又试了两件事:我想可能节点必须有数字标签才能转换为shapefile,而将标签更改为字母表是导致失败的原因。在
我尝试将WKT添加到每个节点,并将以下代码作为FOR循环的最后一行:#Create WKT
wkt='POINT(' + str(x) + ' ' + str(y) + ')'
print wkt
nx.set_node_attributes(g, 'WKT', {d[0]: wkt})
第一个选项没有任何效果,因为错误保持不变。第二个命令将错误更改为以下内容:...
nx.write_shp(g, './shp/trialAgainShp')
File "D:\ProgramFiles\Anaconda\lib\site-packages\networkx\readwrite\nx_shp.py", line 183, in write_shp
g = netgeometry(n, data)
File "D:\ProgramFiles\Anaconda\lib\site-packages\networkx\readwrite\nx_shp.py", line 139, in netgeometry
elif type(key[0]).__name__ == 'tuple': # edge keys are packed tuples
TypeError: 'int' object has no attribute '__getitem__'