Networkx Writing to Shape File(Networkx写入Shape文件)
本文介绍了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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:Networkx写入Shape文件
基础教程推荐
猜你喜欢
- Discord.py 缺少必需的参数 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
