Skip to content

Commit

Permalink
snapping closures
Browse files Browse the repository at this point in the history
  • Loading branch information
korshaknn committed Mar 22, 2021
1 parent ee9a113 commit 7b6ff95
Show file tree
Hide file tree
Showing 6 changed files with 320 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,36 @@ public List<Point> waypointTargetsList() {
@Nullable
public abstract WalkingOptions walkingOptions();

/**
* A semicolon-separated list of booleans affecting snapping of waypoint locations to road
* segments.
* If true, road segments closed due to live-traffic closures will be considered for snapping.
* If false, they will not be considered for snapping.
* If provided, the number of snappingClosures must be the same as the number of
* coordinates.
* Must be used with {@link DirectionsCriteria#PROFILE_DRIVING_TRAFFIC}
*
* @return a String representing a list of booleans
*/
@SerializedName("snapping_closures")
@Nullable
public abstract String snappingClosures();

/**
* A list of booleans affecting snapping of waypoint locations to road segments.
* If true, road segments closed due to live-traffic closures will be considered for snapping.
* If false, they will not be considered for snapping.
* If provided, the number of snappingClosures must be the same as the number of
* coordinates.
* Must be used with {@link DirectionsCriteria#PROFILE_DRIVING_TRAFFIC}
*
* @return a list of booleans
*/
@Nullable
public List<Boolean> snappingClosuresList() {
return ParseUtils.parseToBooleans(snappingClosures());
}

/**
* Gson type adapter for parsing Gson to this class.
*
Expand Down Expand Up @@ -1029,6 +1059,45 @@ public Builder waypointTargetsList(@NonNull List<Point> waypointTargets) {
*/
public abstract Builder walkingOptions(@NonNull WalkingOptions walkingOptions);

/**
* A semicolon-separated list of booleans affecting snapping of waypoint locations to road
* segments.
* If true, road segments closed due to live-traffic closures will be considered for snapping.
* If false, they will not be considered for snapping.
* If provided, the number of snappingClosures must be the same as the number of
* coordinates.
* You can skip a coordinate and show its position in the list with the ; separator.
* If unspecified, this parameter defaults to false.
* Must be used with {@link DirectionsCriteria#PROFILE_DRIVING_TRAFFIC}
*
* @param snappingClosures a semicolon-separated list of booleans
* @return this builder for chaining options together
*/
public abstract Builder snappingClosures(@NonNull String snappingClosures);

/**
* A list of booleans affecting snapping of waypoint locations to road segments.
* If true, road segments closed due to live-traffic closures will be considered for snapping.
* If false, they will not be considered for snapping.
* If provided, the number of snappingClosures must be the same as the number of
* coordinates.
* You can skip a coordinate and show its position in the list with null value.
* If unspecified, this parameter defaults to false.
* Must be used with {@link DirectionsCriteria#PROFILE_DRIVING_TRAFFIC}
*
* @param snappingClosures a list of booleans
* @return this builder for chaining options together
*/
public Builder snappingClosures(@NonNull List<Boolean> snappingClosures) {
String result = FormatUtils.join(";", snappingClosures);
if (result != null) {
snappingClosures(result);
} else {
snappingClosures("");
}
return this;
}

/**
* Builds a new instance of the {@link RouteOptions} object.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class ParseUtils {
private static final String SEMICOLON = ";";
private static final String COMMA = ",";
private static final String UNLIMITED = "unlimited";
private static final String TRUE = "true";
private static final String FALSE = "false";

/**
* Parse a String to a list of Integers.
Expand Down Expand Up @@ -165,4 +167,39 @@ public static List<List<Double>> parseToListOfListOfDoubles(@Nullable String ori

return result;
}

/**
* Parse a String to a list of Boolean.
*
* @param original an original String.
* @return List of Booleans
*/
@Nullable
public static List<Boolean> parseToBooleans(@Nullable String original) {
if (original == null) {
return null;
}

List<Boolean> booleans = new ArrayList<>();
if (original.isEmpty()) {
return booleans;
}

String[] strings = original.split(SEMICOLON, -1);
for (String str : strings) {
if (str != null) {
if (str.isEmpty()) {
booleans.add(null);
} else if (str.equalsIgnoreCase(TRUE)) {
booleans.add(true);
} else if (str.equalsIgnoreCase(FALSE)) {
booleans.add(false);
} else {
booleans.add(null);
}
}
}

return booleans;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mapbox.geojson.Point;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

import static com.mapbox.api.directions.v5.DirectionsCriteria.ANNOTATION_CONGESTION;
Expand All @@ -17,7 +18,7 @@
public class RouteOptionsTest {

private static final String ROUTE_OPTIONS_JSON =
"{\"baseUrl\":\"https://api.mapbox.com\",\"user\":\"mapbox\",\"profile\":\"driving-traffic\",\"coordinates\":[[-122.4003312,37.7736941],[-122.4187529,37.7689715],[-122.4255172,37.7775835]],\"alternatives\":false,\"language\":\"ru\",\"radiuses\":\";unlimited;100\",\"bearings\":\"0,90;90,0;\",\"continue_straight\":false,\"roundabout_exits\":false,\"geometries\":\"polyline6\",\"overview\":\"full\",\"steps\":true,\"annotations\":\"congestion,distance,duration\",\"exclude\":\"toll\",\"voice_instructions\":true,\"banner_instructions\":true,\"voice_units\":\"metric\",\"access_token\":\"token\",\"uuid\":\"12345543221\",\"approaches\":\";curb;\",\"waypoints\":\"0;1;2\",\"waypoint_names\":\";two;\",\"waypoint_targets\":\";12.2,21.2;\"}";
"{\"baseUrl\":\"https://api.mapbox.com\",\"user\":\"mapbox\",\"profile\":\"driving-traffic\",\"coordinates\":[[-122.4003312,37.7736941],[-122.4187529,37.7689715],[-122.4255172,37.7775835]],\"alternatives\":false,\"language\":\"ru\",\"radiuses\":\";unlimited;100\",\"bearings\":\"0,90;90,0;\",\"continue_straight\":false,\"roundabout_exits\":false,\"geometries\":\"polyline6\",\"overview\":\"full\",\"steps\":true,\"annotations\":\"congestion,distance,duration\",\"exclude\":\"toll\",\"voice_instructions\":true,\"banner_instructions\":true,\"voice_units\":\"metric\",\"access_token\":\"token\",\"uuid\":\"12345543221\",\"approaches\":\";curb;\",\"waypoints\":\"0;1;2\",\"waypoint_names\":\";two;\",\"waypoint_targets\":\";12.2,21.2;\",\"snapping_closures\":\";false;true\"}";

@Test
public void toBuilder() {
Expand Down Expand Up @@ -328,6 +329,79 @@ public void annotationsList() {
assertEquals("congestion,distance,maxspeed,speed", routeOptions.annotations());
}

@Test
public void snappingClosuresString() {
String snappingClosuresString = "true;;;false;false;true;;;;" ;

RouteOptions routeOptions = routeOptions()
.toBuilder()
.snappingClosures(snappingClosuresString)
.build();

assertEquals(snappingClosuresString, routeOptions.snappingClosures());

List<Boolean> snappingClosures = routeOptions.snappingClosuresList();
assertEquals(10, snappingClosures.size());
assertEquals(true, snappingClosures.get(0));
assertEquals(null, snappingClosures.get(1));
assertEquals(null, snappingClosures.get(2));
assertEquals(false, snappingClosures.get(3));
assertEquals(false, snappingClosures.get(4));
assertEquals(true, snappingClosures.get(5));
assertEquals(null, snappingClosures.get(6));
assertEquals(null, snappingClosures.get(7));
assertEquals(null, snappingClosures.get(8));
assertEquals(null, snappingClosures.get(9));
}

@Test
public void snappingClosuresEmptyString() {
String snappingClosuresString = "" ;

RouteOptions routeOptions = routeOptions()
.toBuilder()
.snappingClosures(snappingClosuresString)
.build();

assertEquals(snappingClosuresString, routeOptions.snappingClosures());

List<Boolean> snappingClosures = routeOptions.snappingClosuresList();
assertTrue(snappingClosures.isEmpty());
}

@Test
public void snappingClosuresList() {
List<Boolean> snappingClosures = new ArrayList<>();
snappingClosures.add(false);
snappingClosures.add(false);
snappingClosures.add(null);
snappingClosures.add(true);
snappingClosures.add(false);
snappingClosures.add(null);
snappingClosures.add(null);

RouteOptions routeOptions = routeOptions()
.toBuilder()
.snappingClosures(snappingClosures)
.build();

assertEquals(snappingClosures, routeOptions.snappingClosuresList());
assertEquals("false;false;;true;false;;", routeOptions.snappingClosures());
}

@Test
public void snappingClosuresEmptyList() {
List<Boolean> snappingClosures = new ArrayList<>();

RouteOptions routeOptions = routeOptions()
.toBuilder()
.snappingClosures(snappingClosures)
.build();

assertEquals(snappingClosures, routeOptions.snappingClosuresList());
assertTrue(routeOptions.snappingClosures().isEmpty());
}

@Test
public void baseUrlIsValid_fromJson() {
RouteOptions routeOptions = RouteOptions.fromJson(ROUTE_OPTIONS_JSON);
Expand Down Expand Up @@ -571,6 +645,24 @@ public void waypointTargetsListIsValid_fromJson() {
assertEquals(null, options.waypointTargetsList().get(2));
}

@Test
public void snappingIncludeClosuresStringIsValid_fromJson() {
RouteOptions options = RouteOptions.fromJson(ROUTE_OPTIONS_JSON);

assertEquals(";false;true", options.snappingClosures());
}

@Test
public void snappingIncludeClosuresListIsValid_fromJson() {
RouteOptions options = RouteOptions.fromJson(ROUTE_OPTIONS_JSON);

List list = options.snappingClosuresList();
assertEquals(3, list.size());
assertEquals(null, list.get(0));
assertEquals(false, list.get(1));
assertEquals(true, list.get(2));
}

@Test
public void routeOptions_toJson() {
RouteOptions options = routeOptions();
Expand Down Expand Up @@ -742,6 +834,7 @@ private RouteOptions routeOptions() {
.waypointIndices("0;1;2")
.waypointNames(";two;")
.waypointTargets(";12.2,21.2;")
.snappingClosures(";false;true")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public interface DirectionsService {
* @param walkwayBias a factor that modifies the cost when encountering roads or paths
* that do not allow vehicles and are set aside for pedestrian use
* @param alleyBias a factor that modifies the cost when alleys are encountered
* @param snappingClosures a list of booleans affecting snapping of waypoint locations to road
* segments
* @return the {@link DirectionsResponse} in a Call wrapper
* @since 1.0.0
*/
Expand Down Expand Up @@ -90,7 +92,8 @@ Call<DirectionsResponse> getCall(
@Query("enable_refresh") Boolean enableRefresh,
@Query("walking_speed") Double walkingSpeed,
@Query("walkway_bias") Double walkwayBias,
@Query("alley_bias") Double alleyBias
@Query("alley_bias") Double alleyBias,
@Query("snapping_include_closures") String snappingClosures
);

/**
Expand Down Expand Up @@ -134,6 +137,8 @@ Call<DirectionsResponse> getCall(
* @param walkwayBias a factor that modifies the cost when encountering roads or paths
* that do not allow vehicles and are set aside for pedestrian use
* @param alleyBias a factor that modifies the cost when alleys are encountered
* @param snappingClosures a list of booleans affecting snapping of waypoint locations to road
* segments
* @return the {@link DirectionsResponse} in a Call wrapper
* @since 4.6.0
*/
Expand Down Expand Up @@ -166,6 +171,7 @@ Call<DirectionsResponse> postCall(
@Field("enable_refresh") Boolean enableRefresh,
@Field("walking_speed") Double walkingSpeed,
@Field("walkway_bias") Double walkwayBias,
@Field("alley_bias") Double alleyBias
@Field("alley_bias") Double alleyBias,
@Field("snapping_include_closures") String snappingClosures
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ private Call<DirectionsResponse> get() {
enableRefresh(),
walkingSpeed(),
walkwayBias(),
alleyBias()
alleyBias(),
snappingClosures()
);
}

Expand Down Expand Up @@ -143,7 +144,8 @@ private Call<DirectionsResponse> post() {
enableRefresh(),
walkingSpeed(),
walkwayBias(),
alleyBias()
alleyBias(),
snappingClosures()
);
}

Expand Down Expand Up @@ -338,6 +340,9 @@ Double alleyBias() {
return walkingOptions().alleyBias();
}

@Nullable
abstract String snappingClosures();

private boolean hasWalkingOptions() {
return walkingOptions() != null;
}
Expand Down Expand Up @@ -393,6 +398,7 @@ public abstract static class Builder {
private List<Integer> waypointIndices = new ArrayList<>();
private List<String> waypointNames = new ArrayList<>();
private List<Point> waypointTargets = new ArrayList<>();
private List<Boolean> snappingClosures = new ArrayList<>();

/**
* The username for the account that the directions engine runs on. In most cases, this should
Expand Down Expand Up @@ -1040,6 +1046,25 @@ public Builder waypointTargets(@NonNull List<Point> waypointTargets) {

abstract Builder waypointTargets(@Nullable String waypointTargets);

/**
* A list of booleans affecting snapping of waypoint locations to road segments.
* If true, road segments closed due to live-traffic closures will be considered for snapping.
* If false, they will not be considered for snapping.
* If provided, the number of snappingClosures must be the same as the number of
* coordinates.
* You can skip a coordinate and show its position in the list with null value.
* Must be used with {@link DirectionsCriteria#PROFILE_DRIVING_TRAFFIC}
*
* @param snappingClosures a list of booleans
* @return this builder for chaining options together
*/
public Builder snappingClosures(@NonNull List<Boolean> snappingClosures) {
this.snappingClosures = snappingClosures;
return this;
}

abstract Builder snappingClosures(@Nullable String snappingClosures);

/**
* A point to specify drop-off locations that are distinct from the locations specified in
* coordinates.
Expand Down Expand Up @@ -1169,6 +1194,14 @@ public MapboxDirections build() {
approaches(formattedApproaches);
}

if (!snappingClosures.isEmpty()) {
if (snappingClosures.size() != coordinates.size()) {
throw new ServicesException("Number of snapping closures elements must match "
+ "number of coordinates provided.");
}
snappingClosures(FormatUtils.join(";", snappingClosures));
}

coordinates(coordinates);
bearing(FormatUtils.formatBearings(bearings));
annotation(FormatUtils.join(",", annotations));
Expand Down
Loading

0 comments on commit 7b6ff95

Please sign in to comment.