diff --git a/converter.yml b/converter.yml index 4bd01c1..84b20e9 100644 --- a/converter.yml +++ b/converter.yml @@ -4,13 +4,13 @@ nominatim: true nominatimEmail: openCageDataURL: https://api.opencagedata.com/geocode/v1/json -openCageDataKey: -openCageData: false +openCageDataKey: ${OCD_KEY} +openCageData: true -gisgraphyGeocodingURL: https://premium2.gisgraphy.com/geocoding/ -gisgraphyReverseGeocodingURL: https://premium2.gisgraphy.com/reversegeocoding/ -gisgraphySearchURL: https://premium2.gisgraphy.com/fulltext/ -gisgraphyAPIKey: +gisgraphyGeocodingURL: ${GIS_GEO_URL} +gisgraphyReverseGeocodingURL: ${GIS_REV_GEO_URL} +gisgraphySearchURL: ${GIS_SEARCH_URL} +gisgraphyAPIKey: ${GIS_KEY} # e.g. to restrict for local access # ipWhiteList:"localhost,127.0.0.1" diff --git a/src/main/java/com/graphhopper/converter/ConverterApplication.java b/src/main/java/com/graphhopper/converter/ConverterApplication.java index 69e305e..b4a44a7 100644 --- a/src/main/java/com/graphhopper/converter/ConverterApplication.java +++ b/src/main/java/com/graphhopper/converter/ConverterApplication.java @@ -9,6 +9,8 @@ import io.dropwizard.Application; import io.dropwizard.client.JerseyClientBuilder; import io.dropwizard.client.JerseyClientConfiguration; +import io.dropwizard.configuration.EnvironmentVariableSubstitutor; +import io.dropwizard.configuration.SubstitutingSourceProvider; import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Environment; import io.dropwizard.util.Duration; @@ -34,7 +36,13 @@ public String getName() { @Override public void initialize(Bootstrap bootstrap) { - // nothing to do yet + // Enable variable substitution with environment variables + bootstrap.setConfigurationSourceProvider( + new SubstitutingSourceProvider(bootstrap.getConfigurationSourceProvider(), + new EnvironmentVariableSubstitutor(false) + ) + ); + } @Override @@ -65,7 +73,7 @@ public void run(ConverterConfiguration converterConfiguration, Environment envir if (converterConfiguration.isGisgraphy()) { final ConverterResourceGisgraphy resource = new ConverterResourceGisgraphy( - converterConfiguration.getGisgraphyGeocodingURL(), converterConfiguration.getGisgraphyReverseGeocodingURL(),converterConfiguration.getGisgraphySearchURL(),converterConfiguration.getGisgraphyAPIKey(), client); + converterConfiguration.getGisgraphyGeocodingURL(), converterConfiguration.getGisgraphyReverseGeocodingURL(), converterConfiguration.getGisgraphySearchURL(), converterConfiguration.getGisgraphyAPIKey(), client); environment.jersey().register(resource); } diff --git a/src/main/java/com/graphhopper/converter/api/AbstractAddress.java b/src/main/java/com/graphhopper/converter/api/AbstractAddress.java index 6199c72..271c8fb 100644 --- a/src/main/java/com/graphhopper/converter/api/AbstractAddress.java +++ b/src/main/java/com/graphhopper/converter/api/AbstractAddress.java @@ -18,6 +18,8 @@ public abstract class AbstractAddress { @JsonProperty public String state; @JsonProperty + public String county; + @JsonProperty public String town; @JsonProperty public String village; diff --git a/src/main/java/com/graphhopper/converter/api/GHEntry.java b/src/main/java/com/graphhopper/converter/api/GHEntry.java index 7c3d03b..18cd238 100644 --- a/src/main/java/com/graphhopper/converter/api/GHEntry.java +++ b/src/main/java/com/graphhopper/converter/api/GHEntry.java @@ -22,12 +22,13 @@ public class GHEntry { private String country; private String city; private String state; + private String county; private String street; private String houseNumber; private String postcode; private String osmValue; - public GHEntry(Long osmId, String type, double lat, double lng, String name, String osmValue, String country, String city, String state, String street, String houseNumber, String postcode, Extent extent) { + public GHEntry(Long osmId, String type, double lat, double lng, String name, String osmValue, String country, String city, String state, String county, String street, String houseNumber, String postcode, Extent extent) { this.osmId = osmId; this.osmType = type; this.point = new Point(lat, lng); @@ -35,6 +36,7 @@ public GHEntry(Long osmId, String type, double lat, double lng, String name, Str this.country = country; this.city = city; this.state = state; + this.county = county; this.street = street; this.houseNumber = houseNumber; this.postcode = postcode; @@ -43,7 +45,7 @@ public GHEntry(Long osmId, String type, double lat, double lng, String name, Str } public GHEntry(Long osmId, String type, double lat, double lng, String name, String osmValue, AbstractAddress address, Extent extent) { - this(osmId, type, lat, lng, name, osmValue, address.country, address.getGHCity(), address.state, address.getStreetName(), address.houseNumber, address.postcode, extent); + this(osmId, type, lat, lng, name, osmValue, address.country, address.getGHCity(), address.state, address.county, address.getStreetName(), address.houseNumber, address.postcode, extent); } public GHEntry(){} @@ -78,6 +80,16 @@ public String getState() { return state; } + @JsonProperty + public void setCounty(String county) { + this.county = county; + } + + @JsonProperty + public String getCounty() { + return county; + } + @JsonProperty public String getCity() { return city; diff --git a/src/main/java/com/graphhopper/converter/api/GisgraphyAddressEntry.java b/src/main/java/com/graphhopper/converter/api/GisgraphyAddressEntry.java index 65b40e3..2c22285 100644 --- a/src/main/java/com/graphhopper/converter/api/GisgraphyAddressEntry.java +++ b/src/main/java/com/graphhopper/converter/api/GisgraphyAddressEntry.java @@ -17,7 +17,7 @@ public class GisgraphyAddressEntry { private long sourceId; private String countryCode; - + private String country; private String city; @@ -146,8 +146,8 @@ public String getDisplayName() { public String getCountry() { return country; } - - public void setCountry(String country){ + + public void setCountry(String country) { this.country = country; } diff --git a/src/main/java/com/graphhopper/converter/api/OpenCageDataEntry.java b/src/main/java/com/graphhopper/converter/api/OpenCageDataEntry.java index ab70ec8..2221d5c 100644 --- a/src/main/java/com/graphhopper/converter/api/OpenCageDataEntry.java +++ b/src/main/java/com/graphhopper/converter/api/OpenCageDataEntry.java @@ -70,8 +70,6 @@ public OCDComponents() { public String type; @JsonProperty("country_code") public String countryCode; - @JsonProperty("county") - public String county; } public OpenCageDataEntry() { diff --git a/src/main/java/com/graphhopper/converter/core/Converter.java b/src/main/java/com/graphhopper/converter/core/Converter.java index 4e804b6..11e7e30 100644 --- a/src/main/java/com/graphhopper/converter/core/Converter.java +++ b/src/main/java/com/graphhopper/converter/core/Converter.java @@ -16,7 +16,7 @@ public static GHEntry convertFromGisgraphyAddress(GisgraphyAddressEntry gisgraph GHEntry rsp = new GHEntry(null, null, gisgraphyEntry.getLat(), gisgraphyEntry.getLng(), gisgraphyEntry.getDisplayName(), null, gisgraphyEntry.getCountry(), gisgraphyEntry.getCity(), - gisgraphyEntry.getState(), gisgraphyEntry.getStreetName(), + gisgraphyEntry.getState(), null, gisgraphyEntry.getStreetName(), gisgraphyEntry.getHouseNumber(), gisgraphyEntry.getZipCode(), null); return rsp; } @@ -25,7 +25,7 @@ public static GHEntry convertFromGisgraphySearch(GisgraphySearchEntry gisgraphyE GHEntry rsp = new GHEntry(null, null, gisgraphyEntry.getLat(), gisgraphyEntry.getLng(), gisgraphyEntry.getLabel(), null, gisgraphyEntry.getCountry(), gisgraphyEntry.getIsIn(), - gisgraphyEntry.getAdm1Name(), gisgraphyEntry.getName(), + gisgraphyEntry.getAdm1Name(), null, gisgraphyEntry.getName(), gisgraphyEntry.getHouseNumber(), gisgraphyEntry.getZipCode(), null); return rsp; } diff --git a/src/main/java/com/graphhopper/converter/resources/ConverterResourceOpenCageData.java b/src/main/java/com/graphhopper/converter/resources/ConverterResourceOpenCageData.java index 4cd6122..26eeec6 100644 --- a/src/main/java/com/graphhopper/converter/resources/ConverterResourceOpenCageData.java +++ b/src/main/java/com/graphhopper/converter/resources/ConverterResourceOpenCageData.java @@ -49,9 +49,10 @@ public Response handle(@QueryParam("q") @DefaultValue("") String query, WebTarget target = jerseyClient. target(url). queryParam("q", reverse ? point : query). - queryParam("key", key). queryParam("limit", limit); + target = target.queryParam("key", key); + if (!find_osm_id) { target = target.queryParam("no_annotations", "1"); } diff --git a/src/test/java/com/graphhopper/converter/resource/ConverterResourceGisgraphyTest.java b/src/test/java/com/graphhopper/converter/resource/ConverterResourceGisgraphyTest.java index cebcff1..2b90b6a 100644 --- a/src/test/java/com/graphhopper/converter/resource/ConverterResourceGisgraphyTest.java +++ b/src/test/java/com/graphhopper/converter/resource/ConverterResourceGisgraphyTest.java @@ -1,113 +1,112 @@ package com.graphhopper.converter.resource; -import static junit.framework.TestCase.assertTrue; -import static org.assertj.core.api.Assertions.assertThat; +import com.graphhopper.converter.ConverterApplication; +import com.graphhopper.converter.ConverterConfiguration; +import com.graphhopper.converter.api.GHResponse; import io.dropwizard.client.JerseyClientBuilder; import io.dropwizard.testing.ResourceHelpers; import io.dropwizard.testing.junit.DropwizardAppRule; - -import javax.ws.rs.client.Client; -import javax.ws.rs.core.Response; - import org.glassfish.jersey.client.ClientProperties; import org.junit.ClassRule; import org.junit.Test; -import com.graphhopper.converter.ConverterApplication; -import com.graphhopper.converter.ConverterConfiguration; -import com.graphhopper.converter.api.GHResponse; +import javax.ws.rs.client.Client; +import javax.ws.rs.core.Response; + +import static junit.framework.TestCase.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Robin Boldt */ public class ConverterResourceGisgraphyTest { - @ClassRule - public static final DropwizardAppRule RULE = - new DropwizardAppRule<>(ConverterApplication.class, ResourceHelpers.resourceFilePath("converter.yml")); - - @Test - public void testHandleForward() { - Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test forward client"); - - client.property(ClientProperties.CONNECT_TIMEOUT, 100000); - client.property(ClientProperties.READ_TIMEOUT, 100000); - - Response response = client.target( - String.format("http://localhost:%d/gisgraphy?q=berlin", RULE.getLocalPort())) - .request() - .get(); - - assertThat(response.getStatus()).isEqualTo(200); - GHResponse entry = response.readEntity(GHResponse.class); - assertTrue(entry.getHits().size()>0); - - //now try with an Address - response = client.target( - String.format("http://localhost:%d/gisgraphy?q=103+avenue+des+champs+elysees,paris", RULE.getLocalPort())) - .request() - .get(); - - assertThat(response.getStatus()).isEqualTo(200); - entry = response.readEntity(GHResponse.class); - assertTrue(entry.getHits().size()>0); - } - - @Test - public void testHandleReverse() { - Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test reverse client"); - - client.property(ClientProperties.CONNECT_TIMEOUT, 100000); - client.property(ClientProperties.READ_TIMEOUT, 100000); - - Response response = client.target( - String.format("http://localhost:%d/gisgraphy/?point=52.5487429714954,-1.81602098644987&reverse=true", RULE.getLocalPort())) - .request() - .get(); - - assertThat(response.getStatus()).isEqualTo(200); - GHResponse entry = response.readEntity(GHResponse.class); - assertTrue(entry.getHits().size()>0); - - } - - @Test - public void testHandleAutocomplete() { - Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test autocomplete client"); - - client.property(ClientProperties.CONNECT_TIMEOUT, 100000); - client.property(ClientProperties.READ_TIMEOUT, 100000); - - Response response = client.target( - String.format("http://localhost:%d/gisgraphy?q=pari&autocomplete=true", RULE.getLocalPort())) - .request() - .get(); - - assertThat(response.getStatus()).isEqualTo(200); - GHResponse entry = response.readEntity(GHResponse.class); - assertTrue(entry.getHits().size()>0); - - } - - @Test - public void testHandleAutocompleteWithReverseShouldThrows() { - Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test autocomplete-reverse client"); - - client.property(ClientProperties.CONNECT_TIMEOUT, 100000); - client.property(ClientProperties.READ_TIMEOUT, 100000); - - Response response = null; - try { - response = client.target( - String.format("http://localhost:%d/gisgraphy?q=pari&point=52.5487429714954,-1.81602098644987&autocomplete=true&reverse=true", RULE.getLocalPort())) - .request() - .get(); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - assertThat(response.getStatus()).isEqualTo(400); - - } + @ClassRule + public static final DropwizardAppRule RULE = + new DropwizardAppRule<>(ConverterApplication.class, ResourceHelpers.resourceFilePath("converter.yml")); + + @Test + public void testHandleForward() { + Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test forward client"); + + client.property(ClientProperties.CONNECT_TIMEOUT, 100000); + client.property(ClientProperties.READ_TIMEOUT, 100000); + + Response response = client.target( + String.format("http://localhost:%d/gisgraphy?q=berlin", RULE.getLocalPort())) + .request() + .get(); + + assertThat(response.getStatus()).isEqualTo(200); + GHResponse entry = response.readEntity(GHResponse.class); + assertTrue(entry.getHits().size() > 0); + + //now try with an Address + response = client.target( + String.format("http://localhost:%d/gisgraphy?q=103+avenue+des+champs+elysees,paris", RULE.getLocalPort())) + .request() + .get(); + + assertThat(response.getStatus()).isEqualTo(200); + entry = response.readEntity(GHResponse.class); + assertTrue(entry.getHits().size() > 0); + } + + @Test + public void testHandleReverse() { + Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test reverse client"); + + client.property(ClientProperties.CONNECT_TIMEOUT, 100000); + client.property(ClientProperties.READ_TIMEOUT, 100000); + + Response response = client.target( + String.format("http://localhost:%d/gisgraphy/?point=52.5487429714954,-1.81602098644987&reverse=true", RULE.getLocalPort())) + .request() + .get(); + + assertThat(response.getStatus()).isEqualTo(200); + GHResponse entry = response.readEntity(GHResponse.class); + assertTrue(entry.getHits().size() > 0); + + } + + @Test + public void testHandleAutocomplete() { + Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test autocomplete client"); + + client.property(ClientProperties.CONNECT_TIMEOUT, 100000); + client.property(ClientProperties.READ_TIMEOUT, 100000); + + Response response = client.target( + String.format("http://localhost:%d/gisgraphy?q=pari&autocomplete=true", RULE.getLocalPort())) + .request() + .get(); + + assertThat(response.getStatus()).isEqualTo(200); + GHResponse entry = response.readEntity(GHResponse.class); + assertTrue(entry.getHits().size() > 0); + + } + + @Test + public void testHandleAutocompleteWithReverseShouldThrows() { + Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test autocomplete-reverse client"); + + client.property(ClientProperties.CONNECT_TIMEOUT, 100000); + client.property(ClientProperties.READ_TIMEOUT, 100000); + + Response response = null; + try { + response = client.target( + String.format("http://localhost:%d/gisgraphy?q=pari&point=52.5487429714954,-1.81602098644987&autocomplete=true&reverse=true", RULE.getLocalPort())) + .request() + .get(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + assertThat(response.getStatus()).isEqualTo(400); + + } } diff --git a/src/test/java/com/graphhopper/converter/resource/ConverterResourceNominatimTest.java b/src/test/java/com/graphhopper/converter/resource/ConverterResourceNominatimTest.java index 5549f91..2a2036b 100644 --- a/src/test/java/com/graphhopper/converter/resource/ConverterResourceNominatimTest.java +++ b/src/test/java/com/graphhopper/converter/resource/ConverterResourceNominatimTest.java @@ -166,4 +166,23 @@ public void testIncorrectFormattedPoint() { assertThat(response.getStatus()).isEqualTo(400); } + + @Test + public void testIssue50() { + Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test issue 50"); + + client.property(ClientProperties.CONNECT_TIMEOUT, 100000); + client.property(ClientProperties.READ_TIMEOUT, 100000); + + Response response = client.target( + String.format("http://localhost:%d/nominatim?point=48.4882,2.6996&reverse=true", RULE.getLocalPort())) + .request() + .get(); + + assertThat(response.getStatus()).isEqualTo(200); + GHResponse entry = response.readEntity(GHResponse.class); + + // OCD responds with "Seine-et-Marne", both seem to be interlinked: https://en.wikipedia.org/wiki/Fontainebleau + assertEquals("Fontainebleau", entry.getHits().get(0).getCounty()); + } } diff --git a/src/test/java/com/graphhopper/converter/resource/ConverterResourceOpenCageDataTest.java b/src/test/java/com/graphhopper/converter/resource/ConverterResourceOpenCageDataTest.java new file mode 100644 index 0000000..8158ef4 --- /dev/null +++ b/src/test/java/com/graphhopper/converter/resource/ConverterResourceOpenCageDataTest.java @@ -0,0 +1,49 @@ +package com.graphhopper.converter.resource; + +import com.graphhopper.converter.ConverterApplication; +import com.graphhopper.converter.ConverterConfiguration; +import com.graphhopper.converter.api.GHResponse; +import io.dropwizard.client.JerseyClientBuilder; +import io.dropwizard.testing.ResourceHelpers; +import io.dropwizard.testing.junit.DropwizardAppRule; +import org.glassfish.jersey.client.ClientProperties; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; + +import javax.ws.rs.client.Client; +import javax.ws.rs.core.Response; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; + +/** + * In order to successfully run this class, you need to specify the ocdKey as environment variable, for example run: + * export OCD_KEY="My_Key" + * + * @author Robin Boldt + */ +public class ConverterResourceOpenCageDataTest { + @ClassRule + public static final DropwizardAppRule RULE = + new DropwizardAppRule<>(ConverterApplication.class, ResourceHelpers.resourceFilePath("converter.yml")); + + @Test + public void testIssue50() { + Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test issue 50"); + + client.property(ClientProperties.CONNECT_TIMEOUT, 100000); + client.property(ClientProperties.READ_TIMEOUT, 100000); + + Response response = client.target( + String.format("http://localhost:%d/opencagedata?point=48.4882,2.6996&reverse=true", RULE.getLocalPort())) + .request() + .get(); + + assertThat(response.getStatus()).isEqualTo(200); + GHResponse entry = response.readEntity(GHResponse.class); + + assertEquals("Seine-et-Marne", entry.getHits().get(0).getCounty()); + } + +} diff --git a/src/test/resources/converter.yml b/src/test/resources/converter.yml index 9df7343..21bcd09 100644 --- a/src/test/resources/converter.yml +++ b/src/test/resources/converter.yml @@ -1 +1,10 @@ nominatimURL: https://nominatim.openstreetmap.org/search/ + +openCageDataURL: https://api.opencagedata.com/geocode/v1/json +openCageDataKey: ${OCD_KEY} +openCageData: true + +gisgraphyGeocodingURL: ${GIS_GEO_URL} +gisgraphyReverseGeocodingURL: ${GIS_REV_GEO_URL} +gisgraphySearchURL: ${GIS_SEARCH_URL} +gisgraphyAPIKey: ${GIS_KEY} \ No newline at end of file