Search in sources :

Example 1 with Wrapper

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());
}
Also used : Wrapper(org.locationtech.geowave.core.store.CloseableIterator.Wrapper) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) Hints(org.geotools.util.factory.Hints) HashMap(java.util.HashMap) GridCoverage2DReader(org.geotools.coverage.grid.io.GridCoverage2DReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) RasterDataAdapter(org.locationtech.geowave.adapter.raster.adapter.RasterDataAdapter) GridCoverage(org.opengis.coverage.grid.GridCoverage) GeoWaveData(org.locationtech.geowave.core.store.ingest.GeoWaveData) AbstractGridFormat(org.geotools.coverage.grid.io.AbstractGridFormat) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 2 with Wrapper

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());
}
Also used : Wrapper(org.locationtech.geowave.core.store.CloseableIterator.Wrapper) AvroAttributeValues(org.locationtech.geowave.adapter.vector.avro.AvroAttributeValues) ArrayList(java.util.ArrayList) SimpleFeature(org.opengis.feature.simple.SimpleFeature) IOException(java.io.IOException) AvroFeatureDefinition(org.locationtech.geowave.adapter.vector.avro.AvroFeatureDefinition) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) GeoWaveData(org.locationtech.geowave.core.store.ingest.GeoWaveData) FeatureDataAdapter(org.locationtech.geowave.adapter.vector.FeatureDataAdapter)

Aggregations

IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Wrapper (org.locationtech.geowave.core.store.CloseableIterator.Wrapper)2 GeoWaveData (org.locationtech.geowave.core.store.ingest.GeoWaveData)2 HashMap (java.util.HashMap)1 GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)1 AbstractGridFormat (org.geotools.coverage.grid.io.AbstractGridFormat)1 GridCoverage2DReader (org.geotools.coverage.grid.io.GridCoverage2DReader)1 Hints (org.geotools.util.factory.Hints)1 RasterDataAdapter (org.locationtech.geowave.adapter.raster.adapter.RasterDataAdapter)1 FeatureDataAdapter (org.locationtech.geowave.adapter.vector.FeatureDataAdapter)1 AvroAttributeValues (org.locationtech.geowave.adapter.vector.avro.AvroAttributeValues)1 AvroFeatureDefinition (org.locationtech.geowave.adapter.vector.avro.AvroFeatureDefinition)1 GridCoverage (org.opengis.coverage.grid.GridCoverage)1 SimpleFeature (org.opengis.feature.simple.SimpleFeature)1 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1