use of org.opengis.coverage.grid.GridCoverage in project geowave by locationtech.
the class GeoWaveBasicRasterIT method queryGeneralPurpose.
private void queryGeneralPurpose(final String coverageName, final int tileSize, final double westLon, final double eastLon, final double southLat, final double northLat, final int numBands, final int numRasters, final ExpectedValue expectedValue) throws IOException {
final DataStore dataStore = dataStoreOptions.createDataStore();
try (CloseableIterator<?> it = dataStore.query(QueryBuilder.newBuilder().addTypeName(coverageName).constraints(new IndexOnlySpatialQuery(new GeometryFactory().toGeometry(new Envelope(westLon, eastLon, southLat, northLat)))).build())) {
// the expected outcome is:
// band 1,2,3,4,5,6 has every value set correctly, band 0 has every
// even row set correctly and every odd row should be NaN, and band
// 7 has the upper quadrant as NaN and the rest set
final GridCoverage coverage = (GridCoverage) it.next();
final Raster raster = coverage.getRenderedImage().getData();
Assert.assertEquals(tileSize, raster.getWidth());
Assert.assertEquals(tileSize, raster.getHeight());
for (int x = 0; x < tileSize; x++) {
for (int y = 0; y < tileSize; y++) {
for (int b = 0; b < numBands; b++) {
Assert.assertEquals("x=" + x + ",y=" + y + ",b=" + b, expectedValue.getExpectedValue(x, y, b, numRasters, tileSize), raster.getSampleDouble(x, y, b), TestUtils.DOUBLE_EPSILON);
}
}
}
// there should be exactly one
Assert.assertFalse(it.hasNext());
}
}
use of org.opengis.coverage.grid.GridCoverage in project geowave by locationtech.
the class GeoToolsRasterDataStoreIngestPlugin method toGeoWaveData.
@Override
public CloseableIterator<GeoWaveData<GridCoverage>> toGeoWaveData(final URL input, final String[] indexNames) {
final AbstractGridFormat format = prioritizedFindFormat(input);
if (format == null) {
return new Wrapper<>(Collections.emptyIterator());
}
Hints hints = null;
if ((optionProvider.getCrs() != null) && !optionProvider.getCrs().trim().isEmpty()) {
try {
final CoordinateReferenceSystem crs = CRS.decode(optionProvider.getCrs());
if (crs != null) {
hints = new Hints();
hints.put(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, crs);
}
} catch (final Exception e) {
LOGGER.warn("Unable to find coordinate reference system, continuing without hint", e);
}
}
final GridCoverage2DReader reader = format.getReader(input, hints);
if (reader == null) {
LOGGER.error("Unable to get reader instance, getReader returned null");
return new Wrapper<>(Collections.emptyIterator());
}
try {
final GridCoverage2D coverage = reader.read(null);
if (coverage != null) {
final Map<String, String> metadata = new HashMap<>();
final String coverageName = coverage.getName().toString();
try {
// wrapping with try-catch block because often the reader
// does not support operations on coverage name
// if not, we just don't have metadata, and continue
final String[] mdNames = reader.getMetadataNames(coverageName);
if ((mdNames != null) && (mdNames.length > 0)) {
for (final String mdName : mdNames) {
if (mdName != null) {
final String value = reader.getMetadataValue(coverageName, mdName);
if (value != null) {
metadata.put(mdName, value);
}
}
}
}
} catch (final Exception e) {
LOGGER.debug("Unable to find metadata from coverage reader", e);
}
final List<GeoWaveData<GridCoverage>> coverages = new ArrayList<>();
if (optionProvider.isSeparateBands() && (coverage.getNumSampleDimensions() > 1)) {
final String baseName = optionProvider.getCoverageName() != null ? optionProvider.getCoverageName() : FilenameUtils.getName(input.getPath());
final double[][] nodata = optionProvider.getNodata(coverage.getNumSampleDimensions());
for (int b = 0; b < coverage.getNumSampleDimensions(); b++) {
final RasterDataAdapter adapter = new RasterDataAdapter(baseName + "_B" + b, metadata, (GridCoverage2D) RasterUtils.getCoverageOperations().selectSampleDimension(coverage, new int[] { b }), optionProvider.getTileSize(), optionProvider.isBuildPyramid(), optionProvider.isBuildHistogram(), new double[][] { nodata[b] });
coverages.add(new GeoWaveData<>(adapter, indexNames, coverage));
}
} else {
final RasterDataAdapter adapter = new RasterDataAdapter(optionProvider.getCoverageName() != null ? optionProvider.getCoverageName() : input.getPath(), metadata, coverage, optionProvider.getTileSize(), optionProvider.isBuildPyramid(), optionProvider.isBuildHistogram(), optionProvider.getNodata(coverage.getNumSampleDimensions()));
coverages.add(new GeoWaveData<>(adapter, indexNames, coverage));
}
return new Wrapper<GeoWaveData<GridCoverage>>(coverages.iterator()) {
@Override
public void close() {
try {
reader.dispose();
} catch (final IOException e) {
LOGGER.warn("unable to dispose of reader resources", e);
}
}
};
} else {
LOGGER.warn("Null grid coverage from file '" + input.getPath() + "' for discovered geotools format '" + format.getName() + "'");
}
} catch (final IOException e) {
LOGGER.warn("Unable to read grid coverage of file '" + input.getPath() + "' for discovered geotools format '" + format.getName() + "'", e);
}
return new Wrapper<>(Collections.emptyIterator());
}
use of org.opengis.coverage.grid.GridCoverage in project geowave by locationtech.
the class RasterUtils method mosaicGridCoverages.
public static synchronized GridCoverage2D mosaicGridCoverages(final Iterator<GridCoverage> gridCoverages, final Color backgroundColor, final Color outputTransparentColor, final Rectangle pixelDimension, final GeneralEnvelope requestEnvelope, final double levelResX, final double levelResY, final double[][] noDataValues, final boolean xAxisSwitch, final GridCoverageFactory coverageFactory, final String coverageName, final Interpolation interpolation, final Histogram histogram, final boolean scaleTo8BitSet, final boolean scaleTo8Bit, final ColorModel defaultColorModel) {
if (pixelDimension == null) {
LOGGER.error("Pixel dimension can not be null");
throw new IllegalArgumentException("Pixel dimension can not be null");
}
final double rescaleX = levelResX / (requestEnvelope.getSpan(0) / pixelDimension.getWidth());
final double rescaleY = levelResY / (requestEnvelope.getSpan(1) / pixelDimension.getHeight());
final double width = pixelDimension.getWidth() / rescaleX;
final double height = pixelDimension.getHeight() / rescaleY;
final int imageWidth = (int) Math.max(Math.round(width), 1);
final int imageHeight = (int) Math.max(Math.round(height), 1);
BufferedImage image = null;
int numDimensions;
SampleDimension[] sampleDimensions = null;
double[][] extrema = null;
boolean extremaValid = false;
while (gridCoverages.hasNext()) {
final GridCoverage currentCoverage = gridCoverages.next();
if (sampleDimensions == null) {
numDimensions = currentCoverage.getNumSampleDimensions();
sampleDimensions = new SampleDimension[numDimensions];
extrema = new double[2][numDimensions];
extremaValid = true;
for (int d = 0; d < numDimensions; d++) {
sampleDimensions[d] = currentCoverage.getSampleDimension(d);
extrema[0][d] = sampleDimensions[d].getMinimumValue();
extrema[1][d] = sampleDimensions[d].getMaximumValue();
if ((extrema[1][d] - extrema[0][d]) <= 0) {
extremaValid = false;
}
}
}
final Envelope coverageEnv = currentCoverage.getEnvelope();
final RenderedImage coverageImage = currentCoverage.getRenderedImage();
if (image == null) {
image = copyImage(imageWidth, imageHeight, backgroundColor, noDataValues, coverageImage);
}
final int posx = (int) ((coverageEnv.getMinimum(0) - requestEnvelope.getMinimum(0)) / levelResX);
final int posy = (int) ((requestEnvelope.getMaximum(1) - coverageEnv.getMaximum(1)) / levelResY);
image.getRaster().setRect(posx, posy, coverageImage.getData());
}
if (image == null) {
image = getEmptyImage(imageWidth, imageHeight, backgroundColor, // the transparent color
null, // will be used later
defaultColorModel);
}
GeneralEnvelope resultEnvelope = null;
if (xAxisSwitch) {
final Rectangle2D tmp = new Rectangle2D.Double(requestEnvelope.getMinimum(1), requestEnvelope.getMinimum(0), requestEnvelope.getSpan(1), requestEnvelope.getSpan(0));
resultEnvelope = new GeneralEnvelope(tmp);
resultEnvelope.setCoordinateReferenceSystem(requestEnvelope.getCoordinateReferenceSystem());
} else {
resultEnvelope = requestEnvelope;
}
final double scaleX = rescaleX * (width / imageWidth);
final double scaleY = rescaleY * (height / imageHeight);
if ((Math.abs(scaleX - 1) > FloatCompareUtils.COMP_EPSILON) || (Math.abs(scaleY - 1) > FloatCompareUtils.COMP_EPSILON)) {
image = rescaleImageViaPlanarImage(interpolation, rescaleX * (width / imageWidth), rescaleY * (height / imageHeight), image);
}
RenderedImage result = image;
// modification to the original data, we'll use extrema
if (extremaValid && scaleTo8Bit) {
final int dataType = result.getData().getDataBuffer().getDataType();
switch(dataType) {
// with an index color model I would still go to 8 bits
case DataBuffer.TYPE_USHORT:
if (result.getColorModel() instanceof IndexColorModel) {
break;
}
case DataBuffer.TYPE_DOUBLE:
case DataBuffer.TYPE_FLOAT:
if (!scaleTo8BitSet && (dataType != DataBuffer.TYPE_USHORT)) {
break;
}
case DataBuffer.TYPE_INT:
case DataBuffer.TYPE_SHORT:
// rescale to byte
final ImageWorkerPredefineStats w = new ImageWorkerPredefineStats(result);
// it was found that geoserver will perform this, and worse
// perform it on local extrema calculated from a single
// tile, this is our one opportunity to at least ensure this
// transformation is done without too much harm by using
// global extrema
result = w.setExtrema(extrema).rescaleToBytes().getRenderedImage();
break;
default:
// to do nothing
break;
}
}
if (outputTransparentColor != null) {
result = ImageUtilities.maskColor(outputTransparentColor, result);
}
if (histogram != null) {
// we should perform histogram equalization
final int numBands = histogram.getNumBands();
final float[][] cdFeq = new float[numBands][];
final double[][] computedExtrema = new double[2][numBands];
for (int b = 0; b < numBands; b++) {
computedExtrema[0][b] = histogram.getLowValue(b);
computedExtrema[1][b] = histogram.getHighValue(b);
final int numBins = histogram.getNumBins()[b];
cdFeq[b] = new float[numBins];
for (int i = 0; i < numBins; i++) {
cdFeq[b][i] = (float) (i + 1) / (float) (numBins);
}
}
final RenderedImageAdapter adaptedResult = new RenderedImageAdapter(result);
adaptedResult.setProperty("histogram", histogram);
adaptedResult.setProperty("extrema", computedExtrema);
result = JAI.create("matchcdf", adaptedResult, cdFeq);
}
return coverageFactory.create(coverageName, result, resultEnvelope);
}
use of org.opengis.coverage.grid.GridCoverage in project geowave by locationtech.
the class RasterTileResizeHelper method getMergedCoverage.
public GridCoverage getMergedCoverage(final GeoWaveInputKey key, final Iterable<Object> values) throws IOException, InterruptedException {
GridCoverage mergedCoverage = null;
ClientMergeableRasterTile<?> mergedTile = null;
boolean needsMerge = false;
final Iterator it = values.iterator();
while (it.hasNext()) {
final Object value = it.next();
if (value instanceof GridCoverage) {
if (mergedCoverage == null) {
mergedCoverage = (GridCoverage) value;
} else {
if (!needsMerge) {
mergedTile = newAdapter.getRasterTileFromCoverage(mergedCoverage);
needsMerge = true;
}
final ClientMergeableRasterTile thisTile = newAdapter.getRasterTileFromCoverage((GridCoverage) value);
if (mergedTile != null) {
mergedTile.merge(thisTile);
}
}
}
}
if (needsMerge) {
final Pair<byte[], byte[]> pair = key.getPartitionAndSortKey(index);
mergedCoverage = newAdapter.getCoverageFromRasterTile(mergedTile, pair == null ? null : pair.getLeft(), pair == null ? null : pair.getRight(), index);
}
return mergedCoverage;
}
use of org.opengis.coverage.grid.GridCoverage in project geowave by locationtech.
the class RasterTileResizeMapper method mapNativeValue.
@Override
protected void mapNativeValue(final GeoWaveInputKey key, final GridCoverage value, final MapContext<GeoWaveInputKey, GridCoverage, GeoWaveInputKey, Object> context) throws IOException, InterruptedException {
if (helper.isOriginalCoverage(key.getInternalAdapterId())) {
final InternalDataAdapter<?> adapter = super.serializationTool.getInternalAdapter(key.getInternalAdapterId());
if ((adapter != null) && (adapter.getAdapter() != null) && (adapter.getAdapter() instanceof RasterDataAdapter)) {
final Iterator<GridCoverage> coverages = helper.getCoveragesForIndex(value);
if (coverages == null) {
LOGGER.error("Couldn't get coverages instance, getCoveragesForIndex returned null");
throw new IOException("Couldn't get coverages instance, getCoveragesForIndex returned null");
}
while (coverages.hasNext()) {
final GridCoverage c = coverages.next();
// converted above
if (c instanceof FitToIndexGridCoverage) {
final byte[] partitionKey = ((FitToIndexGridCoverage) c).getPartitionKey();
final byte[] sortKey = ((FitToIndexGridCoverage) c).getSortKey();
final GeoWaveKey geowaveKey = new GeoWaveKeyImpl(helper.getNewDataId(c), key.getInternalAdapterId(), partitionKey, sortKey, 0);
final GeoWaveInputKey inputKey = new GeoWaveInputKey(helper.getNewAdapterId(), geowaveKey, helper.getIndexName());
context.write(inputKey, c);
}
}
}
}
}
Aggregations