use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class CoverageUtilities method apply.
/**
* Apply an operation to the coverage's values.
*
* @param coverage the original coverage.
* @param operator the operator to apply.
* @return the new coverage.
* @throws Exception
*/
public static GridCoverage2D apply(GridCoverage2D coverage, DoubleUnaryOperator operator) throws Exception {
RegionMap regionMap = getRegionParamsFromGridCoverage(coverage);
int cols = regionMap.getCols();
int rows = regionMap.getRows();
double nv = HMConstants.getNovalue(coverage);
WritableRaster outWR = renderedImage2WritableRaster(coverage.getRenderedImage(), false);
WritableRandomIter iter = RandomIterFactory.createWritable(outWR, null);
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
double value = iter.getSampleDouble(x, y, 0);
if (!HMConstants.isNovalue(value, nv)) {
double newValue = operator.applyAsDouble(value);
iter.setSample(x, y, 0, newValue);
}
}
}
iter.done();
GridCoverage2D outCoverage = buildCoverage("applied", outWR, regionMap, coverage.getCoordinateReferenceSystem());
return outCoverage;
}
use of javax.media.jai.iterator.WritableRandomIter 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.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class CoverageUtilities method createWritableRasterFromMatrix.
/**
* Create a {@link WritableRaster} from a double matrix.
*
* @param matrix the matrix to take the data from.
* @param matrixIsRowCol a flag to tell if the matrix has rowCol or colRow order.
* @return the produced raster.
*/
public static WritableRaster createWritableRasterFromMatrix(double[][] matrix, boolean matrixIsRowCol) {
int height = matrix.length;
int width = matrix[0].length;
if (!matrixIsRowCol) {
int tmp = height;
height = width;
width = tmp;
}
WritableRaster writableRaster = createWritableRaster(width, height, null, null, HMConstants.doubleNovalue);
WritableRandomIter rasterIter = RandomIterFactory.createWritable(writableRaster, null);
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
if (matrixIsRowCol) {
if (!HMConstants.isNovalue(matrix[row][col]))
rasterIter.setSample(col, row, 0, matrix[row][col]);
} else {
if (!HMConstants.isNovalue(matrix[col][row]))
rasterIter.setSample(col, row, 0, matrix[col][row]);
}
}
}
rasterIter.done();
return writableRaster;
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class CoverageUtilities method createWritableRasterFromMatrix.
/**
* Create a {@link WritableRaster} from a float matrix.
*
* @param matrix the matrix to take the data from.
* @param matrixIsRowCol a flag to tell if the matrix has rowCol or colRow order.
* @return the produced raster.
*/
public static WritableRaster createWritableRasterFromMatrix(float[][] matrix, boolean matrixIsRowCol) {
int height = matrix.length;
int width = matrix[0].length;
if (!matrixIsRowCol) {
int tmp = height;
height = width;
width = tmp;
}
WritableRaster writableRaster = createWritableRaster(width, height, null, null, null);
WritableRandomIter randomIter = RandomIterFactory.createWritable(writableRaster, null);
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
if (matrixIsRowCol) {
randomIter.setSample(col, row, 0, matrix[row][col]);
} else {
randomIter.setSample(col, row, 0, matrix[col][row]);
}
}
}
randomIter.done();
return writableRaster;
}
use of javax.media.jai.iterator.WritableRandomIter in project hortonmachine by TheHortonMachine.
the class CoverageUtilities method createWritableRasterFromMatrix.
public static WritableRaster createWritableRasterFromMatrix(int[][] matrix, boolean matrixIsRowCol) {
int height = matrix.length;
int width = matrix[0].length;
if (!matrixIsRowCol) {
int tmp = height;
height = width;
width = tmp;
}
WritableRaster writableRaster = createWritableRaster(width, height, null, null, null);
WritableRandomIter randomIter = RandomIterFactory.createWritable(writableRaster, null);
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
if (matrixIsRowCol) {
randomIter.setSample(col, row, 0, matrix[row][col]);
} else {
randomIter.setSample(col, row, 0, matrix[col][row]);
}
}
}
randomIter.done();
return writableRaster;
}
Aggregations