微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何比较两个图中的节点和边?

如何解决如何比较两个图中的节点和边?

我有柏林在 2020 年 1 月 1 日和 2021 年 1 月 1 日的图表,但如何比较两张地图中边和节点的变化?

import osmnx as ox
import pandas as pd
import networkx as nx 
import matplotlib.pyplot as plt
from itertools import combinations
from IPython.display import clear_output
import matplotlib.cm as cm
import matplotlib.colors as colors
from IPython.display import Image

在 2021-01-01 获取柏林网络。

ox.utils.config(overpass_settings='[out:json][timeout:180][date:"2021-01-01T00:00:00Z"]')

G_21=ox.graph.graph_from_place('Berlin,Germany',network_type='all_private',simplify=True,retain_all=True,truncate_by_edge=False,which_result=None,buffer_dist=None,clean_periphery=True,custom_filter='["highway"~"cycleway|path|living_street"]["bicycle"!~"no"]')
fig,ax = ox.plot_graph(G_21,node_size=1,edge_linewidth=0.5)

2020-01-01 获取柏林网络

ox.utils.config(overpass_settings='[out:json][timeout:180][date:"2020-01-01T00:00:00Z"]')

G_20=ox.graph.graph_from_place('Berlin,ax = ox.plot_graph(G_20,edge_linewidth=0.5)

现在我想知道如何识别节点和边的变化,然后在G_21中标记添加的和在G_20中删除的。

解决方法

请记住,OSMnx 仅使用 NetworkX 图对象,因此您可以像处理任何 NetworkX 图一样处理您的两个结果图,包括使用设置逻辑处理它们的节点/边:

import osmnx as ox
place = 'Berlin,Germany'
cf = '["highway"~"cycleway|path|living_street"]["bicycle"!~"no"]'
settings = '[out:json][timeout:180][date:"{year}-01-01T00:00:00Z"]'

# get the 2021 graph
ox.utils.config(overpass_settings=settings.format(year=2021))
G_21 = ox.graph.graph_from_place(place,custom_filter=cf,retain_all=True)

# get the 2020 graph
ox.utils.config(overpass_settings=settings.format(year=2020))
G_20 = ox.graph.graph_from_place(place,retain_all=True)

# identify the nodes/edges that were deleted/added
nodes_del = G_20.nodes - G_21.nodes
nodes_add = G_21.nodes - G_20.nodes
edges_del = G_20.edges - G_21.edges
edges_add = G_21.edges - G_20.edges

除了确定删除/添加了哪些节点/边之外,您可能还对它们的属性值的更改感兴趣,在这种情况下,您只需检查节点/边的属性字典并查看哪些键/值组合发生了更改.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。