我有一个伦敦步行路径的图形(从OSMNX转换而来),包含667.588条边,具有不同的highway
属性(openstreetmap中的街道类型)。运行shortest_path算法非常慢(4秒)。为了提高速度,我希望在不丢失主要连接/城市结构的情况下以系统的方式大幅减少边缘数量,但不确定如何做到这一点?有什么建议?有没有办法将一些关闭节点分组到一个更重要的节点,从而减小尺寸?
答案 0 :(得分:2)
您可以从主图$_product = $this->_objectManager->create('Magento\Catalog\Model\Product');
$url = <yourcatname>.'_'.$sku;// just to make it unique
$url = strtolower($url);
$_product->setUrlKey($url);
//now save your product
$_product->save();
中提取所需公路类型的边:
G
在这里,我们首先初始化一个空highways_to_keep = ['motorway', 'trunk', 'primary']
H = nx.MultiDiGraph()
for u,v,attr in G.edges(data=True):
if attr['highway'] in highways_to_keep:
H.add_edge(u,v,attr_dict=attr)
H.node[u] = G.node[u]
H.node[v] = G.node[v]
,这是MultiDiGraph
使用的一种图形,然后使用主图OSMnx
中的数据填充它,如果{{1} } {}属性在我们的G
列表中。您可以在this OpenStreetMap页面找到有关高速公路类型的更多信息。
我们的图表是一个有效的NetworkX图表,但您还需要做一件事,然后才能利用OSMnx功能。如果您执行'highway'
,您将看到包含highways_to_keep
(坐标参考系统)和其他一些内容的图形属性。您应该将此信息添加到新创建的图表中:
G.graph
这是crs
,H.graph = G.graph
:
答案 1 :(得分:0)
这取决于您使用的网络类型(例如,步行,骑自行车,开车,驾车_服务,所有等)。 drive
网络类型将是最小的并且优先考虑主要路线,但是以人行道和通道为代价。
OSMnx还提供simplify the graph's topology内置函数的功能。如果您还没有这样做,这是值得做的,因为它可以有效地将图形大小减少90%,同时忠实地保留所有交叉点和死端节点以及边缘几何形状。
答案 2 :(得分:0)
由于networkx库已更改,因此上述解决方案不再起作用。具体
H.node[u] = G.node[u]
不再受支持。 以下解决方案依赖于osmnx.geo_utils.induce_subgraph,并使用节点列表作为此函数的参数。
highways_to_keep = ['motorway', 'trunk', 'primary', 'secondary', 'tertiary']
H = nx.MultiDiGraph() # new graph
Hlist = [] # node list
for u,v,attr in G.edges(data=True):
if "highway" in attr.keys():
if attr['highway'] in highways_to_keep :
Hlist.append(G.nodes[u]['osmid'])
H = ox.geo_utils.induce_subgraph(G, Hlist)
答案 3 :(得分:0)
osmnx 简化模块在这种情况下对我有用https://osmnx.readthedocs.io/en/stable/osmnx.html#module-osmnx.simplification:
<块引用>osmnx.simplification 模块
简化、纠正和整合网络拓扑。
osmnx.simplification.consolidate_intersections(G, tolerance=10, rebuild_graph=True, dead_ends=False, reconnect_edges=True)
合并包含附近节点集群的交叉点。
osmnx.simplification.simplify_graph(G, strict=True, remove_rings=True)
通过移除间隙节点来简化图的拓扑结构。