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);
}
}
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);
}
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");
}
Aggregations