在 OSMNX 中对 nodes/intersections 应用权重

Applying weightings to nodes/intersections in OSMNX

我一直在寻找一种方法来将权重应用于 OSMNX 上的 nodes/intersections 本身,而不是边缘。任何人都知道或弄清楚该软件包是否允许为节点赋予权重?

您需要从提取的 OSMnx 图中构造一个 geodataframe。因此,您可以添加一个额外的列,您可以在其中根据您的首选标准分配权重。

在下面的示例中,我为所有节点分配了 1.0 的权重,但构成 traffic_signal 的节点除外。对于那些,我指定了 3.0 的权重。

import osmnx as ox
G = ox.graph_from_bbox( -37.8051, -37.7915, 144.9548, 144.9666, network_type='drive')
nodes = ox.graph_to_gdfs(G, nodes=True, edges=False)
nodes['weight'] = 1.0
for i in range(0,len(nodes)):
    if nodes['highway'].iloc[i] == 'traffic_signals':
        nodes['weight'].iloc[i] = 3.0

print(nodes.head())

                    y           x       osmid          highway                     geometry  weight
1725216770 -37.804785  144.964815  1725216770              NaN  POINT (144.96482 -37.80479)     1.0
2209383436 -37.794562  144.958250  2209383436              NaN  POINT (144.95825 -37.79456)     1.0
137181198  -37.800589  144.965390   137181198  traffic_signals  POINT (144.96539 -37.80059)     3.0
324235283  -37.798423  144.965710   324235283              NaN  POINT (144.96571 -37.79842)     1.0
1954066453 -37.795703  144.956705  1954066453              NaN  POINT (144.95670 -37.79570)     1.0

与 Debjit Bhowmick 的回答类似,您可以直接使用 Networkx 更有效地执行此操作:

import networkx as nx
import osmnx as ox
G = ox.graph_from_place('Piedmont, CA, USA', network_type='drive')

# create node weights
w = {k: 3 if v == 'turning_circle' else 1 for k, v in G.nodes(data='highway')}
nx.set_node_attributes(G, w, 'weight')