This repository explains how to install, configure and run OSRM (open source routing machine) for use as a map matching server. This system takes raw, noisy GPS readings, and snaps them to the nearest road. Without this functionality, some vehicles might appear to move off their designated routes. This image illustrates the concept:
The cartographic data must be prepared in advance, according to these instructions
For convenience, an abridged version is given, operating under the assumption that we're running it
in /home/gps/osrm
under the user gps
:
mkdir ~/osrm && cd ~/osrm
- Download the latest map for Moldova from http://download.geofabrik.de/europe/moldova.html,
typically it is a matter of
wget http://download.geofabrik.de/europe/moldova-latest.osm.pbf
- Trim down the map (see next section; this optional, but strongly recommended)
- Pre-process the data
docker run -t -v "${PWD}:/data" --rm osrm/osrm-backend osrm-extract -p /opt/car.lua /data/moldova-latest.osm.pbf
docker run -t -v "${PWD}:/data" --rm osrm/osrm-backend osrm-partition /data/moldova-latest.osrm
docker run -t -v "${PWD}:/data" --rm osrm/osrm-backend osrm-customize /data/moldova-latest.osrm
- After running these commands, make sure all the files are owned by the user
gps
, runchown gps:gps ./*.*
The map provided by Geofabrik includes all the roads in the entire country, even the ones in other cities, and the ones we know fore sure are not a part of the pubic transport network. This means that whenever OSRM performs a match, it will have more work to do, because the search-space is larger. It also implies that sometimes, when the coordinates are particularily noisy, OSRM might "snap" them to the wrong road.
Thus, by removing unnecessary roads, we make map matching more efficient. The raw moldova-latest.osm.pbf
must be processed, such that it only includes the public transport grids that meet the following
criteria:
- are a part of Chișinău, Ialoveni, Trușeni, Stăuceni or Codru (this list is to be extended, when RTEC adds new routes, or when other transport systems join Roataway)
- are a trolleybus or a bus route (i.e. we omit minibuses)
This is how it is done (the easy way):
- Install
osmium
. on Debian systems it isapt install osmium-tool
- Run
osmium getid -r moldova-latest.osm.pbf r6726484 -o chisinau-public-transport.osm.obf
(6726484
is a relation that includes all the data of all public transport networks in Chisinau) - Alternatively, if you want to include trolleybus routes and nothing else, use this command:
osmium getid -r moldova-latest.osm.pbf r7390177 r8649765 r9478330 r9478295 r9478243 r9220092 r8219936 r6768372 r6768261 r6768260 r6768231 r6768200 r6768158 r6768073 r6768055 r6767907 r6767859 r6755788 r9220024 r6862391 r6755732 r6754654 r6754630 r6754620 r6754537 r6754503 r6754451 r6754453 r6768282 -o trolley-only.osm.pbf
(note that the magic numbers are the relation IDs of each route, you can find them inroutes.csv
inside theinfrastructure-data
repo) - Use the output file as an input for the previous section.
This is an alternative way to accomplish the same thing. It takes more steps, but it is a point-and-click approach that some might prefer:
- Go to https://overpass-turbo.eu/
- Type the following query:
relation (6726484); >>; (._;>;); out meta;
(the relation ID corresponds to a relation that includes all public transport in Chisinau and adjacent cities) and pressRun
- The system will warn you that the result might be a large data set, press
continue anyway
- When the query is complete, you'll see all the roads on the map, you can visually inspect them
to make sure everything is alright, then press
Export
. - Choose
download as raw OSM data
and save it to a local directory. - Open this file in
josm
- Then go to
File\Save as
, choose theosm pbf
format, and save it. - Use this file as the input data for the pre-processing steps described above, it might be convenient
to save it as
moldova-latest.osm.pbf
so all the commands in the previous section will work "as is".
- Run the server itself
docker run -t -i --rm -p 5000:5000 -v "${PWD}:/data" osrm/osrm-backend osrm-routed --algorithm mld /data/moldova-latest.osrm
(note that the--rm
flag ensures the container will be deleted after use; no worries - a new one will be created if you startosrm
again via docker) - Send a test request
curl 'http://127.0.0.1:5000/match/v1/driving/28.8260977,47.0231816;28.8260977,47.0231816?radiuses=15;15'
, you should get a JSON response back.
This is done with systemd
, see res/systemd-osrm.template
for details. Pay attention to several
sneaky differences between what is in the service template file and the commands above:
- absolute paths are used, so no
${PWD}
in the service - no
-i
either, because the service doesn't provide an interactive console
sudo systemctl osrm start|stop|restart
sudo journalctl -u osrm -f