Skip to content

Commit 22599ef

Browse files
author
Seth Bourget
authored
Updated IsoChrone API with latest features/options. (#1596)
1 parent b33ae25 commit 22599ef

File tree

5 files changed

+437
-12
lines changed

5 files changed

+437
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ Mapbox welcomes participation and contributions from everyone.
44

55
### main
66

7-
### v7.3.0 - September 23, 2024
8-
7+
- Updated IsoChrone API to support new query parameters including contours_meters, road/route exclusions and departure time. [#1596](https://github.com/mapbox/mapbox-java/pull/1596)
98
- Bumped `okhttp` version to `4.10.0`. [#1595](https://github.com/mapbox/mapbox-java/pull/1595)
109

1110
### v7.2.0 - August 28, 2024

services-isochrone/src/main/java/com/mapbox/api/isochrone/IsochroneCriteria.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public class IsochroneCriteria {
4444
*/
4545
public static final String PROFILE_CYCLING = "cycling";
4646

47+
/**
48+
* For fastest travel by car using current and historic traffic conditions.
49+
* Traffic information is available for the supported geographies listed in our Traffic Data page.
50+
*/
51+
public static final String PROFILE_DRIVING_TRAFFIC = "driving-traffic";
52+
4753
/**
4854
* Queries for a specific geometry type selector.
4955
*
@@ -53,7 +59,8 @@ public class IsochroneCriteria {
5359
@StringDef( {
5460
PROFILE_WALKING,
5561
PROFILE_DRIVING,
56-
PROFILE_CYCLING
62+
PROFILE_CYCLING,
63+
PROFILE_DRIVING_TRAFFIC
5764
})
5865
public @interface IsochroneProfile {
5966
}

services-isochrone/src/main/java/com/mapbox/api/isochrone/IsochroneService.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ public interface IsochroneService {
5151
* optimized generalization to use for the request. Note that the
5252
* generalization of contours can lead to self-intersections, as well
5353
* as intersections of adjacent contours.
54+
* @param contoursMeters A single String which has a comma-separated integers in meters
55+
* to use for each isochrone contour.
56+
* @param exclude Exclude certain road types and custom locations from routing. Default
57+
* is to not exclude anything from the list below. You can specify
58+
* multiple values as a comma-separated list.
59+
* @param depart The departure time from the given coordinates. Formatted in one of
60+
* three ISO 8601 formats.
61+
* See https://docs.mapbox.com/api/navigation/isochrone/
62+
* If not provided then depart_at is considered to be the present time
63+
* in the local timezone of the coordinates. The isochrone contours will
64+
* traffic conditions at the time provided.
5465
* @return a {@link FeatureCollection} in a Call wrapper
5566
* @since 4.7.0
5667
*/
@@ -64,5 +75,8 @@ Call<FeatureCollection> getCall(
6475
@Query("contours_colors") String contoursColors,
6576
@Query("polygons") Boolean polygons,
6677
@Query("denoise") Float denoise,
67-
@Query("generalize") Float generalize);
78+
@Query("generalize") Float generalize,
79+
@Query("contours_meters") String contoursMeters,
80+
@Query("exclude") String exclude,
81+
@Query("depart_at") String depart);
6882
}

services-isochrone/src/main/java/com/mapbox/api/isochrone/MapboxIsochrone.java

Lines changed: 188 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
import com.mapbox.geojson.Point;
1818
import com.mapbox.geojson.gson.GeoJsonAdapterFactory;
1919

20+
import java.util.HashSet;
2021
import java.util.Locale;
22+
import java.util.Set;
2123

2224
import retrofit2.Call;
2325

@@ -50,6 +52,8 @@ protected MapboxIsochrone() {
5052

5153
@Override
5254
protected GsonBuilder getGsonBuilder() {
55+
MapboxIsochrone.Builder b = MapboxIsochrone.builder();
56+
5357
return new GsonBuilder()
5458
.registerTypeAdapterFactory(GeoJsonAdapterFactory.create())
5559
.registerTypeAdapterFactory(GeometryAdapterFactory.create());
@@ -66,7 +70,10 @@ protected Call<FeatureCollection> initializeCall() {
6670
contoursColors(),
6771
polygons(),
6872
denoise(),
69-
generalize()
73+
generalize(),
74+
contoursMeters(),
75+
exclusions(),
76+
departAt()
7077
);
7178
}
7279

@@ -80,6 +87,8 @@ protected Call<FeatureCollection> initializeCall() {
8087
public static Builder builder() {
8188
return new AutoValue_MapboxIsochrone.Builder()
8289
.baseUrl(Constants.BASE_API_URL)
90+
.contoursMinutes("")
91+
.contoursMeters("")
8392
.user(IsochroneCriteria.PROFILE_DEFAULT_USER);
8493
}
8594

@@ -114,6 +123,15 @@ public static Builder builder() {
114123
@Nullable
115124
abstract Float generalize();
116125

126+
@Nullable
127+
abstract String contoursMeters();
128+
129+
@Nullable
130+
abstract String exclusions();
131+
132+
@Nullable
133+
abstract String departAt();
134+
117135
/**
118136
* This builder is used to create a new request to the Mapbox Isochrone API. At a bare minimum,
119137
* your request must include an access token, a directions routing profile (driving, walking,
@@ -131,9 +149,11 @@ public abstract static class Builder {
131149

132150
private Integer[] contoursMinutes;
133151
private String[] contoursColors;
152+
private Integer[] contoursMeters;
153+
private Set<String> exclusions = new HashSet<>();
134154

135155
/**
136-
* Optionally change the APIs base URL to something other then the default Mapbox one.
156+
* Optionally change the APIs base URL to something other than the default Mapbox one.
137157
*
138158
* @param baseUrl base url used as end point
139159
* @return this builder for chaining options together
@@ -164,6 +184,8 @@ public abstract static class Builder {
164184
/**
165185
* A Mapbox Directions routing profile ID. Options are
166186
* {@link IsochroneCriteria#PROFILE_DRIVING} for travel times by car,
187+
* {@link IsochroneCriteria#PROFILE_DRIVING_TRAFFIC} for fastest travel by car using
188+
* current and historic traffic,
167189
* {@link IsochroneCriteria#PROFILE_WALKING} for pedestrian and hiking travel times,
168190
* and {@link IsochroneCriteria#PROFILE_CYCLING} for travel times by bicycle.
169191
*
@@ -217,6 +239,22 @@ public Builder addContoursMinutes(@NonNull @IntRange(from = 0, to = 60)
217239
return this;
218240
}
219241

242+
/**
243+
* An integer list of meter values to use for each isochrone contour.
244+
* The distances, in meters, to use for each isochrone contour. You
245+
* must be in increasing order. The default maximum distance that can be specified
246+
* is 100000 meters (100km), if you need a bigger range contact support.
247+
*
248+
* @param listOfMeterValues an integer list with at least one number
249+
* for the meters which represent each contour
250+
* @return this builder for chaining options together
251+
* @since 7.3.0
252+
*/
253+
public Builder addContoursMeters(Integer... listOfMeterValues) {
254+
this.contoursMeters = listOfMeterValues;
255+
return this;
256+
}
257+
220258
/**
221259
* A single String which is a comma-separated list of time(s) in minutes
222260
* to use for each isochrone contour. You must pass in at least one minute
@@ -226,12 +264,25 @@ public Builder addContoursMinutes(@NonNull @IntRange(from = 0, to = 60)
226264
*
227265
* @param stringListOfMinuteValues a String of at least one number for the
228266
* minutes which represent each contour
229-
* @return this builder for chaining optio.ns together
267+
* @return this builder for chaining options together
230268
*/
231269
// Required for matching with MapboxIsochrone addContoursMinutes() method.
232270
@SuppressWarnings("WeakerAccess")
233271
abstract Builder contoursMinutes(@NonNull String stringListOfMinuteValues);
234272

273+
/**
274+
* A single String which is a comma-separated list of values(s) in meters
275+
* to use for each isochrone contour. The distances, in meters, to use for each
276+
* isochrone contour. You can specify up to four contours. Distances must be in
277+
* increasing order. The default maximum distance that can be specified
278+
* is 100000 meters (100km), if you need a bigger range contact support.
279+
*
280+
* @param stringListOfMeterValues a String of at least one number for the
281+
* meters which represent each contour
282+
* @return this builder for chaining options together
283+
*/
284+
abstract Builder contoursMeters(@NonNull String stringListOfMeterValues);
285+
235286
/**
236287
* A list of separate String which has a list of comma-separated
237288
* HEX color values to use for each isochrone contour.
@@ -300,6 +351,107 @@ public Builder addContoursColors(@Nullable String... contoursColors) {
300351
*/
301352
public abstract Builder generalize(@Nullable @FloatRange(from = 0.0) Float generalize);
302353

354+
abstract Builder exclusions(@Nullable String exclusions);
355+
356+
/**
357+
* Exclude highways or motorways. Available in mapbox/driving and
358+
* mapbox/driving-traffic profiles.
359+
* @param exclude indicates whether motorways should be excluded
360+
* @return this builder for chaining options together
361+
*
362+
* @since 7.3.0
363+
*/
364+
public Builder excludeMotorways(Boolean exclude) {
365+
if (exclude != null && exclude) {
366+
exclusions.add("motorway");
367+
} else {
368+
exclusions.remove("motorway");
369+
}
370+
return this;
371+
}
372+
373+
/**
374+
* Exclude tolls. Available in mapbox/driving and
375+
* mapbox/driving-traffic profiles.
376+
* @param exclude indicates whether tolls should be excluded
377+
* @return this builder for chaining options together
378+
*
379+
* @since 7.3.0
380+
*/
381+
public Builder excludeTolls(Boolean exclude) {
382+
if (exclude != null && exclude) {
383+
exclusions.add("toll");
384+
} else {
385+
exclusions.remove("toll");
386+
}
387+
return this;
388+
}
389+
390+
/**
391+
* Exclude ferries. Available in mapbox/driving and
392+
* mapbox/driving-traffic profiles.
393+
* @param exclude indicates whether ferries should be excluded
394+
* @return this builder for chaining options together
395+
*
396+
* @since 7.3.0
397+
*/
398+
public Builder excludeFerries(Boolean exclude) {
399+
if (exclude != null && exclude) {
400+
exclusions.add("ferry");
401+
} else {
402+
exclusions.remove("ferry");
403+
}
404+
return this;
405+
}
406+
407+
/**
408+
* Exclude unpaved roads. Available in mapbox/driving and
409+
* mapbox/driving-traffic profiles.
410+
* @param exclude indicates whether unpaved roads should be excluded
411+
* @return this builder for chaining options together
412+
*
413+
* @since 7.3.0
414+
*/
415+
public Builder excludeUnpavedRoads(Boolean exclude) {
416+
if (exclude != null && exclude) {
417+
exclusions.add("unpaved");
418+
} else {
419+
exclusions.remove("unpaved");
420+
}
421+
return this;
422+
}
423+
424+
/**
425+
* Exclude cash only toll roads. Available in mapbox/driving and
426+
* mapbox/driving-traffic profiles.
427+
* @param exclude indicates whether cash only toll roads should be excluded
428+
* @return this builder for chaining options together
429+
*
430+
* @since 7.3.0
431+
*/
432+
public Builder excludeCashOnlyTollRoads(Boolean exclude) {
433+
if (exclude != null && exclude) {
434+
exclusions.add("cash_only_tolls");
435+
} else {
436+
exclusions.remove("cash_only_tolls");
437+
}
438+
return this;
439+
}
440+
441+
/**
442+
* The departure time from the given coordinates.
443+
* <a href="https://docs.mapbox.com/api/navigation/isochrone/">One of three formats.</a>.
444+
* If not provided then 'depart at' is considered to be the present time in the local
445+
* timezone of the coordinates. The isochrone contours will reflect traffic
446+
* conditions at the time provided.
447+
*
448+
* @param depart the departure date/time
449+
* @return this builder for chaining options together
450+
*
451+
* @since 7.3.0
452+
*/
453+
public abstract Builder departAt(@NonNull String depart);
454+
303455
/**
304456
* @return this builder for chaining options together
305457
* @since 4.6.0
@@ -314,6 +466,8 @@ public Builder addContoursColors(@Nullable String... contoursColors) {
314466
*/
315467
public MapboxIsochrone build() {
316468

469+
exclusions(TextUtils.join(",", exclusions.toArray()));
470+
317471
if (contoursMinutes != null) {
318472
if (contoursMinutes.length < 1) {
319473
throw new ServicesException("A query with at least one specified "
@@ -331,6 +485,23 @@ public MapboxIsochrone build() {
331485
contoursMinutes(TextUtils.join(",", contoursMinutes));
332486
}
333487

488+
if (contoursMeters != null) {
489+
if (contoursMeters.length < 1) {
490+
throw new ServicesException("A query with at least one specified "
491+
+ "meter value is required.");
492+
}
493+
494+
if (contoursMeters.length >= 2) {
495+
for (int x = 0; x < contoursMeters.length - 1; x++) {
496+
if (contoursMeters[x] > contoursMeters[x + 1]) {
497+
throw new ServicesException("The meters must be listed"
498+
+ " in order from the lowest number to the highest number.");
499+
}
500+
}
501+
contoursMeters(TextUtils.join(",", contoursMeters));
502+
}
503+
}
504+
334505
if (contoursColors != null) {
335506
contoursColors(TextUtils.join(",", contoursColors));
336507
}
@@ -342,6 +513,17 @@ public MapboxIsochrone build() {
342513
+ "must match number of minute elements provided.");
343514
}
344515

516+
if (contoursColors != null
517+
&& contoursMeters != null
518+
&& contoursColors.length != contoursMeters.length) {
519+
throw new ServicesException("Number of color elements "
520+
+ "must match number of meter elements provided.");
521+
}
522+
523+
if (contoursMinutes != null && contoursMeters != null) {
524+
throw new ServicesException("Cannot specify both contoursMinutes and contoursMeters.");
525+
}
526+
345527
MapboxIsochrone isochrone = autoBuild();
346528

347529
if (!MapboxUtils.isAccessTokenValid(isochrone.accessToken())) {
@@ -359,9 +541,10 @@ public MapboxIsochrone build() {
359541
+ " walking, or driving) is required.");
360542
}
361543

362-
if (TextUtils.isEmpty(isochrone.contoursMinutes())) {
544+
if (TextUtils.isEmpty(isochrone.contoursMinutes())
545+
&& TextUtils.isEmpty(isochrone.contoursMeters())) {
363546
throw new ServicesException("A query with at least one specified minute amount"
364-
+ " is required.");
547+
+ " or meter value is required.");
365548
}
366549

367550
if (isochrone.contoursColors() != null) {

0 commit comments

Comments
 (0)