如何比较两个图中的节点和边? [英] How to compare the nodes and edges in two graphs?

查看:79
本文介绍了如何比较两个图中的节点和边?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got graphs of Berlin on January 1, 2020 and January 1, 2021, but how do I compare the changes of edges and nodes in the two maps?

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 获取柏林网络.

Getting the network of Berlin in 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 获取柏林网络

Getting the network of Berlin in 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,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_20,node_size=1,edge_linewidth=0.5)

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

Now I wonder how to identify the changes of nodes and edges, then mark the added ones in G_21 and deleted ones in G_20.

推荐答案

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

Remember that OSMnx just uses NetworkX graph objects, so you can treat your two resulting graphs the same way you would any NetworkX graph, including handling their nodes/edges with set logic:

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, custom_filter=cf, 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

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

Beyond identifying which nodes/edges were deleted/added, you may also be interested in the changes to their attribute values, in which case you can just inspect the attribute dicts of your nodes/edges and see which key/value combos changed.

这篇关于如何比较两个图中的节点和边?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