Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for directions responses with "geojson" geometries #1219

Open
miptildar opened this issue Nov 28, 2020 · 7 comments
Open

Add support for directions responses with "geojson" geometries #1219

miptildar opened this issue Nov 28, 2020 · 7 comments

Comments

@miptildar
Copy link

I am working on a project where our users will be sharing the same route and use Mapbox in their client apps for navigation.
Since it is important for us to use the same route, it was decided to generate route on a backend side and clients were suppose to download route in json format, parse it and give route to Mapbox Navigation SDK.
I tried to solve this task in 2 ways.

1. Using REST API

I tried to use your REST API for generating a route and it works well, but when I try to parse JSON using DirectionsRoute.fromJson(String json) method I am getting an exception

Expected a string but was BEGIN_OBJECT at line 1 column 969 path $.legs[0].steps[0].geometry

although JSON itself has a valid format.

2. Using Java SDK

On a backed side I was generating a route, but using Mapbox SDK:

String accessToken =
                "accees_token";

        Point originPoint = Point.fromLngLat(**, **);
        Point destinationPoint = Point.fromLngLat(**, **);

        MapboxDirections client = MapboxDirections.builder()
                .origin(originPoint)
                .destination(destinationPoint)
                .overview(DirectionsCriteria.OVERVIEW_FULL)
                .profile(DirectionsCriteria.PROFILE_WALKING)
                .accessToken(accessToken)
                .build();

client.enqueueCall(new Callback<>() {
            @Override
            public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {

                DirectionsRoute directionsRoute = response.body().routes().get(0);
                Gson gson = new Gson();
                String json = gson.toJson(directionsRoute, DirectionsRoute.class);

            }

            @Override
            public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
                System.out.println("Error: " + throwable.getMessage());
            }
        });

But this time converting DirectionsRoute to json returns {}, although route array is not empty.

I only need to generate route on backend side as json and use that route on client side.
Any of these solutions are suitable for us if they will work properly.
Could you help me resolve any of them?

@miptildar
Copy link
Author

miptildar commented Nov 30, 2020

Okay, let me simplify testing sequence.

public class MapBoxRequest {

  private static final String PATTERN =
      "https://api.mapbox.com/directions/v5/mapbox/walking/%s,%s;%s,%s?alternatives=true&geometries=geojson&steps=true&access_token=%s";

  final String ENCODING = "UTF-8";

  public static void main(String[] args) throws IOException, URISyntaxException {
    HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory();

    String accessToken =
        "access_toekn";

    // set right location values
    String string = String.format(PATTERN, longitude1, latitude1, longitude2, latitude2, accessToken);

      URI uri = new URI(string);

    HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(uri.toString()));
    String rawResponse = request.execute().parseAsString();

    DirectionsResponse.fromJson(rawResponse);
  }
}

Just try to run this code.

I am getting

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 546 path $.routes[0].legs[0].steps[0].geometry
	at com.google.gson.Gson.fromJson(Gson.java:939)
	at com.google.gson.Gson.fromJson(Gson.java:892)
	at com.google.gson.Gson.fromJson(Gson.java:841)
	at com.google.gson.Gson.fromJson(Gson.java:813)
	at com.mapbox.api.directions.v5.models.DirectionsResponse.fromJson(DirectionsResponse.java:133)
	at dating.walking.service.walking.setup.MapBoxRequest.main(MapBoxRequest.java:34)
Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 546 path $.routes[0].legs[0].steps[0].geometry
	at com.google.gson.stream.JsonReader.nextString(JsonReader.java:825)

And maven dependency:

<dependency>
        <groupId>com.google.http-client</groupId>
        <artifactId>google-http-client</artifactId>
        <version>1.38.0</version>
 </dependency>

 <dependency>
       <groupId>com.mapbox.mapboxsdk</groupId>
       <artifactId>mapbox-sdk-services</artifactId>
       <version>5.6.0</version>
 </dependency>

