Search in sources :

Example 26 with WritableRandomIter

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;
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster) RegionMap(org.hortonmachine.gears.utils.RegionMap) Point(java.awt.Point)

Example 27 with WritableRandomIter

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;
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster) RegionMap(org.hortonmachine.gears.utils.RegionMap) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) Point(java.awt.Point)

Example 28 with WritableRandomIter

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;
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster) Point(java.awt.Point)

Example 29 with WritableRandomIter

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;
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster) Point(java.awt.Point)

Example 30 with WritableRandomIter

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;
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster) Point(java.awt.Point)

Aggregations

WritableRandomIter (javax.media.jai.iterator.WritableRandomIter)99 WritableRaster (java.awt.image.WritableRaster)85 RandomIter (javax.media.jai.iterator.RandomIter)60 RegionMap (org.hortonmachine.gears.utils.RegionMap)59 Execute (oms3.annotations.Execute)49 Coordinate (org.locationtech.jts.geom.Coordinate)20 GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)18 ModelsIllegalargumentException (org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException)18 GridGeometry2D (org.geotools.coverage.grid.GridGeometry2D)17 RenderedImage (java.awt.image.RenderedImage)16 Point (java.awt.Point)11 ArrayList (java.util.ArrayList)11 Geometry (org.locationtech.jts.geom.Geometry)11 GridCoordinates2D (org.geotools.coverage.grid.GridCoordinates2D)10 FlowNode (org.hortonmachine.gears.libs.modules.FlowNode)10 DirectPosition2D (org.geotools.geometry.DirectPosition2D)9 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)8 Envelope (org.locationtech.jts.geom.Envelope)7 DirectPosition (org.opengis.geometry.DirectPosition)7 SampleModel (java.awt.image.SampleModel)6