Search in sources :

Example 21 with RandomIter

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

the class OmsMultiTca method process.

@Execute
public void process() {
    if (!concatOr(outMultiTca == null, doReset)) {
        return;
    }
    checkNull(inPit, inFlow, inCp9);
    HashMap<String, Double> regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inFlow);
    int cols = regionMap.get(CoverageUtilities.COLS).intValue();
    int rows = regionMap.get(CoverageUtilities.ROWS).intValue();
    // pm.message();
    @SuppressWarnings("unused") int ipos, jpos, i, j, ncicli = 0;
    double sum, delta, pos;
    // create new matrix
    double[] elevationArray = new double[cols * rows];
    double[] indexOfElevation = new double[cols * rows];
    // pm.message();
    RandomIter flowIter = CoverageUtilities.getRandomIterator(inFlow);
    RandomIter pitIter = CoverageUtilities.getRandomIterator(inPit);
    RandomIter cp9Iter = CoverageUtilities.getRandomIterator(inCp9);
    WritableRaster alreadyDonePixelWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, 0.0);
    WritableRandomIter alreadyDoneIter = RandomIterFactory.createWritable(alreadyDonePixelWR, null);
    WritableRaster multiTcaWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, 1.0);
    WritableRandomIter multiTcaIter = RandomIterFactory.createWritable(multiTcaWR, null);
    /*
         * store the value of elevation in an array
         */
    for (int t = 0; t < rows; t++) {
        for (int s = 0; s < cols; s++) {
            elevationArray[((t) * cols) + s] = pitIter.getSampleDouble(s, t, 0);
            indexOfElevation[((t) * cols) + s] = ((t) * cols) + s + 1;
        }
    }
    // pm.message();
    try {
        QuickSortAlgorithm sortAlgorithm = new QuickSortAlgorithm(pm);
        sortAlgorithm.sort(elevationArray, indexOfElevation);
    } catch (Exception e) {
        e.printStackTrace();
    }
    /*
         * start to working with the highest value of elevation.
         */
    for (int l = cols * rows - 1; l >= 0; l--) {
        ncicli = cols * rows - l;
        if (elevationArray[l] <= 0) {
            break;
        } else {
            pos = indexOfElevation[l];
            // extract the index of the matrix from the arrays index.
            i = (int) (pos - 1) % cols;
            j = (int) (pos - 1) / cols;
            if (alreadyDoneIter.getSampleDouble(i, j, 0) == 0.0) {
                alreadyDoneIter.setSample(i, j, 0, 1.0);
                if (cp9Iter.getSampleDouble(i, j, 0) == 10 || cp9Iter.getSampleDouble(i, j, 0) == 20 || cp9Iter.getSampleDouble(i, j, 0) == 30 || cp9Iter.getSampleDouble(i, j, 0) == 40 || cp9Iter.getSampleDouble(i, j, 0) == 50 || cp9Iter.getSampleDouble(i, j, 0) == 60) {
                    sum = 0;
                    for (int k = 1; k <= 8; k++) {
                        ipos = i + dir[k][0];
                        jpos = j + dir[k][1];
                        delta = pitIter.getSampleDouble(i, j, 0) - pitIter.getSampleDouble(ipos, jpos, 0);
                        if (delta == 0) {
                            if (alreadyDoneIter.getSampleDouble(ipos, jpos, 0) == 0.0 && flowIter.getSampleDouble(ipos, jpos, 0) == dirIn[k][2]) {
                                resolveFlat(ipos, jpos, cols, rows, pitIter, multiTcaIter, alreadyDoneIter, flowIter, cp9Iter);
                            }
                        }
                        if (delta > 0.0 && pitIter.getSampleDouble(ipos, jpos, 0) > 0.0) {
                            sum += delta;
                        }
                    }
                    for (int k = 1; k <= 8; k++) {
                        ipos = i + dir[k][0];
                        jpos = j + dir[k][1];
                        delta = pitIter.getSampleDouble(i, j, 0) - pitIter.getSampleDouble(ipos, jpos, 0);
                        if (delta > 0.0 && pitIter.getSampleDouble(ipos, jpos, 0) > 0.0) {
                            multiTcaIter.setSample(ipos, jpos, 0, multiTcaIter.getSampleDouble(ipos, jpos, 0) + multiTcaIter.getSampleDouble(i, j, 0) * (delta / sum));
                        } else if (delta == 0.0 && flowIter.getSampleDouble(i, j, 0) == dirIn[k][2]) {
                            multiTcaIter.setSample(ipos, jpos, 0, multiTcaIter.getSampleDouble(ipos, jpos, 0) + multiTcaIter.getSampleDouble(i, j, 0));
                        }
                    }
                } else if (cp9Iter.getSampleDouble(i, j, 0) == 70 || cp9Iter.getSampleDouble(i, j, 0) == 80 || cp9Iter.getSampleDouble(i, j, 0) == 90) {
                    for (int k = 1; k <= 8; k++) {
                        ipos = i + dir[k][0];
                        jpos = j + dir[k][1];
                        double delta2 = pitIter.getSampleDouble(i, j, 0) - pitIter.getSampleDouble(ipos, jpos, 0);
                        if (delta2 == 0) {
                            if (alreadyDoneIter.getSampleDouble(ipos, jpos, 0) == 0.0 && flowIter.getSampleDouble(ipos, jpos, 0) == dirIn[k][2]) {
                                resolveFlat(ipos, jpos, cols, rows, pitIter, multiTcaIter, alreadyDoneIter, flowIter, cp9Iter);
                            }
                        }
                    }
                    for (int k = 1; k <= 8; k++) {
                        ipos = i + dir[k][0];
                        jpos = j + dir[k][1];
                        if (flowIter.getSampleDouble(i, j, 0) != 10 && flowIter.getSampleDouble(i, j, 0) == dir[k][2]) {
                            multiTcaIter.setSample(ipos, jpos, 0, multiTcaIter.getSampleDouble(ipos, jpos, 0) + multiTcaIter.getSampleDouble(i, j, 0));
                            break;
                        }
                    }
                }
            }
        }
    }
    for (int t = 0; t < rows; t++) {
        for (int s = 0; s < cols; s++) {
            if (isNovalue(cp9Iter.getSampleDouble(s, t, 0)) || isNovalue(flowIter.getSampleDouble(s, t, 0)))
                multiTcaIter.setSample(s, t, 0, HMConstants.doubleNovalue);
        }
    }
    outMultiTca = CoverageUtilities.buildCoverage("multiTca", multiTcaWR, regionMap, inFlow.getCoordinateReferenceSystem());
}
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) QuickSortAlgorithm(org.hortonmachine.gears.utils.sorting.QuickSortAlgorithm) Execute(oms3.annotations.Execute)

