Migrating from v1¶
While all of the functionality from v1 is retained in v2, the API has been completely
overhauled. The old Router class is no longer present (being replaced by simple functions,
find_route() and find_route_without_turn_around()). Datastore has
been replaced by osm.Graph and osm.LiveGraph classes.
Datastore¶
Main usage (instantiating a graph, finding nodes close to start and end position, finding the shortest path and converting node ids to lat-lon pairs) is presented in the main page.
Instead of r = Router("car", "my-local-file.osm.pbf", localFileType="pbf") do this:
with open("my-local-file.osm.pbf", "rb") as f:
graph = pyroutelib3.osm.Graph.from_file(pyroutelib3.osm.CarProfile(), f)
To load data automatically from OSM, instead of r = Router("car"), do this:
graph = pyroutelib3.osm.LiveGraph(pyroutelib3.osm.CarProfile())
Initialization arguments
v1 |
v2 |
|---|---|
transport |
profile |
localfile |
|
localfileType |
|
expireData |
tile_expiry_seconds ( |
ignoreDataErrs |
no direct replacement, use |
distFunction |
not applicable, OSM data implies |
pyroutelib3.TILES_ZOOM |
|
Attributes/Properties
v1 |
v2 |
|---|---|
rnodes |
|
routing |
|
mandatoryMoves |
not applicable, turn restrictions are directly represented in the graph |
forbiddenMoves |
not applicable, turn restrictions are directly represented in the graph |
distance |
not applicable, OSM data implies |
transport |
not applicable, some profiles have a |
type |
|
localFile |
not applicable, use |
pyroutelib3.TILES_ZOOM |
|
Methods
v1 |
v2 |
|---|---|
getArea |
|
loadOsm |
|
storeWay |
not possible to add a single way |
storeRestriction |
not possible to add a single restriction |
equivalent |
depends on the |
findNode |
|
nodeLatLon |
|
report |
no replacement |
doRoute |
see Router section |
Profiles¶
Profiles (also called types in v1 documentation) has seen the largest overhaul in v2.
In v1 routing profiles were represented by a dictionary of the profile name, weights
and access tags; and pre-defined profiles could be provided as strings. In v2, profiles
are abstracted away into a simple protocol, osm.Profile.
V2 comes with 4 base implementations of that interface: osm.SkeletonProfile,
osm.RailwayProfile, HighwayProfile and NonMotorroadHighwayProfile.
Instead of using weights, preferences are now expressed using penalties. Version 1
calculated the cost of an edge as distance_between_nodes / weight, which lead to
violations of the A* heuristic
if weight was greater than 1. In version 2, the calculation was changed to distance_between_nodes * penalty,
with a check that the penalty is not smaller than 1, ensuring the A* heuristic behaves correctly.
To convert weights to penalties, divide each weight by the minimal weight, e.g.:
{"primary": 0.2, "secondary": 0.6, "tertiary": 1.0}
→ {"primary": 0.2 / 0.2, "secondary": 0.6 / 0.2, "tertiary": 1.0 / 0.2}
→ {"primary": 1.0, "secondary": 3.0, "tertiary": 5.0}
Pre-made profiles can be replaced by directly instantiating the following classes:
v1 |
v2 |
|---|---|
car |
|
bus |
|
cycle |
|
horse |
∅ |
foot |
|
tram |
|
train |
|
∅ |
Each of the pre-made profiles accepts custom name, penalties and access arguments,
to override default values. Custom profiles can be implemented by inheriting from
base profiles (see FootProfile for a good example
of that), or by completely custom class implementing osm.Profile.
Router¶
The Router class is no longer present in version 2, the doRoute method has been
replaced by the find_route_without_turn_around() and find_route() functions.
Prefer to use the first function, unless you are absolutely sure there are no turn
restrictions in your data.
The status string is no longer being used, instead, find_route family of functions behave in the following manner:
v1 status |
v2 behavior |
|---|---|
success |
non-empty list returned |
no_route |
empty list returned |
gave_up |
|
Instead of the SEARCH_LIMIT module constants, find_route functions take a
step_limit argument.
Instead of the distance Datastore attribute / distFunction init parameter,
find_route functions take a distance parameter.
Distance functions¶
v1 |
v2 |
|---|---|
distHaversine |
|
distEuclidian |
|
∅ |