use of org.locationtech.geowave.core.store.CloseableIterator.Wrapper in project geowave by locationtech.
the class GeoToolsRasterDataStoreIngestPlugin method toGeoWaveData.
@Override
public CloseableIterator<GeoWaveData<GridCoverage>> toGeoWaveData(final URL input, final String[] indexNames) {
final AbstractGridFormat format = prioritizedFindFormat(input);
if (format == null) {
return new Wrapper<>(Collections.emptyIterator());
}
Hints hints = null;
if ((optionProvider.getCrs() != null) && !optionProvider.getCrs().trim().isEmpty()) {
try {
final CoordinateReferenceSystem crs = CRS.decode(optionProvider.getCrs());
if (crs != null) {
hints = new Hints();
hints.put(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, crs);
}
} catch (final Exception e) {
LOGGER.warn("Unable to find coordinate reference system, continuing without hint", e);
}
}
final GridCoverage2DReader reader = format.getReader(input, hints);
if (reader == null) {
LOGGER.error("Unable to get reader instance, getReader returned null");
return new Wrapper<>(Collections.emptyIterator());
}
try {
final GridCoverage2D coverage = reader.read(null);
if (coverage != null) {
final Map<String, String> metadata = new HashMap<>();
final String coverageName = coverage.getName().toString();
try {
// wrapping with try-catch block because often the reader
// does not support operations on coverage name
// if not, we just don't have metadata, and continue
final String[] mdNames = reader.getMetadataNames(coverageName);
if ((mdNames != null) && (mdNames.length > 0)) {
for (final String mdName : mdNames) {
if (mdName != null) {
final String value = reader.getMetadataValue(coverageName, mdName);
if (value != null) {
metadata.put(mdName, value);
}
}
}
}
} catch (final Exception e) {
LOGGER.debug("Unable to find metadata from coverage reader", e);
}
final List<GeoWaveData<GridCoverage>> coverages = new ArrayList<>();
if (optionProvider.isSeparateBands() && (coverage.getNumSampleDimensions() > 1)) {
final String baseName = optionProvider.getCoverageName() != null ? optionProvider.getCoverageName() : FilenameUtils.getName(input.getPath());
final double[][] nodata = optionProvider.getNodata(coverage.getNumSampleDimensions());
for (int b = 0; b < coverage.getNumSampleDimensions(); b++) {
final RasterDataAdapter adapter = new RasterDataAdapter(baseName + "_B" + b, metadata, (GridCoverage2D) RasterUtils.getCoverageOperations().selectSampleDimension(coverage, new int[] { b }), optionProvider.getTileSize(), optionProvider.isBuildPyramid(), optionProvider.isBuildHistogram(), new double[][] { nodata[b] });
coverages.add(new GeoWaveData<>(adapter, indexNames, coverage));
}
} else {
final RasterDataAdapter adapter = new RasterDataAdapter(optionProvider.getCoverageName() != null ? optionProvider.getCoverageName() : input.getPath(), metadata, coverage, optionProvider.getTileSize(), optionProvider.isBuildPyramid(), optionProvider.isBuildHistogram(), optionProvider.getNodata(coverage.getNumSampleDimensions()));
coverages.add(new GeoWaveData<>(adapter, indexNames, coverage));
}
return new Wrapper<GeoWaveData<GridCoverage>>(coverages.iterator()) {
@Override
public void close() {
try {
reader.dispose();
} catch (final IOException e) {
LOGGER.warn("unable to dispose of reader resources", e);
}
}
};
} else {
LOGGER.warn("Null grid coverage from file '" + input.getPath() + "' for discovered geotools format '" + format.getName() + "'");
}
} catch (final IOException e) {
LOGGER.warn("Unable to read grid coverage of file '" + input.getPath() + "' for discovered geotools format '" + format.getName() + "'", e);
}
return new Wrapper<>(Collections.emptyIterator());
}
use of org.locationtech.geowave.core.store.CloseableIterator.Wrapper in project geowave by locationtech.
the class GeoWaveAvroIngestPlugin method toGeoWaveDataInternal.
@Override
protected CloseableIterator<GeoWaveData<SimpleFeature>> toGeoWaveDataInternal(final AvroSimpleFeatureCollection featureCollection, final String[] indexNames) {
final AvroFeatureDefinition featureDefinition = featureCollection.getFeatureType();
final List<GeoWaveData<SimpleFeature>> retVal = new ArrayList<>();
SimpleFeatureType featureType;
try {
featureType = GeoWaveAvroFeatureUtils.avroFeatureDefinitionToGTSimpleFeatureType(featureDefinition);
final FeatureDataAdapter adapter = new FeatureDataAdapter(featureType);
final List<String> attributeTypes = featureDefinition.getAttributeTypes();
for (final AvroAttributeValues attributeValues : featureCollection.getSimpleFeatureCollection()) {
try {
final SimpleFeature simpleFeature = GeoWaveAvroFeatureUtils.avroSimpleFeatureToGTSimpleFeature(featureType, attributeTypes, attributeValues);
retVal.add(new GeoWaveData<>(adapter, indexNames, simpleFeature));
} catch (final Exception e) {
LOGGER.warn("Unable to read simple feature from Avro", e);
}
}
} catch (final ClassNotFoundException e) {
LOGGER.warn("Unable to read simple feature type from Avro", e);
}
return new Wrapper<>(retVal.iterator());
}
Aggregations