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

MapMaptchingMatching to DirectionsRoute #759

Merged
merged 1 commit into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ directions-fixtures:

mapmatching-fixtures:
curl "https://api.mapbox.com/matching/v5/mapbox/driving/$(MAP_MATCHING_COORDINATES)?geometries=polyline&language=sv&steps=true&access_token=$(MAPBOX_ACCESS_TOKEN)" \
-o services-matching/src/test/resources/mapmatching_v5_polyline.json
-o services-matching/src/test/resources/map_matching_v5_polyline.json

# Unmatchable MapMatching request
curl "https://api.mapbox.com/matching/v5/mapbox/driving/0,-40;0,-20?access_token=$(MAPBOX_ACCESS_TOKEN)" \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import com.mapbox.api.directions.v5.DirectionsCriteria.GeometriesCriteria;
import com.mapbox.api.directions.v5.DirectionsCriteria.OverviewCriteria;
import com.mapbox.api.directions.v5.DirectionsCriteria.ProfileCriteria;
import com.mapbox.api.directions.v5.models.RouteOptions;
import com.mapbox.api.matching.v5.models.MapMatchingAdapterFactory;
import com.mapbox.api.matching.v5.models.MapMatchingError;
import com.mapbox.api.matching.v5.models.MapMatchingMatching;
import com.mapbox.api.matching.v5.models.MapMatchingResponse;
import com.mapbox.core.MapboxService;
import com.mapbox.core.constants.Constants;
Expand Down Expand Up @@ -104,7 +106,11 @@ public Response<MapMatchingResponse> executeCall() throws IOException {
if (!response.isSuccessful()) {
errorDidOccur(null, response);
}
return response;

return Response.success(response.body()
.toBuilder()
.matchings(generateRouteOptions(response))
.build());
}

/**
Expand All @@ -124,8 +130,19 @@ public void onResponse(Call<MapMatchingResponse> call,
if (!response.isSuccessful()) {
errorDidOccur(callback, response);
return;

} else if (response.body() == null || response.body().matchings().isEmpty()) {
// If null just pass the original object back since there's nothing to modify.
callback.onResponse(call, response);
return;
}
callback.onResponse(call, response);
MapMatchingResponse newResponse =
response
.body()
.toBuilder()
.matchings(generateRouteOptions(response))
.build();
callback.onResponse(call, Response.success(newResponse));
}

@Override
Expand All @@ -152,6 +169,47 @@ private void errorDidOccur(@Nullable Callback<MapMatchingResponse> callback,
}
}

private List<MapMatchingMatching> generateRouteOptions(Response<MapMatchingResponse> response) {
List<MapMatchingMatching> matchings = response.body().matchings();
List<MapMatchingMatching> modifiedMatchings = new ArrayList<>();
for (MapMatchingMatching matching : matchings) {
modifiedMatchings.add(matching.toBuilder().routeOptions(
RouteOptions.builder()
.profile(profile())
.coordinates(formatCoordinates(coordinates()))
.annotations(annotations())
.language(language())
.radiuses(radiuses())
.user(user())
.voiceInstructions(voiceInstructions())
.bannerInstructions(bannerInstructions())
//.continueStraight(continueStraight())
//.bearings(bearings())
//.alternatives(alternatives())
//.exclude(exclude())
//.voiceUnits(voiceUnits())
.requestUuid("mapmatching")
.accessToken(accessToken())
.baseUrl(baseUrl())
.build()
).build());
}
return modifiedMatchings;
}

private static List<Point> formatCoordinates(String coordinates) {
String[] coordPairs = coordinates.split(";");
List<Point> coordinatesFormatted = new ArrayList<>();
for (String coordPair : coordPairs) {
String[] coords = coordPair.split(",");
coordinatesFormatted.add(
Point.fromLngLat(Double.valueOf(coords[0]), Double.valueOf(coords[1])));

}
return coordinatesFormatted;
}


@Nullable
abstract String clientAppName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.gson.annotations.SerializedName;
import com.mapbox.api.directions.v5.models.RouteLeg;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.api.directions.v5.models.RouteOptions;

import java.io.Serializable;
import java.util.List;
Expand Down Expand Up @@ -90,6 +91,17 @@ public static Builder builder() {
*/
public abstract double confidence();

/**
* Holds onto the parameter information used when making the directions request. Useful for
* re-requesting a directions route using the same information previously used.
*
* @return a {@link RouteOptions}s object which holds onto critical information from the request
* that cannot be derived directly from the directions route
* @since 3.0.0
*/
@Nullable
public abstract RouteOptions routeOptions();

/**
* Convert the current {@link MapMatchingMatching} to its builder holding the currently assigned
* values. This allows you to modify a single variable and then rebuild the project resulting in
Expand All @@ -101,6 +113,24 @@ public static Builder builder() {
*/
public abstract MapMatchingMatching.Builder toBuilder();

/**
* Map this MapMatchingMatching object to a {@link DirectionsRoute} object.
*
* @return a {@link DirectionsRoute} object
*/
public DirectionsRoute toDirectionRoute() {

return DirectionsRoute.builder()
.legs(legs())
.geometry(geometry())
.weightName(weightName())
.weight(weight())
.duration(duration())
.distance(distance())
.routeOptions(routeOptions())
.build();
}

/**
* Gson type adapter for parsing Gson to this class.
*
Expand Down Expand Up @@ -185,6 +215,16 @@ public abstract static class Builder {
*/
public abstract Builder confidence(double confidence);

/**
* Holds onto the parameter information used when making the directions request.
*
* @param routeOptions a {@link RouteOptions}s object which holds onto critical information from
* the request that cannot be derived directly from the directions route
* @return this builder for chaining options together
* @since 3.0.0
*/
public abstract MapMatchingMatching.Builder routeOptions(@Nullable RouteOptions routeOptions);

/**
* Build a new {@link MapMatchingMatching} object.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static Builder builder() {
* defined in this {@link MapMatchingResponse}
* @since 3.0.0
*/
public abstract MapMatchingResponse.Builder toBuilder();
public abstract Builder toBuilder();

/**
* Gson type adapter for parsing Gson to this class.
Expand Down
Loading