Search in sources :

Example 1 with FeatureExtractionException

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

the class GazetteerUpdateCommandTest method testFeatureIndexing.

@Test
public void testFeatureIndexing() throws Exception {
    String resource = "example.geojson";
    final FeatureExtractor featureExtractor = spy(new FeatureExtractor() {

        @Override
        public void pushFeaturesToExtractionCallback(String resource, ExtractionCallback extractionCallback) throws FeatureExtractionException {
        /* stub */
        }
    });
    final FeatureIndexer featureIndexer = spy(new FeatureIndexer() {

        @Override
        public void updateIndex(String resource, FeatureExtractor featureExtractor, boolean create, IndexCallback callback) throws FeatureExtractionException, FeatureIndexingException {
        /* stub */
        }
    });
    gazetteerUpdateCommand.setResource(resource);
    gazetteerUpdateCommand.setFeatureExtractor(featureExtractor);
    gazetteerUpdateCommand.setFeatureIndexer(featureIndexer);
    gazetteerUpdateCommand.executeWithSubject();
    verify(featureIndexer, times(1)).updateIndex(eq(resource), eq(featureExtractor), eq(false), any(FeatureIndexer.IndexCallback.class));
}
Also used : FeatureExtractor(org.codice.ddf.spatial.geocoding.FeatureExtractor) FeatureIndexingException(org.codice.ddf.spatial.geocoding.FeatureIndexingException) FeatureExtractionException(org.codice.ddf.spatial.geocoding.FeatureExtractionException) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ExtractionCallback(org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback) FeatureIndexer(org.codice.ddf.spatial.geocoding.FeatureIndexer) Test(org.junit.Test)

Example 2 with FeatureExtractionException

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

the class GeoJSONFeatureExtractor method pushFeaturesToExtractionCallback.

@Override
public void pushFeaturesToExtractionCallback(String resource, ExtractionCallback extractionCallback) throws FeatureExtractionException {
    Validate.notNull(extractionCallback, "extractionCallback can't be null");
    Validate.notNull(resource, "resource can't be null");
    File file = new File(resource);
    if (file.length() == 0) {
        throw new FeatureExtractionException("Empty resource");
    }
    FeatureIterator<SimpleFeature> iterator = null;
    try (Reader reader = new InputStreamReader(new FileInputStream(file), "UTF-8")) {
        iterator = getFeatureIterator(reader);
        while (iterator != null && iterator.hasNext()) {
            SimpleFeature feature = iterator.next();
            Object featureGeometry = feature.getDefaultGeometry();
            if (featureGeometry instanceof Geometry) {
                Geometry geometry = (Geometry) feature.getDefaultGeometry();
                if (geometry == null) {
                    LOGGER.debug("Failed to get geometry from {}", resource);
                    continue;
                }
                Geometry simplifiedGeometry = getSimplifiedGeometry(geometry);
                if (simplifiedGeometry == null) {
                    LOGGER.debug("Failed to simplify geometry below {} point maximum", MAX_POINTS_PER_FEATURE);
                } else {
                    feature.setDefaultGeometry(simplifiedGeometry);
                    extractionCallback.extracted(feature);
                }
            }
        }
    } catch (IOException | FeatureIndexingException e) {
        throw new FeatureExtractionException("Unable to extract feature from " + resource, e);
    } finally {
        if (iterator != null) {
            iterator.close();
        }
    }
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) InputStreamReader(java.io.InputStreamReader) FeatureIndexingException(org.codice.ddf.spatial.geocoding.FeatureIndexingException) FeatureExtractionException(org.codice.ddf.spatial.geocoding.FeatureExtractionException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) File(java.io.File) SimpleFeature(org.opengis.feature.simple.SimpleFeature) FileInputStream(java.io.FileInputStream)

Example 3 with FeatureExtractionException

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

the class GazetteerUpdateCommand method executeWithSubject.

