首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Networkx写入Shape文件

Networkx写入Shape文件
EN

Stack Overflow用户
提问于 2020-07-06 18:37:21
回答 2查看 343关注 0票数 2

我正在尝试使用python 3.8和Networkx v2.4编写一个shapefile。

代码语言:javascript
复制
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中找到的答案,我根据节点本身的坐标重新标记节点。

这是错误:

代码语言:javascript
复制
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)

我不确定如何解决这个问题,也不知道是什么导致了这个问题。

EN

回答 2

Stack Overflow用户

发布于 2020-08-13 18:56:08

这应该是一个评论,但我缺乏声誉:

我在不同的情况下遇到了同样的错误,我解决了这个问题,通过将我所有的整数值转换为float或string,因为shapefile在一般实现中不会造成int和float之间的差异。而ogr的Python绑定没有对此进行适当的类型转换。但我不确定您是否正在尝试将任何整数数据写入您的属性。

也许这会有帮助:

代码语言:javascript
复制
xx, yy = float(G.nodes[str(node)]['x']), float(G.nodes[str(node)]['y'])

但我通过检查ogr.py和nx_shp.py的源代码了解到了这一点(并临时修改了ogr.py,这样我就可以弄清楚当时处理的是哪种值)。也许这对你也有帮助。

票数 0
EN

Stack Overflow用户

发布于 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')应可解决此错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62754277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档