Search in sources :

Example 76 with WritableRandomIter

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

the class OmsGradientIM method processCell.

@Override
protected void processCell(int readCol, int readRow, int writeCol, int writeRow, int readCols, int readRows, int writeCols, int writeRows) {
    RandomIter elevIter = inRasterIterators.get(0);
    double gradient;
    switch(pMode) {
        case 1:
            gradient = OmsGradient.doGradientHornOnCell(elevIter, readCol, readRow, xRes, yRes, doDegrees);
            break;
        case 2:
            gradient = OmsGradient.doGradientEvansOnCell(elevIter, readCol, readRow, xRes, yRes, doDegrees);
            break;
        default:
            gradient = OmsGradient.doGradientDiffOnCell(elevIter, readCol, readRow, xRes, yRes, doDegrees);
            break;
    }
    WritableRandomIter outDataIter = outRasterIterators.get(0);
    outDataIter.setSample(writeCol, writeRow, 0, gradient);
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter)

Example 77 with WritableRandomIter

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

the class WatershedAlgorithm method process.

@Execute
public void process() throws Exception {
    checkNull(inRaster, outRaster);
    GridCoverage2D inRasterGC = getRaster(inRaster);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRasterGC);
    int nCols = regionMap.getCols();
    int nRows = regionMap.getRows();
    byte[] input = CoverageUtilities.renderedImage2ByteArray(inRasterGC.getRenderedImage(), true);
    /**
     * First step : the pixels are sorted according to increasing grey values *
     */
    WatershedStructure watershedStructure = new WatershedStructure(input, nCols, nRows, pm);
    /**
     * Start flooding *
     */
    WatershedFIFO queue = new WatershedFIFO();
    int curlab = 0;
    int heightIndex1 = 0;
    int heightIndex2 = 0;
    for (int h = 0; h < 256; h++) /*Geodesic SKIZ of level h-1 inside level h */
    {
        for (int pixelIndex = heightIndex1; pixelIndex < watershedStructure.size(); pixelIndex++) /*mask all pixels at level h*/
        {
            WatershedPixel p = watershedStructure.get(pixelIndex);
            if (p.getIntHeight() != h) {
                /**
                 * This pixel is at level h+1 *
                 */
                heightIndex1 = pixelIndex;
                break;
            }
            p.setLabelToMASK();
            List<WatershedPixel> neighbours = p.getNeighbours();
            for (int i = 0; i < neighbours.size(); i++) {
                WatershedPixel q = (WatershedPixel) neighbours.get(i);
                if (q.getLabel() >= 0) {
                    /*Initialise queue with neighbours at level h of current basins or watersheds*/
                    p.setDistance(1);
                    queue.fifo_add(p);
                    break;
                }
            // end if
            }
        // end for
        }
        // end for
        int curdist = 1;
        queue.fifo_add_FICTITIOUS();
        while (true) /**
         * extend basins *
         */
        {
            WatershedPixel p = queue.fifo_remove();
            if (p.isFICTITIOUS())
                if (queue.fifo_empty())
                    break;
                else {
                    queue.fifo_add_FICTITIOUS();
                    curdist++;
                    p = queue.fifo_remove();
                }
            List<WatershedPixel> neighbours = p.getNeighbours();
            for (int i = 0; i < neighbours.size(); i++) /* Labelling p by inspecting neighbours */
            {
                WatershedPixel q = (WatershedPixel) neighbours.get(i);
                /* Original algorithm : 
                       if( (q.getDistance() < curdist) &&
                       (q.getLabel()>0 || q.isLabelWSHED()) ) {*/
                if ((q.getDistance() <= curdist) && (q.getLabel() >= 0)) {
                    if (q.getLabel() > 0) {
                        if (p.isLabelMASK())
                            // Removed from original algorithm || p.isLabelWSHED() )
                            p.setLabel(q.getLabel());
                        else if (p.getLabel() != q.getLabel())
                            p.setLabelToWSHED();
                    } else // end if lab>0
                    if (p.isLabelMASK())
                        p.setLabelToWSHED();
                } else if (q.isLabelMASK() && (q.getDistance() == 0)) {
                    q.setDistance(curdist + 1);
                    queue.fifo_add(q);
                }
            }
        // end for, end processing neighbours
        }
        /* Detect and process new minima at level h */
        for (int pixelIndex = heightIndex2; pixelIndex < watershedStructure.size(); pixelIndex++) {
            WatershedPixel p = watershedStructure.get(pixelIndex);
            if (p.getIntHeight() != h) {
                /**
                 * This pixel is at level h+1 *
                 */
                heightIndex2 = pixelIndex;
                break;
            }
            p.setDistance(0);
            if (p.isLabelMASK()) {
                /* the pixel is inside a new minimum */
                curlab++;
                p.setLabel(curlab);
                queue.fifo_add(p);
                while (!queue.fifo_empty()) {
                    WatershedPixel q = queue.fifo_remove();
                    List<WatershedPixel> neighbours = q.getNeighbours();
                    for (int i = 0; i < neighbours.size(); i++) /* inspect neighbours of p2*/
                    {
                        WatershedPixel r = (WatershedPixel) neighbours.get(i);
                        if (r.isLabelMASK()) {
                            r.setLabel(curlab);
                            queue.fifo_add(r);
                        }
                    }
                }
            // end while
            }
        // end if
        }
    // end for
    }
    /**
     * End of flooding *
     */
    WritableRaster outWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, doubleNovalue);
    WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
    pm.beginTask("Setting watersheds...", watershedStructure.size());
    for (int pixelIndex = 0; pixelIndex < watershedStructure.size(); pixelIndex++) {
        WatershedPixel p = watershedStructure.get(pixelIndex);
        int c = p.getX();
        int r = p.getY();
        if (p.isLabelWSHED() && !p.allNeighboursAreWSHED()) {
            if (doBorders)
                outIter.setSample(c, r, 0, 1.0);
        } else {
            if (!doBorders)
                outIter.setSample(c, r, 0, 1.0);
        }
        pm.worked(1);
    }
    pm.done();
    outIter.done();
    GridCoverage2D outRasterGC = CoverageUtilities.buildCoverage("normalized", outWR, regionMap, inRasterGC.getCoordinateReferenceSystem());
    dumpRaster(outRasterGC, outRaster);
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) WatershedFIFO(org.hortonmachine.lesto.modules.vegetation.watershed.WatershedFIFO) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WatershedPixel(org.hortonmachine.lesto.modules.vegetation.watershed.WatershedPixel) WritableRaster(java.awt.image.WritableRaster) WatershedStructure(org.hortonmachine.lesto.modules.vegetation.watershed.WatershedStructure) RegionMap(org.hortonmachine.gears.utils.RegionMap) Execute(oms3.annotations.Execute)

