Search in sources :

Example 31 with RandomIter

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;
}
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 32 with RandomIter

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;
}
Also used : DirectPosition(org.opengis.geometry.DirectPosition) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) GridCoverageFactory(org.geotools.coverage.grid.GridCoverageFactory) RegionMap(org.hortonmachine.gears.utils.RegionMap) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) ComponentSampleModel(java.awt.image.ComponentSampleModel) Envelope2D(org.geotools.geometry.Envelope2D) GridEnvelope2D(org.geotools.coverage.grid.GridEnvelope2D) Point(java.awt.Point) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) WritableRaster(java.awt.image.WritableRaster) GridCoordinates2D(org.geotools.coverage.grid.GridCoordinates2D)

Example 33 with RandomIter

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

Example 34 with RandomIter

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

Example 35 with RandomIter

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

Aggregations

RandomIter (javax.media.jai.iterator.RandomIter)126 WritableRandomIter (javax.media.jai.iterator.WritableRandomIter)79 WritableRaster (java.awt.image.WritableRaster)71 RegionMap (org.hortonmachine.gears.utils.RegionMap)62 Execute (oms3.annotations.Execute)57 RenderedImage (java.awt.image.RenderedImage)29 GridGeometry2D (org.geotools.coverage.grid.GridGeometry2D)29 Coordinate (org.locationtech.jts.geom.Coordinate)26 GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)22 Point (java.awt.Point)17 ArrayList (java.util.ArrayList)17 ModelsIllegalargumentException (org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException)16 SimpleFeature (org.opengis.feature.simple.SimpleFeature)16 DefaultFeatureCollection (org.geotools.feature.DefaultFeatureCollection)15 Geometry (org.locationtech.jts.geom.Geometry)15 Point (org.locationtech.jts.geom.Point)14 GridCoordinates2D (org.geotools.coverage.grid.GridCoordinates2D)12 GridNode (org.hortonmachine.gears.libs.modules.GridNode)12 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)10 FlowNode (org.hortonmachine.gears.libs.modules.FlowNode)9