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.