Example 78 with WritableRandomIter

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

the class HMTestMaps method createGC.

private static GridCoverage2D createGC(double[][] data, Class<?> dataClass) {
    RegionMap rm = getEnvelopeparams();
    int nRows = rm.getRows();
    int nCols = rm.getCols();
    WritableRaster pitRaster = CoverageUtilities.createWritableRaster(nCols, nRows, dataClass, null, null);
    WritableRandomIter iter = CoverageUtilities.getWritableRandomIterator(pitRaster);
    for (int r = 0; r < nRows; r++) {
        for (int c = 0; c < nCols; c++) {
            double value = data[r][c];
            iter.setSample(c, r, 0, value);
        }
    }
    iter.done();
    return CoverageUtilities.buildCoverage("gc", pitRaster, rm, crs);
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster) RegionMap(org.hortonmachine.gears.utils.RegionMap)

Example 79 with WritableRandomIter

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

the class OmsExtractNetwork method extractNetMode2.

/**
 * this method the network is extracted by considering only concave points
 * as being part of the channel network.
 * @throws Exception
 */
private WritableRaster extractNetMode2(RenderedImage flowRI, int novalue, RenderedImage tcaRI, RenderedImage classRI, RenderedImage slopeRI) throws Exception {
    RandomIter flowRandomIter = RandomIterFactory.create(flowRI, null);
    RandomIter tcaRandomIter = RandomIterFactory.create(tcaRI, null);
    RandomIter classRandomIter = RandomIterFactory.create(classRI, null);
    RandomIter slopeRandomIter = RandomIterFactory.create(slopeRI, null);
    WritableRaster netImage = CoverageUtilities.createWritableRaster(cols, rows, Short.class, null, shortNovalue);
    // try the operation!!
    WritableRandomIter netRandomIter = RandomIterFactory.createWritable(netImage, null);
    try {
        // $NON-NLS-1$
        pm.beginTask(msg.message("extractnetwork.extracting"), rows * cols);
        processGrid(cols, rows, false, (c, r) -> {
            if (pm.isCanceled()) {
                return;
            }
            double tcaValue = tcaRandomIter.getSample(c, r, 0);
            double slopeValue = slopeRandomIter.getSampleDouble(c, r, 0);
            if (!isNovalue(tcaValue) && !isNovalue(slopeValue)) {
                tcaValue = pow(tcaValue, pExp) * slopeValue;
                if (tcaValue >= pThres && classRandomIter.getSample(c, r, 0) == 15) {
                    netRandomIter.setSample(c, r, 0, NETVALUE);
                    FlowNode flowNode = new FlowNode(flowRandomIter, cols, rows, c, r, novalue);
                    FlowNode runningNode = flowNode;
                    while ((runningNode = runningNode.goDownstream()) != null) {
                        int rCol = runningNode.col;
                        int rRow = runningNode.row;
                        short tmpNetValue = (short) netRandomIter.getSample(rCol, rRow, 0);
                        if (!isNovalue(tmpNetValue)) {
                            break;
                        }
                        if (runningNode.isMarkedAsOutlet()) {
                            netRandomIter.setSample(rCol, rRow, 0, NETVALUE);
                            break;
                        } else if (runningNode.touchesBound()) {
                            Node goDownstream = runningNode.goDownstream();
                            if (goDownstream == null || !goDownstream.isValid()) {
                                netRandomIter.setSample(rCol, rRow, 0, NETVALUE);
                                break;
                            }
                        }
                        netRandomIter.setSample(rCol, rRow, 0, NETVALUE);
                    }
                }
            }
            pm.worked(1);
        });
        pm.done();
        return netImage;
    } finally {
        flowRandomIter.done();
        tcaRandomIter.done();
        classRandomIter.done();
        slopeRandomIter.done();
        netRandomIter.done();
    }
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster) FlowNode(org.hortonmachine.gears.libs.modules.FlowNode) Node(org.hortonmachine.gears.libs.modules.Node) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) FlowNode(org.hortonmachine.gears.libs.modules.FlowNode)

Example 80 with WritableRandomIter

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

the class OmsExtractNetwork method extractNetTcaThreshold.

/**
 * this method calculates the network using a threshold value on the
 * contributing areas or on magnitudo
 * @throws Exception
 */
private WritableRaster extractNetTcaThreshold(RenderedImage tcaRI) throws Exception {
    RandomIter tcaIter = RandomIterFactory.create(tcaRI, null);
    WritableRaster netWR = CoverageUtilities.createWritableRaster(cols, rows, Short.class, null, shortNovalue);
    WritableRandomIter netIter = RandomIterFactory.createWritable(netWR, null);
    try {
        // $NON-NLS-1$
        pm.beginTask(msg.message("extractnetwork.extracting"), rows * cols);
        processGrid(cols, rows, false, (c, r) -> {
            if (pm.isCanceled()) {
                return;
            }
            int tcaValue = tcaIter.getSample(c, r, 0);
            if (!isNovalue(tcaValue)) {
                if (tcaValue >= pThres) {
                    // FIXME needs power here?
                    netIter.setSample(c, r, 0, NETVALUE);
                }
            }
            pm.worked(1);
        });
        pm.done();
        return netWR;
    } finally {
        netIter.done();
        tcaIter.done();
    }
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter)

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