Search in sources :

Example 6 with NearbyLocation

use of org.codice.ddf.spatial.geocoding.context.NearbyLocation in project ddf by codice.

the class TestGeoNamesQueryLuceneIndex method testNearestCitiesWithLessThanMaxResults.

@Test
public void testNearestCitiesWithLessThanMaxResults() throws java.text.ParseException, GeoEntryQueryException {
    String testPoint = "POINT (56.78 1.5)";
    final int requestedMaxResults = 2;
    final int actualResults = 1;
    final List<NearbyLocation> nearestCities = directoryIndex.getNearestCities(testPoint, 50, requestedMaxResults);
    assertThat(nearestCities.size(), is(actualResults));
    /* This distance value was obtained from http://www.movable-type.co.uk/scripts/latlong.html

           Additionally, "Phoenix Airport" (GEO_ENTRY_2) is within 50 km of (56.78, 1.5), but it
           should not be included in the results because its feature code is AIRP (not a city).
        */
    final NearbyLocation first = nearestCities.get(0);
    assertThat(first.getCardinalDirection(), is("N"));
    final double distance = first.getDistance();
    assertThat(String.format("%.2f", distance), is("29.58"));
    assertThat(first.getName(), is("Phoenix"));
}
Also used : NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 7 with NearbyLocation

use of org.codice.ddf.spatial.geocoding.context.NearbyLocation in project ddf by codice.

the class TestGeoNamesQueryLuceneIndex method testNearestCitiesWithMaxResults.

@Test
public void testNearestCitiesWithMaxResults() throws java.text.ParseException, GeoEntryQueryException {
    String testPoint = "POINT (56.78 1)";
    final int requestedMaxResults = 2;
    final List<NearbyLocation> nearestCities = directoryIndex.getNearestCities(testPoint, 50, requestedMaxResults);
    assertThat(nearestCities.size(), is(requestedMaxResults));
    /* These distances values were obtained from
           http://www.movable-type.co.uk/scripts/latlong.html

           Phoenix is first because it has a higher population.
           Additionally, "Phoenix Airport" (GEO_ENTRY_2) is within 50 km of (56.78, 1), but it
           should not be included in the results because its feature code is AIRP (not a city).
        */
    final NearbyLocation first = nearestCities.get(0);
    assertThat(first.getCardinalDirection(), is("S"));
    final double firstDistance = first.getDistance();
    assertThat(String.format("%.2f", firstDistance), is("26.02"));
    assertThat(first.getName(), is("Phoenix"));
    final NearbyLocation second = nearestCities.get(1);
    assertThat(second.getCardinalDirection(), is("W"));
    final double secondDistance = second.getDistance();
    assertThat(String.format("%.2f", secondDistance), is("24.46"));
    assertThat(second.getName(), is("Glendale"));
}
Also used : NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 8 with NearbyLocation

use of org.codice.ddf.spatial.geocoding.context.NearbyLocation in project ddf by codice.

the class TestGeoNamesLocalIndex method testGetNearbyCitiesNoResult.

@Test
public void testGetNearbyCitiesNoResult() throws ParseException, GeoEntryQueryException {
    NearbyLocation mockNearbyLocation = mock(NearbyLocation.class);
    when(mockNearbyLocation.getCardinalDirection()).thenReturn("W");
    when(mockNearbyLocation.getDistance()).thenReturn(10.24);
    when(mockNearbyLocation.getName()).thenReturn("The City");
    List<NearbyLocation> nearbyLocations = mock(List.class);
    when(nearbyLocations.size()).thenReturn(0);
    when(geoEntryQueryable.getNearestCities("POINT(1.0 20)", 50, 1)).thenReturn(nearbyLocations);
    NearbyLocation returnedNearbyLocation = geoNamesLocalIndex.getNearbyCity("POINT(1.0 20)");
    assertThat(returnedNearbyLocation, nullValue());
}
Also used : NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) Test(org.junit.Test)

Example 9 with NearbyLocation

use of org.codice.ddf.spatial.geocoding.context.NearbyLocation in project ddf by codice.

the class TestGeoNamesLocalIndex method testGetNearbyCities.

@Test
public void testGetNearbyCities() throws ParseException, GeoEntryQueryException {
    NearbyLocation mockNearbyLocation = mock(NearbyLocation.class);
    when(mockNearbyLocation.getCardinalDirection()).thenReturn("W");
    when(mockNearbyLocation.getDistance()).thenReturn(10.24);
    when(mockNearbyLocation.getName()).thenReturn("The City");
    List<NearbyLocation> nearbyLocations = mock(List.class);
    when(nearbyLocations.size()).thenReturn(1);
    when(nearbyLocations.get(0)).thenReturn(mockNearbyLocation);
    when(geoEntryQueryable.getNearestCities("POINT(1.0 20)", 50, 1)).thenReturn(nearbyLocations);
    NearbyLocation returnedNearbyLocation = geoNamesLocalIndex.getNearbyCity("POINT(1.0 20)");
    assertThat(returnedNearbyLocation, equalTo(mockNearbyLocation));
}
Also used : NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) Test(org.junit.Test)

Example 10 with NearbyLocation

use of org.codice.ddf.spatial.geocoding.context.NearbyLocation in project ddf by codice.

the class GeoCoderEndpoint method getNearbyCities.

@GET
@Path("nearby/cities/{wkt}")
public Response getNearbyCities(@PathParam("wkt") String wkt) {
    GeoCoder geoCoder = geoCoderFactory.getService();
    try {
        NearbyLocation nearbyLocation = geoCoder.getNearbyCity(wkt);
        if (nearbyLocation != null) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("direction", nearbyLocation.getCardinalDirection());
            jsonObject.put("distance", nearbyLocation.getDistance());
            jsonObject.put("name", nearbyLocation.getName());
            return Response.ok(jsonObject.toJSONString()).build();
        } else {
            return Response.status(Response.Status.NO_CONTENT).build();
        }
    } catch (GeoEntryQueryException e) {
        LOGGER.debug("Error querying GeoNames resource with wkt:{}", wkt, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}
Also used : GeoCoder(org.codice.ddf.spatial.geocoder.GeoCoder) JSONObject(net.minidev.json.JSONObject) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

NearbyLocation (org.codice.ddf.spatial.geocoding.context.NearbyLocation)12 Test (org.junit.Test)10 Matchers.anyString (org.mockito.Matchers.anyString)4 GeoEntryQueryException (org.codice.ddf.spatial.geocoding.GeoEntryQueryException)2 Point (org.locationtech.spatial4j.shape.Point)2 PointImpl (org.locationtech.spatial4j.shape.impl.PointImpl)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 JSONObject (net.minidev.json.JSONObject)1 IndexReader (org.apache.lucene.index.IndexReader)1 CustomScoreQuery (org.apache.lucene.queries.CustomScoreQuery)1 FunctionQuery (org.apache.lucene.queries.function.FunctionQuery)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 BoostQuery (org.apache.lucene.search.BoostQuery)1 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 Query (org.apache.lucene.search.Query)1