pyrosm.OSM.to_graph

Contents

pyrosm.OSM.to_graph#

static OSM.to_graph(nodes, edges, graph_type='igraph', direction='oneway', from_id_col='u', to_id_col='v', edge_id_col='id', node_id_col='id', force_bidirectional=False, network_type=None, retain_all=False, osmnx_compatible=True, pandana_weights=['length'], simplify=True, simplify_kwargs=None, progress=None)#

Export OSM network to routable graph. Supported output graph types are:

  • “igraph” (default),

  • “networkx”,

  • “pandarm”,

  • “pandana” (deprecated, removed in pyrosm 1.0; use “pandarm”)

For walking, the output graph will be bidirectional by default (i.e. travel along the street is allowed to both directions). For driving and cycling, one-way streets are taken into account by default and the travel is restricted based on the rules in OSM data (the “oneway” attribute; cycling additionally honours “oneway:bicycle” so that contraflow cycling on one-way streets is modelled correctly).

The graph is topologically simplified before export: interstitial degree-2 nodes are removed and each intersection-to-intersection chain is collapsed into a single edge that keeps the full geometry, except where maxspeed or highway changes along it. This reduces the graph size while preserving routing distances and is equivalent to osmnx.simplification.simplify_graph with edge_attrs_differ=["maxspeed", "highway"]. Pass simplify=False to keep every segment of every way as its own edge.

The export shows its progress as progress says: by default a bar on stderr once it has run 2 seconds, from 0 to 100 % with the step it is in (directing the edges, keeping the largest connected part, simplifying, building the graph) in its label, cleared at the end, or without a terminal one line with the time it took.

Parameters:
  • nodes (GeoDataFrame) – GeoDataFrame containing nodes of the road network. Note: Use osm.get_network(nodes=True) to retrieve both the nodes and edges.

  • edges (GeoDataFrame) – GeoDataFrame containing the edges of the road network.

  • graph_type (str) –

    Type of the output graph. Available graphs are:
    • ”igraph” –> returns an igraph.Graph -object.

    • ”networkx” –> returns a networkx.MultiDiGraph -object.

    • ”pandarm” –> returns a pandarm.Network -object.

    • ”pandana” –> returns a pandana.Network -object. (deprecated: pandana is unmaintained and incompatible with NumPy 2 on Windows; use “pandarm” instead. Removed in pyrosm 1.0.)

  • direction (str) – Name for the column containing information about the allowed driving directions

  • from_id_col (str) – Name for the column having the from-node-ids of edges.

  • to_id_col (str) – Name for the column having the to-node-ids of edges.

  • edge_id_col (str) – Name for the column having the unique id for edges.

  • node_id_col (str) – Name for the column having the unique id for nodes.

  • force_bidirectional (bool) – If True, all edges will be created as bidirectional (allow travel to both directions).

  • network_type (str (optional)) – Network type for the given data. Determines how the graph will be constructed. If not given, the type that get_network recorded on the frames (edges.attrs["network_type"]) is used; pass it for frames that do not carry one, such as frames read from a file. By default, a bidirectional graph is created for walking and all, and a directed graph for driving and cycling (oneway streets are taken into account; cycling additionally honours oneway:bicycle for contraflow). Possible values are: ‘walking’, ‘cycling’, ‘driving’, ‘driving+service’, ‘all’ and ‘all_public’, plus the OSMnx names ‘walk’, ‘bike’, ‘drive’ and ‘drive_service’.

  • retain_all (bool) – if True, return the entire graph even if it is not connected. otherwise, retain only the connected edges.

  • osmnx_compatible (bool (default True)) – if True, modifies the edge and node-attribute naming to be compatible with OSMnx (allows utilizing all OSMnx functionalities): the node/edge id is renamed to osmid, the nodes get x/y from their geometry, in the CRS of the frames, in place of lon/lat, and node tags are stored as a JSON string. The node osmid is the key of the node in the graph and is not repeated as a node attribute. The edges get OSMnx’s oneway and reversed booleans, and the OSM text of the oneway tag is oneway_tag (see pyrosm.graphs.to_networkx()). NOTE: Only applicable with “networkx” graph type.

  • pandana_weights (list) – Columns that are used as weights when exporting to Pandana graph. By default uses “length” column.

  • simplify (bool (default True)) – If True, topologically simplify the graph before export: interstitial nodes that only carry geometry (degree-2 pass-throughs) are removed and each chain between intersections/endpoints is collapsed into a single edge that keeps the full geometry. A chain is not merged across a change of maxspeed or highway, so each edge keeps one speed limit and one road class. Edge length (and, for pandana and pandarm graphs, the pandana_weights) is summed along the chain, and so is a travel_time column, as in OSMnx; the merged geometry runs from u to v. Any other column that differs along the chain becomes a list, unless simplify_kwargs["edge_attr_aggs"] aggregates it (e.g. {"travel_time_s": sum}, as OSMnx’s edge_attr_aggs) or simplify_kwargs["length_cols"] lists it to be summed. See pyrosm.graph_simplify.simplify_graph for details. If False, every segment of every way is its own edge.

  • simplify_kwargs (dict (optional)) – Keyword arguments forwarded to pyrosm.graph_simplify.simplify_graph when simplify=True, e.g. edge_attrs_differ (the columns whose change stops a merge; None merges across every change, as OSMnx does by default), node_attrs_include (keep the nodes with a value for one of the listed node columns or tags as endpoints), remove_rings, track_merged, length_cols or edge_attr_aggs.

  • progress (bool | callable | None (default: None)) – None follows pyrosm.enable_logging(progress=…), which is True unless set. True shows the bar, False nothing, and a callable is called as progress(done, 100) with the percentage of the export done, from 0 to 100 and never back.