Search in sources :

Example 41 with Shape

use of org.locationtech.spatial4j.shape.Shape in project ddf by codice.

the class TestGeoNamesQueryLuceneIndex method testDoGetNearestCitiesIOExceptionBranch.

@Test(expected = GeoEntryQueryException.class)
public void testDoGetNearestCitiesIOExceptionBranch() throws IOException, GeoEntryQueryException {
    doThrow(IOException.class).when(directoryIndex).createIndexReader(directory);
    Shape shape = mock(Shape.class);
    directoryIndex.doGetNearestCities(shape, 10, 10, directory);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) Test(org.junit.Test)

Example 42 with Shape

use of org.locationtech.spatial4j.shape.Shape in project ddf by codice.

the class TestGeoNamesQueryLuceneIndex method testDoGetNearestCitiesNullDirectory.

@Test
public void testDoGetNearestCitiesNullDirectory() throws IOException, GeoEntryQueryException {
    Shape shape = mock(Shape.class);
    List<NearbyLocation> nearestCities = directoryIndex.doGetNearestCities(shape, 10, 10, null);
    assertThat(nearestCities, is(Collections.emptyList()));
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) Test(org.junit.Test)

Example 43 with Shape

use of org.locationtech.spatial4j.shape.Shape in project ddf by codice.

the class TestGeoNamesQueryLuceneIndex method createDocumentFromGeoEntry.

private Document createDocumentFromGeoEntry(final GeoEntry geoEntry) {
    final Document document = new Document();
    document.add(new TextField(GeoNamesLuceneConstants.NAME_FIELD, geoEntry.getName(), Field.Store.YES));
    document.add(new StoredField(GeoNamesLuceneConstants.LATITUDE_FIELD, geoEntry.getLatitude()));
    document.add(new StoredField(GeoNamesLuceneConstants.LONGITUDE_FIELD, geoEntry.getLongitude()));
    document.add(new StringField(GeoNamesLuceneConstants.FEATURE_CODE_FIELD, geoEntry.getFeatureCode(), Field.Store.YES));
    document.add(new StoredField(GeoNamesLuceneConstants.POPULATION_FIELD, geoEntry.getPopulation()));
    document.add(new NumericDocValuesField(GeoNamesLuceneConstants.POPULATION_DOCVALUES_FIELD, geoEntry.getPopulation()));
    document.add(new StringField(GeoNamesLuceneConstants.COUNTRY_CODE_FIELD, geoEntry.getCountryCode(), Field.Store.YES));
    document.add(new TextField(GeoNamesLuceneConstants.ALTERNATE_NAMES_FIELD, geoEntry.getAlternateNames(), Field.Store.NO));
    final Shape point = SPATIAL_CONTEXT.getShapeFactory().pointXY(geoEntry.getLongitude(), geoEntry.getLatitude());
    for (IndexableField field : strategy.createIndexableFields(point)) {
        document.add(field);
    }
    return document;
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) StoredField(org.apache.lucene.document.StoredField) Shape(org.locationtech.spatial4j.shape.Shape) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) StringField(org.apache.lucene.document.StringField) TextField(org.apache.lucene.document.TextField) Document(org.apache.lucene.document.Document)

Example 44 with Shape

use of org.locationtech.spatial4j.shape.Shape in project ddf by codice.

the class GeoNamesLuceneIndexer method addDocument.

private void addDocument(final IndexWriter indexWriter, final GeoEntry geoEntry, final SpatialStrategy strategy) throws IOException {
    final Document document = new Document();
    document.add(new TextField(GeoNamesLuceneConstants.NAME_FIELD, geoEntry.getName(), Field.Store.YES));
    document.add(new StoredField(GeoNamesLuceneConstants.LATITUDE_FIELD, geoEntry.getLatitude()));
    document.add(new StoredField(GeoNamesLuceneConstants.LONGITUDE_FIELD, geoEntry.getLongitude()));
    document.add(new StringField(GeoNamesLuceneConstants.FEATURE_CODE_FIELD, geoEntry.getFeatureCode(), Field.Store.YES));
    document.add(new TextField(GeoNamesLuceneConstants.COUNTRY_CODE_FIELD, geoEntry.getCountryCode(), Field.Store.YES));
    document.add(new StoredField(GeoNamesLuceneConstants.POPULATION_FIELD, geoEntry.getPopulation()));
    // This DocValues field is used for sorting by population.
    document.add(new NumericDocValuesField(GeoNamesLuceneConstants.POPULATION_DOCVALUES_FIELD, geoEntry.getPopulation()));
    document.add(new TextField(GeoNamesLuceneConstants.ALTERNATE_NAMES_FIELD, geoEntry.getAlternateNames(), Field.Store.NO));
    // Add each entry's spatial information for fast spatial filtering.
    final Shape point = SPATIAL_CONTEXT.getShapeFactory().pointXY(geoEntry.getLongitude(), geoEntry.getLatitude());
    for (IndexableField field : strategy.createIndexableFields(point)) {
        document.add(field);
    }
    final float boost = calculateBoost(geoEntry);
    document.add(new FloatDocValuesField(GeoNamesLuceneConstants.BOOST_FIELD, boost));
    indexWriter.addDocument(document);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) StoredField(org.apache.lucene.document.StoredField) Shape(org.locationtech.spatial4j.shape.Shape) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) StringField(org.apache.lucene.document.StringField) TextField(org.apache.lucene.document.TextField) FloatDocValuesField(org.apache.lucene.document.FloatDocValuesField) Document(org.apache.lucene.document.Document)

Example 45 with Shape

use of org.locationtech.spatial4j.shape.Shape in project ddf by codice.

the class GeoNamesQueryLuceneDirectoryIndex method getNearestCities.

@Override
public List<NearbyLocation> getNearestCities(final String location, final int radiusInKm, final int maxResults) throws ParseException, GeoEntryQueryException {
    if (location == null) {
        throw new IllegalArgumentException("GeoNamesQueryLuceneDirectoryIndex.getNearestCities(): argument 'location' may not be null.");
    }
    Shape shape = getShape(location);
    final Directory directory = openDirectoryAndCheckForIndex();
    return doGetNearestCities(shape, radiusInKm, maxResults, directory);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory)

Aggregations

Shape (org.locationtech.spatial4j.shape.Shape)81 Test (org.junit.Test)18 Point (org.locationtech.spatial4j.shape.Point)16 ArrayList (java.util.ArrayList)14 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)13 CoordinatesBuilder (org.elasticsearch.common.geo.builders.CoordinatesBuilder)13 PolygonBuilder (org.elasticsearch.common.geo.builders.PolygonBuilder)11 Document (org.apache.lucene.document.Document)10 Field (org.apache.lucene.document.Field)9 Query (org.apache.lucene.search.Query)8 Rectangle (org.locationtech.spatial4j.shape.Rectangle)8 StoredField (org.apache.lucene.document.StoredField)7 SpatialOperation (org.apache.lucene.spatial.query.SpatialOperation)7 LineStringBuilder (org.elasticsearch.common.geo.builders.LineStringBuilder)7 UnitNRShape (org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape)6 StringField (org.apache.lucene.document.StringField)5 TextField (org.apache.lucene.document.TextField)4 IndexableField (org.apache.lucene.index.IndexableField)4 UnsupportedSpatialOperation (org.apache.lucene.spatial.query.UnsupportedSpatialOperation)4 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)4