Tutorial: Create Custom Cycling Routes with GraphHopper

/* Css In-Brief */

Tutorial: Create Custom Cycling Routes with GraphHopper

Key PointsDetails to Remember
📖 DefinitionGraphHopper is an open-source routing engine.
🚴 AdvantagesOptimized routes for cycling prioritizing safety and comfort.
⚙️ InstallationDocker or local installation depending on your needs.
🛣️ Cycling ProfilesCustomization of bike type and track preferences.
🎯 CustomizationSettings for speed, elevation, and avoidance of high-traffic roads.
📤 Export & IntegrationGPX, JSON and JavaScript integration into your WordPress pages.

If you dream of creating tailor-made cycling routes, GraphHopper proves to be a robust and flexible ally. Behind its minimalist interface lies a powerful engine capable of processing OpenStreetMap data to plot the route best suited to your desires, whether it’s an urban ride or a multi-stage itinerary. This article guides you step-by-step, from installing the service to exporting your tracks, including fine customization of bike profiles.

Why choose GraphHopper for cycle touring?

GraphHopper is not just a simple routing tool: it relies on an active community, regular updates, and a GPLv3 foundation. You benefit from an increasingly optimized algorithm for cycling, capable of integrating new bike lanes and cycle routes. Compared to other solutions, its modularity (JSON profile, HTTP API) offers great freedom, especially if you want to host it on your own server to avoid dependence on third-party services.

Reliability and Performance

The core of GraphHopper is based on compressed data structures, which accelerate in-memory calculation. Result: your cycling route requests execute in a few milliseconds, even on a dataset covering an entire country.

Profile Flexibility

You can define a “urban bike”, “road bike” or “mountain bike” profile by adjusting average speeds and types of roads to avoid. This very granular configuration significantly improves the quality of the route offered to the user.

Initial Installation and Configuration

Via Docker

Launch GraphHopper in a few commands: retrieve the official image, mount a volume for OSM data, and expose the HTTP port. This approach isolates your environment and guarantees simplified updates.

docker pull graphhopper/graphhopper
docker run -d -v cheminguide/osm:/data -p 8989:8989 graphhopper/graphhopper

Local installation

If you prefer to compile yourself, clone the GitHub repository, install Java 11 and Maven, then run:

mvn clean package -DskipTests

Then copy your OpenStreetMap PBF file into the graphs folder and start the application with java -jar web/target/graphhopper-web-*.jar server config.yml.

Creating custom cycling profiles

The core of customization lies in the JSON files in profiles. Example for a mountain bike (MTB):

{
  "profiles": [
    {
      "name": "vtt",
      "vehicle": "bike",
      "weighting": "fastest",
      "turn_costs": false,
      "hints": {"elevation": true}
    }
  ]
}

By adjusting weighting and hints, you influence the choice of paths: forest trails, bike lanes, or secondary roads.

Add a mixed profile

For a route combining paved roads and trails, copy the JSON block from an existing profile, change the vehicle property to custom_bike, and create a recipe in graphhopper.yml to load your mixed profile.

Generating custom routes

Once the service is ready, query the REST API by sending a POST to /route with your profile and coordinates:

{
  "points": [[48.8566,2.3522],[45.7640,4.8357]],
  "profile": "bike",
  "locale": "fr"
}

The returned result contains several sections, including paths for the GPS track and instructions for each segment, with zoom level and turn information.

GraphHopper interface showing a custom cycling route

Export and integration into WordPress

To embed your interactive map, you can:

  • Display the GPX track in a dedicated JavaScript player.
  • Expose the GraphHopper API directly behind a WordPress shortcode.

In a PHP template, retrieve the JSON points, convert them to GeoJSON, then integrate them via Leaflet or OpenLayers. If you want to enrich the presentation, you can display a road map of France as a background to locate your routes.

Advanced use cases and best practices

Besides cycling planning, GraphHopper is also used for more specific routes, for example the Loire Castles route or for routes with significant elevation gain. Keep in mind to:

  • Regularly update your OSM data to benefit from the latest trails.
  • Test several profiles before deploying to production.
  • Limit simultaneous requests or cache responses to avoid overload.

FAQ

Do you need a powerful server to host GraphHopper?

A VPS with 2 vCPU and 4 GB RAM is sufficient for cycling use over an entire country. Beyond that, upgrade to 8 GB for very dense areas.

How to obtain a GPS-compatible GPX export for hiking?

The API already returns an array of points adapted to the GPX format. You just need to transform this JSON stream into XML <trkpt> tags.

Can multiple modes of transport be mixed?

Yes: create distinct profiles for each segment (bike + train, bike + ferry) and synchronize the transition points.

A lire  Step-by-step itinerary of the Loire Valley Castles route: plan, maps, and tips

Leave a comment