@morethal
Copy link

There seems to be an issue indeed. I can reproduce the issue on my side, using exactly the same json as provided by the Directions API or trying a minimized version with less information - shouldn't this just work out of the box?

@TheNewCivilian
Copy link

TheNewCivilian commented Mar 19, 2021

@miptildar Actually after having a closer look into this issue it seams like this might not be necessarily a bug.
Investigating the de-serialization process, it turns out several subobjects are de-serialized sequentially.

This means i.e. the geometry property you passed needs to be still a string when passed to the related de-serializer.
I suppose this will fix your issue from above.

@TheNewCivilian
Copy link

TheNewCivilian commented Mar 20, 2021

Working example routes can be found here: https://github.com/mapbox/mapbox-navigation-android/tree/main/examples/src/main/res/raw
These routes hold the geometry as "polyline6" as mapbox calls it.
So your code above might finally work if you set:
private static final String PATTERN = "https://api.mapbox.com/directions/v5/mapbox/walking/%s,%s;%s,%s?alternatives=true&geometries=polyline6&steps=true&access_token=%s";

@LukasPaczos
Copy link
Member

@TheNewCivilian makes the right point - this library only supports polyline and polyline6 encoded geometries, even though the Directions API also supports the LineString (geojson) encoding. This is why the deserializer expects a string in the geometry field, not a list of points.

See

* @param geometries null if you'd like the default geometry, else one of the options found in
* {@link GeometriesCriteria}.
* @return this builder for chaining options together
* @since 2.0.0
*/
public abstract Builder geometries(@GeometriesCriteria String geometries);
and
/**
* Retention policy for the various direction geometries.
*
* @since 3.0.0
*/
@Retention(RetentionPolicy.CLASS)
@StringDef( {
GEOMETRY_POLYLINE,
GEOMETRY_POLYLINE6
})
public @interface GeometriesCriteria {
}
.

We can keep this ticket around as a feature request.

@LukasPaczos LukasPaczos changed the title Create route on a backend side and share a route with Android/iOS clients as a JSON Add support for directions responses with "geojson" geometries Jun 3, 2021
@sahil-kapoor
Copy link

Has this been fixed?

@miptildar
Copy link
Author

This is what I do on backend side

import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
import com.mapbox.api.directions.v5.DirectionsCriteria;
import com.mapbox.api.directions.v5.MapboxDirections;
import com.mapbox.api.directions.v5.models.DirectionsResponse;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.geojson.Point;

private final Gson gson =
            new GsonBuilder()
                    .registerTypeAdapterFactory(DirectionsAdapterFactory.create())
                    .create();

private String getRoute(Point point1, Point point2) throws IOException {
        MapboxDirections client =
                MapboxDirections.builder()
                        .origin(point1)
                        .destination(point2)
                        .overview(DirectionsCriteria.OVERVIEW_FULL)
                        .profile(DirectionsCriteria.PROFILE_WALKING)
                        .steps(true)
                        .accessToken(mapboxAccessToken)
                        .voiceInstructions(false)
                        .language(Locale.ENGLISH)
                        .bannerInstructions(false)
                        .build();

        Response<DirectionsResponse> responseFor12 = client.executeCall();
        if (responseFor12.body() == null) {
            LOGGER.error("No routes found, make sure you set the right user and access token.");
        } else if (responseFor12.body().routes().size() < 1) {
            LOGGER.error("No routes found");
        } else {
            DirectionsRoute directionsRoute = responseFor12.body().routes().get(0);
            directionsRoute.geometry();
            return gson.toJson(directionsRoute);
        }

        return null;
}

This is how I parse json on Android side

Gson gson =
                    new GsonBuilder()
                            .registerTypeAdapterFactory(DirectionsAdapterFactory.create())
                            .create();
DirectionsRoute parsedFromJsonRoute = gson.fromJson(jsonAsString, DirectionsRoute.class);

And it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants