Search in sources :

Example 16 with WritableRandomIter

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

the class OmsWateroutlet method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outBasin == null, doReset)) {
        return;
    }
    checkNull(inFlow);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inFlow);
    ncols = regionMap.getCols();
    nrows = regionMap.getRows();
    double xRes = regionMap.getXres();
    double yRes = regionMap.getYres();
    double north = regionMap.getNorth();
    double west = regionMap.getWest();
    double south = regionMap.getSouth();
    double east = regionMap.getEast();
    if (pNorth == -1 || pEast == -1) {
        throw new ModelsIllegalargumentException("No outlet coordinates were supplied.", this.getClass().getSimpleName(), pm);
    }
    if (pNorth > north || pNorth < south || pEast > east || pEast < west) {
        throw new ModelsIllegalargumentException("The outlet point lies outside the map region.", this.getClass().getSimpleName(), pm);
    }
    RenderedImage flowRI = inFlow.getRenderedImage();
    WritableRaster flowWR = CoverageUtilities.renderedImage2WritableRaster(flowRI, false);
    WritableRandomIter flowIter = RandomIterFactory.createWritable(flowWR, null);
    WritableRaster basinWR = CoverageUtilities.createWritableRaster(ncols, nrows, null, null, null);
    WritableRandomIter basinIter = RandomIterFactory.createWritable(basinWR, null);
    drain_ptrs = new double[size_array(pt_seg, nrows, ncols)];
    // bas = (CELL *) G_calloc (size_array (&ba_seg, nrows, ncols),
    // sizeof(CELL));
    bas_ptrs = new double[size_array(ba_seg, nrows, ncols)];
    pm.beginTask(msg.message("wateroutlet.extracting"), 2 * nrows);
    for (int r = 0; r < nrows; r++) {
        if (pm.isCanceled()) {
            return;
        }
        for (int c = 0; c < ncols; c++) {
            // adapt to the grass drainagedirection format "grass
            // flow=(fluidturtle flow-1)"
            double flowValue = flowIter.getSampleDouble(c, r, 0);
            if (isNovalue(flowValue) || flowValue == 0) {
                flowIter.setSample(c, r, 0, -1.0);
            } else if (flowValue == 1.0) {
                flowIter.setSample(c, r, 0, 8.0);
            } else if (!isNovalue(flowValue)) {
                flowIter.setSample(c, r, 0, flowValue - 1);
            }
            drain_ptrs[seg_index(pt_seg, r, c)] = flowIter.getSample(c, r, 0);
        }
        pm.worked(1);
    }
    int row = (int) ((north - pNorth) / yRes);
    int col = (int) ((pEast - west) / xRes);
    if (row >= 0 && col >= 0 && row < nrows && col < ncols)
        overland_cells(row, col);
    for (int r = 0; r < nrows; r++) {
        for (int c = 0; c < ncols; c++) {
            double basinValue = bas_ptrs[seg_index(ba_seg, r, c)];
            basinIter.setSample(c, r, 0, basinValue);
            if (isNovalue(flowIter.getSampleDouble(c, r, 0)) || basinIter.getSampleDouble(c, r, 0) == 0.0) {
                basinIter.setSample(c, r, 0, doubleNovalue);
            } else {
                outArea = outArea + 1;
            }
        }
        pm.worked(1);
    }
    pm.done();
    outArea = outArea * xRes * yRes;
    outBasin = CoverageUtilities.buildCoverage("basin", basinWR, regionMap, inFlow.getCoordinateReferenceSystem());
}
Also used : ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster) RegionMap(org.hortonmachine.gears.utils.RegionMap) RenderedImage(java.awt.image.RenderedImage) Execute(oms3.annotations.Execute)

Example 17 with WritableRandomIter

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

