GraphHopper.com | Forum | GitHub | Maps | Blog

Directions Java client: MatrixRequest.fromPoints() takes a String


#1

Hi,

I am testing the java client for the directions API, more precisely the matrixPost.

The problem I have is related to the from_points and to_points fields. Both of them are supposed to be passed as a List<List<Double>> on the Json request body. However, when using the Java client, the methods MatrixRequest.fromPoints and MatrixRequest.toPoints get a String as argument, and the fields are then serialized as Strings.

So, when I execute a matrix post request with this code:

MatrixRequest request = new MatrixRequest()
		.points(null)
		.fromPoints("[[2.311129,48.844055], [2.351729,48.836792], [2.366178,48.884376]]")
		.toPoints("[[2.325425,48.896827], [2.212104,48.893765]]")
		.outArrays(Arrays.asList("times", "distances"))
		.vehicle("car");
MatrixResponse response = apiInstance.matrixPost(API_KEY, request);

Then the posted request body is the following:

{
	"from_points": "[[2.311129,48.844055], [2.351729,48.836792], [2.366178,48.884376]]",
	"to_points": "[[2.325425,48.896827], [2.212104,48.893765]]",
	"out_arrays": ["times", "distances"],
	"vehicle": "car"
}

This is obviously not correctly handled by the server and ends up whit a ‘Server Error’ api exception:

Exception in thread "main" com.graphhopper.directions.api.client.ApiException: Server Error
	at com.graphhopper.directions.api.client.ApiClient.handleResponse(ApiClient.java:1047)
	at com.graphhopper.directions.api.client.ApiClient.execute(ApiClient.java:970)
	at com.graphhopper.directions.api.client.api.MatrixApi.matrixPostWithHttpInfo(MatrixApi.java:289)
	at com.graphhopper.directions.api.client.api.MatrixApi.matrixPost(MatrixApi.java:274)
	at com.praxedo.executables.forecast.ghdirectionsapi.DirectionsApi.main(DirectionsApi.java:22)

The expected request body should be like this:

{
    "out_arrays": [
        "distances",
        "times"
    ],
    "from_points": [
        [
            2.311129,
            48.844055
        ],
        [
            2.351729,
            48.836792
        ],
        [
            2.366178,
            48.884376
        ]
    ],
    "to_points": [
        [
            2.325425,
            48.896827
        ],
        [
            2.212104,
            48.893765
        ]
    ],
    "vehicle": "car"
}

So my question is: why the methods MatrixRequest.fromPoints and MatrixRequest.toPoints receive a String and how should I use the Java client to post correct matrix requests?

Thanks in advance,
Jose


#2

Thanks, this needs an improvement. Until this is done, can you try the handcoded client? Which is also more memory efficient:

https://search.maven.org/#artifactdetails|com.graphhopper|directions-api-client-hc|0.10.0|jar

Have created https://github.com/graphhopper/directions-api-clients/issues/17 for all follow up information


#3

Using MatrixApi version 0.10.0 you can now do:

MatrixRequest request = new MatrixRequest()
    .points(null)
    .fromPoints(Arrays.asList(Arrays.asList(2.311129, 48.844055), Arrays.asList(2.351729, 48.836792), Arrays.asList(2.366178, 48.884376)))
    .toPoints(Arrays.asList(Arrays.asList(2.325425, 48.896827), Arrays.asList(2.212104, 48.893765)))
    .outArrays(Arrays.asList("times", "distances"))
    .vehicle("car");
MatrixResponse response = new MatrixApi().matrixPost(KEY, request);

#4

It works great, thanks a lot for the fix :slight_smile: