Help

Pick the Best Electric Vehicle Charging Station Along a Route with GraphHopper

Nearly all electric vehicles to date that rely 100% on electric energy have a range problem, especially in cold seasons. The only exceptions are maybe Tesla and fuel cell driven cars with over 400km and some even over 500km range. If you still want to plan a longer-distance tour you’ll have to include one (or more) charging station as stopover.

But how to find the best one with the least detour e.g. in this example from Hamburg to Munich?

You calculate all routes to all charging stations and pick the best. That sounds crazy but is completely reasonable in real time with GraphHopper even for hundreds of stations. In this blog post we’ll use the GraphHopper Matrix API and have created the JavaScript charging station example.

Calculate detours with the GraphHopper Matrix API

The GraphHopper Matrix API is a specially crafted piece of software to make many-to-many requests very fast. For example in logistics you need this to determine the optimal order for e.g. delivering parcels.

Now to calculate the best detour we need a similar logic: we need
A) one request, the one-to-many and
B) another request, the many-to-one.

Finally we’ll pick the fastest tour (smallest A+B). The same approach we would take to calculate an optimal ridesharing where you ‘pick up’ and ‘deliver’ people.

We also need all charging stations and grab them from OpenStreetMap via the Overpass API and use the wizard to specify amenity=charging_station and car=yes. We ignore for a moment the socket type of the station and a possible required authentication or fee.

For the German tour from Hamburg to Munich we fetch all stations from middle Germany (roughly 80) and select all which are in the center of our route e.g. within a 400km radius (reducing this is necessary for the free package).

There are several optimizations possible like the route calculating itself to consider elevation to avoid hills or a completely other vehicle like an electric bicycle. Or in real world you have to consider the opening hours of the stations or seek for multiple stops for shorter ranges, then the Tour Optimization API or special routing expertise could be necessary.

In our Hamburg-Munich case the result is the following tour of ~750km and roughly 8.5 hours plus the time required for charging or replacing the battery at this charging station in Erfurt. Compare this to the normal route which takes only 8 hours and is 775km long.

best-station-tour