Search in sources :

Example 11 with GeoEntry

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

the class TestGeoNamesCreator method testWrongFieldOrder.

@Test(expected = NumberFormatException.class)
public void testWrongFieldOrder() {
    /* This string has the correct number of fields but the fields are in the wrong order. The
           GeoNamesCreator will attempt to parse the latitude and longitude from the string, but
           because there are non-double values where the latitude and longitude should be, a
           NumberFormatException should be thrown. */
    final String wrongFormat = "5289282\t33.30616\t-111.84125\t" + "Candler,Candleris,Chandler,Chandlur\tChandler\tChandler\tP\tPPL\tUS\tUS\tAZ\t" + "013\t012\t011\t236123\t370\t368\tAmerica/Phoenix\t2011-05-14";
    final GeoEntry geoEntry = GEONAMES_CREATOR.createGeoEntry(wrongFormat);
}
Also used : GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) Test(org.junit.Test)

Example 12 with GeoEntry

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

the class TestGeoNamesLocalIndex method testWithResults.

@Test
public void testWithResults() throws GeoEntryQueryException {
    final List<GeoEntry> topResults = Arrays.asList(GEO_ENTRY_1, GEO_ENTRY_2);
    doReturn(topResults).when(geoEntryQueryable).query("Phoenix", 1);
    final GeoResult geoResult = geoNamesLocalIndex.getLocation("Phoenix");
    assertThat(geoResult.getFullName(), is(equalTo(GEO_ENTRY_1.getName())));
    final Point point = new PointImpl(new DirectPositionImpl(GEO_ENTRY_1.getLongitude(), GEO_ENTRY_1.getLatitude()));
    assertThat(geoResult.getPoint(), is(equalTo(point)));
}
Also used : DirectPositionImpl(org.geotools.geometry.jts.spatialschema.geometry.DirectPositionImpl) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoResult(org.codice.ddf.spatial.geocoder.GeoResult) Point(org.opengis.geometry.primitive.Point) PointImpl(org.geotools.geometry.jts.spatialschema.geometry.primitive.PointImpl) Test(org.junit.Test)

Example 13 with GeoEntry

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

the class TestGeoNamesUpdateCommand method testProgressOutput.

@Test
public void testProgressOutput() throws IOException {
    final GeoEntryExtractor geoEntryExtractor = spy(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) {
            extractionCallback.updateProgress(50);
            assertThat(consoleInterceptor.getOutput(), containsString("50%"));
            extractionCallback.updateProgress(100);
            assertThat(consoleInterceptor.getOutput(), containsString("100%"));
        }

        @Override
        public void setUrl(String url) {
            return;
        }
    });
    final GeoEntryExtractor geoEntryUrlExtractor = spy(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) {
            extractionCallback.updateProgress(50);
            assertThat(consoleInterceptor.getOutput(), containsString("50%"));
            extractionCallback.updateProgress(100);
            assertThat(consoleInterceptor.getOutput(), containsString("100%"));
        }

        @Override
        public void setUrl(String url) {
            return;
        }
    });
    final GeoEntryIndexer geoEntryIndexer = spy(new GeoEntryIndexer() {

        @Override
        public void updateIndex(final List<GeoEntry> newEntries, final boolean create, final ProgressCallback progressCallback) {
        }

        @Override
        public void updateIndex(final String resource, final GeoEntryExtractor geoEntryExtractor, final boolean create, final ProgressCallback progressCallback) throws GeoNamesRemoteDownloadException, GeoEntryIndexingException, GeoEntryExtractionException {
            final ExtractionCallback extractionCallback = new ExtractionCallback() {

                @Override
                public void extracted(final GeoEntry newEntry) {
                }

                @Override
                public void updateProgress(final int progress) {
                    progressCallback.updateProgress(progress);
                }
            };
            geoEntryExtractor.pushGeoEntriesToExtractionCallback(resource, extractionCallback);
        }
    });
    List<GeoEntryExtractor> geoEntryExtractors = new ArrayList<GeoEntryExtractor>();
    geoEntryExtractors.add(geoEntryExtractor);
    geoEntryExtractors.add(geoEntryUrlExtractor);
    geoNamesUpdateCommand.setGeoEntryExtractor(geoEntryExtractor);
    geoNamesUpdateCommand.setGeoEntryIndexer(geoEntryIndexer);
    geoNamesUpdateCommand.setResource("test");
    geoNamesUpdateCommand.execute();
    consoleInterceptor.resetSystemOut();
    consoleInterceptor.closeBuffer();
}
Also used : GeoNamesRemoteDownloadException(org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoEntryExtractor(org.codice.ddf.spatial.geocoding.GeoEntryExtractor) GeoEntryIndexer(org.codice.ddf.spatial.geocoding.GeoEntryIndexer) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) ArrayList(java.util.ArrayList) List(java.util.List) ExtractionCallback(org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException) Test(org.junit.Test)

