Route optimization with Graphhopper API

#1

Hello there and welcome to my very first How To :raised_hands:

This one goes out to our travelling salesmen, small scale logistic companies or our favorite Chinese take-away around the corner… or if, like me, you prefer the most time efficient route between various waypoints! With dashdash route optimization is just a few steps away :star_struck:

In Part 1, I will introduce you to a template for basic route optimization between various waypoints. We will look at a real life example, namely the three most visited places during my studies in NYC:

 (A) NYU Library
 (B) Whole Foods
 (C) Favorite Doughnut Shop

There are only 4 steps to the optimal route

  1. Geocode your waypoints
  2. Grab an API key for Graphhopper
  3. Send the GET request for the route
  4. PARSE the request for routing instructions

Geocode your waypoints
We can safely skip this part, as it has already been documented. Thanks @humberto!!!

If implemented correctly, you will be able to insert an address of your choosing and receive latitude and longitude values in return.

image

Grab an API key for Graphhopper
Visit Graphhopper for a free trial key.

Send GET request
Graphhopper offers various map-related APIs, but this time their standard Routing API will suffice.

The standard GET request looks as follows, whereas the bold parts mark the differing parameters and the italic parts are the parameters definitions:

https://graphhopper.com/api/1/route?point=51.131,12.414&point=48.224,3.867&vehicle=car&locale=de&key=[YOUR_KEY]

You can add parameters to your GET request as your case requires. Refer to Graphhopper for documentation.

Let’s build our custom GET request. First, we need to define our parameters:

latitude point  A   A1 = 40.7295668
longitude point A   A2 = -73.9965785
latitude point  B   B1= 40.7346359
longitude point B   B2 = -73.9897659
latitude point  C   C1 = 40.7393418
longitude point C   C2 = -73.9924034
locale              D1 = en
vehicle             D2 = bike
optimize            D3 = TRUE 
API key             D4 = dde361b4-0....

Now, let’s assemble our GET request!

G1 = get(“https://graphhopper.com/api/1/route?point="&A1&","&A2&"&point="&B1&","&B2&"&point="&C1&","&C2&"&locale="&D1&"&vehicle="&D2&"&optimize="&D3&"&key="&D4&”")

Rookie tip on how to insert parameters to your request:

  Add parameter by inserting                 &[parameter]=

  Define the parameter by
    (i) hardcoding the definition, e.g.      &vehicle=car
    (ii) dynamically reference a cell, e.g.  &vehicle="&D3&"

PARSE the request
Luckily, Graphhopper also provides documentation on how to PARSE the GET for the desired routing instructions.

We are interested in the route instructions, so we will PARSE as follows:

P0 =parse(G1,"paths[0].instructions[0].text")
P1 =parse(G1,"paths[0].instructions[1].text")
...
Pn =parse(G1,"paths[0].instructions[n].text")

PARSE until you make it … to “Arrive at destination” :muscle:
image

And this is how you can define the optimal route between various waypoints with dashdash. Piece of cake, don’t you think? :grin:

Stay tuned for Part 2, where we will dive into more complex routing optimization cases that could involve various drivers, pick-up points and delivery locations!

P.S.: If you ever are in NY, try Dough Doughnuts on 14 W 19th St. They do actually serve the best around :doughnut::doughnut::doughnut:

4 Likes