Search in sources :

Example 1 with IteratorWrapper

use of org.locationtech.geowave.core.store.util.IteratorWrapper in project geowave by locationtech.

the class RasterDataAdapter method convertToIndex.

@Override
public Iterator<GridCoverage> convertToIndex(final Index index, final GridCoverage gridCoverage) {
    final HierarchicalNumericIndexStrategy indexStrategy = CompoundHierarchicalIndexStrategyWrapper.findHierarchicalStrategy(index.getIndexStrategy());
    if (indexStrategy != null) {
        final CoordinateReferenceSystem sourceCrs = gridCoverage.getCoordinateReferenceSystem();
        final Envelope sampleEnvelope = gridCoverage.getEnvelope();
        final ReferencedEnvelope sampleReferencedEnvelope = new ReferencedEnvelope(new org.locationtech.jts.geom.Envelope(sampleEnvelope.getMinimum(0), sampleEnvelope.getMaximum(0), sampleEnvelope.getMinimum(1), sampleEnvelope.getMaximum(1)), gridCoverage.getCoordinateReferenceSystem());
        ReferencedEnvelope projectedReferenceEnvelope = sampleReferencedEnvelope;
        final CoordinateReferenceSystem indexCrs = GeometryUtils.getIndexCrs(index);
        if (!indexCrs.equals(sourceCrs)) {
            try {
                projectedReferenceEnvelope = sampleReferencedEnvelope.transform(indexCrs, true);
            } catch (TransformException | FactoryException e) {
                LOGGER.warn("Unable to transform envelope of grid coverage to Index CRS", e);
            }
        }
        final MultiDimensionalNumericData bounds;
        if (indexCrs.equals(GeometryUtils.getDefaultCRS())) {
            bounds = IndexUtils.clampAtIndexBounds(GeometryUtils.basicConstraintSetFromEnvelope(projectedReferenceEnvelope).getIndexConstraints(indexStrategy), indexStrategy);
        } else {
            bounds = IndexUtils.clampAtIndexBounds(GeometryUtils.getBoundsFromEnvelope(projectedReferenceEnvelope), indexStrategy);
        }
        final GridEnvelope gridEnvelope = gridCoverage.getGridGeometry().getGridRange();
        // only one set of constraints..hence reference '0' element
        final double[] tileRangePerDimension = new double[bounds.getDimensionCount()];
        final Double[] maxValuesPerDimension = bounds.getMaxValuesPerDimension();
        final Double[] minValuesPerDimension = bounds.getMinValuesPerDimension();
        for (int d = 0; d < tileRangePerDimension.length; d++) {
            tileRangePerDimension[d] = ((maxValuesPerDimension[d] - minValuesPerDimension[d]) * tileSize) / gridEnvelope.getSpan(d);
        }
        final TreeMap<Double, SubStrategy> substrategyMap = new TreeMap<>();
        for (final SubStrategy pyramidLevel : indexStrategy.getSubStrategies()) {
            final double[] idRangePerDimension = pyramidLevel.getIndexStrategy().getHighestPrecisionIdRangePerDimension();
            // to create a pyramid, ingest into each substrategy that is
            // lower resolution than the sample set in at least one
            // dimension and the one substrategy that is at least the same
            // resolution or higher resolution to retain the original
            // resolution as well as possible
            double maxSubstrategyResToSampleSetRes = -Double.MAX_VALUE;
            for (int d = 0; d < tileRangePerDimension.length; d++) {
                final double substrategyResToSampleSetRes = idRangePerDimension[d] / tileRangePerDimension[d];
                maxSubstrategyResToSampleSetRes = Math.max(maxSubstrategyResToSampleSetRes, substrategyResToSampleSetRes);
            }
            substrategyMap.put(maxSubstrategyResToSampleSetRes, pyramidLevel);
        }
        // all entries will be greater than 1 (lower resolution pyramid
        // levels)
        // also try to find the one entry that is closest to 1.0 without
        // going over (this will be the full resolution level)
        // add an epsilon to try to catch any roundoff error
        final double fullRes = 1.0 + MathUtils.EPSILON;
        final Entry<Double, SubStrategy> fullResEntry = substrategyMap.floorEntry(fullRes);
        final List<SubStrategy> pyramidLevels = new ArrayList<>();
        if (fullResEntry != null) {
            pyramidLevels.add(fullResEntry.getValue());
        }
        if (buildPyramid) {
            final NavigableMap<Double, SubStrategy> map = substrategyMap.tailMap(fullRes, false);
            pyramidLevels.addAll(map.values());
        }
        if (pyramidLevels.isEmpty()) {
            // this case shouldn't occur theoretically, but just in case,
            // make sure the substrategy closest to 1.0 is used
            final Entry<Double, SubStrategy> bestEntry = substrategyMap.higherEntry(1.0);
            pyramidLevels.add(bestEntry.getValue());
        }
        return new IteratorWrapper<>(pyramidLevels.iterator(), new MosaicPerPyramidLevelBuilder(bounds, gridCoverage, tileSize, backgroundValuesPerBand, RasterUtils.getFootprint(projectedReferenceEnvelope, gridCoverage), interpolation, projectedReferenceEnvelope.getCoordinateReferenceSystem()));
    }
    LOGGER.warn("Strategy is not an instance of HierarchicalNumericIndexStrategy : " + index.getIndexStrategy().getClass().getName());
    return Collections.<GridCoverage>emptyIterator();
}
Also used : FactoryException(org.opengis.referencing.FactoryException) ArrayList(java.util.ArrayList) GeneralEnvelope(org.geotools.geometry.GeneralEnvelope) Envelope(org.opengis.geometry.Envelope) GridEnvelope(org.opengis.coverage.grid.GridEnvelope) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) SubStrategy(org.locationtech.geowave.core.index.HierarchicalNumericIndexStrategy.SubStrategy) MultiDimensionalNumericData(org.locationtech.geowave.core.index.numeric.MultiDimensionalNumericData) HierarchicalNumericIndexStrategy(org.locationtech.geowave.core.index.HierarchicalNumericIndexStrategy) GridEnvelope(org.opengis.coverage.grid.GridEnvelope) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) TreeMap(java.util.TreeMap) Point(java.awt.Point) IteratorWrapper(org.locationtech.geowave.core.store.util.IteratorWrapper) GridCoverage(org.opengis.coverage.grid.GridCoverage) FitToIndexGridCoverage(org.locationtech.geowave.adapter.raster.FitToIndexGridCoverage)

Aggregations

Point (java.awt.Point)1 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 GeneralEnvelope (org.geotools.geometry.GeneralEnvelope)1 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)1 FitToIndexGridCoverage (org.locationtech.geowave.adapter.raster.FitToIndexGridCoverage)1 HierarchicalNumericIndexStrategy (org.locationtech.geowave.core.index.HierarchicalNumericIndexStrategy)1 SubStrategy (org.locationtech.geowave.core.index.HierarchicalNumericIndexStrategy.SubStrategy)1 MultiDimensionalNumericData (org.locationtech.geowave.core.index.numeric.MultiDimensionalNumericData)1 IteratorWrapper (org.locationtech.geowave.core.store.util.IteratorWrapper)1 GridCoverage (org.opengis.coverage.grid.GridCoverage)1 GridEnvelope (org.opengis.coverage.grid.GridEnvelope)1 Envelope (org.opengis.geometry.Envelope)1 FactoryException (org.opengis.referencing.FactoryException)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1 TransformException (org.opengis.referencing.operation.TransformException)1