Search in sources :

Example 1 with GHGeocodingRequest

use of com.graphhopper.api.model.GHGeocodingRequest in project graphhopper by graphhopper.

the class GraphHopperGeocoding method geocode.

/**
 * Perform a geocoding request. Both forward and revers are possible, just configure the <code>request</code>
 * accordingly.
 *
 * @param request the request to send to the API
 * @return found results for your request
 */
public GHGeocodingResponse geocode(GHGeocodingRequest request) {
    String url = buildUrl(request);
    try {
        Request okRequest = new Request.Builder().url(url).build();
        ResponseBody rspBody = getClientForRequest(request).newCall(okRequest).execute().body();
        return objectMapper.readValue(rspBody.bytes(), GHGeocodingResponse.class);
    } catch (Exception ex) {
        throw new RuntimeException("Problem performing geocoding for " + url + ": " + ex.getMessage(), ex);
    }
}
Also used : Request(okhttp3.Request) GHGeocodingRequest(com.graphhopper.api.model.GHGeocodingRequest) ResponseBody(okhttp3.ResponseBody)

Example 2 with GHGeocodingRequest

use of com.graphhopper.api.model.GHGeocodingRequest in project graphhopper by graphhopper.

the class GraphHopperGeocodingIT method testReverseGeocoding.

@Test
public void testReverseGeocoding() {
    GHGeocodingResponse response = geocoding.geocode(new GHGeocodingRequest(52.5170365, 13.3888599, "en", 5));
    assertEquals(5, response.getHits().size());
    GHGeocodingEntry entry = response.getHits().get(0);
    assertTrue(entry.getName().contains("Berlin"));
    assertEquals("place", entry.getOsmKey());
    assertEquals(0, entry.getExtent().length);
}
Also used : GHGeocodingRequest(com.graphhopper.api.model.GHGeocodingRequest) GHGeocodingEntry(com.graphhopper.api.model.GHGeocodingEntry) GHGeocodingResponse(com.graphhopper.api.model.GHGeocodingResponse) Test(org.junit.Test)

Example 3 with GHGeocodingRequest

use of com.graphhopper.api.model.GHGeocodingRequest in project graphhopper by graphhopper.

the class GraphHopperGeocodingIT method testExtent.

@Test
public void testExtent() {
    GHGeocodingResponse response = geocoding.geocode(new GHGeocodingRequest("new york", "en", 7));
    BBox extent = response.getHits().get(0).getExtendBBox();
    assertTrue(extent.isValid());
    assertTrue(extent.minLon < -79);
    assertTrue(extent.maxLon > -72);
    assertTrue(extent.minLat < 40.5);
    assertTrue(extent.maxLat > 45);
}
Also used : BBox(com.graphhopper.util.shapes.BBox) GHGeocodingRequest(com.graphhopper.api.model.GHGeocodingRequest) GHGeocodingResponse(com.graphhopper.api.model.GHGeocodingResponse) Test(org.junit.Test)

Example 4 with GHGeocodingRequest

use of com.graphhopper.api.model.GHGeocodingRequest in project graphhopper by graphhopper.

the class GraphHopperGeocodingIT method testForwardGeocoding.

@Test
public void testForwardGeocoding() {
    GHGeocodingResponse response = geocoding.geocode(new GHGeocodingRequest("Berlin", "en", 7));
    assertEquals(7, response.getHits().size());
    assertTrue(response.getHits().get(0).getName().contains("Berlin"));
}
Also used : GHGeocodingRequest(com.graphhopper.api.model.GHGeocodingRequest) GHGeocodingResponse(com.graphhopper.api.model.GHGeocodingResponse) Test(org.junit.Test)

Example 5 with GHGeocodingRequest

use of com.graphhopper.api.model.GHGeocodingRequest in project graphhopper by graphhopper.

the class GraphHopperGeocodingIT method testForwardGeocodingNominatim.

@Test
public void testForwardGeocodingNominatim() {
    GHGeocodingResponse response = geocoding.geocode(new GHGeocodingRequest(false, null, "Berlin", "en", 5, "nominatim", 5000));
    assertEquals(5, response.getHits().size());
    assertTrue(response.getHits().get(0).getName().contains("Berlin"));
}
Also used : GHGeocodingRequest(com.graphhopper.api.model.GHGeocodingRequest) GHGeocodingResponse(com.graphhopper.api.model.GHGeocodingResponse) Test(org.junit.Test)

Aggregations

GHGeocodingRequest (com.graphhopper.api.model.GHGeocodingRequest)5 GHGeocodingResponse (com.graphhopper.api.model.GHGeocodingResponse)4 Test (org.junit.Test)4 GHGeocodingEntry (com.graphhopper.api.model.GHGeocodingEntry)1 BBox (com.graphhopper.util.shapes.BBox)1 Request (okhttp3.Request)1 ResponseBody (okhttp3.ResponseBody)1