the class OmsAb method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outAb == null, doReset)) {
        return;
    }
    checkNull(inTca, inPlan);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inTca);
    int nCols = regionMap.getCols();
    int nRows = regionMap.getRows();
    double xRes = regionMap.getXres();
    RandomIter tcaIter = CoverageUtilities.getRandomIterator(inTca);
    RandomIter planIter = CoverageUtilities.getRandomIterator(inPlan);
    WritableRaster alungWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, null);
    WritableRandomIter alungIter = RandomIterFactory.createWritable(alungWR, null);
    WritableRaster bWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, null);
    WritableRandomIter bIter = RandomIterFactory.createWritable(bWR, null);
    try {
        pm.beginTask(msg.message("ab.calculating"), nRows * nCols);
        processGrid(nCols, nRows, (c, r) -> {
            if (pm.isCanceled()) {
                return;
            }
            double planSample = planIter.getSampleDouble(c, r, 0);
            if (!isNovalue(planSample) && planSample != 0.0) {
                if (xRes > 1 / planSample && planSample >= 0.0) {
                    bIter.setSample(c, r, 0, 0.1 * xRes);
                } else if (xRes > Math.abs(1 / planSample) && planSample < 0.0) {
                    bIter.setSample(c, r, 0, xRes + 0.9 * xRes);
                } else {
                    double bSample = 2 * Math.asin(xRes / (2 * (1 / planSample))) * (1 / planSample - xRes);
                    bIter.setSample(c, r, 0, bSample);
                    if (planSample >= 0.0 && bSample < 0.1 * xRes) {
                        bIter.setSample(c, r, 0, 0.1 * xRes);
                    }
                    if (planSample < 0.0 && bSample > (xRes + 0.9 * xRes)) {
                        bIter.setSample(c, r, 0, xRes + 0.9 * xRes);
                    }
                }
            }
            if (planSample == 0.0) {
                bIter.setSample(c, r, 0, xRes);
            }
            alungIter.setSample(c, r, 0, tcaIter.getSample(c, r, 0) * xRes * xRes / bIter.getSampleDouble(c, r, 0));
            if (isNovalue(planSample)) {
                alungIter.setSample(c, r, 0, doubleNovalue);
                bIter.setSample(c, r, 0, doubleNovalue);
            }
            pm.worked(1);
        });
        pm.done();
    } finally {
        tcaIter.done();
        planIter.done();
        alungIter.done();
        bIter.done();
    }
    outAb = CoverageUtilities.buildCoverage("alung", alungWR, regionMap, inTca.getCoordinateReferenceSystem());
    outB = CoverageUtilities.buildCoverage("b", bWR, regionMap, inTca.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) Execute(oms3.annotations.Execute)

Example 18 with WritableRandomIter

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

the class OmsAspectIM 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 novalue = inRasterNovalues.get(0);
    GridNode node = new GridNode(elevIter, readCols, readRows, xRes, yRes, readCol, readRow, novalue);
    double aspect = OmsAspect.calculateAspect(node, radtodeg, doRound);
    WritableRandomIter outDataIter = outRasterIterators.get(0);
    outDataIter.setSample(writeCol, writeRow, 0, aspect);
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) GridNode(org.hortonmachine.gears.libs.modules.GridNode) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter)

Example 19 with WritableRandomIter

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

the class OmsGeomorphon method process.

@Execute
public void process() throws Exception {
    checkNull(inElev);
    if (pRadius <= 0) {
        throw new ModelsIllegalargumentException("The search radius has to be > 0.", this, pm);
    }
    final double diagonalDelta = pRadius / sqrt(2.0);
    final RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
    int cols = regionMap.getCols();
    final int rows = regionMap.getRows();
    final RandomIter elevIter = CoverageUtilities.getRandomIterator(inElev);
    final GridGeometry2D gridGeometry = inElev.getGridGeometry();
    WritableRaster[] outWRHolder = new WritableRaster[1];
    outRaster = CoverageUtilities.createCoverageFromTemplate(inElev, HMConstants.doubleNovalue, outWRHolder);
    final WritableRandomIter outIter = CoverageUtilities.getWritableRandomIterator(outWRHolder[0]);
    pm.beginTask("Calculate classes...", cols);
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            try {
                double classification = calculateGeomorphon(elevIter, gridGeometry, pRadius, pThreshold, diagonalDelta, c, r);
                outIter.setSample(c, r, 0, classification);
            } catch (TransformException e) {
                e.printStackTrace();
            }
        }
        pm.worked(1);
    }
    pm.done();
}
Also used : ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster) RegionMap(org.hortonmachine.gears.utils.RegionMap) TransformException(org.opengis.referencing.operation.TransformException) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) ProfilePoint(org.hortonmachine.gears.utils.coverage.ProfilePoint) Execute(oms3.annotations.Execute)

Example 20 with WritableRandomIter

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

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