我正在尝试使用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')我之所以从图中创建DIGRAPH的副本,因为networkx write_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:\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)我不确定如何解决这个问题,也不知道是什么导致了这个问题。
发布于 2020-08-13 18:56:08
这应该是一个评论,但我缺乏声誉:
我在不同的情况下遇到了同样的错误,我解决了这个问题,通过将我所有的整数值转换为float或string,因为shapefile在一般实现中不会造成int和float之间的差异。而ogr的Python绑定没有对此进行适当的类型转换。但我不确定您是否正在尝试将任何整数数据写入您的属性。
也许这会有帮助:
xx, yy = float(G.nodes[str(node)]['x']), float(G.nodes[str(node)]['y'])但我通过检查ogr.py和nx_shp.py的源代码了解到了这一点(并临时修改了ogr.py,这样我就可以弄清楚当时处理的是哪种值)。也许这对你也有帮助。
发布于 2021-08-13 22:32:15
我只是遇到了同样的错误,并花了很长时间来解决它。简而言之,Feature_SetField只能接受int, str, float。在您的代码nx.set_node_attributes(H, {str(node): (xx, yy)}, 'loc')中,节点特性是一个元组。
删除此行或仅更改为nx.set_node_attributes(H, {str(node): xx}, 'loc')应可解决此错误。
https://stackoverflow.com/questions/62754277
复制相似问题