transitio.edit.snap_to_network

transitio.edit.snap_to_network#

transitio.edit.snap_to_network(waypoints, pbf, *, network_type='driving', custom_filter=None, filter_type='keep')#

Route a waypoint sequence along an OSM network.

Loads the network from an OSM extract (as fetched by transitio.fetch_pbf()), snaps each waypoint to its nearest network node, and connects consecutive waypoints with the shortest network path — the mechanism behind snapped route drawing for bus, tram and rail alignments.

Parameters:
  • waypoints (sequence of (lat, lon)) – At least two points, in visit order.

  • pbf (str or pathlib.Path) – OSM .osm.pbf extract covering the waypoints.

  • network_type (str, default "driving") – pyrosm network type (walking, cycling, driving, driving+service, all). Ignored when custom_filter is given.

  • custom_filter (dict, optional) – A pyrosm Overpass-style tag filter selecting which OSM ways form the routing network, e.g. {"railway": ["tram"]} to snap tram alignments to their rails or {"railway": ["rail", "light_rail"]} for heavy rail. When given, the network is restricted to exactly the matching ways (pyrosm network_type "all" with filter_type); when None, the plain network_type network is used.

  • filter_type (str, default "keep") – pyrosm filter mode for custom_filter ("keep" or "exclude").

Returns:

The snapped alignment in WGS84 (x = longitude, y = latitude).

Return type:

shapely.LineString

Notes

Requires the networkx package (install transitio[snap]). Snapping tram or rail routes needs an extract that actually contains the railway ways (some cropped extracts drop them); without a custom_filter the driving network carries street-running trams but not dedicated rails. The network is loaded once per extract and filter and reused by later calls; an extract rewritten since is loaded again.