@Override
protected Object executeWithSubject() throws Exception {
    final PrintStream console = System.out;
    final ProgressCallback progressCallback = progress -> {
        console.printf("\r%d%%", progress);
        console.flush();
    };
    final FeatureIndexer.IndexCallback featureIndexCallback = count -> {
        console.printf("\r%d features indexed", count);
        console.flush();
    };
    console.println("Updating...");
    try {
        if (isResourceGeoJSON()) {
            featureIndexer.updateIndex(resource, featureExtractor, create, featureIndexCallback);
        } else {
            geoEntryIndexer.updateIndex(resource, geoEntryExtractor, create, progressCallback);
        }
        console.println("\nDone.");
    } catch (GeoEntryExtractionException | FeatureExtractionException e) {
        LOGGER.info("Error extracting data from resource {}", resource, e);
        console.printf("Could not extract data from resource %s.%n Message: %s%n Check the logs for more details.%n", resource, e.getMessage());
    } catch (GeoEntryIndexingException | FeatureIndexingException e) {
        LOGGER.info("Error indexing data", e);
        console.printf("Could not index the  data.%n Message: %s%n Check the logs for more details.%n", e.getMessage());
    } catch (GeoNamesRemoteDownloadException e) {
        LOGGER.info("Error downloading resource from remote source {}", resource, e);
        console.printf("Could not download the GeoNames file %s.%n  Message: %s%n Check the logs for more details.%n", resource, e.getMessage());
    }
    return null;
}
Also used : PrintStream(java.io.PrintStream) GeoEntryExtractor(org.codice.ddf.spatial.geocoding.GeoEntryExtractor) Logger(org.slf4j.Logger) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) GeoEntryIndexer(org.codice.ddf.spatial.geocoding.GeoEntryIndexer) FeatureIndexer(org.codice.ddf.spatial.geocoding.FeatureIndexer) LoggerFactory(org.slf4j.LoggerFactory) Argument(org.apache.karaf.shell.api.action.Argument) FeatureExtractor(org.codice.ddf.spatial.geocoding.FeatureExtractor) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) Command(org.apache.karaf.shell.api.action.Command) Reference(org.apache.karaf.shell.api.action.lifecycle.Reference) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException) GeoNamesRemoteDownloadException(org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException) Locale(java.util.Locale) FeatureIndexingException(org.codice.ddf.spatial.geocoding.FeatureIndexingException) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Option(org.apache.karaf.shell.api.action.Option) FeatureExtractionException(org.codice.ddf.spatial.geocoding.FeatureExtractionException) SubjectCommands(org.codice.ddf.commands.catalog.SubjectCommands) GeoNamesRemoteDownloadException(org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException) PrintStream(java.io.PrintStream) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) FeatureIndexingException(org.codice.ddf.spatial.geocoding.FeatureIndexingException) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) FeatureExtractionException(org.codice.ddf.spatial.geocoding.FeatureExtractionException) FeatureIndexer(org.codice.ddf.spatial.geocoding.FeatureIndexer) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException)

Aggregations

FeatureExtractionException (org.codice.ddf.spatial.geocoding.FeatureExtractionException)3 FeatureIndexingException (org.codice.ddf.spatial.geocoding.FeatureIndexingException)3 FeatureExtractor (org.codice.ddf.spatial.geocoding.FeatureExtractor)2 FeatureIndexer (org.codice.ddf.spatial.geocoding.FeatureIndexer)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PrintStream (java.io.PrintStream)1 Reader (java.io.Reader)1 Locale (java.util.Locale)1 Argument (org.apache.karaf.shell.api.action.Argument)1 Command (org.apache.karaf.shell.api.action.Command)1 Option (org.apache.karaf.shell.api.action.Option)1 Reference (org.apache.karaf.shell.api.action.lifecycle.Reference)1 Service (org.apache.karaf.shell.api.action.lifecycle.Service)1 SubjectCommands (org.codice.ddf.commands.catalog.SubjectCommands)1 GeoEntryExtractionException (org.codice.ddf.spatial.geocoding.GeoEntryExtractionException)1 GeoEntryExtractor (org.codice.ddf.spatial.geocoding.GeoEntryExtractor)1 ExtractionCallback (org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback)1