Networkx写入Shape文件 [英] Networkx Writing to Shape File

查看:32
本文介绍了Networkx写入Shape文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python3.8和Networkx v2.4编写shapefile。


import networkx as nx
import pandas as pd


f= open('Onslowedge.edgelist','rb')
G = nx.read_edgelist(f)

latlong = pd.read_csv('latlong.csv',index_col=0)

for index in latlong.index:
    G.nodes[str(index)]['x'] = latlong["Longitude"][index]
    G.nodes[str(index)]['y'] = latlong["Latitude"][index]

H= nx.DiGraph()
counter = 0
mapping = dict()
for node in list(G.nodes()):
    xx, yy = G.nodes[str(node)]['x'], G.nodes[str(node)]['y']
    H.add_node(str(node))
    nx.set_node_attributes(H, {str(node): (xx, yy)}, 'loc')
    mapping[node] = (xx,yy)

H1 = nx.relabel_nodes(H,mapping)    
for edge in list(G.edges()):
    e = (mapping[str(edge[0])], mapping[str(edge[1])])
    H1.add_edge(*e)

nx.write_shp(H1, '\shapefile')
我之所以从GRAPH创建DIGRAPH的副本作为networkxwrite_shp函数,原因是它只能接受DIGRAPH作为输入。 根据在StackOverflow中找到的答案,我根据节点本身的坐标重新标记了它们。

这是错误:

nx.write_shp(H1, '\shapefile')
Traceback (most recent call last):

  File "<ipython-input-61-796466305294>", line 1, in <module>
    nx.write_shp(H1, '\shapefile')

  File "C:Userssssaha.condaenvs	fgpulibsite-packages
etworkx
eadwrite
x_shp.py", line 308, in write_shp
    create_feature(g, nodes, attributes)

  File "C:Userssssaha.condaenvs	fgpulibsite-packages
etworkx
eadwrite
x_shp.py", line 259, in create_feature
    feature.SetField(field, data)

  File "C:Userssssaha.condaenvs	fgpulibsite-packagesosgeoogr.py", line 4492, in SetField
    return _ogr.Feature_SetField(self, *args)

NotImplementedError: Wrong number or type of arguments for overloaded function 'Feature_SetField'.
  Possible C/C++ prototypes are:
    OGRFeatureShadow::SetField(int,char const *)
    OGRFeatureShadow::SetField(char const *,char const *)
    OGRFeatureShadow::SetField(int,double)
    OGRFeatureShadow::SetField(char const *,double)
    OGRFeatureShadow::SetField(int,int,int,int,int,int,float,int)
    OGRFeatureShadow::SetField(char const *,int,int,int,int,int,float,int)

我不确定如何解决此问题,也不确定问题的实际原因。

推荐答案

我刚刚遇到了同样的错误,花了很长时间才弄清楚。简言之,Feature_SetField只能接受int, str, float。在您的代码nx.set_node_attributes(H, {str(node): (xx, yy)}, 'loc')中,节点功能是一个元组。

删除此行或仅更改为nx.set_node_attributes(H, {str(node): xx}, 'loc')即可解决此错误。

这篇关于Networkx写入Shape文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