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));
}
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();
}
}
}
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");
}
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());
}
}
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");
}
Aggregations