Skip to content

Commit bd7b0e5

Browse files
authored
Add annotations, approaches request parameter & distances to the response object (#911)
* adding a distance component to the matrix response * adding documentation * adding more documentation * fixing documentation * removing annotation enum since it already exists * updated fixtures, tests added approaches
1 parent 066f388 commit bd7b0e5

8 files changed

Lines changed: 230 additions & 33 deletions

File tree

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,16 @@ graphs:
4848
./gradlew :services-turf:generateDependencyGraphMapboxLibraries
4949

5050
directions-matrix-fixtures:
51+
# request a symmetric 1x3 matrix for pedestrians
52+
curl "https://api.mapbox.com/directions-matrix/v1/mapbox/walking/-122.42,37.78;-122.45,37.91;-122.48,37.73?sources=1&annotations=distance,duration&approaches=curb;curb;curb&access_token=$(MAPBOX_ACCESS_TOKEN)" \
53+
-o services-matrix/src/test/resources/directions_matrix_1x3.json
54+
5155
# request a symmetric 3x3 matrix for cars
5256
curl "https://api.mapbox.com/directions-matrix/v1/mapbox/driving/-122.42,37.78;-122.45,37.91;-122.48,37.73?access_token=$(MAPBOX_ACCESS_TOKEN)" \
5357
-o services-matrix/src/test/resources/directions_matrix_3x3.json
5458

5559
# request an asymmetric 2x3 matrix for bicycles
56-
curl "https://api.mapbox.com/directions-matrix/v1/mapbox/cycling/-122.42,37.78;-122.45,37.91;-122.48,37.73?sources=0;2&destinations=all&access_token=$(MAPBOX_ACCESS_TOKEN)" \
60+
curl "https://api.mapbox.com/directions-matrix/v1/mapbox/cycling/-122.42,37.78;-122.45,37.91;-122.48,37.73?sources=0;2&access_token=$(MAPBOX_ACCESS_TOKEN)" \
5761
-o services-matrix/src/test/resources/directions_matrix_2x3.json
5862

5963
geocoding-fixtures:

services-matrix/src/main/java/com/mapbox/api/matrix/v1/MapboxMatrix.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ protected Call<MatrixResponse> initializeCall() {
6565
profile(),
6666
coordinates(),
6767
accessToken(),
68+
annotations(),
69+
approaches(),
6870
destinations(),
6971
sources());
7072
}
@@ -87,6 +89,12 @@ protected Call<MatrixResponse> initializeCall() {
8789
@Nullable
8890
abstract String sources();
8991

92+
@Nullable
93+
abstract String annotations();
94+
95+
@Nullable
96+
abstract String approaches();
97+
9098
@Nullable
9199
abstract String destinations();
92100

@@ -126,6 +134,8 @@ public static Builder builder() {
126134
public abstract static class Builder {
127135

128136
private List<Point> coordinates = new ArrayList<>();
137+
private String[] annotations;
138+
private String[] approaches;
129139
private Integer[] destinations;
130140
private Integer[] sources;
131141

@@ -175,9 +185,10 @@ public Builder coordinate(@NonNull Point coordinate) {
175185
/**
176186
* This selects which mode of transportation the user will be using to accurately give the
177187
* matrix durations. The options include driving, driving considering traffic, walking, and
178-
* cycling. Using each of these profiles will result in different durations
188+
* cycling. Using each of these profiles will result in different durations.
179189
*
180-
* @param profile required to be one of the String values found in the {@link ProfileCriteria}
190+
* @param profile required to be one of the String values found in the
191+
* {@link ProfileCriteria}
181192
* @return this builder for chaining options together
182193
* @since 2.1.0
183194
*/
@@ -194,6 +205,46 @@ public Builder coordinate(@NonNull Point coordinate) {
194205
*/
195206
public abstract Builder accessToken(@NonNull String accessToken);
196207

208+
209+
/**
210+
* Optionally pass in annotations to control to change which data to return.
211+
*
212+
* @param annotations 1 or more annotations
213+
* @return this builder for chaining options together
214+
* @since 4.1.0
215+
*/
216+
public Builder addAnnotations(
217+
@Nullable @DirectionsCriteria.AnnotationCriteria String... annotations) {
218+
this.annotations = annotations;
219+
return this;
220+
}
221+
222+
// Required for matching with MapboxMatrix annotations() method.
223+
abstract Builder annotations(@Nullable String annotations);
224+
225+
/**
226+
* A semicolon-separated list indicating the side of the road from
227+
* which to approach waypoints in a requested route.
228+
* Accepts unrestricted (default, route can arrive at the waypoint from either
229+
* side of the road) or curb (route will arrive at the waypoint on
230+
* the driving_side of the region).
231+
* If provided, the number of approaches must be the same as the number of waypoints.
232+
* However, you can skip a coordinate and show its position in the list with the ; separator.
233+
*
234+
* @param approaches null if you'd like the default approaches,
235+
* else one of the options found in
236+
* {@link com.mapbox.api.directions.v5.DirectionsCriteria.ApproachesCriteria}.
237+
* @return this builder for chaining options together
238+
* @since 4.1.0
239+
*/
240+
public Builder addApproaches(@Nullable String... approaches) {
241+
this.approaches = approaches;
242+
return this;
243+
}
244+
245+
abstract Builder approaches(
246+
@Nullable @DirectionsCriteria.ApproachesCriteria String approaches);
247+
197248
/**
198249
* Optionally pass in indexes to generate an asymmetric matrix.
199250
*
@@ -264,6 +315,8 @@ public MapboxMatrix build() {
264315

265316
sources(TextUtils.join(";", sources));
266317
destinations(TextUtils.join(";", destinations));
318+
annotations(TextUtils.join(",", annotations));
319+
approaches(TextUtils.join(";", approaches));
267320

268321
// Generate build so that we can check that values are valid.
269322
MapboxMatrix matrix = autoBuild();

services-matrix/src/main/java/com/mapbox/api/matrix/v1/MatrixService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ public interface MatrixService {
2323
* @param profile the profile directions should use
2424
* @param coordinates the coordinates the route should follow
2525
* @param accessToken Mapbox access token
26+
* @param annotations Used to specify the resulting matrices.
27+
* Possible values are: duration (default),
28+
* distance, or both values separated by comma
29+
* @param approaches A semicolon-separated list indicating the side of the road from
30+
* which to approach waypoints in a requested route.
31+
* Accepts unrestricted (default, route can arrive at the waypoint
32+
* from either side of the road) or curb (route will arrive at
33+
* the waypoint on the driving_side of the region).
34+
* If provided, the number of approaches must be the same
35+
* as the number of waypoints.
36+
* However, you can skip a coordinate and
37+
* show its position in the list with the ; separator.
2638
* @param destinations array of waypoint objects. Each waypoints is an input coordinate snapped to
2739
* the road and path network. The waypoints appear in the array in the order
2840
* of the input coordinates, or in the order as specified in the destinations
@@ -42,6 +54,8 @@ Call<MatrixResponse> getCall(
4254
@Path("profile") String profile,
4355
@Path("coordinates") String coordinates,
4456
@Query("access_token") String accessToken,
57+
@Query("annotations") String annotations,
58+
@Query("approaches") String approaches,
4559
@Query("destinations") String destinations,
4660
@Query("sources") String sources
4761
);

services-matrix/src/main/java/com/mapbox/api/matrix/v1/models/MatrixResponse.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.mapbox.api.matrix.v1.models;
22

3-
import android.support.annotation.NonNull;
3+
import java.io.Serializable;
4+
import java.util.List;
5+
46
import android.support.annotation.Nullable;
57
import com.google.auto.value.AutoValue;
68
import com.google.gson.Gson;
79
import com.google.gson.TypeAdapter;
810
import com.mapbox.api.directions.v5.models.DirectionsWaypoint;
911

10-
import java.io.Serializable;
11-
import java.util.List;
12+
import android.support.annotation.NonNull;
1213

1314
/**
1415
* This contains the Matrix API response information which can be used to display the results.
@@ -83,6 +84,18 @@ public static Builder builder() {
8384
@Nullable
8485
public abstract List<Double[]> durations();
8586

87+
/**
88+
* Distances as a list of arrays representing the matrix in row-major order.
89+
* distances[i][j] gives the travel distance from the i-th source to the j-th destination.
90+
* All values are in meters. The duration between the same coordinate is always 0.
91+
* If a distance can not be found, the result is null.
92+
*
93+
* @return an array of array with each entry being a distance value given in meters.
94+
* @since 4.1.0
95+
*/
96+
@Nullable
97+
public abstract List<Double[]> distances();
98+
8699
/**
87100
* Convert the current {@link MatrixResponse} to its builder holding the currently assigned
88101
* values. This allows you to modify a single variable and then rebuild the object resulting in
@@ -167,6 +180,18 @@ public abstract static class Builder {
167180
*/
168181
public abstract Builder durations(@Nullable List<Double[]> durations);
169182

183+
/**
184+
* Distances as a list of arrays representing the matrix in row-major order.
185+
* distances[i][j] gives the travel distance from the i-th source to the j-th destination.
186+
* All values are in meters. The duration between the same coordinate is always 0.
187+
* If a distance can not be found, the result is null.
188+
*
189+
* @param distances an array of array with each entry being a distance value given in meters
190+
* @return this builder for chaining options together
191+
* @since 4.1.0
192+
*/
193+
public abstract Builder distances(@Nullable List<Double[]> distances);
194+
170195
/**
171196
* Build a new {@link MatrixResponse} object.
172197
*

services-matrix/src/test/java/com/mapbox/api/matrix/v1/MapboxMatrixTest.java

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@
2222
import okhttp3.mockwebserver.RecordedRequest;
2323
import retrofit2.Response;
2424

25+
import static com.mapbox.api.directions.v5.DirectionsCriteria.ANNOTATION_DISTANCE;
26+
import static com.mapbox.api.directions.v5.DirectionsCriteria.ANNOTATION_DURATION;
27+
import static com.mapbox.api.directions.v5.DirectionsCriteria.APPROACH_CURB;
2528
import static org.hamcrest.core.StringStartsWith.startsWith;
2629
import static org.junit.Assert.assertEquals;
2730
import static org.junit.Assert.assertNotNull;
2831
import static org.junit.Assert.assertTrue;
2932

3033
public class MapboxMatrixTest extends TestUtils {
3134

32-
private static final String DIRECTIONS_MATRIX_3X3_FIXTURE = "directions_matrix_3x3.json";
33-
private static final String DIRECTIONS_MATRIX_2x3_FIXTURE = "directions_matrix_2x3.json";
35+
private static final String DIRECTIONS_MATRIX_WALKING_1X3_FIXTURE = "directions_matrix_1x3.json";
36+
private static final String DIRECTIONS_MATRIX_CYCLING_2X3_FIXTURE = "directions_matrix_2x3.json";
37+
private static final String DIRECTIONS_MATRIX_DRIVING_3X3_FIXTURE = "directions_matrix_3x3.json";
3438

3539
private MockWebServer server;
3640
private HttpUrl mockUrl;
@@ -49,7 +53,17 @@ public void setUp() throws IOException {
4953
@Override
5054
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
5155
try {
52-
String response = loadJsonFixture(DIRECTIONS_MATRIX_3X3_FIXTURE);
56+
String resource;
57+
if (request.getPath().contains("walking")) {
58+
resource = DIRECTIONS_MATRIX_WALKING_1X3_FIXTURE;
59+
60+
} else if (request.getPath().contains("cycling")) {
61+
resource = DIRECTIONS_MATRIX_CYCLING_2X3_FIXTURE;
62+
63+
} else { // driving
64+
resource = DIRECTIONS_MATRIX_DRIVING_3X3_FIXTURE;
65+
}
66+
String response = loadJsonFixture(resource);
5367
return new MockResponse().setBody(response);
5468
} catch (IOException ioException) {
5569
throw new RuntimeException(ioException);
@@ -77,7 +91,7 @@ public void tearDown() throws IOException {
7791
public void testCallSanity() throws ServicesException, IOException {
7892
MapboxMatrix client = MapboxMatrix.builder()
7993
.accessToken(ACCESS_TOKEN)
80-
.profile(DirectionsCriteria.PROFILE_WALKING)
94+
.profile(DirectionsCriteria.PROFILE_DRIVING)
8195
.coordinates(positions)
8296
.baseUrl(mockUrl.toString())
8397
.build();
@@ -88,6 +102,7 @@ public void testCallSanity() throws ServicesException, IOException {
88102
assertNotNull(response.body());
89103
assertEquals(3, response.body().destinations().size());
90104
assertNotNull(response.body().destinations().get(0).name());
105+
assertEquals(3, response.body().durations().size());
91106
}
92107

93108
@Test
@@ -155,19 +170,56 @@ public void testUserAgent() throws ServicesException, IOException {
155170
}
156171

157172
@Test
158-
public void testResponse() throws ServicesException, IOException {
173+
public void request_containsAnnotations() throws ServicesException, IOException {
174+
MapboxMatrix client = MapboxMatrix.builder()
175+
.accessToken(ACCESS_TOKEN)
176+
.profile(DirectionsCriteria.PROFILE_WALKING)
177+
.coordinates(positions)
178+
.addAnnotations(ANNOTATION_DISTANCE, ANNOTATION_DURATION)
179+
.sources(0, 2)
180+
.build();
181+
182+
assertTrue(client.cloneCall()
183+
.request().url().toString()
184+
.contains("annotations=distance"));
185+
}
186+
187+
@Test
188+
public void request_containsApproaches() throws ServicesException, IOException {
189+
MapboxMatrix client = MapboxMatrix.builder()
190+
.accessToken(ACCESS_TOKEN)
191+
.profile(DirectionsCriteria.PROFILE_WALKING)
192+
.coordinates(positions)
193+
.addApproaches(APPROACH_CURB, APPROACH_CURB, APPROACH_CURB)
194+
.build();
195+
196+
String c = client.cloneCall().request().url().toString();
197+
198+
199+
assertTrue(c.contains("approaches=curb"));
200+
}
201+
202+
@Test
203+
public void annotationsAndApproaches() throws ServicesException, IOException {
159204
MapboxMatrix client = MapboxMatrix.builder()
160205
.accessToken(ACCESS_TOKEN)
161-
.profile(DirectionsCriteria.PROFILE_DRIVING)
206+
.profile(DirectionsCriteria.PROFILE_WALKING)
162207
.coordinates(positions)
208+
.addAnnotations(ANNOTATION_DISTANCE, ANNOTATION_DURATION)
209+
.addApproaches(APPROACH_CURB, APPROACH_CURB, APPROACH_CURB)
210+
.sources(0,2)
163211
.baseUrl(mockUrl.toString())
164212
.build();
165-
Response<MatrixResponse> response = client.executeCall();
166213

167-
assertEquals(response.body().sources().size(), 3);
168-
assertEquals(response.body().sources().get(0).location().longitude(), -122.420019, DELTA);
169-
assertEquals(response.body().destinations().get(0).location().longitude(), -122.420019, DELTA);
170-
// assertEquals(response.body().durations().get(0), 1888.3, DELTA);
171-
assertEquals(response.body().destinations().get(0).name(), "McAllister Street");
214+
Response<MatrixResponse> response = client.executeCall();
215+
assertEquals(200, response.code());
216+
assertEquals(1, response.body().sources().size());
217+
assertEquals(1, response.body().distances().size());
218+
assertEquals(1, response.body().durations().size());
219+
assertEquals(-122.461997, response.body().sources().get(0).location().longitude(), DELTA);
220+
assertEquals(-122.420019, response.body().destinations().get(0).location().longitude(), DELTA);
221+
assertEquals(19711.7, response.body().durations().get(0)[2], DELTA);
222+
assertEquals(27192.3, response.body().distances().get(0)[2], DELTA);
223+
assertEquals("McAllister Street", response.body().destinations().get(0).name());
172224
}
173225
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"distances": [
3+
[
4+
18664.5,
5+
0,
6+
27192.3
7+
]
8+
],
9+
"durations": [
10+
[
11+
13490.5,
12+
0,
13+
19711.7
14+
]
15+
],
16+
"destinations": [
17+
{
18+
"name": "McAllister Street",
19+
"location": [
20+
-122.420019,
21+
37.780091
22+
]
23+
},
24+
{
25+
"name": "Playa Verde",
26+
"location": [
27+
-122.461997,
28+
37.89621
29+
]
30+
},
31+
{
32+
"name": "",
33+
"location": [
34+
-122.480005,
35+
37.730039
36+
]
37+
}
38+
],
39+
"sources": [
40+
{
41+
"name": "Playa Verde",
42+
"location": [
43+
-122.461997,
44+
37.89621
45+
]
46+
}
47+
],
48+
"code": "Ok"
49+
}

0 commit comments

Comments
 (0)