A playground containing examples how to read OSM data and calculate routes. Also provides a basic Swing application for routing and a website to for routing and finding POIs.
Sebastian Hesse
You need JDK 8 to compile the sources (or JRE 8 to run them). Download it here. Additionally Maven must be installed on your system.
You can download OSM files for Germany (and other areas as well) on this website.
In order to use the TMC support, you need a LCL and ECL file. This can be requested here for Germany.
You need some TMC data files. Each file should have the format *HH.dat
, e.g. 20170101-19.dat for the time slot
19:00 - 19:59 on the day 01.01.2017. As you can see, only full hours are supported. The application will also
save an optimized version of the file with the format *HH.dat.cdat
to the same directory. This improves the
loading of TMC data, because not all data is relevant. If there occur any errors, it might be possible that the user
who is executing the application has not enough rights to write into that folder.
Here are some example classes for different use cases.
Note: I prefer to use the word node instead of vertex in terms of graphs. So don't be irritated.
Example to show how to read nodes and ways of an OSM PBF file. Uses Graphhopper to read an OSM PBF file. See PbfReader
Example to show how to simply read nodes and ways into a graph of nodes and edges. Uses Graphhopper to read an OSM PBF file. See NodeEdgeReader
Example to show how to connect OSM data with a Swing application and also be able to route between two points. Uses JMapViewer to start a Swing application and show a map. See OsmMapViewer
A Dropwizard application setting up a routing API accessible via REST. See this package
In order to start the server, just hand over a configuration file and an OSM file. The *.yml and *.osm.pbf file must be somewhere on the local system. Take a look at the example *.yml file.
$ mvn clean package
$ java -jar target/osm-routing.jar server config.yml osm_data.osn.pbf
If you're using a huge OSM file, you should increase the heap size for the JVM, e.g. -Xmx10g
for 10 GB.
A web frontend will be served from:
http://localhost:8080/index.html
### Get a path between two points (lat1,lon1) and (lat2,lon2) ###
vehicle: car, pedestrian
mode: fastest, shortest
GET /api/route?lat1=...&lon1=...&lat2=...&lon2=...&vehicle=car&mode=fastest
### Get certain POIs around a given position ###
pid: if known, the id of the position/node, otherwise just -1
maxDistance: maximum distance to search for gas stations around the position
typeKey: key of a Nominatim category, e.g. Amenity
typeValue: value of a Nominatim category, e.g. Fuel
GET /api/pois?lat=...&lon=...&pid=...&maxDistance=20&typeKey=...&typeValue=...
### Request all available POI types ###
OPTIONS /api/pois
### Get last updated ways where TMC traffic events have fired for certain hour ###
GET /api/traffic
### Update the routing graph with traffic data for a certain hour ###
PUT /api/traffic/{hour}
### Remove all traffic data from the routing graph ###
DELETE /api/traffic
### Get meta information about the OSM backend ###
GET /api/meta
Optionally you can start the server by providing some TMC data. For this you need the a Location Code List (e.g. for Germany, you can request one here: https://www.bast.de/DE/Verkehrstechnik/Fachthemen/v2-LCL/location-code-list_node.html) and an event list (most likely delivered together with the Location Code List). You also need some TMC data files. Then you can start the server with the following arguments:
$ java -jar target/osm-routing.jar server config.yml osm_data.osm.pbf lcl.csv event_list.csv /path/to/tmc/directory
- Check out the repository:
git clone https://github.com/seeebiii/osm-routing-examples
- Run
main
method fromDropwizardApplication.java
with the arguments:server /path/to/config.yml /path/to/osm/file
- Run Gulp:
gulp watch
- Start coding!
MIT License
Copyright (c) 2017 Sebastian Hesse
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.