Search in sources :

Example 1 with GDALGeoTiffReader

use of org.locationtech.geowave.adapter.raster.plugin.gdal.GDALGeoTiffReader in project geowave by locationtech.

the class RasterIngestRunner method nextBand.

@Override
protected void nextBand(final SimpleFeature band, final AnalysisInfo analysisInfo) {
    super.nextBand(band, analysisInfo);
    if (ingestOptions.isCoveragePerBand()) {
        try {
            final BandData bandData = getBandData(band);
            final GridCoverage2D coverage = bandData.coverage;
            final String coverageName = bandData.name;
            final GDALGeoTiffReader reader = bandData.reader;
            Writer writer = writerCache.get(coverageName);
            final GridCoverage2D nextCov = coverage;
            if (writer == null) {
                final Map<String, String> metadata = new HashMap<>();
                final String[] mdNames = reader.getMetadataNames();
                if ((mdNames != null) && (mdNames.length > 0)) {
                    for (final String mdName : mdNames) {
                        metadata.put(mdName, reader.getMetadataValue(mdName));
                    }
                }
                final double nodataValue = getNoDataValue(band);
                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);
            if (!ingestOptions.isRetainImages()) {
                if (!bandData.geotiffFile.delete()) {
                    LOGGER.warn("Unable to delete '" + bandData.geotiffFile.getAbsolutePath() + "'");
                }
            }
        } 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) GDALGeoTiffReader(org.locationtech.geowave.adapter.raster.plugin.gdal.GDALGeoTiffReader) 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)

Example 2 with GDALGeoTiffReader

use of org.locationtech.geowave.adapter.raster.plugin.gdal.GDALGeoTiffReader in project geowave by locationtech.

the class RasterIngestRunner method getBandData.

protected BandData getBandData(final SimpleFeature band) throws IOException, TemplateException {
    final Map<String, Object> model = new HashMap<>();
    final SimpleFeatureType type = band.getFeatureType();
    for (final AttributeDescriptor attr : type.getAttributeDescriptors()) {
        final String attrName = attr.getLocalName();
        final Object attrValue = band.getAttribute(attrName);
        if (attrValue != null) {
            model.put(attrName, attrValue);
        }
    }
    final String coverageName = FreeMarkerTemplateUtils.processTemplateIntoString(coverageNameTemplate, model);
    final File geotiffFile = DownloadRunner.getDownloadFile(band, landsatOptions.getWorkspaceDir());
    final GDALGeoTiffReader reader = new GDALGeoTiffReader(geotiffFile);
    GridCoverage2D coverage = reader.read(null);
    reader.dispose();
    if ((ingestOptions.getCoverageConverter() != null) && !ingestOptions.getCoverageConverter().trim().isEmpty()) {
        // a converter was supplied, attempt to use it
        final Landsat8BandConverterSpi 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 = landsatOptions.getCqlFilter();
        if (filter != null) {
            final ExtractGeometryFilterVisitorResult geometryAndCompareOp = ExtractGeometryFilterVisitor.getConstraints(filter, GeometryUtils.getDefaultCRS(), 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));
                        final double nodataValue = getNoDataValue(band);
                        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 BandData(coverageName, coverage, reader, geotiffFile);
}
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) 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) GDALGeoTiffReader(org.locationtech.geowave.adapter.raster.plugin.gdal.GDALGeoTiffReader) Geometry(org.locationtech.jts.geom.Geometry) AbstractOperation(org.geotools.coverage.processing.AbstractOperation) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Filter(org.opengis.filter.Filter) File(java.io.File)

Example 3 with GDALGeoTiffReader

use of org.locationtech.geowave.adapter.raster.plugin.gdal.GDALGeoTiffReader in project geowave by locationtech.

the class TheiaImageryProvider method getCoverage.

/**
 * Fetch the coverage of the specified band in the specified workspace directory
 */
@Override
public RasterBandData getCoverage(final SimpleFeature band, final String workspaceDir) throws IOException {
    final File sceneDir = DownloadRunner.getSceneDirectory(band, workspaceDir);
    final String entityId = (String) band.getAttribute(SceneFeatureIterator.ENTITY_ID_ATTRIBUTE_NAME);
    final String productId = (String) band.getAttribute(SceneFeatureIterator.PRODUCT_ID_ATTRIBUTE_NAME);
    final String bandName = (String) band.getAttribute(BandFeatureIterator.BAND_ATTRIBUTE_NAME);
    final File file = sceneDir;
    final String[] fileList = sceneDir.list();
    if (fileList != null) {
        for (final String name : fileList) {
            final File temp = new File(file.getAbsolutePath() + File.separatorChar + name);
            if (temp.isDirectory() && name.toUpperCase(Locale.ENGLISH).startsWith(productId.toUpperCase(Locale.ENGLISH))) {
                // We provide the coverage in ground reflectance with the
                // correction of slope effects.
                // The full description of the product format is here:
                // 'https://theia.cnes.fr/atdistrib/documents/PSC-NT-411-0362-CNES_01_00_SENTINEL-2A_L2A_Products_Description.pdf'
                // A more succinct one is also available here:
                // 'http://www.cesbio.ups-tlse.fr/multitemp/?page_id=8352'
                // 
                final File geotiffFile = new File(file.getAbsolutePath() + File.separatorChar + name + File.separatorChar + name + "_FRE_" + bandName + ".tif");
                if (geotiffFile.exists()) {
                    final GDALGeoTiffReader reader = new GDALGeoTiffReader(geotiffFile);
                    final GridCoverage2D coverage = reader.read(null);
                    reader.dispose();
                    return new RasterBandData(entityId + "_" + bandName, coverage, reader, NO_DATA_VALUE);
                }
            }
        }
    }
    throw new IOException("The file of the '" + productId + "_" + bandName + "' coverage does not exist");
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) GDALGeoTiffReader(org.locationtech.geowave.adapter.raster.plugin.gdal.GDALGeoTiffReader) IOException(java.io.IOException) File(java.io.File) RasterBandData(org.locationtech.geowave.format.sentinel2.RasterBandData)

Aggregations

GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)3 GDALGeoTiffReader (org.locationtech.geowave.adapter.raster.plugin.gdal.GDALGeoTiffReader)3 File (java.io.File)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 TemplateException (freemarker.template.TemplateException)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 RasterBandData (org.locationtech.geowave.format.sentinel2.RasterBandData)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