use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class CoverageUtilities method mergeCoverages.
/**
* Coverage merger.
*
* <p>Values from valuesMap are placed into the onMap coverage, if they are valid.</p>
*
* @param valuesMap the map from which to take teh valid values to place in the output map.
* @param onMap the base map on which to place the valuesMap values.
* @return the merged map of valuesMap over onMap.
*/
public static GridCoverage2D mergeCoverages(GridCoverage2D valuesMap, GridCoverage2D onMap) {
RegionMap valuesRegionMap = getRegionParamsFromGridCoverage(valuesMap);
int cs = valuesRegionMap.getCols();
int rs = valuesRegionMap.getRows();
RegionMap onRegionMap = getRegionParamsFromGridCoverage(onMap);
int tmpcs = onRegionMap.getCols();
int tmprs = onRegionMap.getRows();
if (cs != tmpcs || rs != tmprs) {
throw new IllegalArgumentException("The raster maps have to be of equal size to be mapped.");
}
RandomIter valuesIter = RandomIterFactory.create(valuesMap.getRenderedImage(), null);
WritableRaster outWR = renderedImage2WritableRaster(onMap.getRenderedImage(), false);
WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
for (int r = 0; r < rs; r++) {
for (int c = 0; c < cs; c++) {
double value = valuesIter.getSampleDouble(c, r, 0);
if (!isNovalue(value))
outIter.setSample(c, r, 0, value);
}
}
// $NON-NLS-1$
GridCoverage2D outCoverage = buildCoverage("merged", outWR, onRegionMap, valuesMap.getCoordinateReferenceSystem());
return outCoverage;
}
use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class CoverageUtilities method clipCoverage.
/**
* Clip a coverage to a given envelope.
*
* The region is cut to keep the resolution consistent.
*/
public static GridCoverage2D clipCoverage(GridCoverage2D coverage, ReferencedEnvelope envelope) throws Exception {
RegionMap regionMap = getRegionParamsFromGridCoverage(coverage);
double xRes = regionMap.getXres();
double yRes = regionMap.getYres();
envelope = envelope.transform(coverage.getCoordinateReferenceSystem(), true);
// snap the envelope to the grid in order to properly crop the raster
RegionMap subRegion = regionMap.toSubRegion(envelope);
envelope = new ReferencedEnvelope(subRegion.toEnvelope(), coverage.getCoordinateReferenceSystem());
double west = envelope.getMinX();
double south = envelope.getMinY();
double east = envelope.getMaxX();
double north = envelope.getMaxY();
int cols = (int) ((east - west) / xRes);
int rows = (int) ((north - south) / yRes);
ComponentSampleModel sampleModel = new ComponentSampleModel(DataBuffer.TYPE_DOUBLE, cols, rows, 1, cols, new int[] { 0 });
WritableRaster writableRaster = RasterFactory.createWritableRaster(sampleModel, null);
Envelope2D writeEnvelope = new Envelope2D(coverage.getCoordinateReferenceSystem(), west, south, east - west, north - south);
GridCoverageFactory factory = CoverageFactoryFinder.getGridCoverageFactory(null);
GridCoverage2D clippedCoverage = factory.create("newraster", writableRaster, writeEnvelope);
GridGeometry2D destGG = clippedCoverage.getGridGeometry();
GridGeometry2D sourceGG = coverage.getGridGeometry();
RandomIter iter = getRandomIterator(coverage);
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
DirectPosition world = destGG.gridToWorld(new GridCoordinates2D(x, y));
GridCoordinates2D sourceGrid = sourceGG.worldToGrid(world);
double value = iter.getSampleDouble(sourceGrid.x, sourceGrid.y, 0);
writableRaster.setSample(x, y, 0, value);
}
}
iter.done();
return clippedCoverage;
}
use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class CoverageUtilities method renderedImage2IntegerArray.
/**
* Transform a double values rendered image in its integer array representation by scaling the values.
*
* @param renderedImage the rendered image to transform.
* @param multiply value by which to multiply the values before casting to integer.
* @return the array holding the data.
*/
public static int[] renderedImage2IntegerArray(RenderedImage renderedImage, double multiply) {
int width = renderedImage.getWidth();
int height = renderedImage.getHeight();
int[] values = new int[width * height];
RandomIter imageIter = RandomIterFactory.create(renderedImage, null);
int index = 0;
;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
double sample = imageIter.getSampleDouble(x, y, 0);
sample = sample * multiply;
values[index++] = (int) sample;
}
}
imageIter.done();
return values;
}
use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class CoverageUtilities method renderedImage2ByteArray.
/**
* Transform a double values rendered image in its byte array.
*
* <p>No check is done if the double value fits in a byte.</p>
*
* @param renderedImage the rendered image to transform.
* @param doRowsThenCols if <code>true</code>, rows are processed in the outer loop.
* @return the array holding the data.
*/
public static byte[] renderedImage2ByteArray(RenderedImage renderedImage, boolean doRowsThenCols) {
int width = renderedImage.getWidth();
int height = renderedImage.getHeight();
byte[] values = new byte[width * height];
RandomIter imageIter = RandomIterFactory.create(renderedImage, null);
int index = 0;
if (doRowsThenCols) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
double sample = imageIter.getSampleDouble(x, y, 0);
values[index++] = (byte) sample;
}
}
} else {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
double sample = imageIter.getSampleDouble(x, y, 0);
values[index++] = (byte) sample;
}
}
}
imageIter.done();
return values;
}
use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.
the class CoverageUtilities method replaceNovalue.
/**
* Replace the current internal novalue with a given value.
*
* @param renderedImage a {@link RenderedImage}.
* @param newValue the value to put in instead of the novalue.
* @return the rendered image with the substituted novalue.
*/
public static WritableRaster replaceNovalue(RenderedImage renderedImage, double newValue) {
WritableRaster tmpWR = (WritableRaster) renderedImage.getData();
RandomIter pitTmpIterator = RandomIterFactory.create(renderedImage, null);
int height = renderedImage.getHeight();
int width = renderedImage.getWidth();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (isNovalue(pitTmpIterator.getSampleDouble(x, y, 0))) {
tmpWR.setSample(x, y, 0, newValue);
}
}
}
pitTmpIterator.done();
return tmpWR;
}
Aggregations