Skip to content

Commit f8599bc

Browse files
authored
Added via-waypoints property (#1364)
1 parent 899ce65 commit f8599bc

5 files changed

Lines changed: 159 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Mapbox welcomes participation and contributions from everyone.
44

55
### main
6+
- Exposed `viaWaypoints` in `RouteLeg` [#1364](https://github.com/mapbox/mapbox-java/pull/1364)
67

78
### v6.3.0-beta.1 - January 28, 2021
89
- Exposed [turf.lineIntersect](https://turfjs.org/docs/#lineIntersect) via `TurfMisc#lineIntersect` using an algorithm with O(nm) time complexity which should suite small to medium sized geometries until a more performant solution is implemented. [#1348](https://github.com/mapbox/mapbox-java/pull/1348)

services-directions-models/src/main/java/com/mapbox/api/directions/v5/models/RouteLeg.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mapbox.api.directions.v5.models;
22

3+
import androidx.annotation.NonNull;
34
import androidx.annotation.Nullable;
45
import com.google.auto.value.AutoValue;
56
import com.google.gson.Gson;
@@ -28,6 +29,17 @@ public static Builder builder() {
2829
return new AutoValue_RouteLeg.Builder();
2930
}
3031

32+
/**
33+
* When the semicolon-separated list waypoints parameter is used in the request,
34+
* an array per leg is returned that describes where a particular waypoint from
35+
* the root-level array matches to the route.
36+
* This provides a way for tracking when a waypoint is passed along the route.
37+
* @return a list of silent waypoints
38+
*/
39+
@Nullable
40+
@SerializedName("via_waypoints")
41+
public abstract List<SilentWaypoint> viaWaypoints();
42+
3143
/**
3244
* The distance traveled from one waypoint to another.
3345
*
@@ -157,13 +169,23 @@ public static RouteLeg fromJson(String json) {
157169
@AutoValue.Builder
158170
public abstract static class Builder {
159171

172+
/**
173+
* A list of silent waypoints which were used to request a route.
174+
*
175+
* @param viaWaypoints a list of silent waypoints
176+
* @return this builder for chaining options together
177+
*/
178+
@NonNull
179+
public abstract Builder viaWaypoints(@Nullable List<SilentWaypoint> viaWaypoints);
180+
160181
/**
161182
* The distance traveled from one waypoint to another.
162183
*
163184
* @param distance a double number with unit meters
164185
* @return this builder for chaining options together
165186
* @since 3.0.0
166187
*/
188+
@NonNull
167189
public abstract Builder distance(@Nullable Double distance);
168190

169191
/**
@@ -173,6 +195,7 @@ public abstract static class Builder {
173195
* @return this builder for chaining options together
174196
* @since 1.0.0
175197
*/
198+
@NonNull
176199
public abstract Builder duration(@Nullable Double duration);
177200

178201
/**
@@ -185,6 +208,7 @@ public abstract static class Builder {
185208
* @return this builder for chaining options together
186209
* @since 5.5.0
187210
*/
211+
@NonNull
188212
public abstract Builder durationTypical(@Nullable Double durationTypical);
189213

190214
/**
@@ -194,6 +218,7 @@ public abstract static class Builder {
194218
* @return this builder for chaining options together
195219
* @since 3.0.0
196220
*/
221+
@NonNull
197222
public abstract Builder summary(@Nullable String summary);
198223

199224
/**
@@ -204,6 +229,7 @@ public abstract static class Builder {
204229
* @param admins Array with admins
205230
* @return this builder for chaining options together
206231
*/
232+
@NonNull
207233
public abstract Builder admins(@Nullable List<Admin> admins);
208234

209235
/**
@@ -213,6 +239,7 @@ public abstract static class Builder {
213239
* @return this builder for chaining options together
214240
* @since 3.0.0
215241
*/
242+
@NonNull
216243
public abstract Builder steps(@Nullable List<LegStep> steps);
217244

218245
/**
@@ -221,6 +248,7 @@ public abstract static class Builder {
221248
* @param incidents a list of {@link Incident}
222249
* @return this builder for chaining options together
223250
*/
251+
@NonNull
224252
public abstract Builder incidents(@Nullable List<Incident> incidents);
225253

226254
/**
@@ -232,6 +260,7 @@ public abstract static class Builder {
232260
* @return this builder for chaining options together
233261
* @since 3.0.0
234262
*/
263+
@NonNull
235264
public abstract Builder annotation(@Nullable LegAnnotation annotation);
236265

237266
/**
@@ -240,6 +269,7 @@ public abstract static class Builder {
240269
* @param closures a list of {@link Closure}
241270
* @return this builder for chaining options together
242271
*/
272+
@NonNull
243273
public abstract Builder closures(@Nullable List<Closure> closures);
244274

245275
/**
@@ -248,6 +278,7 @@ public abstract static class Builder {
248278
* @return a new {@link RouteLeg} using the provided values in this builder
249279
* @since 3.0.0
250280
*/
281+
@NonNull
251282
public abstract RouteLeg build();
252283
}
253284
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.mapbox.api.directions.v5.models;
2+
3+
import androidx.annotation.NonNull;
4+
import com.google.auto.value.AutoValue;
5+
import com.google.gson.Gson;
6+
import com.google.gson.TypeAdapter;
7+
import com.google.gson.annotations.SerializedName;
8+
9+
/**
10+
* Represents an information about silent waypoint in the leg index.
11+
* See {@link RouteLeg#viaWaypoints()} for more details.
12+
*/
13+
@AutoValue
14+
public abstract class SilentWaypoint extends DirectionsJsonObject {
15+
16+
/**
17+
* Create a new instance of this class by using the {@link SilentWaypoint.Builder} class.
18+
*
19+
* @return {@link SilentWaypoint.Builder} for creating a new instance
20+
*/
21+
@NonNull
22+
public static Builder builder() {
23+
return new AutoValue_SilentWaypoint.Builder();
24+
}
25+
26+
/**
27+
* The associated waypoint index, excluding the origin (index 0) and destination.
28+
*
29+
* @return the associated waypoint index, excluding the origin (index 0) and destination.
30+
*/
31+
@SerializedName("waypoint_index")
32+
public abstract int waypointIndex();
33+
34+
/**
35+
* The calculated distance, in meters, from the leg origin.
36+
*
37+
* @return the calculated distance, in meters, from the leg origin.
38+
*/
39+
@SerializedName("distance_from_start")
40+
public abstract double distanceFromStart();
41+
42+
/**
43+
* The associated leg shape index of the via waypoint location.
44+
*
45+
* @return the associated leg shape index of the via waypoint location.
46+
*/
47+
@SerializedName("geometry_index")
48+
public abstract int geometryIndex();
49+
50+
/**
51+
* Gson type adapter for parsing Gson to this class.
52+
*
53+
* @param gson the built {@link Gson} object
54+
* @return the type adapter for this class
55+
*/
56+
@NonNull
57+
public static TypeAdapter<SilentWaypoint> typeAdapter(Gson gson) {
58+
return new AutoValue_SilentWaypoint.GsonTypeAdapter(gson);
59+
}
60+
61+
/**
62+
* This builder can be used to set the values describing the {@link SilentWaypoint}.
63+
*/
64+
@AutoValue.Builder
65+
public abstract static class Builder {
66+
67+
/**
68+
* @param waypointIndex the associated waypoint index, excluding
69+
* the origin (index 0) and destination.
70+
* @return this builder
71+
*/
72+
@NonNull
73+
public abstract Builder waypointIndex(int waypointIndex);
74+
75+
/**
76+
* @param distanceFromStart the calculated distance, in meters, from the leg origin.
77+
* @return this builder
78+
*/
79+
@NonNull
80+
public abstract Builder distanceFromStart(double distanceFromStart);
81+
82+
/**
83+
* @param geometryIndex the associated leg shape index of the via waypoint location.
84+
* @return this builder
85+
*/
86+
@NonNull
87+
public abstract Builder geometryIndex(int geometryIndex);
88+
89+
/**
90+
* Build a new {@link SilentWaypoint} object.
91+
*
92+
* @return a new {@link SilentWaypoint} object
93+
*/
94+
@NonNull
95+
public abstract SilentWaypoint build();
96+
}
97+
}

services-directions-models/src/test/java/com/mapbox/api/directions/v5/models/DirectionsResponseTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.mapbox.core.TestUtils;
55
import com.mapbox.geojson.Point;
66

7+
import java.io.IOException;
8+
import java.util.Arrays;
79
import java.util.List;
810
import org.junit.Assert;
911
import org.junit.Test;
@@ -19,6 +21,7 @@ public class DirectionsResponseTest extends TestUtils {
1921
private static final String DIRECTIONS_V5_MULTIPLE_ROUTES = "directions_v5_multiple_routes.json";
2022
private static final String DIRECTIONS_V5_MULTIPLE_ROUTES_WITH_OPTIONS =
2123
"directions_v5_multiple_routes_with_options.json";
24+
private static final String DIRECTIONS_V5_SILENT_WAYPOINT = "directions_v5_silent_waypoints.json";
2225

2326
@Test
2427
public void sanity() throws Exception {
@@ -109,4 +112,30 @@ public void fromJson_deserializesOptions() throws Exception {
109112
assertNotNull(routes.get(0).routeOptions());
110113
assertNotNull(routes.get(1).routeOptions());
111114
}
115+
116+
@Test
117+
public void fromJson_deserializeWiaWaypoints() throws IOException {
118+
String json = loadJsonFixture(DIRECTIONS_V5_SILENT_WAYPOINT);
119+
120+
DirectionsRoute route = DirectionsResponse.fromJson(json).routes().get(0);
121+
122+
List<SilentWaypoint> viaWaypoints = route.legs().get(0).viaWaypoints();
123+
assertNotNull(viaWaypoints);
124+
assertEquals(1, viaWaypoints.size());
125+
SilentWaypoint waypoint = viaWaypoints.get(0);
126+
assertEquals(1, waypoint.waypointIndex());
127+
assertEquals(616.839, waypoint.distanceFromStart(), 0.001);
128+
assertEquals(58, waypoint.geometryIndex());
129+
}
130+
131+
@Test
132+
public void fromToJsonForRouteWithSilentWaypoints() throws IOException {
133+
String json = loadJsonFixture(DIRECTIONS_V5_SILENT_WAYPOINT);
134+
135+
DirectionsResponse initial = DirectionsResponse.fromJson(json);
136+
String serialized = initial.toJson();
137+
DirectionsResponse deserialized = DirectionsResponse.fromJson(serialized);
138+
139+
assertEquals(initial, deserialized);
140+
}
112141
}

0 commit comments

Comments
 (0)