Search in sources :

Example 1 with FeatureIndexingException

use of org.codice.ddf.spatial.geocoding.FeatureIndexingException 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 FeatureIndexingException

use of org.codice.ddf.spatial.geocoding.FeatureIndexingException 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 FeatureIndexingException

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

the class CatalogFeatureIndexer method findMetacardForFeature.

private Metacard findMetacardForFeature(SimpleFeature feature) throws FeatureIndexingException {
    Object nameObject = feature.getAttribute(NAME_KEY);
    if (nameObject instanceof String) {
        String name = (String) nameObject;
        QueryRequest queryRequest = new QueryRequestImpl(catalogHelper.getQueryForName(name));
        try {
            SourceResponse response = catalogFramework.query(queryRequest);
            if (response.getResults().isEmpty()) {
                return null;
            }
            return response.getResults().get(0).getMetacard();
        } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
            throw new FeatureIndexingException(e.getMessage());
        }
    }
    throw new FeatureIndexingException("Unable to find feature");
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) FeatureIndexingException(org.codice.ddf.spatial.geocoding.FeatureIndexingException) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) FederationException(ddf.catalog.federation.FederationException)

Example 4 with FeatureIndexingException

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

the class CatalogFeatureIndexer method createOrUpdateMetacardForFeature.

private void createOrUpdateMetacardForFeature(SimpleFeature feature, boolean create) throws FeatureIndexingException {
    try {
        security.runWithSubjectOrElevate(() -> {
            Metacard metacard = null;
            if (!create) {
                metacard = findMetacardForFeature(feature);
            }
            if (metacard == null) {
                metacard = createMetacardForFeature(feature);
                catalogFramework.create(new CreateRequestImpl(metacard));
            } else {
                catalogFramework.update(new UpdateRequestImpl(metacard.getId(), metacard));
            }
            return null;
        });
    } catch (SecurityServiceException | InvocationTargetException e) {
        throw new FeatureIndexingException(e.getMessage());
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) SecurityServiceException(ddf.security.service.SecurityServiceException) FeatureIndexingException(org.codice.ddf.spatial.geocoding.FeatureIndexingException) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with FeatureIndexingException

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

the class CatalogFeatureIndexer method createMetacardForFeature.

private Metacard createMetacardForFeature(SimpleFeature feature) throws FeatureIndexingException {
    Metacard metacard = new MetacardImpl(metacardTypes.stream().findFirst().orElseThrow(() -> new FeatureIndexingException(GEO_ENTRY_METACARD_TYPE_MISSING_ERROR_MSG)));
    String countryCode = feature.getID();
    Object nameObject = feature.getAttribute(NAME_KEY);
    if (nameObject instanceof String) {
        metacard.setAttribute(new AttributeImpl(Core.TITLE, (String) nameObject));
        metacard.setAttribute(new AttributeImpl(Location.COUNTRY_CODE, countryCode));
        String wkt = WKT_WRITER_THREAD_LOCAL.get().write((Geometry) feature.getDefaultGeometry());
        metacard.setAttribute(new AttributeImpl(Core.LOCATION, wkt));
        List<Serializable> tags = Arrays.asList(GAZETTEER_METACARD_TAG, GeoCodingConstants.COUNTRY_TAG);
        metacard.setAttribute(new AttributeImpl(Core.METACARD_TAGS, tags));
        metacard.setAttribute(new AttributeImpl(GeoEntryAttributes.GAZETTEER_SORT_VALUE, GeoCodingConstants.COUNTRY_GAZETTEER_SORT_VALUE));
        if (isCountry(feature)) {
            metacard.setAttribute(new AttributeImpl(GeoEntryAttributes.FEATURE_CODE_ATTRIBUTE_NAME, GeoCodingConstants.POLITICAL_ENTITY));
        }
        return metacard;
    }
    throw new FeatureIndexingException("Malformed feature");
}
Also used : Metacard(ddf.catalog.data.Metacard) Serializable(java.io.Serializable) FeatureIndexingException(org.codice.ddf.spatial.geocoding.FeatureIndexingException) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Aggregations

FeatureIndexingException (org.codice.ddf.spatial.geocoding.FeatureIndexingException)6 FeatureExtractionException (org.codice.ddf.spatial.geocoding.FeatureExtractionException)3 Metacard (ddf.catalog.data.Metacard)2 FeatureExtractor (org.codice.ddf.spatial.geocoding.FeatureExtractor)2 FeatureIndexer (org.codice.ddf.spatial.geocoding.FeatureIndexer)2 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)1 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)1 FederationException (ddf.catalog.federation.FederationException)1 QueryRequest (ddf.catalog.operation.QueryRequest)1 SourceResponse (ddf.catalog.operation.SourceResponse)1 CreateRequestImpl (ddf.catalog.operation.impl.CreateRequestImpl)1 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)1 UpdateRequestImpl (ddf.catalog.operation.impl.UpdateRequestImpl)1 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)1 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)1 SecurityServiceException (ddf.security.service.SecurityServiceException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1