Welcome to the fascinating world of map matching, where we can accurately snap GPS traces to actual roads! This blog will guide you through using the GraphHopper routing engine for map matching, letting your GPS data shine with precision. Let’s embark on this journey step-by-step!
Understanding Map Matching
Before we dive into the practical aspects, let’s clarify what map matching is. Consider it like connecting dots in a painting: when you have a sketch (your GPS trace), map matching fills in the details by aligning those dots (GPS points) onto a canvas (the road). This allows for a clearer, more accurate understanding of where the data points actually belong in the real world.
The black line represents the actual GPS track, while the green line shows the matched result on the road.
Getting Set Up
Here are the prerequisites you’ll need:
- Java 8
- Maven 3.3 or higher
Building the Project
To build the project, use the following command:
bash
mvn package -DskipTests
Importing OSM Data
Now, you’ll need to import map data for the area you want to map-match. Here’s how to do it:
bash
java -jar matching-web/target/graphhopper-map-matching-web-3.0-SNAPSHOT.jar import map-data/leipzig_germany.osm.pbf
Make sure to obtain OpenStreetMap data in PBF or XML format from Geofabrik.
Additionally, you can specify a vehicle type (like car, bike, etc.) using the optional parameter --vehicle. Check the available types in the FlagEncoderFactory.
Matching GPX Traces
After importing the data, it’s time to match your GPX traces:
bash
java -jar matching-web/target/graphhopper-map-matching-web-3.0-SNAPSHOT.jar match matching-web/src/test/resources/*.gpx
If you’ve used multiple vehicles for import, select the desired vehicle with the --vehicle option.
Accessing the Web App
To run the web application:
bash
java -jar matching-web/target/graphhopper-map-matching-web-3.0-SNAPSHOT.jar server config.yml
Your simple UI will then be accessible via localhost:8989. Here, you can post GPX files and receive the snapped results either in GPX format or as compatible GraphHopper JSON. For instance, to make a request via curl:
bash
curl -XPOST -H "Content-Type: application/gpx+xml" -d @matching-web/src/test/resources/test1.gpx localhost:8989/match?vehicle=car&type=json
Tools for GPX Files
You can also determine the bounding box of one or more GPX files with the following command:
bash
java -jar matching-web/target/graphhopper-map-matching-web-3.0-SNAPSHOT.jar getbounds matching-web/src/test/resources/*.gpx
Integrating Map Matching into Your Project
If you want to incorporate map matching into your own Java project, check out the MapMatchingResource.java for implementation details. Ensure you add the following Maven dependency:
com.graphhopper
graphhopper-map-matching-core
3.0-SNAPSHOT
Troubleshooting
If you face issues during setup, ensure the following:
- Check if Java and Maven are properly installed and configured.
- Ensure the paths to your GPX files and OSM data are correct.
- If you get errors related to data format, verify that you are using the expected PBF or XML formats from the OpenStreetMap data source.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Note on Data Change
It’s important to remember that edge and node IDs from GraphHopper may change with different PBF file updates. Always verify your data against the most recent sources.
References to Know More
The map matching algorithm utilized here mainly follows the approach described in the paper by Newson and Krumm, focusing on effectively locating GPS points within a network of possible routes. The Viterbi algorithm is employed to calculate the most likely sequence of candidates, balancing GPS measurements with routing distances.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

