使用 write_shp 在 NetworkX 中写入 shapefile

Write shapefile in NetworkX using write_shp

我正在尝试比较有序的街道网络与随机生成的街道网络。

作为起点,我使用 NetworkX 创建了一个随机图,每个节点都有一个随机位置。我想将它导出到一个 shapefile,以便我可以在 ArcGIS 中使用它。我检查了 NetworkX 文档,很高兴看到他们有一个 write_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')

我收到以下错误跟踪:

File "D:\ProgramFiles\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
    execfile(filename, namespace)

  File "D:\ProgramFiles\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Users/Dipto/Desktop/CSProceedings_AuthorTools_Word_2003/createGrapg.py", line 62, in <module>
    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 157, in netgeometry
    fkey = [float(x) for x in key]

ValueError: could not convert string to float: A

但是,当我打开文件夹 'trialAgainShp' 时,我确实看到了用名称 'node' 创建的 shapefile,但它们是空的。

我不确定我哪里错了


编辑:

我又尝试了两件事:

  1. 我认为可能节点必须具有数字标签才能转换为 shapefile,而将标签更改为字母导致失败。
  2. 我尝试使用以下代码将 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__'

如果你想使用我认为合理的元组键,你必须将坐标存储为节点的键(而不是属性)。

快速试用修改一下您的代码:

In [26]:

mapping = {}
counter = 0
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})
        mapping[counter] = co_od
        counter += 1

In [28]:

g=nx.relabel_nodes(g, mapping)

In [29]:

nx.write_shp(g, '...')

希望对您有所帮助。