GraphHopper.com | Forum | GitHub | Maps | Blog

How to send a request to locally hosted graphhopper


#1

So, I was able to get graphhopper to work by running the “graphhopper-web-0.10.0-with-dep.jar”. Now I can go to localhost:898 and see a picture of the map of berlin. What I would like to do next is to use graphhopper to answer shortest path requests. My understanding is that I can do this by sending json messages to the thing I set up on localhost:8989 and get messages back which answer the queries.

How do I send these messages? For example, is their a python library for it? …or am I totally misunderstanding the way graphhopper is supposed to work?


#2

…so, I’ve installed this library here - https://github.com/graphhopper/directions-api-clients/tree/master/python - but the default example is for sending messages back and forth with the actual graphhopper website. Is there a way to use it in the context of a locally hosted server?


#3

Yes, the clients should work independent of the host. In Java it will be something like:

ApiClient client = new ApiClient().setBasePath("http://localhost:8989/");
RoutingApi api = new RoutingApi(client);

instead of

RoutingApi api = new RoutingApi(); // defaults to https://graphhopper.com/api/1

#4

I’m not aware of a python library. But here is a toy example for GraphHopper isochrones in python.
I wish I could have uploaded the file but it seems I can’t upload a .py file

#!/usr/bin/env python
import json

import click
import folium
import matplotlib.cm as cm
import matplotlib.colors
from matplotlib.colors import Normalize
import requests

import webbrowser


@click.command()
@click.argument('source_point')
@click.option('--graphhopper-host', default='localhost:8989')
@click.option('--graphhopper-profile', default='car')
@click.option('-b', '--buckets', default=3)
@click.option('--json-output-file', default='test.json')
@click.option('--html-output-file', default='test.html')
@click.option('--range-minutes', default=15)
def graphhopper(source_point, graphhopper_host, graphhopper_profile, buckets,
               json_output_file, html_output_file, range_minutes):
    """
    Plot the isochrone for a given lat,lon point using graphhopper
    """
    lat, lon = list(map(float, source_point.split(',')))
    isochrone_url = 'http://{}/isochrone'.format(graphhopper_host)
    resp = requests.get(isochrone_url, params={
        'point': source_point,
        'time_limit': range_minutes*60,
        'buckets': buckets,
        'vehicle': graphhopper_profile
    })

    resp_json = resp.json()
    geo_json = {
        'type': 'FeatureCollection',
        'features': resp_json['polygons']
     }
    click.echo('Took: {} ms'.format(resp_json['info']['took']), err=True)

    cmap = cm.magma
    norm = Normalize(vmin=0, vmax=buckets)

    with open(json_output_file, 'w') as fp:
        json.dump(geo_json, fp, indent=True)
    m = folium.Map(location=[lat, lon])
    folium.GeoJson(geo_json, name='geojson',
                   style_function=lambda x:
                   {'color': matplotlib.colors.rgb2hex(
                       cmap(norm(x['properties']['bucket'])))}).add_to(m)
    m.save(html_output_file)
    webbrowser.open(html_output_file)


if __name__ == "__main__":
    graphhopper()

If you put this in a file test_graphhopper_isochrone.py, make it executable, install the requirements and run
./test_graphhopper_isochrone.py 52.520016,13.399082 --graphhopper-host localhost:8989 --graphhopper-profile car
You should see the isochrone in your favourite browser.

Not sure if this helps you, but it might help you get started with python code.


#5

Thanks everyone. I’m still struggling though. Would anyone happen to have a complete piece of code that requests a path between two points from localhost:8989 in python or java?


#6

Java examples are here:


#7

I’m still failing at this. Would anyone be kind enough to post the python code that used the library above to setup the api with that interacts with the localhost and then runs a shortest path query between two points?


#8

I run GraphHopper as a cost-matrix provider for JSprit (eg, implement AbstractForwardVehicleRoutingTransportCosts). Maybe you can do that too?