Example 22 with RandomIter

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

the class OmsSlope method process.

@Execute
public void process() {
    if (!concatOr(outSlope == null, doReset)) {
        return;
    }
    checkNull(inPit, inFlow);
    HashMap<String, Double> regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inPit);
    int nCols = regionMap.get(CoverageUtilities.COLS).intValue();
    int nRows = regionMap.get(CoverageUtilities.ROWS).intValue();
    double xRes = regionMap.get(CoverageUtilities.XRES);
    double yRes = regionMap.get(CoverageUtilities.YRES);
    double novalue = HMConstants.getNovalue(inPit);
    RenderedImage elevationRI = inPit.getRenderedImage();
    RandomIter elevationIter = RandomIterFactory.create(elevationRI, null);
    RenderedImage flowRI = inFlow.getRenderedImage();
    RandomIter flowIter = RandomIterFactory.create(flowRI, null);
    WritableRaster slopeWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, doubleNovalue);
    pm.beginTask(msg.message("slope.calculating"), nCols);
    for (int r = 0; r < nRows; r++) {
        for (int c = 0; c < nCols; c++) {
            double flowValue = flowIter.getSampleDouble(c, r, 0);
            GridNode node = new GridNode(elevationIter, nCols, nRows, xRes, yRes, c, r, novalue);
            double value = calculateSlope(node, flowValue);
            if (doHandleNegativeSlope && value < 0) {
                value = Double.MIN_VALUE;
            }
            slopeWR.setSample(c, r, 0, value);
        }
        pm.worked(1);
    }
    pm.done();
    outSlope = CoverageUtilities.buildCoverageWithNovalue("slope", slopeWR, regionMap, inPit.getCoordinateReferenceSystem(), doubleNovalue);
}
Also used : GridNode(org.hortonmachine.gears.libs.modules.GridNode) WritableRaster(java.awt.image.WritableRaster) RandomIter(javax.media.jai.iterator.RandomIter) RenderedImage(java.awt.image.RenderedImage) Execute(oms3.annotations.Execute)

