Search in sources :

Example 31 with WritableRandomIter

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

the class CoverageUtilities method getWritableRandomIterator.

/**
 * Creates a {@link WritableRandomIter}.
 *
 * <p>It is important to use this method since it supports also
 * large GRASS rasters.
 *
 * <p>If the size would throw an integer overflow, a {@link GrassLegacyRandomIter}
 * will be proposed to try to save the saveable.
 *
 * @param raster the coverage on which to wrap a {@link WritableRandomIter}.
 * @return the iterator.
 */
public static WritableRandomIter getWritableRandomIterator(int width, int height) {
    WritableRaster pitRaster = CoverageUtilities.createWritableRaster(width, height, null, null, null);
    WritableRandomIter iter = RandomIterFactory.createWritable(pitRaster, null);
    return iter;
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster)

Example 32 with WritableRandomIter

use of javax.media.jai.iterator.WritableRandomIter 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 33 with WritableRandomIter

use of javax.media.jai.iterator.WritableRandomIter 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)

Example 34 with WritableRandomIter

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

the class OmsDistanceToOutlet method process.

@Execute
public void process() {
    if (!concatOr(outDistance == null, doReset)) {
        return;
    }
    checkInParameters();
    RandomIter pitIter = null;
    if (inPit != null) {
        pitIter = CoverageUtilities.getRandomIterator(inPit);
    }
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inFlow);
    int cols = regionMap.get(CoverageUtilities.COLS).intValue();
    int rows = regionMap.get(CoverageUtilities.ROWS).intValue();
    RenderedImage flowRI = inFlow.getRenderedImage();
    WritableRaster flowWR = CoverageUtilities.renderedImage2WritableRaster(flowRI, true);
    WritableRandomIter flowIter = RandomIterFactory.createWritable(flowWR, null);
    WritableRaster distanceWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, 0.0);
    WritableRandomIter distanceIter = CoverageUtilities.getWritableRandomIterator(distanceWR);
    if (pMode == 1) {
        ModelsEngine.outletdistance(flowIter, distanceIter, regionMap, pm);
    } else if (pMode == 0) {
        ModelsEngine.topologicalOutletdistance(flowIter, pitIter, distanceIter, regionMap, pm);
    }
    for (int j = 0; j < rows; j++) {
        for (int i = 0; i < cols; i++) {
            if (isNovalue(flowIter.getSampleDouble(i, j, 0))) {
                distanceIter.setSample(i, j, 0, HMConstants.doubleNovalue);
            }
        }
    }
    outDistance = CoverageUtilities.buildCoverage("distanceToOutlet", distanceWR, regionMap, inFlow.getCoordinateReferenceSystem());
}
Also used : 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) RenderedImage(java.awt.image.RenderedImage) Execute(oms3.annotations.Execute)

Example 35 with WritableRandomIter

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

the class OmsHackLength method hacklength.

private void hacklength(RandomIter flowIter, int novalue, RandomIter tcaIter, RandomIter elevIter) {
    double runningDistance = 0.0;
    double maxTca = 0.0;
    WritableRaster hacklengthWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, doubleNovalue);
    WritableRandomIter hacklengthIter = RandomIterFactory.createWritable(hacklengthWR, null);
    // $NON-NLS-1$
    pm.beginTask(msg.message("hacklength.calculating"), nRows);
    for (int r = 0; r < nRows; r++) {
        for (int c = 0; c < nCols; c++) {
            FlowNode flowNode = new FlowNode(flowIter, nCols, nRows, c, r, novalue);
            if (flowNode.isSource() && !flowNode.isHeadingOutside()) {
                runningDistance = 0;
                flowNode.setValueInMap(hacklengthIter, runningDistance);
                maxTca = 1;
                FlowNode oldNode = flowNode;
                FlowNode runningNode = oldNode.goDownstream();
                while (runningNode != null && runningNode.isValid() && !runningNode.isMarkedAsOutlet()) {
                    boolean isMax = tcaMax(runningNode, tcaIter, hacklengthIter, maxTca, runningDistance);
                    if (isMax) {
                        double distance = Direction.forFlow((int) oldNode.flow).getDistance(xRes, yRes);
                        if (elevIter != null) {
                            double d1 = oldNode.getValueFromMap(elevIter);
                            double d2 = runningNode.getValueFromMap(elevIter);
                            double dz = d1 - d2;
                            runningDistance += sqrt(pow(distance, 2) + pow(dz, 2));
                        } else {
                            runningDistance += distance;
                        }
                        runningNode.setValueInMap(hacklengthIter, runningDistance);
                        maxTca = runningNode.getValueFromMap(tcaIter);
                    }
                    oldNode = runningNode;
                    runningNode = runningNode.goDownstream();
                }
                if (runningNode != null && runningNode.isMarkedAsOutlet()) {
                    if (tcaMax(runningNode, tcaIter, hacklengthIter, maxTca, runningDistance)) {
                        double distance = Direction.forFlow((int) oldNode.flow).getDistance(xRes, yRes);
                        if (elevIter != null) {
                            double d1 = oldNode.getValueFromMap(elevIter);
                            double d2 = runningNode.getValueFromMap(elevIter);
                            double dz = d1 - d2;
                            runningDistance += sqrt(pow(distance, 2) + pow(dz, 2));
                        } else {
                            runningDistance += distance;
                        }
                        runningNode.setValueInMap(hacklengthIter, runningDistance);
                    }
                }
            }
        }
        pm.worked(1);
    }
    pm.done();
    hacklengthIter.done();
    outHacklength = CoverageUtilities.buildCoverage("Hacklength", hacklengthWR, regionMap, inFlow.getCoordinateReferenceSystem());
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster) FlowNode(org.hortonmachine.gears.libs.modules.FlowNode)

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