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