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-m http.server 443--使用SSL? 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 沿轴计算直方图 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01