Search in sources :

Example 1 with GeoEntry

use of org.codice.ddf.spatial.geocoding.GeoEntry in project ddf by codice.

the class TestGeoNamesCreator method testNoEmptyFields.

@Test
public void testNoEmptyFields() {
    final String geoNamesEntryStr = "5289282\tChandler\tChandler\t" + "Candler,Candleris,Chandler,Chandlur\t33.30616\t-111.84125\tP\tPPL\tUS\tUS\tAZ\t" + "013\t012\t011\t236123\t370\t368\tAmerica/Phoenix\t2011-05-14";
    final GeoEntry geoEntry = GEONAMES_CREATOR.createGeoEntry(geoNamesEntryStr);
    verifyGeoEntry(geoEntry, "Chandler", 33.30616, -111.84125, "PPL", 236123, "Candler,Candleris,Chandler,Chandlur", "US");
}
Also used : GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) Test(org.junit.Test)

Example 2 with GeoEntry

use of org.codice.ddf.spatial.geocoding.GeoEntry in project ddf by codice.

the class TestGeoNamesCreator method testNotTabDelimited.

@Test(expected = ArrayIndexOutOfBoundsException.class)
public void testNotTabDelimited() {
    final String wrongFormat = "5289282,Chandler,Chandler," + "Candler,Candleris,Chandler,Chandlu,33.30616,-111.84125,P,PPL,US,US,AZ," + "013,012,011,236123,370,368,America/Phoenix,2011-05-14";
    final GeoEntry geoEntry = GEONAMES_CREATOR.createGeoEntry(wrongFormat);
}
Also used : GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) Test(org.junit.Test)

Example 3 with GeoEntry

use of org.codice.ddf.spatial.geocoding.GeoEntry in project ddf by codice.

the class TestGeoNamesCreator method testNotEnoughFields.

@Test(expected = ArrayIndexOutOfBoundsException.class)
public void testNotEnoughFields() {
    final String wrongFormat = "5288858\tCave Creek\tCave Creek\tAlternate names\t33.83333";
    final GeoEntry geoEntry = GEONAMES_CREATOR.createGeoEntry(wrongFormat);
}
Also used : GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) Test(org.junit.Test)

Example 4 with GeoEntry

use of org.codice.ddf.spatial.geocoding.GeoEntry in project ddf by codice.

the class GeoNamesQueryLuceneIndex method doQuery.

protected List<GeoEntry> doQuery(final String queryString, final int maxResults, final Directory directory) throws GeoEntryQueryException {
    if (StringUtils.isBlank(queryString)) {
        throw new IllegalArgumentException("The query string cannot be null or empty.");
    }
    if (maxResults < 1) {
        throw new IllegalArgumentException("maxResults must be positive.");
    }
    if (directory == null) {
        return Collections.emptyList();
    }
    try (final IndexReader indexReader = createIndexReader(directory)) {
        final IndexSearcher indexSearcher = createIndexSearcher(indexReader);
        final Query query = createQuery(queryString);
        final TopDocs topDocs = indexSearcher.search(query, maxResults);
        if (topDocs.totalHits > 0) {
            final List<GeoEntry> results = new ArrayList<>();
            for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
                final Document document = indexSearcher.doc(scoreDoc.doc);
                // The alternate names aren't being stored (they are only used for queries),
                // so we don't retrieve them here.
                results.add(new GeoEntry.Builder().name(document.get(GeoNamesLuceneConstants.NAME_FIELD)).latitude(Double.parseDouble(document.get(GeoNamesLuceneConstants.LATITUDE_FIELD))).longitude(Double.parseDouble(document.get(GeoNamesLuceneConstants.LONGITUDE_FIELD))).featureCode(document.get(GeoNamesLuceneConstants.FEATURE_CODE_FIELD)).population(Long.parseLong(document.get(GeoNamesLuceneConstants.POPULATION_FIELD))).countryCode(document.get(GeoNamesLuceneConstants.COUNTRY_CODE_FIELD)).build());
            }
            return results;
        } else {
            return Collections.emptyList();
        }
    } catch (IOException e) {
        throw new GeoEntryQueryException("Error reading the index", e);
    } catch (ParseException e) {
        throw new GeoEntryQueryException("Error parsing query", e);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) IndexReader(org.apache.lucene.index.IndexReader) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 5 with GeoEntry

use of org.codice.ddf.spatial.geocoding.GeoEntry in project ddf by codice.

the class TestGeoNamesLuceneIndexerExceptions method testExceptionWhenAddingDocumentByExtractor.

@Test
public void testExceptionWhenAddingDocumentByExtractor() throws GeoEntryIndexingException, GeoEntryExtractionException, GeoNamesRemoteDownloadException, IOException {
    configureMocks();
    geoNamesLuceneIndexer.setIndexLocation(ABSOLUTE_PATH + TEST_PATH + "index");
    try {
        final GeoEntryExtractor geoEntryExtractor = new GeoEntryExtractor() {

            @Override
            public List<GeoEntry> getGeoEntries(final String resource, final ProgressCallback progressCallback) {
                return null;
            }

            @Override
            public void pushGeoEntriesToExtractionCallback(final String resource, final ExtractionCallback extractionCallback) throws GeoEntryExtractionException {
                extractionCallback.updateProgress(0);
                try {
                    extractionCallback.extracted(GEO_ENTRY);
                } catch (GeoEntryIndexingException e) {
                    throw new GeoEntryExtractionException("Unable to add entry.", e);
                }
            }

            @Override
            public void setUrl(String url) {
                return;
            }
        };
        geoNamesLuceneIndexer.updateIndex("", geoEntryExtractor, true, null);
        fail("Should have thrown a GeoEntryExtractionException; extractionCallback.extract() threw " + "a GeoEntryIndexingException because addDocument() threw an " + "IOException.");
    } catch (GeoEntryExtractionException e) {
        assertThat(e.getCause(), instanceOf(GeoEntryIndexingException.class));
    }
}
Also used : GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) GeoEntryExtractor(org.codice.ddf.spatial.geocoding.GeoEntryExtractor) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException) Test(org.junit.Test)

Aggregations

GeoEntry (org.codice.ddf.spatial.geocoding.GeoEntry)17 Test (org.junit.Test)12 Matchers.anyString (org.mockito.Matchers.anyString)4 GeoEntryExtractionException (org.codice.ddf.spatial.geocoding.GeoEntryExtractionException)3 ExtractionCallback (org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback)3 GeoEntryIndexingException (org.codice.ddf.spatial.geocoding.GeoEntryIndexingException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 SpatialStrategy (org.apache.lucene.spatial.SpatialStrategy)2 RecursivePrefixTreeStrategy (org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy)2 GeohashPrefixTree (org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)2 SpatialPrefixTree (org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree)2 GeoResult (org.codice.ddf.spatial.geocoder.GeoResult)2 GeoEntryExtractor (org.codice.ddf.spatial.geocoding.GeoEntryExtractor)2 GeoEntryQueryException (org.codice.ddf.spatial.geocoding.GeoEntryQueryException)2 ProgressCallback (org.codice.ddf.spatial.geocoding.ProgressCallback)2 List (java.util.List)1 Document (org.apache.lucene.document.Document)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1