4

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

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)

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)

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.

gboeing
  • 5,691
  • 2
  • 15
  • 41
Peter Fu2
  • 41
  • 3

1 Answers1

2

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.

gboeing
  • 5,691
  • 2
  • 15
  • 41