Example 23 with RandomIter

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

the class OmsTca3d method area3d.

private WritableRaster area3d(WritableRaster pitImage, WritableRaster flowImage, WritableRaster tca3dImage) {
    int[][] tri = { { 0, 0 }, { 1, 2 }, /* tri 012 */
    { 3, 2 }, /* tri 023 */
    { 3, 4 }, /* tri 034 |4|3|2| */
    { 5, 4 }, /* tri 045 |5|0|1| */
    { 5, 6 }, /* tri 056 |6|7|8| */
    { 7, 6 }, /* tri 067 */
    { 7, 8 }, /* tri 078 */
    { 1, 8 } /* tri 089 */
    };
    int[][] dir = ModelsSupporter.DIR_WITHFLOW_EXITING_INVERTED;
    int nnov = 0;
    double dx = xRes;
    double dy = yRes;
    double semiptr = 0.0, area = 0.0, areamed = 0.0;
    // areatr contains areas of 8 triangles having vertex in the 8 pixel
    // around
    double[] grid = new double[11];
    // grid contains the dimension of pixels according with flow directions
    grid[0] = grid[9] = grid[10] = 0;
    grid[1] = grid[5] = abs(dx);
    grid[3] = grid[7] = abs(dy);
    grid[2] = grid[4] = grid[6] = grid[8] = sqrt(dx * dx + dy * dy);
    // contains the triangle's side
    double[] latitr = new double[3];
    // per ogni lato del triangolo contiene il dislivello e la distanza
    // planimetrica
    double[][] dzdiff = new double[3][2];
    RandomIter pitIter = RandomIterFactory.create(pitImage, null);
    WritableRandomIter tca3dIter = RandomIterFactory.createWritable(tca3dImage, null);
    // $NON-NLS-1$
    pm.beginTask(msg.message("tca3d.woringon"), rows - 2);
    for (int j = 1; j < rows - 1; j++) {
        for (int i = 1; i < cols - 1; i++) {
            double pitAtIJ = pitIter.getSampleDouble(i, j, 0);
            nnov = 0;
            area = 0;
            areamed = 0;
            final double[] areatr = new double[9];
            if (!isNovalue(pitAtIJ)) {
                // calculates the area of the triangle
                for (int k = 1; k <= 8; k++) {
                    double pitAtK0 = pitIter.getSampleDouble(i + dir[tri[k][0]][0], j + dir[tri[k][0]][1], 0);
                    double pitAtK1 = pitIter.getSampleDouble(i + dir[tri[k][1]][0], j + dir[tri[k][1]][1], 0);
                    if (!isNovalue(pitAtK0) && !isNovalue(pitAtK1)) {
                        nnov++;
                        // calcola per ogni lato del triangolo in dislivello
                        // e la distanza planimetrica tra i pixel
                        // considerati.
                        dzdiff[0][0] = abs(pitAtIJ - pitAtK0);
                        dzdiff[0][1] = grid[dir[tri[k][0]][2]];
                        dzdiff[1][0] = abs(pitAtIJ - pitAtK1);
                        dzdiff[1][1] = grid[dir[tri[k][1]][2]];
                        dzdiff[2][0] = abs(pitAtK0 - pitAtK1);
                        dzdiff[2][1] = grid[1];
                        // calcola i lati del tringolo considerato
                        latitr[0] = sqrt(pow(dzdiff[0][0], 2) + pow(dzdiff[0][1], 2));
                        latitr[1] = sqrt(pow(dzdiff[1][0], 2) + pow(dzdiff[1][1], 2));
                        latitr[2] = sqrt(pow(dzdiff[2][0], 2) + pow(dzdiff[2][1], 2));
                        // calcola il semiperimetro del triangolo
                        semiptr = 0.5 * (latitr[0] + latitr[1] + latitr[2]);
                        // calcola l'area di ciascun triangolo
                        areatr[k] = sqrt(semiptr * (semiptr - latitr[0]) * (semiptr - latitr[1]) * (semiptr - latitr[2]));
                    }
                }
                if (nnov == 8) // calcolo l'area del pixel sommando le aree degli 8
                // triangoli.
                {
                    for (int k = 1; k <= 8; k++) {
                        area = area + areatr[k] / 4;
                    }
                    tca3dIter.setSample(i, j, 0, area);
                } else // se il pixel e' circondato da novalue, non e' possibile
                // comporre
                // 8 triangoli, si calcola quindi l'area relativa ai
                // triangoli completi
                // si calcola la media dei loro valori e quindi si spalma il
                // valore
                // ottenuto sul pixel.
                {
                    for (int k = 1; k <= 8; k++) {
                        area = area + areatr[k] / 4;
                    }
                    areamed = area / nnov;
                    tca3dIter.setSample(i, j, 0, areamed * 8);
                }
            } else
                tca3dIter.setSample(i, j, 0, doubleNovalue);
        }
        pm.worked(1);
    }
    pm.done();
    RandomIter flowIter = RandomIterFactory.create(flowImage, null);
    return ModelsEngine.sumDownstream(flowIter, tca3dIter, cols, rows, null, null, pm);
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter)

