Search in sources :

Example 36 with RandomIter

use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.

the class CoverageUtilities method doProfile.

/**
 * Calculates the profile of a raster map between given {@link Coordinate coordinates}.
 *
 * @param coverage the coverage from which to extract the profile.
 * @param coordinates the coordinates to use to trace the profile.
 * @return the list of {@link ProfilePoint}s.
 * @throws Exception
 */
public static List<ProfilePoint> doProfile(GridCoverage2D coverage, Coordinate... coordinates) throws Exception {
    GridGeometry2D gridGeometry = coverage.getGridGeometry();
    RenderedImage renderedImage = coverage.getRenderedImage();
    RandomIter iter = RandomIterFactory.create(renderedImage, null);
    List<ProfilePoint> profilePointsList = doProfile(iter, gridGeometry, coordinates);
    iter.done();
    return profilePointsList;
}
Also used : GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) RenderedImage(java.awt.image.RenderedImage)

Example 37 with RandomIter

use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.

the class CoverageUtilities method renderedImage2DoubleArray.

/**
 * Transform a rendered image in its array representation.
 *
 * @param renderedImage the rendered image to transform.
 * @param bandNumber the the number of the band to consider..
 * @return the array holding the data.
 */
public static double[] renderedImage2DoubleArray(RenderedImage renderedImage, int bandNumber) {
    int width = renderedImage.getWidth();
    int height = renderedImage.getHeight();
    double[] values = new double[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, bandNumber);
            values[index++] = sample;
        }
    }
    imageIter.done();
    return values;
}
Also used : RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) Point(java.awt.Point)

Example 38 with RandomIter

use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.

the class CoverageUtilities method sumCoverages.

public static GridCoverage2D sumCoverages(GridCoverage2D gc1, GridCoverage2D gc2) {
    RegionMap gc1RegionMap = getRegionParamsFromGridCoverage(gc1);
    int c1 = gc1RegionMap.getCols();
    int r1 = gc1RegionMap.getRows();
    RegionMap gc2RegionMap = getRegionParamsFromGridCoverage(gc2);
    int c2 = gc2RegionMap.getCols();
    int r2 = gc2RegionMap.getRows();
    if (c1 != c2 || r1 != r2) {
        throw new IllegalArgumentException("The raster maps have to be of equal size to be mapped.");
    }
    RandomIter gc1Iter = RandomIterFactory.create(gc1.getRenderedImage(), null);
    WritableRaster outWR = renderedImage2WritableRaster(gc2.getRenderedImage(), false);
    WritableRandomIter gc2Iter = RandomIterFactory.createWritable(outWR, null);
    double nv1 = HMConstants.getNovalue(gc1);
    for (int r = 0; r < r1; r++) {
        for (int c = 0; c < c1; c++) {
            double v1 = gc1Iter.getSampleDouble(c, r, 0);
            double v2 = gc2Iter.getSampleDouble(c, r, 0);
            if (!HMConstants.isNovalue(v1, nv1)) {
                double newV = v1 + v2;
                gc2Iter.setSample(c, r, 0, newV);
            }
        }
    }
    GridCoverage2D outCoverage = buildCoverage("merged", outWR, gc2RegionMap, gc1.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 39 with RandomIter

use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.

the class CoverageUtilities method getRandomIterator.

/**
 * Creates a {@link RandomIter} for the given {@link GridCoverage2D}.
 *
 * <p>It is important to use this method since it supports also
 * large GRASS rasters.
 *
 * @param coverage the coverage on which to wrap a {@link RandomIter}.
 * @return the iterator.
 */
public static RandomIter getRandomIterator(GridCoverage2D coverage) {
    // if (coverage instanceof GrassLegacyGridCoverage2D) {
    // GrassLegacyGridCoverage2D grassGC = (GrassLegacyGridCoverage2D) coverage;
    // GrassLegacyRandomIter iter = new GrassLegacyRandomIter(grassGC.getData());
    // return iter;
    // }
    RenderedImage renderedImage = coverage.getRenderedImage();
    RandomIter iter = RandomIterFactory.create(renderedImage, null);
    return iter;
}
Also used : RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) RenderedImage(java.awt.image.RenderedImage)

Example 40 with RandomIter

use of javax.media.jai.iterator.RandomIter in project hortonmachine by TheHortonMachine.

the class OmsVectorizer method maskRaster.

private GridCoverage2D maskRaster() {
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
    int nCols = regionMap.getCols();
    int nRows = regionMap.getRows();
    RandomIter rasterIter = CoverageUtilities.getRandomIterator(inRaster);
    WritableRaster[] holder = new WritableRaster[1];
    GridCoverage2D outGC = CoverageUtilities.createCoverageFromTemplate(inRaster, novalue, holder);
    WritableRandomIter outIter = RandomIterFactory.createWritable(holder[0], null);
    pm.beginTask("Masking map...", nRows);
    for (int r = 0; r < nRows; r++) {
        for (int c = 0; c < nCols; c++) {
            double value = rasterIter.getSampleDouble(c, r, 0);
            boolean doNull = false;
            if (!isNovalue(value, novalue)) {
                if (!Double.isNaN(pMaskThreshold)) {
                    // check threshold
                    if (value < pMaskThreshold) {
                        doNull = true;
                    } else {
                        doNull = false;
                    }
                }
            } else {
                doNull = true;
            }
            if (!doNull)
                outIter.setSample(c, r, 0, 1);
        }
        pm.worked(1);
    }
    pm.done();
    return outGC;
}
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)

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