Операции с графами

Этот ноутбук показывает распространённые операции редактирования и преобразования объектов UrbanGraph.

# To install IduEdu in a clean environment:
# !pip install IduEdu

OSM_ID = 1114252
from iduedu import get_walk_graph

G = get_walk_graph(osm_id=OSM_ID, simplify=False, keep_largest_subgraph=True)
print(G)
2026-07-07 15:18:10.561 | WARNING  | Removing 618 nodes outside the largest connected component. Retaining 38246 of 38864 nodes.
UrbanGraph(nodes=38246, edges=45042, is_multigraph=True, is_directed=False, edge_direction_column=None, crs='EPSG:32636', type='walk')

Порождённый подграф и перенумерация

subgraph_by_nodes сохраняет исходные идентификаторы узлов. relabel переводит их в плотный RangeIndex.

nodes = G.nodes_gdf.index[:150]
subgraph = G.subgraph_by_nodes(nodes)
relabeled = subgraph.relabel()

print("subgraph index sample:", list(subgraph.nodes_gdf.index[:5]))
print("relabeled index sample:", list(relabeled.nodes_gdf.index[:5]))
print(subgraph)
print(relabeled)
subgraph index sample: [0, 1, 2, 3, 4]
relabeled index sample: [0, 1, 2, 3, 4]
UrbanGraph(nodes=150, edges=146, is_multigraph=True, is_directed=False, edge_direction_column=None, crs='EPSG:32636', type='walk')
UrbanGraph(nodes=150, edges=146, is_multigraph=True, is_directed=False, edge_direction_column=None, crs='EPSG:32636', type='walk')

Обрезка по геометрии

clip оставляет узлы внутри полигона, а затем рёбра, оба конца которых остались в графе. Как и territory в функциях построения графов, полигон shapely сам по себе читается в EPSG:4326 (долгота, широта); полигон в других координатах передаётся в GeoSeries или GeoDataFrame со своей системой координат и перепроецируется в систему координат графа.

import geopandas as gpd

center = G.nodes_gdf.geometry.iloc[0]
# The buffer is in metres, in the graph CRS: wrap it with that CRS.
clip_polygon = gpd.GeoSeries([center.buffer(700)], crs=G.crs)
clipped = G.clip(clip_polygon).relabel()

print("before:", len(G.nodes_gdf), "nodes,", len(G.edges_gdf), "edges")
print("after :", len(clipped.nodes_gdf), "nodes,", len(clipped.edges_gdf), "edges")
before: 38246 nodes, 45042 edges
after : 5365 nodes, 6459 edges
import matplotlib.pyplot as plt

clip_boundary = clip_polygon
fig, ax = plt.subplots(figsize=(7, 7))
G.edges_gdf.plot(ax=ax, color="#8FA1AB", linewidth=0.65, alpha=0.65)
clipped.edges_gdf.plot(ax=ax, color="#496A81", linewidth=1.0, alpha=0.9)
clip_boundary.boundary.plot(ax=ax, color="#D84A3A", linewidth=1.5)
ax.set_axis_off()
plt.show()
../_images/5224dd9dbe430046e7f43cdf03d8bcf06c595a18fef881316b4d2948907695d1.png

Упрощение кратных рёбер

simplify_multiedges сводит кратные рёбра, выбирая минимальное или максимальное значение веса.

simple_graph = G.simplify_multiedges(weight="time_min", rule="min")
print("multigraph:", G.is_multigraph, "edges:", len(G.edges_gdf))
print("simple    :", simple_graph.is_multigraph, "edges:", len(simple_graph.edges_gdf))
multigraph: True edges: 45042
simple    : False edges: 45042

Ориентированный и неориентированный вид

Используйте эти преобразования, когда алгоритму нужна определённая модель направлений.

directed = G.to_directed(edge_direction_column="oneway", default_direction_value=False)
undirected = directed.to_undirected()

print(directed.is_directed, directed.edge_direction_column)
print(undirected.is_directed, undirected.edge_direction_column)
True oneway
False None

Объединение совместимых графов

join_urban_graphs и UrbanGraph.join объединяют таблицы совместимых графов. В примере ниже идентификаторы узлов второго небольшого графа сдвинуты, чтобы ключи не совпадали.

left = clipped.relabel()
right = clipped.copy().relabel()

node_offset = int(left.nodes_gdf.index.max()) + 1 if len(left.nodes_gdf) else 0
right.nodes_gdf.index = right.nodes_gdf.index + node_offset
if not right.edges_gdf.empty:
    right.edges_gdf["u"] = right.edges_gdf["u"] + node_offset
    right.edges_gdf["v"] = right.edges_gdf["v"] + node_offset
right.validate()

joined = left.join(right, graph_type="joined_walk_sample")
joined
UrbanGraph(nodes=10730, edges=12918, is_multigraph=True, is_directed=False, edge_direction_column=None, crs='EPSG:32636', type='joined_walk_sample')