From b300f37f7bc0d81bff2ed6a91f6cdcb8d276d8f2 Mon Sep 17 00:00:00 2001 From: Yury Kanetski Date: Wed, 27 Apr 2022 18:52:42 +0200 Subject: [PATCH] MapMatching: added ignore param (#1424) * MapMatching: added ignore param --- .../api/matching/v5/MapMatchingService.java | 4 + .../api/matching/v5/MapboxMapMatching.java | 98 +++++++++++++++++++ .../v5/models/MapMatchingMatching.java | 10 ++ .../matching/v5/MapboxMapMatchingTest.java | 26 +++++ 4 files changed, 138 insertions(+) diff --git a/services-matching/src/main/java/com/mapbox/api/matching/v5/MapMatchingService.java b/services-matching/src/main/java/com/mapbox/api/matching/v5/MapMatchingService.java index 383b13e37..833ac6189 100644 --- a/services-matching/src/main/java/com/mapbox/api/matching/v5/MapMatchingService.java +++ b/services-matching/src/main/java/com/mapbox/api/matching/v5/MapMatchingService.java @@ -62,6 +62,7 @@ public interface MapMatchingService { * @param waypointIndices which input coordinates should be treated as waypoints/separate legs. * Note: coordinate indices not added here act as silent waypoints * @param waypointNames custom names for waypoints used for the arrival instruction. + * @param ignore ignore certain routing restrictions when map matching. * @param approaches which side of the road to approach a waypoint. * @return the MapMatchingResponse in a Call wrapper * @since 2.0.0 @@ -87,6 +88,7 @@ Call getCall( @Query("voice_units") String voiceUnits, @Query("waypoints") String waypointIndices, @Query("waypoint_names") String waypointNames, + @Query("ignore") String ignore, @Query("approaches") String approaches); @@ -134,6 +136,7 @@ Call getCall( * @param waypointIndices which input coordinates should be treated as waypoints/separate legs. * Note: coordinate indices not added here act as silent waypoints * @param waypointNames custom names for waypoints used for the arrival instruction. + * @param ignore ignore certain routing restrictions when map matching. * @param approaches which side of the road to approach a waypoint. * @return the MapMatchingResponse in a Call wrapper * @since 4.4.0 @@ -160,5 +163,6 @@ Call postCall( @Field("voice_units") String voiceUnits, @Field("waypoints") String waypointIndices, @Field("waypoint_names") String waypointNames, + @Field("ignore") String ignore, @Field("approaches") String approaches); } diff --git a/services-matching/src/main/java/com/mapbox/api/matching/v5/MapboxMapMatching.java b/services-matching/src/main/java/com/mapbox/api/matching/v5/MapboxMapMatching.java index 6815bd09e..70a54c1ea 100644 --- a/services-matching/src/main/java/com/mapbox/api/matching/v5/MapboxMapMatching.java +++ b/services-matching/src/main/java/com/mapbox/api/matching/v5/MapboxMapMatching.java @@ -4,6 +4,7 @@ import androidx.annotation.IntRange; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.annotation.StringDef; import com.google.auto.value.AutoValue; import com.google.gson.GsonBuilder; @@ -25,6 +26,10 @@ import com.mapbox.geojson.Point; import java.io.IOException; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -49,6 +54,21 @@ public abstract class MapboxMapMatching extends MapboxService { + /** + * Ignore access restrictions related to mode of travel. + */ + public static final String IGNORE_ACCESS = "access"; + + /** + * Ignore one-way restrictions. + */ + public static final String IGNORE_ONEWAYS = "oneways"; + + /** + * Ignore other restrictions, such as time-based or turn restrictions. + */ + public static final String IGNORE_RESTRICTIONS = "restrictions"; + protected MapboxMapMatching() { super(MapMatchingService.class); } @@ -102,6 +122,7 @@ private Call get() { voiceUnits(), waypointIndices(), waypointNames(), + ignore(), approaches()); } @@ -126,6 +147,7 @@ private Call post() { voiceUnits(), waypointIndices(), waypointNames(), + ignore(), approaches()); } @@ -232,6 +254,9 @@ public void onFailure(Call call, Throwable throwable) { @Nullable abstract String waypointNames(); + @Nullable + abstract String ignore(); + @Nullable abstract String approaches(); @@ -268,6 +293,7 @@ public abstract static class Builder { private Integer[] waypointIndices; private String[] waypointNames; private String[] approaches; + private String[] ignores; /** * Use POST method to request data. @@ -276,6 +302,7 @@ public abstract static class Builder { * @return this builder for chaining options together * @since 4.4.0 */ + @NonNull public Builder post() { usePostMethod(true); return this; @@ -287,11 +314,14 @@ public Builder post() { * @return this builder for chaining options together * @since 4.4.0 */ + @NonNull public Builder get() { usePostMethod(false); return this; } + + @NonNull abstract Builder usePostMethod(@NonNull Boolean usePost); /** @@ -303,6 +333,7 @@ public Builder get() { * @return this builder for chaining options together * @since 2.1.0 */ + @NonNull public abstract Builder accessToken(@NonNull String accessToken); /** @@ -314,6 +345,7 @@ public Builder get() { * @return this builder for chaining options together * @since 3.0.0 */ + @NonNull public abstract Builder tidy(@Nullable Boolean tidy); /** @@ -325,6 +357,7 @@ public Builder get() { * @return this builder for chaining options together * @since 2.1.0 */ + @NonNull public abstract Builder user(@NonNull String user); /** @@ -336,6 +369,7 @@ public Builder get() { * @return this builder for chaining options together * @since 2.1.0 */ + @NonNull public abstract Builder profile(@NonNull @ProfileCriteria String profile); /** @@ -353,6 +387,7 @@ public Builder get() { * @return this builder for chaining options together * @since 2.0.0 */ + @NonNull public abstract Builder geometries(@NonNull @GeometriesCriteria String geometries); /** @@ -368,12 +403,14 @@ public Builder get() { * @return this builder for chaining options together * @since 1.0.0 */ + @NonNull public Builder radiuses(@Nullable @FloatRange(from = 0) Double... radiuses) { this.radiuses = radiuses; return this; } // Required for matching with MapboxMapMatching radiuses() method. + @NonNull abstract Builder radiuses(@Nullable String radiuses); @@ -392,6 +429,7 @@ public Builder radiuses(@Nullable @FloatRange(from = 0) Double... radiuses) { * @deprecated you should now use {@link #waypointIndices(Integer[])} */ @Deprecated + @NonNull public Builder waypoints(@Nullable @IntRange(from = 0) Integer... waypoints) { this.waypointIndices = waypoints; return this; @@ -411,11 +449,13 @@ public Builder waypoints(@Nullable @IntRange(from = 0) Integer... waypoints) { * @return this builder for chaining options together * @since 3.0.0 */ + @NonNull public Builder waypointIndices(@Nullable @IntRange(from = 0) Integer... waypointIndices) { this.waypointIndices = waypointIndices; return this; } + @NonNull abstract Builder waypointIndices(@Nullable String waypointIndices); /** @@ -431,6 +471,7 @@ public Builder waypointIndices(@Nullable @IntRange(from = 0) Integer... waypoint * @return this builder for chaining options together * @since 1.0.0 */ + @NonNull public abstract Builder steps(@Nullable Boolean steps); /** @@ -444,6 +485,7 @@ public Builder waypointIndices(@Nullable @IntRange(from = 0) Integer... waypoint * @return this builder for chaining options together * @since 1.0.0 */ + @NonNull public abstract Builder overview(@Nullable @OverviewCriteria String overview); /** @@ -458,6 +500,7 @@ public Builder waypointIndices(@Nullable @IntRange(from = 0) Integer... waypoint * @return this builder for chaining options together * @since 3.0.0 */ + @NonNull public abstract Builder bannerInstructions(@Nullable Boolean bannerInstructions); @@ -472,6 +515,7 @@ public Builder waypointIndices(@Nullable @IntRange(from = 0) Integer... waypoint * @return this builder for chaining options together * @since 3.0.0 */ + @NonNull public abstract Builder voiceInstructions(@Nullable Boolean voiceInstructions); /** @@ -482,6 +526,7 @@ public Builder waypointIndices(@Nullable @IntRange(from = 0) Integer... waypoint * @return this builder for chaining options together * @since 3.0.0 */ + @NonNull public abstract Builder voiceUnits( @Nullable @DirectionsCriteria.VoiceUnitCriteria String voiceUnits ); @@ -497,6 +542,7 @@ public abstract Builder voiceUnits( * @return this builder for chaining options together * @since 3.0.0 */ + @NonNull public abstract Builder roundaboutExits(@Nullable Boolean roundaboutExits); /** @@ -514,6 +560,7 @@ public abstract Builder voiceUnits( * documentation * @since 2.1.0 */ + @NonNull public Builder annotations(@Nullable @AnnotationCriteria String... annotations) { this.annotations = annotations; return this; @@ -521,6 +568,7 @@ public Builder annotations(@Nullable @AnnotationCriteria String... annotations) // Required for matching with MapboxMapMatching annotations() method. @SuppressWarnings("WeakerAccess") + @NonNull protected abstract Builder annotations(@Nullable String annotations); /** @@ -532,6 +580,7 @@ public Builder annotations(@Nullable @AnnotationCriteria String... annotations) * @return this builder for chaining options together * @since 2.1.0 */ + @NonNull public Builder timestamps(@Nullable String... timestamps) { this.timestamps = timestamps; return this; @@ -539,6 +588,7 @@ public Builder timestamps(@Nullable String... timestamps) { // Required for matching with MapboxMapMatching timestamps() method. @SuppressWarnings("WeakerAccess") + @NonNull protected abstract Builder timestamps(@Nullable String timestamps); /** @@ -551,6 +601,7 @@ public Builder timestamps(@Nullable String... timestamps) { * @return this builder for chaining options together * @since 2.1.0 */ + @NonNull public Builder coordinates(@NonNull List coordinates) { this.coordinates.addAll(coordinates); return this; @@ -558,6 +609,7 @@ public Builder coordinates(@NonNull List coordinates) { // Required for matching with MapboxMapMatching coordinates() method. @SuppressWarnings("WeakerAccess") + @NonNull protected abstract Builder coordinates(@NonNull String coordinates); /** @@ -569,6 +621,7 @@ public Builder coordinates(@NonNull List coordinates) { * @return this builder for chaining options together * @since 3.0.0 */ + @NonNull public Builder coordinate(@NonNull Point coordinate) { this.coordinates.add(coordinate); return this; @@ -588,6 +641,7 @@ public Builder coordinate(@NonNull Point coordinate) { * Languages * @since 3.0.0 */ + @NonNull public Builder language(@Nullable Locale language) { if (language != null) { language(language.getLanguage()); @@ -607,6 +661,7 @@ public Builder language(@Nullable Locale language) { * Languages * @since 2.2.0 */ + @NonNull public abstract Builder language(String language); /** @@ -616,6 +671,7 @@ public Builder language(@Nullable Locale language) { * @return this builder for chaining options together * @since 1.0.0 */ + @NonNull public abstract Builder clientAppName(@NonNull String clientAppName); /** @@ -634,11 +690,13 @@ public Builder language(@Nullable Locale language) { * @return this builder for chaining options together * @since 3.2.0 */ + @NonNull public Builder addApproaches(@Nullable String... approaches) { this.approaches = approaches; return this; } + @NonNull abstract Builder approaches(@Nullable String approaches); /** @@ -651,13 +709,32 @@ public Builder addApproaches(@Nullable String... approaches) { * @return this builder for chaining options together * @since 3.3.0 */ + @NonNull public Builder addWaypointNames(@Nullable String... waypointNames) { this.waypointNames = waypointNames; return this; } + @NonNull abstract Builder waypointNames(@Nullable String waypointNames); + /** + * Ignore certain routing restrictions when map matching. + *

+ * This option is only available for the mapbox/driving profile. + * + * @param ignore routing restrictions + * @return this builder for chaining options together + */ + @NonNull + public Builder addIgnore(@IgnoreScope @Nullable String... ignore) { + this.ignores = ignore; + return this; + } + + @NonNull + abstract Builder ignore(@Nullable String ignore); + /** * Optionally change the APIs base URL to something other then the default Mapbox one. * @@ -665,9 +742,11 @@ public Builder addWaypointNames(@Nullable String... waypointNames) { * @return this builder for chaining options together * @since 2.1.0 */ + @NonNull public abstract Builder baseUrl(String baseUrl); @SuppressWarnings("WeakerAccess") + @NonNull protected abstract MapboxMapMatching autoBuild(); /** @@ -679,6 +758,7 @@ public Builder addWaypointNames(@Nullable String... waypointNames) { * @throws ServicesException when a provided parameter is detected to be incorrect * @since 2.1.0 */ + @NonNull public MapboxMapMatching build() { if (coordinates == null || coordinates.size() < 2) { throw new ServicesException("At least two coordinates must be provided with your API" @@ -732,6 +812,11 @@ public MapboxMapMatching build() { approaches(formattedApproaches); } + if (ignores != null) { + String formattedIgnores = FormatUtils.join(",", Arrays.asList(ignores)); + ignore(formattedIgnores); + } + coordinates(formatCoordinates(coordinates)); timestamps(TextUtils.join(";", timestamps)); annotations(TextUtils.join(",", annotations)); @@ -758,4 +843,17 @@ private static String formatCoordinates(List coordinates) { return TextUtils.join(";", coordinatesFormatted.toArray()); } } + + /** + * Ignore certain routing restrictions when map matching. + * + * @see MapMetching docs + */ + @Retention(RetentionPolicy.CLASS) + @StringDef( { + IGNORE_ACCESS, IGNORE_RESTRICTIONS, IGNORE_ONEWAYS + }) + @Target({ElementType.ANNOTATION_TYPE, ElementType.PARAMETER}) + public @interface IgnoreScope { + } } diff --git a/services-matching/src/main/java/com/mapbox/api/matching/v5/models/MapMatchingMatching.java b/services-matching/src/main/java/com/mapbox/api/matching/v5/models/MapMatchingMatching.java index f2510fe4b..db36b3530 100644 --- a/services-matching/src/main/java/com/mapbox/api/matching/v5/models/MapMatchingMatching.java +++ b/services-matching/src/main/java/com/mapbox/api/matching/v5/models/MapMatchingMatching.java @@ -191,6 +191,7 @@ public abstract static class Builder { * @return this builder for chaining options together * @since 3.0.0 */ + @NonNull public abstract Builder distance(double distance); /** @@ -200,6 +201,7 @@ public abstract static class Builder { * @return this builder for chaining options together * @since 3.0.0 */ + @NonNull public abstract Builder duration(double duration); /** @@ -209,6 +211,7 @@ public abstract static class Builder { * @return this builder for chaining options together * @since 3.0.0 */ + @NonNull public abstract Builder geometry(@Nullable String geometry); /** @@ -218,6 +221,7 @@ public abstract static class Builder { * @return this builder for chaining options together * @since 3.0.0 */ + @NonNull public abstract Builder weight(double weight); /** @@ -229,6 +233,7 @@ public abstract static class Builder { * @return this builder for chaining options together * @since 3.0.0 */ + @NonNull public abstract Builder weightName(String weightName); /** @@ -238,6 +243,7 @@ public abstract static class Builder { * @return this builder for chaining options together * @since 3.0.0 */ + @NonNull public abstract Builder legs(List legs); /** @@ -247,6 +253,7 @@ public abstract static class Builder { * @return this builder for chaining options together * @since 3.0.0 */ + @NonNull public abstract Builder confidence(double confidence); /** @@ -257,6 +264,7 @@ public abstract static class Builder { * @return this builder for chaining options together * @since 3.0.0 */ + @NonNull public abstract Builder routeOptions(@Nullable RouteOptions routeOptions); /** @@ -269,6 +277,7 @@ public abstract static class Builder { * @return this builder for chaining options together * @since 3.4.0 */ + @NonNull public abstract Builder voiceLanguage(@Nullable String voiceLanguage); /** @@ -295,6 +304,7 @@ public abstract static class Builder { * @return a new {@link MapMatchingMatching} using the provided values in this builder * @since 3.0.0 */ + @NonNull public abstract MapMatchingMatching build(); } } diff --git a/services-matching/src/test/java/com/mapbox/api/matching/v5/MapboxMapMatchingTest.java b/services-matching/src/test/java/com/mapbox/api/matching/v5/MapboxMapMatchingTest.java index 472948203..c2a576e79 100644 --- a/services-matching/src/test/java/com/mapbox/api/matching/v5/MapboxMapMatchingTest.java +++ b/services-matching/src/test/java/com/mapbox/api/matching/v5/MapboxMapMatchingTest.java @@ -699,6 +699,32 @@ public void testWithWaypointNames() throws Exception { assertEquals("Ok", response.body().code()); } + @Test + public void testIgnore() throws Exception { + MapboxMapMatching mapMatching = MapboxMapMatching.builder() + .profile(PROFILE_DRIVING) + .coordinates(Arrays.asList( + Point.fromLngLat(2.344003915786743,48.85805170891599), + Point.fromLngLat(2.346750497817993,48.85727523615161))) + .addIgnore( + MapboxMapMatching.IGNORE_ONEWAYS, + MapboxMapMatching.IGNORE_ACCESS, + MapboxMapMatching.IGNORE_RESTRICTIONS + ) + .accessToken(ACCESS_TOKEN) + .baseUrl(mockUrl.toString()) + .build(); + + mapMatching.setCallFactory(null); + Response response = mapMatching.executeCall(); + assertEquals(200, response.code()); + assertEquals("Ok", response.body().code()); + + assertEquals("oneways,access,restrictions", mapMatching.ignore()); + assertEquals("oneways,access,restrictions", + mapMatching.cloneCall().request().url().queryParameter("ignore")); + } + @Test public void testUsePostMethod() throws Exception { MapboxMapMatching mapMatching = MapboxMapMatching.builder()