python - 使用 networkx.algorithms.approximation.steinertree.steiner_tree 提取 Steiner 树时出错
问题描述
我导出了两组数据:
- 我从 QGIS 导出为 .shp 文件的道路数据
- 我从 QGIS 导出为 .shp 文件的节点(经纬度)的点层
我想使用 networkx 库来提取连接给定道路上所有节点的施泰纳树。为此,我在 jupyter notebook 上编写了以下代码:
import networkx as nx #importing the NetworkX library
Road = nx.read_shp('proj_data/roads/cmbRoads.shp') #Reading Road Data
Base = nx.read_shp('proj_data/bs/bsSnapped.shp') #Reading Terminal Node Data
nodes = list(Base.nodes) #Creating list of terminal nodes
from networkx.algorithms import approximation as ax
st_tree = ax.steinertree.steiner_tree(Road,nodes,weight='length')
直到 Steiner 树提取的所有代码行都已执行,没有任何问题。我收到以下错误消息:
---------------------------------------------------------------------------
NetworkXNotImplemented Traceback (most recent call last)
<ipython-input-5-99884445086e> in <module>
1 from networkx.algorithms import approximation as ax
----> 2 st_tree = ax.steinertree.steiner_tree(Road,nodes,weight='length')
<c:\users\nandula\appdata\local\programs\python\python37\lib\site-packages\decorator.py:decorator-gen-849> in steiner_tree(G, terminal_nodes, weight)
c:\users\nandula\appdata\local\programs\python\python37\lib\site-packages\networkx\utils\decorators.py in _not_implemented_for(not_implement_for_func, *args, **kwargs)
80 raise nx.NetworkXNotImplemented(msg)
81 else:
---> 82 return not_implement_for_func(*args, **kwargs)
83 return _not_implemented_for
84
<c:\users\nandula\appdata\local\programs\python\python37\lib\site-packages\decorator.py:decorator-gen-848> in steiner_tree(G, terminal_nodes, weight)
c:\users\nandula\appdata\local\programs\python\python37\lib\site-packages\networkx\utils\decorators.py in _not_implemented_for(not_implement_for_func, *args, **kwargs)
78 if match:
79 msg = 'not implemented for %s type' % ' '.join(graph_types)
---> 80 raise nx.NetworkXNotImplemented(msg)
81 else:
82 return not_implement_for_func(*args, **kwargs)
NetworkXNotImplemented: not implemented for directed type
对我在这里可能做错的任何见解或我可能完成此操作的替代方式(可能是geopandas)都会有所帮助。
注意:我没有使用 QGIS 本身中的处理工具箱,因为我的个人计算机上的 RAM 不足(数据集非常大),我需要在 CentOS 服务器上运行此代码。
解决方案
问题是您从 QGIS 数据创建的图似乎是有向图,并且该算法仅适用于无向图。
我建议您Road
使用nx.is_directed
and来检查您的图表是哪种类型的图表nx.is_multigraphical
。
您可以转换为无向图undirected_roads = nx.Graph(Road)
,然后调用算法ax.steinertree.steiner_tree(undirected_roads,nodes,weight='length')
。但是,根据原始图形的不对称程度,您会丢失一些信息。
推荐阅读
- postgresql - postgresql - 如何在聚合表和连接表输出中显示空值?
- python - Python Pandas - 根据多个条件清理数据列
- sml - 来自 2 个列表 SML 的交替元素
- objective-c - 从 Swift 访问 Objective-C #define 常量
- python - 仅使用列表列表中的某些项目创建 dict
- laravel - 如何在 eloquent 的子查询函数中使用“Select()”
- prometheus - Grafana Prometheus“信息”指标
- python-3.x - 仅设置图形大小的宽度,没有高度(Python)
- c# - 从 ADLS Gen2 获取数据并将其绑定到 Angular UI 的有效方法是什么?
- python - 将列表值转换为布尔值