Traffic in Navit
Navit currently (as of November 2017) does not support routing around traffic problems, although the core functionality exists in the routing engine.
Internally, Navit uses traffic distortions to model traffic issues which may affect the choice of route. A traffic distortion can represent the following:
- Slow traffic: speed on the affected segment is below the posted limit (or whatever Navit assumes the default speed for the segment to be), e.g. because of dense traffic or because of temporary speed limits. This is represented by setting maxspeed to the expected speed.
- Delays: there are waiting times or other time penalties on the affected segment. This is represented by setting delay to the expected delay.
- Closures: the segment is closed to all traffic. This is represented by setting maxspeed to zero.
Both maxspeed and delay can be set at the same time and their effects will add up.
At this moment, traffic distortions are (ab)used to generate penalties for turning around if these are defined in the current vehicle profile. This is done in route.c, in the
route_path_new() function, which can be used as an example for inserting traffic distortions.
The GTK GUI has menu items to enter traffic distortions, but they don’t work in a straightforward manner. If you find Navit happily routing even over the segment you just marked as closed, read on.
All the menu items do is write the segment and its traffic distortion to a map in textfile format is called distortion.txt, found in Navit’s data dir. However, that file doesn’t do anything unless it is added to the active mapset. Apparently this is a feature that never got fully implemented to the point of being usable out of the box.
Traffic Distortion in Maps
Traffic distortions are map items and can (presumably) be stored in any map, whether in memory or read from a file. As mentioned under #GUI, this is what happens with traffic distortions entered manually through the GTK GUI—they get written to a textfile map. Here’s an example of its contents:
type=traffic_distortion maxspeed=0 0x13a07c 0x5e9634 0x139e87 0x5e98ae type=traffic_distortion maxspeed=10 0x139f22 0x5d6a5a 0x139ec5 0x5d6a13 0x139e88 0x5d69e7 0x139e83 0x5d69e4 type=traffic_distortion delay=3600000 0x13a0f3 0x5d6a41 0x13a101 0x5d6a33 0x13a110 0x5d6a29 0x13a154 0x5d6a03
The first line for each distortion is the item data: item type (always
traffic_distortion here) and its attributes. The following lines are the coordinates for the affected segment.
In the default configuration, that file doesn’t do anything as it is just written to, but never read. You can change that by adding the following line to the active mapset in your navit.xml file:
<map type="textfile" enabled="yes" data="distortion.txt"/>
Restart Navit, and it will route around the distortions you have just entered. Note that no path is necessary, as the file resides in Navit’s default data path.
In tests, changes to the file (made through the GTK GUI) do not take effect until Navit is restarted. Simply recalculating the route is not sufficient.
The only function to edit traffic distortions is found in route.c:
static void route_graph_set_traffic_distortion(struct route_graph *this, struct route_graph_segment *seg, int delay);
- Sets or clears a traffic distortion for a segment.
- This sets a delay (setting speed is not supported) or clears an existing traffic distortion. Note that, although setting a speed is not supported, calling this function with a delay of 0 will also clear an existing speed constraint.
The route graph
The segment to which the traffic distortion applies
Delay in tenths of a second, or 0 to clear an existing traffic distortion
- This function prototype is not part of the header file, i.e. not part of the public API. Currently, it is only used internally.
- There is currently no way to set a maxspeed or closure. However, calling
route_graph_set_traffic_distortion()with a zero delay will unset any existing traffic distortion.