我正在尝试使用python 3.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')
我之所以从图形中创建有向图副本作为networkxwrite_shp
函数,原因是它只能将有向图作为输入。
根据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:\Users\sssaha\.conda\envs\tfgpu\lib\site-packages\networkx\readwrite\nx_shp.py", line 308, in write_shp
create_feature(g, nodes, attributes)
File "C:\Users\sssaha\.conda\envs\tfgpu\lib\site-packages\networkx\readwrite\nx_shp.py", line 259, in create_feature
feature.SetField(field, data)
File "C:\Users\sssaha\.conda\envs\tfgpu\lib\site-packages\osgeo\ogr.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')
将解决此错误这应该是一个评论,但我缺乏声誉:
我在另一个案例中遇到了相同的错误,我解决了这个问题,将所有整数值转换为float或string,因为在一般实现中,shapefile不会在int和float之间产生差异。而ogr的Python绑定没有为此进行适当的类型转换。但我不确定您是否尝试将任何整数数据写入属性中
也许这有助于:
但我是通过检查ogr.py和nx_shp.py的源代码了解到这一点的(并暂时修改了ogr.py,这样我就可以知道当时处理的是什么类型的值)。也许这对你也有帮助
相关问题 更多 >
编程相关推荐