Search in sources :

Example 1 with GridCoverageReader

use of org.opengis.coverage.grid.GridCoverageReader in project geowave by locationtech.

the class RasterIngestRunner method getBandData.

protected RasterBandData getBandData(final SimpleFeature band) throws IOException, TemplateException {
    final Map<String, Object> model = new HashMap<>();
    final SimpleFeatureType type = band.getFeatureType();
    for (final AttributeDescriptor descriptor : type.getAttributeDescriptors()) {
        final String name = descriptor.getLocalName();
        final Object value = band.getAttribute(name);
        if (value != null) {
            model.put(name, value);
        }
    }
    final String coverageName = FreeMarkerTemplateUtils.processTemplateIntoString(coverageNameTemplate, model);
    final RasterBandData bandData = provider.getCoverage(band, sentinel2Options.getWorkspaceDir());
    GridCoverage2D coverage = bandData.coverage;
    final GridCoverageReader reader = bandData.reader;
    final double nodataValue = bandData.nodataValue;
    if ((ingestOptions.getCoverageConverter() != null) && !ingestOptions.getCoverageConverter().trim().isEmpty()) {
        // a converter was supplied, attempt to use it
        final Sentinel2BandConverterSpi converter = getConverter(ingestOptions.getCoverageConverter());
        if (converter != null) {
            coverage = converter.convert(coverageName, coverage, band);
        }
    }
    if (ingestOptions.isSubsample()) {
        coverage = (GridCoverage2D) RasterUtils.getCoverageOperations().filteredSubsample(coverage, ingestOptions.getScale(), ingestOptions.getScale(), null);
    }
    // its unclear whether cropping should be done first or subsampling
    if (ingestOptions.isCropToSpatialConstraint()) {
        boolean cropped = false;
        final Filter filter = sentinel2Options.getCqlFilter();
        if (filter != null) {
            final ExtractGeometryFilterVisitorResult geometryAndCompareOp = ExtractGeometryFilterVisitor.getConstraints(filter, GeoWaveGTRasterFormat.DEFAULT_CRS, SceneFeatureIterator.SHAPE_ATTRIBUTE_NAME);
            Geometry geometry = geometryAndCompareOp.getGeometry();
            if (geometry != null) {
                // go ahead and intersect this with the scene geometry
                final Geometry sceneShape = (Geometry) band.getAttribute(SceneFeatureIterator.SHAPE_ATTRIBUTE_NAME);
                if (geometry.contains(sceneShape)) {
                    cropped = true;
                } else {
                    geometry = geometry.intersection(sceneShape);
                    final CoverageProcessor processor = CoverageProcessor.getInstance();
                    final AbstractOperation op = (AbstractOperation) processor.getOperation("CoverageCrop");
                    final ParameterValueGroup params = op.getParameters();
                    params.parameter("Source").setValue(coverage);
                    try {
                        final MathTransform transform = CRS.findMathTransform(GeometryUtils.getDefaultCRS(), coverage.getCoordinateReferenceSystem(), true);
                        params.parameter(Crop.CROP_ROI.getName().getCode()).setValue(JTS.transform(geometry, transform));
                        params.parameter(Crop.NODATA.getName().getCode()).setValue(RangeFactory.create(nodataValue, nodataValue));
                        params.parameter(Crop.DEST_NODATA.getName().getCode()).setValue(new double[] { nodataValue });
                        coverage = (GridCoverage2D) op.doOperation(params, null);
                        cropped = true;
                    } catch (InvalidParameterValueException | ParameterNotFoundException | FactoryException | MismatchedDimensionException | TransformException e) {
                        LOGGER.warn("Unable to crop image", e);
                    }
                }
            }
            if (!cropped) {
                LOGGER.warn("Option to crop spatially was set but no spatial constraints were provided in CQL expression");
            }
        }
    }
    return new RasterBandData(coverageName, coverage, reader, nodataValue);
}
Also used : MathTransform(org.opengis.referencing.operation.MathTransform) HashMap(java.util.HashMap) CoverageProcessor(org.geotools.coverage.processing.CoverageProcessor) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) FactoryException(org.opengis.referencing.FactoryException) GridCoverageReader(org.opengis.coverage.grid.GridCoverageReader) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) InvalidParameterValueException(org.opengis.parameter.InvalidParameterValueException) ExtractGeometryFilterVisitorResult(org.locationtech.geowave.core.geotime.util.ExtractGeometryFilterVisitorResult) ParameterNotFoundException(org.opengis.parameter.ParameterNotFoundException) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) TransformException(org.opengis.referencing.operation.TransformException) Geometry(org.locationtech.jts.geom.Geometry) AbstractOperation(org.geotools.coverage.processing.AbstractOperation) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Filter(org.opengis.filter.Filter)

