Skip to content

Commit 20e01b5

Browse files
authored
[geocoding] Add intersection search support. (#1074)
* [geocoding] Add intersection search support. * [geocoding] Set geocodingType to address by default when intersectionStreet is used. * [geocoding] Fixes after @langsmith's review. * [geocoding] Add @SInCE annotation. Co-Authored-By: Langston Smith <[email protected]> * [geocoding] Add test case for checking key word . * [geocoding] Add change log.
1 parent 2130ea7 commit 20e01b5

File tree

5 files changed

+403
-25
lines changed

5 files changed

+403
-25
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
Mapbox welcomes participation and contributions from everyone.
44

5+
### master
6+
7+
- Added intersection search support to MapboxGeocoding [#1074](https://github.com/mapbox/mapbox-java/pull/1074)
8+
59
### v4.9.0-alpha.1 - September 4, 2019
610

711
- Fixed up bintray publish script, generate sources and javadoc jars #1065

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ public abstract static class Builder {
247247

248248
private List<String> countries = new ArrayList<>();
249249

250+
private List<String> intersectionStreets = new ArrayList<>();
251+
250252
/**
251253
* Perform a reverse geocode on the provided {@link Point}. Only one point can be passed in as
252254
* the query and isn't guaranteed to return a result. If you are an enterprise customer and
@@ -581,6 +583,22 @@ public abstract Builder reverseMode(
581583

582584
abstract MapboxGeocoding autoBuild();
583585

586+
587+
/**
588+
* Specify the two street names for intersection search.
589+
*
590+
* @param streetOneName First street name of the intersection
591+
* @param streetTwoName Second street name of the intersection
592+
* @return this builder for chaining options together
593+
* @since 4.10.0
594+
*/
595+
public Builder intersectionStreets(@NonNull String streetOneName,
596+
@NonNull String streetTwoName) {
597+
intersectionStreets.add(streetOneName);
598+
intersectionStreets.add(streetTwoName);
599+
return this;
600+
}
601+
584602
/**
585603
* Build a new {@link MapboxGeocoding} object.
586604
*
@@ -593,6 +611,11 @@ public MapboxGeocoding build() {
593611
country(TextUtils.join(",", countries.toArray()));
594612
}
595613

614+
if (intersectionStreets.size() == 2) {
615+
query(TextUtils.join(" and ", intersectionStreets.toArray()));
616+
geocodingTypes(GeocodingCriteria.TYPE_ADDRESS);
617+
}
618+
596619
// Generate build so that we can check that values are valid.
597620
MapboxGeocoding geocoding = autoBuild();
598621

@@ -607,6 +630,22 @@ public MapboxGeocoding build() {
607630
&& geocoding.limit() != null && !geocoding.limit().equals("1")) {
608631
throw new ServicesException("Limit must be combined with a single type parameter");
609632
}
633+
634+
if (intersectionStreets.size() == 2) {
635+
if (!(geocoding.mode().equals(GeocodingCriteria.MODE_PLACES)
636+
|| geocoding.mode().equals(GeocodingCriteria.MODE_PLACES_PERMANENT))) {
637+
throw new ServicesException("Geocoding mode must be GeocodingCriteria.MODE_PLACES "
638+
+ "or GeocodingCriteria.MODE_PLACES_PERMANENT for intersection search.");
639+
}
640+
if (TextUtils.isEmpty(geocoding.geocodingTypes())
641+
|| !geocoding.geocodingTypes().equals(GeocodingCriteria.TYPE_ADDRESS)) {
642+
throw new ServicesException("Geocoding type must be set to Geocoding "
643+
+ "Criteria.TYPE_ADDRESS for intersection search.");
644+
}
645+
if (TextUtils.isEmpty(geocoding.proximity())) {
646+
throw new ServicesException("Geocoding proximity must be set for intersection search.");
647+
}
648+
}
610649
return geocoding;
611650
}
612651
}

services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/GeocodingTestUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class GeocodingTestUtils extends TestUtils {
2020
private static final String FORWARD_INVALID = "forward_invalid.json";
2121
private static final String FORWARD_VALID_ZH = "forward_valid_zh.json";
2222
private static final String FORWARD_BATCH_GEOCODING = "geocoding_batch.json";
23+
private static final String FORWARD_INTERSECTION = "forward_intersection.json";
2324

2425
private MockWebServer server;
2526
protected HttpUrl mockUrl;
@@ -43,6 +44,8 @@ public MockResponse dispatch(RecordedRequest request) throws InterruptedExceptio
4344
response = loadJsonFixture(FORWARD_GEOCODING);
4445
} else if (request.getPath().contains("sandy")) {
4546
response = loadJsonFixture(FORWARD_INVALID);
47+
} else if (request.getPath().contains("%20and%20")) {
48+
response = loadJsonFixture(FORWARD_INTERSECTION);
4649
} else {
4750
response = loadJsonFixture(FORWARD_VALID_ZH);
4851
}

services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/MapboxGeocodingTest.java

Lines changed: 98 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
package com.mapbox.api.geocoding.v5;
22

33
import com.mapbox.api.geocoding.v5.models.GeocodingResponse;
4-
import com.mapbox.core.TestUtils;
54
import com.mapbox.core.exceptions.ServicesException;
65
import com.mapbox.geojson.BoundingBox;
76
import com.mapbox.geojson.Point;
87

9-
import org.hamcrest.junit.ExpectedException;
10-
import org.junit.After;
11-
import org.junit.Before;
12-
import org.junit.Rule;
138
import org.junit.Test;
149

1510
import java.io.IOException;
1611
import java.util.List;
1712
import java.util.Locale;
1813

19-
import okhttp3.HttpUrl;
20-
import okhttp3.mockwebserver.MockResponse;
21-
import okhttp3.mockwebserver.MockWebServer;
22-
import okhttp3.mockwebserver.RecordedRequest;
2314
import retrofit2.Response;
2415

2516
import static org.hamcrest.Matchers.startsWith;
@@ -67,7 +58,7 @@ public void executeBatchCall_exceptionThrownWhenModeNotSetCorrectly() throws Exc
6758
}
6859

6960
@Test
70-
public void build_noAccessTokenExceptionThrown() throws Exception {
61+
public void build_noAccessTokenExceptionThrown() {
7162
thrown.expect(IllegalStateException.class);
7263
thrown.expectMessage("Missing required properties: accessToken");
7364
MapboxGeocoding.builder()
@@ -77,7 +68,7 @@ public void build_noAccessTokenExceptionThrown() throws Exception {
7768
}
7869

7970
@Test
80-
public void build_invalidAccessTokenExceptionThrown() throws Exception {
71+
public void build_invalidAccessTokenExceptionThrown() {
8172
thrown.expect(ServicesException.class);
8273
thrown.expectMessage(
8374
startsWith("Using Mapbox Services requires setting a valid access token."));
@@ -113,7 +104,7 @@ public void query_acceptsPointsCorrectly() throws Exception {
113104
}
114105

115106
@Test
116-
public void baseUrl_doesChangeTheRequestUrl() throws Exception {
107+
public void baseUrl_doesChangeTheRequestUrl() {
117108
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
118109
.accessToken(ACCESS_TOKEN)
119110
.baseUrl("https://foobar.com")
@@ -124,7 +115,7 @@ public void baseUrl_doesChangeTheRequestUrl() throws Exception {
124115
}
125116

126117
@Test
127-
public void country_localeCountryConvertsCorrectly() throws Exception {
118+
public void country_localeCountryConvertsCorrectly() {
128119
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
129120
.accessToken(ACCESS_TOKEN)
130121
.baseUrl(mockUrl.toString())
@@ -135,7 +126,7 @@ public void country_localeCountryConvertsCorrectly() throws Exception {
135126
}
136127

137128
@Test
138-
public void country_localeCountryHandlesMultipleCountriesCorrectly() throws Exception {
129+
public void country_localeCountryHandlesMultipleCountriesCorrectly() {
139130
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
140131
.accessToken(ACCESS_TOKEN)
141132
.baseUrl(mockUrl.toString())
@@ -148,7 +139,7 @@ public void country_localeCountryHandlesMultipleCountriesCorrectly() throws Exce
148139
}
149140

150141
@Test
151-
public void proximity_doesGetAddedToUrlCorrectly() throws Exception {
142+
public void proximity_doesGetAddedToUrlCorrectly() {
152143
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
153144
.accessToken(ACCESS_TOKEN)
154145
.baseUrl(mockUrl.toString())
@@ -160,7 +151,7 @@ public void proximity_doesGetAddedToUrlCorrectly() throws Exception {
160151
}
161152

162153
@Test
163-
public void geocodingTypes_getsAddedToUrlCorrectly() throws Exception {
154+
public void geocodingTypes_getsAddedToUrlCorrectly() {
164155
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
165156
.accessToken(ACCESS_TOKEN)
166157
.baseUrl(mockUrl.toString())
@@ -172,7 +163,7 @@ public void geocodingTypes_getsAddedToUrlCorrectly() throws Exception {
172163
}
173164

174165
@Test
175-
public void autocomplete_getsAddedToUrlCorrectly() throws Exception {
166+
public void autocomplete_getsAddedToUrlCorrectly() {
176167
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
177168
.accessToken(ACCESS_TOKEN)
178169
.baseUrl(mockUrl.toString())
@@ -184,7 +175,7 @@ public void autocomplete_getsAddedToUrlCorrectly() throws Exception {
184175
}
185176

186177
@Test
187-
public void bbox_getsFormattedCorrectlyForUrl() throws Exception {
178+
public void bbox_getsFormattedCorrectlyForUrl() {
188179
BoundingBox bbox = BoundingBox.fromLngLats(
189180
-77.083056, 38.908611, -76.997778, 38.959167);
190181
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
@@ -198,7 +189,7 @@ public void bbox_getsFormattedCorrectlyForUrl() throws Exception {
198189
}
199190

200191
@Test
201-
public void bbox_asPoints_getsFormattedCorrectlyForUrl() throws Exception {
192+
public void bbox_asPoints_getsFormattedCorrectlyForUrl() {
202193
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
203194
.accessToken(ACCESS_TOKEN)
204195
.baseUrl(mockUrl.toString())
@@ -213,7 +204,7 @@ public void bbox_asPoints_getsFormattedCorrectlyForUrl() throws Exception {
213204
}
214205

215206
@Test
216-
public void limit_getsAddedToUrlCorrectly() throws Exception {
207+
public void limit_getsAddedToUrlCorrectly() {
217208
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
218209
.accessToken(ACCESS_TOKEN)
219210
.baseUrl(mockUrl.toString())
@@ -225,7 +216,7 @@ public void limit_getsAddedToUrlCorrectly() throws Exception {
225216
}
226217

227218
@Test
228-
public void language_getsConvertedToUrlCorrectly() throws Exception {
219+
public void language_getsConvertedToUrlCorrectly() {
229220
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
230221
.accessToken(ACCESS_TOKEN)
231222
.baseUrl(mockUrl.toString())
@@ -237,7 +228,7 @@ public void language_getsConvertedToUrlCorrectly() throws Exception {
237228
}
238229

239230
@Test
240-
public void clientAppName_hasAppInString() throws Exception {
231+
public void clientAppName_hasAppInString() {
241232
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
242233
.accessToken(ACCESS_TOKEN)
243234
.baseUrl(mockUrl.toString())
@@ -249,7 +240,7 @@ public void clientAppName_hasAppInString() throws Exception {
249240
}
250241

251242
@Test
252-
public void reverseMode_getsAddedToUrlCorrectly() throws Exception {
243+
public void reverseMode_getsAddedToUrlCorrectly() {
253244
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
254245
.accessToken(ACCESS_TOKEN)
255246
.baseUrl("https://foobar.com")
@@ -270,7 +261,7 @@ public void reverseMode_getsAddedToUrlCorrectly() throws Exception {
270261
}
271262

272263
@Test
273-
public void reverseMode_onlyLimit1_Allowed() throws Exception {
264+
public void reverseMode_onlyLimit1_Allowed() {
274265
thrown.expect(ServicesException.class);
275266
thrown.expectMessage(startsWith("Limit must be combined with a single type"));
276267
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
@@ -296,7 +287,7 @@ public void fuzzyMatchSanity() throws Exception {
296287
}
297288

298289
@Test
299-
public void fuzzyMatch_getsAddedToUrlCorrectly() throws Exception {
290+
public void fuzzyMatch_getsAddedToUrlCorrectly() {
300291
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
301292
.accessToken(ACCESS_TOKEN)
302293
.query("wahsington")
@@ -307,4 +298,86 @@ public void fuzzyMatch_getsAddedToUrlCorrectly() throws Exception {
307298
assertTrue(mapboxGeocoding.cloneCall().request().url().toString()
308299
.contains("fuzzyMatch=true"));
309300
}
301+
302+
@Test
303+
public void intersectionSearchSanity() throws IOException {
304+
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
305+
.accessToken(ACCESS_TOKEN)
306+
.intersectionStreets("Market Street", "Fremont Street")
307+
.proximity("-122.39738575285674,37.792514711136945")
308+
.geocodingTypes(GeocodingCriteria.TYPE_ADDRESS)
309+
.baseUrl(mockUrl.toString())
310+
.build();
311+
assertNotNull(mapboxGeocoding);
312+
Response<GeocodingResponse> response = mapboxGeocoding.executeCall();
313+
assertEquals(200, response.code());
314+
}
315+
316+
@Test
317+
public void intersectionSearchAndAddedCorrectly() {
318+
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
319+
.accessToken(ACCESS_TOKEN)
320+
.intersectionStreets("Street one", "Street two")
321+
.proximity("-122.39738575285674,37.792514711136945")
322+
.geocodingTypes(GeocodingCriteria.TYPE_ADDRESS)
323+
.baseUrl(mockUrl.toString())
324+
.build();
325+
assertNotNull(mapboxGeocoding);
326+
assertTrue(mapboxGeocoding.cloneCall().request().url().toString().contains("and"));
327+
}
328+
329+
@Test
330+
public void intersectionSearch_AddIntersectionToQuery() {
331+
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
332+
.accessToken(ACCESS_TOKEN)
333+
.intersectionStreets("Market Street", "Fremont Street")
334+
.proximity("-122.39738575285674,37.792514711136945")
335+
.geocodingTypes(GeocodingCriteria.TYPE_ADDRESS)
336+
.baseUrl(mockUrl.toString())
337+
.build();
338+
assertNotNull(mapboxGeocoding);
339+
assertTrue(mapboxGeocoding.cloneCall().request().url().toString()
340+
.contains("Market%20Street%20and%20Fremont%20Street"));
341+
}
342+
343+
@Test
344+
public void intersectionSearch_WrongMode() {
345+
thrown.expect(ServicesException.class);
346+
thrown.expectMessage(
347+
startsWith("Geocoding mode must be GeocodingCriteria.MODE_PLACES or GeocodingCriteria.MODE_PLACES_PERMANENT for intersection search."));
348+
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
349+
.accessToken(ACCESS_TOKEN)
350+
.mode("RandomMode")
351+
.intersectionStreets("Market Street", "Fremont Street")
352+
.proximity("-122.39738575285674,37.792514711136945")
353+
.geocodingTypes(GeocodingCriteria.TYPE_ADDRESS)
354+
.baseUrl(mockUrl.toString())
355+
.build();
356+
}
357+
358+
@Test
359+
public void intersectionSearch_AutoSetGeocodingType() {
360+
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
361+
.accessToken(ACCESS_TOKEN)
362+
.intersectionStreets("Market Street", "Fremont Street")
363+
.proximity("-122.39738575285674,37.792514711136945")
364+
.baseUrl(mockUrl.toString())
365+
.build();
366+
assertNotNull(mapboxGeocoding);
367+
assertTrue(mapboxGeocoding.cloneCall().request().url().toString()
368+
.contains("types=address"));
369+
}
370+
371+
@Test
372+
public void intersectionSearch_EmptyPromixity() {
373+
thrown.expect(ServicesException.class);
374+
thrown.expectMessage(
375+
startsWith("Geocoding proximity must be set for intersection search."));
376+
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
377+
.accessToken(ACCESS_TOKEN)
378+
.intersectionStreets("Market Street", "Fremont Street")
379+
.geocodingTypes(GeocodingCriteria.TYPE_ADDRESS)
380+
.baseUrl(mockUrl.toString())
381+
.build();
382+
}
310383
}

0 commit comments

Comments
 (0)