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文件


基础教程推荐
猜你喜欢
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 包装空间模型 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01