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


基础教程推荐
猜你喜欢
- Python 的 List 是如何实现的? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01