Example 2 with GridCoverageReader

use of org.opengis.coverage.grid.GridCoverageReader in project geowave by locationtech.

the class RasterIngestRunner method nextBand.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected void nextBand(final SimpleFeature band, final AnalysisInfo analysisInfo) {
    super.nextBand(band, analysisInfo);
    if (ingestOptions.isCoveragePerBand()) {
        // using a user supplied freemarker template
        try {
            final RasterBandData bandData = getBandData(band);
            final GridCoverage2D coverage = bandData.coverage;
            final String coverageName = bandData.name;
            final GridCoverageReader reader = bandData.reader;
            final double nodataValue = bandData.nodataValue;
            Writer writer = writerCache.get(coverageName);
            final GridCoverage2D nextCov = coverage;
            if (writer == null) {
                final Map<String, String> metadata = new HashMap<>();
                final String[] metadataNames = reader.getMetadataNames();
                if ((metadataNames != null) && (metadataNames.length > 0)) {
                    for (final String metadataName : metadataNames) {
                        metadata.put(metadataName, reader.getMetadataValue(metadataName));
                    }
                }
                final RasterDataAdapter adapter = new RasterDataAdapter(coverageName, metadata, nextCov, ingestOptions.getTileSize(), ingestOptions.isCreatePyramid(), ingestOptions.isCreateHistogram(), new double[][] { new double[] { nodataValue } }, new NoDataMergeStrategy());
                store.addType(adapter, indices);
                writer = store.createWriter(adapter.getTypeName());
                writerCache.put(coverageName, writer);
            }
            writer.write(nextCov);
        } catch (IOException | TemplateException e) {
            LOGGER.error("Unable to ingest band " + band.getID() + " because coverage name cannot be resolved from template", e);
        }
    } else {
        lastSceneBands.add(band);
    }
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) GridCoverageReader(org.opengis.coverage.grid.GridCoverageReader) IOException(java.io.IOException) RasterDataAdapter(org.locationtech.geowave.adapter.raster.adapter.RasterDataAdapter) NoDataMergeStrategy(org.locationtech.geowave.adapter.raster.adapter.merge.nodata.NoDataMergeStrategy) Writer(org.locationtech.geowave.core.store.api.Writer)

Aggregations

HashMap (java.util.HashMap)2 GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)2 GridCoverageReader (org.opengis.coverage.grid.GridCoverageReader)2 TemplateException (freemarker.template.TemplateException)1 IOException (java.io.IOException)1 AbstractOperation (org.geotools.coverage.processing.AbstractOperation)1 CoverageProcessor (org.geotools.coverage.processing.CoverageProcessor)1 RasterDataAdapter (org.locationtech.geowave.adapter.raster.adapter.RasterDataAdapter)1 NoDataMergeStrategy (org.locationtech.geowave.adapter.raster.adapter.merge.nodata.NoDataMergeStrategy)1 ExtractGeometryFilterVisitorResult (org.locationtech.geowave.core.geotime.util.ExtractGeometryFilterVisitorResult)1 Writer (org.locationtech.geowave.core.store.api.Writer)1 Geometry (org.locationtech.jts.geom.Geometry)1 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)1 AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)1 Filter (org.opengis.filter.Filter)1 MismatchedDimensionException (org.opengis.geometry.MismatchedDimensionException)1 InvalidParameterValueException (org.opengis.parameter.InvalidParameterValueException)1 ParameterNotFoundException (org.opengis.parameter.ParameterNotFoundException)1 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)1 FactoryException (org.opengis.referencing.FactoryException)1