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.pbfextract covering the waypoints.network_type (str, default "driving") – pyrosm network type (
walking,cycling,driving,driving+service,all). Ignored whencustom_filteris 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 (pyrosmnetwork_type"all"withfilter_type); whenNone, the plainnetwork_typenetwork 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
networkxpackage (installtransitio[snap]). Snapping tram or rail routes needs an extract that actually contains therailwayways (some cropped extracts drop them); without acustom_filterthe 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.