graphhopper / directions-api-clients

API clients for various languages for the GraphHopper Directions API
https://docs.graphhopper.com/
Apache License 2.0
30 stars 39 forks source link

usage of Matrix API for python #22

Closed karussell closed 6 years ago

vladi-strilets commented 6 years ago

Could you provide some example of using _frompoint and _topoint in python with more then 1 point. I mean, the type of this parameters is str, so what format it has to be for the correct request.

Thanks.

karussell commented 6 years ago

Sure, currently investigating this as a Java guy ;) ...

karussell commented 6 years ago

This was ugly. I fixed the POST request and made point, to_point and from_point the same type but kept the GET request in this wrong state. This is now fixed with the latest commit and a request like this should work:

from __future__ import print_function
import time
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint

# create an instance of the API class
api_instance = swagger_client.MatrixApi()
key = 'YOUR_KEY' # str | Get your key at graphhopper.com
point = ['47.989922,3.251953', '47.040182,5.361328'] # list[str] | Specifiy multiple points for which the weight-, route-, time- or distance-matrix should be calculated. In this case the sta
from_point = [] # str | The starting points for the routes. E.g. if you want to calculate the three routes A->1, A->2, A->3 then you have one from_point parameter and three to_point
to_point = [] # str | The destination points for the routes. Is a string with the format latitude,longitude. (optional)
out_array = ['times'] # list[str] | Specifies which arrays should be included in the response. Specify one or more of the following options 'weights', 'times', 'distances'. To specify more t
vehicle = 'car' # str | The vehicle for which the route should be calculated. Other vehicles are foot, small_truck etc (optional) (default to car)

try:
    # Matrix API
    api_response = api_instance.matrix_get(key, point=point, from_point=from_point, to_point=to_point, out_array=out_array, vehicle=vehicle)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling MatrixApi->matrix_get: %s\n" % e)
vladi-strilets commented 6 years ago

Great! Thank you :)

karussell commented 6 years ago

Please let me know if this works for you and maybe you can even add an example for a POST request?

vladi-strilets commented 6 years ago

Yes, it works property.

As an example I could add 2 possible ways of GET request. The first way serves for the calculation of a symmetric matrix of points:

point = ['47.482732, 4.415299','47.449249, 4.328732','47.433521, 4.381711']
api_response = api_instance.matrix_get(key, point=point, out_array=out_array, vehicle=vehicle)
pprint(api_response)

Meanwhile the second case is used for getting the information about a group of start points _frompoint and end points _topoint:

from_point = ['47.482732, 4.415299']
to_point = ['47.449249, 4.328732','47.433521, 4.381711']
api_response = api_instance.matrix_get(key, from_point=from_point, to_point=to_point, out_array=out_array, vehicle=vehicle)
pprint(api_response)
karussell commented 6 years ago

Thanks, didn't know that one can omit the unused parameters in python.

Do you know how you to utilize the matrix POST request?

https://github.com/graphhopper/directions-api-clients/blob/master/python/swagger_client/api/matrix_api.py#L155

The GET request has a limitation at a certain number of locations as the points are just added to the URL. The POST request uses a custom JSON file instead and does not have any limitation. See here for the documentation: https://graphhopper.com/api/1/docs/matrix/#http-post-request

vladi-strilets commented 6 years ago

I have just tried to make a POST request using the next code:

points = [[40.411666, -3.704657],[40.423488, -3.711327],[40.415308, -3.684525]]
out_arrays = ['times','distance']
vehicle = 'car'
body = swagger_client.MatrixRequest(points=points, out_arrays=out_arrays, vehicle=vehicle)
api_response = api_instance.matrix_post(key,body=body)

But I'm getting this error message:

ApiException: (400) HTTP response body: {"message":"Bad Request","hints":[{"details":"java.lang.IllegalArgumentException","message":"Cannot find from_points: 0, 1, 2"},{"details":"java.lang.IllegalArgumentException","message":"Cannot find to_points: 0, 1, 2"}]}

Using _frompoints and _topoints instead of points I´ve got the same message. Also I have tried to put this parameters like = [], but it´s not permitted to combine them.

What could be wrong?

karussell commented 6 years ago

Thanks for providing the snippet. The problem is that we use the following order point=lat,lon for strings in the URL (commonly used in things like Google Maps) and order [lon,lat] for an array in JSON (GeoJSON). So this will work:

from __future__ import print_function
import time
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint

# create an instance of the API class
api_instance = swagger_client.MatrixApi()
key = 'YOUR_KEY' # str | Get your key at graphhopper.com

try:
    # Matrix API
    points = [[-3.704657,40.411666],[-3.711327,40.423488],[-3.684525,40.415308]]
    out_arrays = ['times','distances']
    vehicle = 'car'
    body = swagger_client.MatrixRequest(points=points, out_arrays=out_arrays, vehicle=vehicle)
    api_response = api_instance.matrix_post(key,body=body)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling MatrixApi->matrix_get: %s\n" % e)

Please also notice the change in out_arrays (distances)

vladi-strilets commented 6 years ago

Correctly, my mistake.

Thanks a lot for your support.

karussell commented 6 years ago

Thanks!

Correctly, my mistake.

I wouldn't call it mistake. Something like this happens when you have different kinds of standards ... but we'll try to move to the GeoJSON in later versions although moving with an API is far from easy.