Skip to content

Commit 5a3f31c

Browse files
authored
Batch geocoding support (#267)
* new geocoding batch fixture * add a new geocoding-batch-fixtures rule and fix the paths * reformat fixture for easy reading * add batch methods and tests * update @SInCE 2.0.0 tags * remove unnecessary code from MapboxGeocodingBatchTest * o o p s
1 parent 18b7a87 commit 5a3f31c

5 files changed

Lines changed: 513 additions & 12 deletions

File tree

Makefile

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,37 @@ dex-count:
5555
geocoding-fixtures:
5656
# Geocoding: 1600 Pennsylvania Ave NW
5757
curl "https://api.mapbox.com/geocoding/v5/mapbox.places/1600+pennsylvania+ave+nw.json?access_token=$(MAPBOX_ACCESS_TOKEN)" \
58-
-o libjava/lib/src/test/fixtures/geocoding.json
58+
-o mapbox/libjava-services/src/test/fixtures/geocoding.json
5959

6060
# Reverse geocoding: -77.0366, 38.8971
6161
curl "https://api.mapbox.com/geocoding/v5/mapbox.places/-77.0366,38.8971.json?access_token=$(MAPBOX_ACCESS_TOKEN)" \
62-
-o libjava/lib/src/test/fixtures/geocoding_reverse.json
62+
-o mapbox/libjava-services/src/test/fixtures/geocoding_reverse.json
6363

64-
#
64+
# Not supported country
6565
curl "https://api.mapbox.com/geocoding/v5/mapbox.places/1600+pennsylvania+ave+nw.json?country=aq&access_token=$(MAPBOX_ACCESS_TOKEN)" \
66-
-o libjava/lib/src/test/fixtures/geocoding_country_not_supported.json
66+
-o mapbox/libjava-services/src/test/fixtures/geocoding_country_not_supported.json
67+
68+
geocoding-batch-fixtures:
69+
curl "https://api.mapbox.com/geocoding/v5/mapbox.places-permanent/20001;20009;22209.json?access_token=$(MAPBOX_ACCESS_TOKEN)" \
70+
-o mapbox/libjava-services/src/test/fixtures/geocoding_batch.json
6771

6872
directions-fixtures:
6973
curl "https://api.mapbox.com/directions/v5/mapbox/driving/-122.416667,37.783333;-121.900000,37.333333?geometries=polyline&steps=true&access_token=$(MAPBOX_ACCESS_TOKEN)" \
70-
-o libjava/lib/src/test/fixtures/directions_v5.json
74+
-o mapbox/libjava-services/src/test/fixtures/directions_v5.json
7175

7276
mapmatching-fixtures:
7377
# Geometry polyline
74-
curl -X POST --header "Content-Type:application/json" -d @libjava/lib/src/test/fixtures/mapmatching_trace.json \
78+
curl -X POST --header "Content-Type:application/json" -d @mapbox/libjava-services/src/test/fixtures/mapmatching_trace.json \
7579
"https://api.mapbox.com/matching/v4/mapbox.driving.json?geometry=polyline&access_token=$(MAPBOX_ACCESS_TOKEN)" \
76-
-o libjava/lib/src/test/fixtures/mapmatching_v5_polyline.json
80+
-o mapbox/libjava-services/src/test/fixtures/mapmatching_v5_polyline.json
7781

7882
# No geometry
79-
curl -X POST --header "Content-Type:application/json" -d @libjava/lib/src/test/fixtures/mapmatching_trace.json \
83+
curl -X POST --header "Content-Type:application/json" -d @mapbox/libjava-services/src/test/fixtures/mapmatching_trace.json \
8084
"https://api.mapbox.com/matching/v4/mapbox.driving.json?geometry=false&access_token=$(MAPBOX_ACCESS_TOKEN)" \
81-
-o libjava/lib/src/test/fixtures/mapmatching_v5_no_geometry.json
85+
-o mapbox/libjava-services/src/test/fixtures/mapmatching_v5_no_geometry.json
8286

8387
distance-fixtures:
8488
# Retrieve a duration matrix
85-
curl -X POST --header "Content-Type:application/json" -d @libjava/lib/src/test/fixtures/distance_coordinates.json \
89+
curl -X POST --header "Content-Type:application/json" -d @mapbox/libjava-services/src/test/fixtures/distance_coordinates.json \
8690
"https://api.mapbox.com/distances/v1/mapbox/driving?access_token=$(MAPBOX_ACCESS_TOKEN)" \
87-
-o libjava/lib/src/test/fixtures/distance_v1.json
91+
-o mapbox/libjava-services/src/test/fixtures/distance_v1.json

mapbox/libjava-services/src/main/java/com/mapbox/services/api/geocoding/v5/GeocodingService.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.mapbox.services.api.geocoding.v5.models.GeocodingResponse;
44

5+
import java.util.List;
6+
57
import retrofit2.Call;
68
import retrofit2.http.GET;
79
import retrofit2.http.Header;
@@ -19,7 +21,7 @@ public interface GeocodingService {
1921
* Call-based interface
2022
*
2123
* @param userAgent The user
22-
* @param mode mapbox.places or mapbox.places-permanent for enterprise/batch geocoding.
24+
* @param mode mapbox.places or mapbox.places-permanent for enterprise geocoding.
2325
* @param query a location; a place name for forward geocoding or a coordinate pair
2426
* (longitude, latitude location) for reverse geocoding
2527
* @param accessToken Mapbox access token.
@@ -44,4 +46,34 @@ Call<GeocodingResponse> getCall(
4446
@Query("autocomplete") Boolean autocomplete,
4547
@Query("bbox") String bbox,
4648
@Query("limit") String limit);
49+
50+
/**
51+
* Call-based interface
52+
*
53+
* @param userAgent The user
54+
* @param mode mapbox.places-permanent for batch geocoding.
55+
* @param query a location; a place name for forward geocoding or a coordinate pair
56+
* (longitude, latitude location) for reverse geocoding
57+
* @param accessToken Mapbox access token.
58+
* @param country ISO 3166 alpha 2 country codes, separated by commas.
59+
* @param proximity Location around which to bias results.
60+
* @param types Filter results by one or more type.
61+
* @param autocomplete True if you want auto complete.
62+
* @param bbox Optionally pass in a bounding box to limit results in.
63+
* @param limit Optionally pass in a limit the amount of returning results.
64+
* @return A retrofit Call object
65+
* @since 1.0.0
66+
*/
67+
@GET("/geocoding/v5/{mode}/{query}.json")
68+
Call<List<GeocodingResponse>> getBatchCall(
69+
@Header("User-Agent") String userAgent,
70+
@Path("mode") String mode,
71+
@Path("query") String query,
72+
@Query("access_token") String accessToken,
73+
@Query("country") String country,
74+
@Query("proximity") String proximity,
75+
@Query("types") String types,
76+
@Query("autocomplete") Boolean autocomplete,
77+
@Query("bbox") String bbox,
78+
@Query("limit") String limit);
4779
}

mapbox/libjava-services/src/main/java/com/mapbox/services/api/geocoding/v5/MapboxGeocoding.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.mapbox.services.api.geocoding.v5.models.GeocodingResponse;
1313

1414
import java.io.IOException;
15+
import java.util.List;
1516
import java.util.Locale;
1617

1718
import retrofit2.Call;
@@ -30,6 +31,7 @@ public class MapboxGeocoding extends MapboxService<GeocodingResponse> {
3031
private Builder builder = null;
3132
private GeocodingService service = null;
3233
private Call<GeocodingResponse> call = null;
34+
private Call<List<GeocodingResponse>> batchCall = null;
3335

3436
/**
3537
* Public constructor.
@@ -80,6 +82,10 @@ public Call<GeocodingResponse> getCall() {
8082
return call;
8183
}
8284

85+
if (builder.getQuery().contains(";")) {
86+
throw new IllegalArgumentException("Use getBatchCall() for batch calls.");
87+
}
88+
8389
call = getService().getCall(
8490
getHeaderUserAgent(builder.getClientAppName()),
8591
builder.getMode(),
@@ -95,6 +101,37 @@ public Call<GeocodingResponse> getCall() {
95101
return call;
96102
}
97103

104+
/**
105+
* Used internally.
106+
*
107+
* @return batch call
108+
* @since 2.0.0
109+
*/
110+
public Call<List<GeocodingResponse>> getBatchCall() {
111+
// No need to recreate it
112+
if (batchCall != null) {
113+
return batchCall;
114+
}
115+
116+
if (!builder.getQuery().contains(";")) {
117+
throw new IllegalArgumentException("Use getCall() for non-batch calls.");
118+
}
119+
120+
batchCall = getService().getBatchCall(
121+
getHeaderUserAgent(builder.getClientAppName()),
122+
builder.getMode(),
123+
builder.getQuery(),
124+
builder.getAccessToken(),
125+
builder.getCountry(),
126+
builder.getProximity(),
127+
builder.getGeocodingTypes(),
128+
builder.getAutocomplete(),
129+
builder.getBbox(),
130+
builder.getLimit());
131+
132+
return batchCall;
133+
}
134+
98135
/**
99136
* Execute the call
100137
*
@@ -107,6 +144,17 @@ public Response<GeocodingResponse> executeCall() throws IOException {
107144
return getCall().execute();
108145
}
109146

147+
/**
148+
* Execute the batch call
149+
*
150+
* @return The Geocoding v5 response
151+
* @throws IOException Signals that an I/O exception of some sort has occurred.
152+
* @since 2.0.0
153+
*/
154+
public Response<List<GeocodingResponse>> executeBatchCall() throws IOException {
155+
return getBatchCall().execute();
156+
}
157+
110158
/**
111159
* Execute the call
112160
*
@@ -118,6 +166,16 @@ public void enqueueCall(Callback<GeocodingResponse> callback) {
118166
getCall().enqueue(callback);
119167
}
120168

169+
/**
170+
* Execute the batch call
171+
*
172+
* @param callback A Retrofit callback.
173+
* @since 2.0.0
174+
*/
175+
public void enqueueBatchCall(Callback<List<GeocodingResponse>> callback) {
176+
getBatchCall().enqueue(callback);
177+
}
178+
121179
/**
122180
* Cancel the call
123181
*
@@ -128,6 +186,15 @@ public void cancelCall() {
128186
getCall().cancel();
129187
}
130188

189+
/**
190+
* Cancel the batch call
191+
*
192+
* @since 2.0.0
193+
*/
194+
public void cancelBatchCall() {
195+
getBatchCall().cancel();
196+
}
197+
131198
/**
132199
* clone the call
133200
*
@@ -139,6 +206,16 @@ public Call<GeocodingResponse> cloneCall() {
139206
return getCall().clone();
140207
}
141208

209+
/**
210+
* clone the batch call
211+
*
212+
* @return cloned call
213+
* @since 2.0.0
214+
*/
215+
public Call<List<GeocodingResponse>> cloneBatchCall() {
216+
return getBatchCall().clone();
217+
}
218+
142219
/**
143220
* Builds your geocoder query by adding parameters.
144221
*

0 commit comments

Comments
 (0)