Example 24 with RandomIter

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

the class OmsH2cA method process.

@Execute
public void process() {
    if (!concatOr(outAttribute == null, doReset)) {
        return;
    }
    checkNull(inFlow, inNet, inAttribute);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inFlow);
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    int novalue = HMConstants.getIntNovalue(inFlow);
    RenderedImage flowRI = inFlow.getRenderedImage();
    WritableRaster flowWR = CoverageUtilities.renderedImage2WritableRaster(flowRI, true);
    WritableRandomIter flowIter = RandomIterFactory.createWritable(flowWR, null);
    RandomIter attributeIter = CoverageUtilities.getRandomIterator(inAttribute);
    RandomIter netIter = CoverageUtilities.getRandomIterator(inNet);
    // $NON-NLS-1$
    pm.beginTask("Marking the network...", rows);
    /*
         * mark network as outlet, in order to easier stop on the net 
         * while going downstream
         */
    for (int j = 0; j < rows; j++) {
        for (int i = 0; i < cols; i++) {
            if (netIter.getSampleDouble(i, j, 0) == FlowNode.NETVALUE)
                flowIter.setSample(i, j, 0, FlowNode.OUTLET);
        }
        pm.worked(1);
    }
    pm.done();
    netIter.done();
    WritableRaster h2caWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
    WritableRandomIter h2caIter = RandomIterFactory.createWritable(h2caWR, null);
    ModelsEngine.markHillSlopeWithLinkValue(flowIter, novalue, attributeIter, h2caIter, cols, rows, pm);
    h2caIter.done();
    attributeIter.done();
    flowIter.done();
    outAttribute = CoverageUtilities.buildCoverage("h2ca", h2caWR, 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 25 with RandomIter

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

the class OmsTopIndex method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outTopindex == null, doReset)) {
        return;
    }
    checkNull(inTca, inSlope);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inTca);
    int nCols = regionMap.getCols();
    int nRows = regionMap.getRows();
    RandomIter tcaIter = CoverageUtilities.getRandomIterator(inTca);
    RandomIter slopeIter = CoverageUtilities.getRandomIterator(inSlope);
    WritableRaster topindexWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, doubleNovalue);
    WritableRandomIter topindexIter = RandomIterFactory.createWritable(topindexWR, null);
    try {
        pm.beginTask(msg.message("topindex.calculating"), nRows * nCols);
        processGrid(nCols, nRows, (c, r) -> {
            if (pm.isCanceled()) {
                return;
            }
            int tcaValue = tcaIter.getSample(c, r, 0);
            if (!isNovalue(tcaValue)) {
                if (slopeIter.getSampleDouble(c, r, 0) != 0) {
                    topindexIter.setSample(c, r, 0, Math.log(tcaValue / slopeIter.getSampleDouble(c, r, 0)));
                }
            }
            pm.worked(1);
        });
        pm.done();
        outTopindex = CoverageUtilities.buildCoverage("topindex", topindexWR, regionMap, inTca.getCoordinateReferenceSystem());
    } finally {
        tcaIter.done();
        slopeIter.done();
        topindexIter.done();
    }
}
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) Execute(oms3.annotations.Execute)

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