Example 14 with GeoEntry

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

the class GeoNamesLuceneIndexer method updateIndex.

@Override
public void updateIndex(final String resource, final GeoEntryExtractor geoEntryExtractor, final boolean create, final ProgressCallback progressCallback) throws GeoEntryIndexingException, GeoEntryExtractionException, GeoNamesRemoteDownloadException {
    Directory directory;
    try {
        directory = FSDirectory.open(Paths.get(indexLocation));
    } catch (IOException e) {
        throw new GeoEntryIndexingException("Couldn't open the directory for the index, " + indexLocation, e);
    }
    // Try-with-resources to ensure the IndexWriter always gets closed.
    try (final IndexWriter indexWriter = createIndexWriter(create, directory)) {
        final SpatialPrefixTree grid = new GeohashPrefixTree(SPATIAL_CONTEXT, GeoNamesLuceneConstants.GEOHASH_LEVELS);
        final SpatialStrategy strategy = new RecursivePrefixTreeStrategy(grid, GeoNamesLuceneConstants.GEO_FIELD);
        final ExtractionCallback extractionCallback = new ExtractionCallback() {

            @Override
            public void extracted(final GeoEntry newEntry) throws GeoEntryIndexingException {
                try {
                    addDocument(indexWriter, newEntry, strategy);
                } catch (IOException e) {
                    throw new GeoEntryIndexingException("Error writing to the index.", e);
                }
            }

            @Override
            public void updateProgress(final int progress) {
                if (progressCallback != null) {
                    progressCallback.updateProgress(progress);
                }
            }
        };
        try {
            geoEntryExtractor.pushGeoEntriesToExtractionCallback(resource, extractionCallback);
        } catch (GeoEntryExtractionException e) {
            // Need to roll back here before the IndexWriter is closed at the end of the try
            // block.
            indexWriter.rollback();
            throw e;
        }
    } catch (IOException e) {
        throw new GeoEntryIndexingException("Error writing to the index.", e);
    }
}
Also used : GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) IndexWriter(org.apache.lucene.index.IndexWriter) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) SpatialPrefixTree(org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree) IOException(java.io.IOException) ExtractionCallback(org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) GeohashPrefixTree(org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)

Example 15 with GeoEntry

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

the class TestGeoNamesQueryLuceneIndex method testQueryWithExactlyMaxResults.

@Test
public void testQueryWithExactlyMaxResults() throws IOException, ParseException, GeoEntryQueryException {
    final int requestedMaxResults = 2;
    final String queryString = "phoenix";
    final List<GeoEntry> results = directoryIndex.query(queryString, requestedMaxResults);
    assertThat(results.size(), is(requestedMaxResults));
    final GeoEntry firstResult = results.get(0);
    // We don't store the alternate names, so we don't get them back with the query results.
    // The entry with the name "phoenix" will come first because the name matches the query
    // exactly.
    verifyGeoEntry(firstResult, NAME_1, LAT_1, LON_1, FEATURE_CODE_1, POP_1, null, COUNTRY_CODE1);
    final GeoEntry secondResult = results.get(1);
    // We don't store the alternate names, so we don't get them back with the query results.
    verifyGeoEntry(secondResult, NAME_2, LAT_2, LON_2, FEATURE_CODE_2, POP_2, null, COUNTRY_CODE2);
}
Also used : GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) Matchers.anyString(org.mockito.Matchers.anyString) 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