Search in sources :

Example 81 with RandomIter

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

the class OmsSumDownStream method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outSummed == null, doReset)) {
        return;
    }
    checkNull(inFlow, inToSum);
    RandomIter flowIter = CoverageUtilities.getRandomIterator(inFlow);
    RandomIter toSumIter = CoverageUtilities.getRandomIterator(inToSum);
    int[] colsRows = CoverageUtilities.getRegionColsRows(inFlow);
    WritableRaster summedWR = ModelsEngine.sumDownstream(flowIter, toSumIter, colsRows[0], colsRows[1], pUpperThres, pLowerThres, pm);
    flowIter.done();
    toSumIter.done();
    HashMap<String, Double> params = CoverageUtilities.getRegionParamsFromGridCoverage(inFlow);
    // $NON-NLS-1$
    outSummed = CoverageUtilities.buildCoverage("summeddownstream", summedWR, params, inFlow.getCoordinateReferenceSystem());
}
Also used : WritableRaster(java.awt.image.WritableRaster) RandomIter(javax.media.jai.iterator.RandomIter) Execute(oms3.annotations.Execute)

Example 82 with RandomIter

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

the class OmsExtractBasin method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outBasin == null, doReset)) {
        return;
    }
    checkNull(inFlow);
    crs = inFlow.getCoordinateReferenceSystem();
    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);
    }
    Coordinate snapOutlet = snapOutlet();
    if (snapOutlet != null) {
        pEast = snapOutlet.x;
        pNorth = snapOutlet.y;
    }
    int novalue = HMConstants.getIntNovalue(inFlow);
    RandomIter flowIter = CoverageUtilities.getRandomIterator(inFlow);
    WritableRaster basinWR = CoverageUtilities.createWritableRaster(ncols, nrows, Short.class, null, shortNovalue);
    WritableRandomIter basinIter = RandomIterFactory.createWritable(basinWR, null);
    try {
        Coordinate outlet = new Coordinate(pEast, pNorth);
        int[] outletColRow = CoverageUtilities.colRowFromCoordinate(outlet, inFlow.getGridGeometry(), null);
        int outletFlow = flowIter.getSample(outletColRow[0], outletColRow[1], 0);
        if (isNovalue(outletFlow)) {
            throw new IllegalArgumentException("The chosen outlet point doesn't have a valid value.");
        }
        FlowNode runningNode = new FlowNode(flowIter, ncols, nrows, outletColRow[0], outletColRow[1], novalue);
        runningNode.setIntValueInMap(basinIter, 1);
        outArea++;
        ConcurrentLinkedQueue<FlowNode> enteringNodes = new ConcurrentLinkedQueue<>(runningNode.getEnteringNodes());
        pm.beginTask(msg.message("wateroutlet.extracting"), -1);
        while (enteringNodes.size() > 0) {
            if (pm.isCanceled()) {
                return;
            }
            ConcurrentLinkedQueue<FlowNode> newEnteringNodes = new ConcurrentLinkedQueue<>();
            enteringNodes.parallelStream().forEach(flowNode -> {
                if (pm.isCanceled()) {
                    return;
                }
                if (!alreadyWarned && flowNode.touchesBound()) {
                    pm.errorMessage(MessageFormat.format("WARNING: touched boundaries in col/row = {0}/{1}. You might consider to review your processing region.", flowNode.col, flowNode.row));
                    alreadyWarned = true;
                }
                flowNode.setIntValueInMap(basinIter, 1);
                outArea++;
                List<FlowNode> newEntering = flowNode.getEnteringNodes();
                newEnteringNodes.addAll(newEntering);
            });
            enteringNodes = newEnteringNodes;
        }
        pm.done();
        outArea = outArea * xRes * yRes;
        outBasin = CoverageUtilities.buildCoverageWithNovalue("basin", basinWR, regionMap, crs, shortNovalue);
        extractVectorBasin();
    } finally {
        flowIter.done();
        basinIter.done();
    }
}
Also used : RegionMap(org.hortonmachine.gears.utils.RegionMap) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) Point(org.locationtech.jts.geom.Point) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) Coordinate(org.locationtech.jts.geom.Coordinate) WritableRaster(java.awt.image.WritableRaster) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) FlowNode(org.hortonmachine.gears.libs.modules.FlowNode) Execute(oms3.annotations.Execute)

Example 83 with RandomIter

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

the class OmsCurvatures method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outProf == null, doReset)) {
        return;
    }
    checkNull(inElev);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
    int nCols = regionMap.getCols();
    int nRows = regionMap.getRows();
    double xRes = regionMap.getXres();
    double yRes = regionMap.getYres();
    double novalue = HMConstants.getNovalue(inElev);
    RandomIter elevationIter = CoverageUtilities.getRandomIterator(inElev);
    WritableRaster profWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, novalue);
    WritableRaster planWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, novalue);
    WritableRaster tangWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, novalue);
    final double[] planTangProf = new double[3];
    try {
        /*
                 * calculate curvatures
                 */
        pm.beginTask(msg.message("curvatures.calculating"), (nRows - 2) * (nCols - 2));
        processGrid(nCols, nRows, true, (c, r) -> {
            if (pm.isCanceled()) {
                return;
            }
            GridNode node = new GridNode(elevationIter, nCols, nRows, xRes, yRes, c, r, novalue);
            if (node.isValid() && !node.touchesNovalue() && !node.touchesBound()) {
                calculateCurvatures2(node, planTangProf);
                planWR.setSample(c, r, 0, planTangProf[0]);
                tangWR.setSample(c, r, 0, planTangProf[1]);
                profWR.setSample(c, r, 0, planTangProf[2]);
            }
            pm.worked(1);
        });
        pm.done();
    } finally {
        elevationIter.done();
    }
    if (pm.isCanceled()) {
        return;
    }
    outProf = CoverageUtilities.buildCoverageWithNovalue("prof_curvature", profWR, regionMap, inElev.getCoordinateReferenceSystem(), novalue);
    outPlan = CoverageUtilities.buildCoverageWithNovalue("plan_curvature", planWR, regionMap, inElev.getCoordinateReferenceSystem(), novalue);
    outTang = CoverageUtilities.buildCoverageWithNovalue("tang_curvature", tangWR, regionMap, inElev.getCoordinateReferenceSystem(), novalue);
}
Also used : GridNode(org.hortonmachine.gears.libs.modules.GridNode) WritableRaster(java.awt.image.WritableRaster) RegionMap(org.hortonmachine.gears.utils.RegionMap) RandomIter(javax.media.jai.iterator.RandomIter) Execute(oms3.annotations.Execute)

Example 84 with RandomIter

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

the class OmsCurvaturesBivariate method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outProf == null, doReset)) {
        return;
    }
    checkNull(inElev);
    novalue = HMConstants.getNovalue(inElev);
    if (pCells < 3) {
        pCells = 3;
    }
    if (pCells % 2 == 0) {
        pCells = pCells + 1;
    }
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
    int nCols = regionMap.getCols();
    int nRows = regionMap.getRows();
    double xRes = regionMap.getXres();
    double yRes = regionMap.getYres();
    RandomIter elevationIter = CoverageUtilities.getRandomIterator(inElev);
    WritableRaster profWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, novalue);
    WritableRaster planWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, novalue);
    WritableRaster slopeWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, novalue);
    WritableRaster aspectWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, novalue);
    final double[] planProfSlopeAspect = new double[4];
    double disXX = Math.pow(xRes, 2.0);
    double disYY = Math.pow(yRes, 2.0);
    /*
         * calculate curvatures
         */
    pm.beginTask("Processing...", nRows - 2);
    for (int r = 1; r < nRows - 1; r++) {
        if (pm.isCanceled()) {
            return;
        }
        for (int c = 1; c < nCols - 1; c++) {
            calculateCurvatures(elevationIter, novalue, planProfSlopeAspect, nCols, nRows, c, r, xRes, yRes, disXX, disYY, pCells);
            planWR.setSample(c, r, 0, planProfSlopeAspect[0]);
            profWR.setSample(c, r, 0, planProfSlopeAspect[1]);
            slopeWR.setSample(c, r, 0, planProfSlopeAspect[2]);
            aspectWR.setSample(c, r, 0, planProfSlopeAspect[3]);
        }
        pm.worked(1);
    }
    pm.done();
    if (pm.isCanceled()) {
        return;
    }
    CoordinateReferenceSystem crs = inElev.getCoordinateReferenceSystem();
    outProf = CoverageUtilities.buildCoverageWithNovalue("prof_curvature", profWR, regionMap, crs, novalue);
    outPlan = CoverageUtilities.buildCoverageWithNovalue("plan_curvature", planWR, regionMap, crs, novalue);
    outSlope = CoverageUtilities.buildCoverageWithNovalue("slope", slopeWR, regionMap, crs, novalue);
    outAspect = CoverageUtilities.buildCoverageWithNovalue("aspect", aspectWR, regionMap, crs, novalue);
}
Also used : WritableRaster(java.awt.image.WritableRaster) RegionMap(org.hortonmachine.gears.utils.RegionMap) RandomIter(javax.media.jai.iterator.RandomIter) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Execute(oms3.annotations.Execute)

Example 85 with RandomIter

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

the class OmsDrainDir method orlandiniD8LTD.

private void orlandiniD8LTD(int[] indexes, WritableRaster deviationsWR, BitMatrix analizedMatrix, WritableRaster pitWR, WritableRaster flowWR, WritableRaster tcaWR, WritableRaster dirWR, int nelev) {
    /*
         * it indicates the position of the triangle's vertexes
         */
    // ncelle = 0;
    RandomIter pitRandomIter = RandomIterFactory.create(pitWR, null);
    RandomIter flowRandomIter = RandomIterFactory.create(flowWR, null);
    WritableRandomIter tcaRandomIter = RandomIterFactory.createWritable(tcaWR, null);
    WritableRandomIter deviationRandomIter = RandomIterFactory.createWritable(deviationsWR, null);
    WritableRandomIter dirRandomIter = RandomIterFactory.createWritable(dirWR, null);
    try {
        int ncelle = 0;
        pm.beginTask(msg.message("draindir.orlandiniltd"), rows * cols);
        for (int i = rows * cols - 1; i >= 0; i--) {
            if (pm.isCanceled()) {
                return;
            }
            double count = indexes[i] - 1;
            int row = (int) Math.floor(count / cols);
            int col = (int) (count % cols);
            if (!isNovalue(pitRandomIter.getSampleDouble(col, row, 0)) && !isNovalue(flowRandomIter.getSample(col, row, 0))) {
                ncelle = ncelle + 1;
                double[] maxSlopeData = calculateMaximumSlope(analizedMatrix, pitRandomIter, tcaRandomIter, col, row);
                if (maxSlopeData[1] > 0) {
                    double dev1 = (xRes * Math.sin(maxSlopeData[2]));
                    double dev2 = (xRes * Math.sqrt(2.0) * Math.sin(PI / 4 - maxSlopeData[2]));
                    if (maxSlopeData[9] == 1) {
                        dev2 = -dev2;
                    } else {
                        dev1 = -dev1;
                    }
                    calculateDrainageArea(row, col, maxSlopeData, analizedMatrix, deviationRandomIter, pitRandomIter, tcaRandomIter, dirRandomIter, cols, rows);
                    double sumdev = maxSlopeData[6];
                    double sumdev1 = dev1 + pLambda * sumdev;
                    double sumdev2 = dev2 + pLambda * sumdev;
                    if (Math.abs(sumdev1) <= Math.abs(sumdev2) && (maxSlopeData[3] - maxSlopeData[4]) > 0.0) {
                        dirRandomIter.setSample(col, row, 0, (int) maxSlopeData[7]);
                        deviationRandomIter.setSample(col, row, 0, sumdev1);
                    } else if (Math.abs(sumdev1) > Math.abs(sumdev2) || (maxSlopeData[3] - maxSlopeData[5]) > 0.0) {
                        dirRandomIter.setSample(col, row, 0, (int) maxSlopeData[8]);
                        deviationRandomIter.setSample(col, row, 0, sumdev2);
                    } else {
                        break;
                    }
                } else if (maxSlopeData[1] == 0) {
                    if (ncelle == nelev) {
                        /* sono all'uscita */
                        calculateDrainageArea(row, col, maxSlopeData, analizedMatrix, deviationRandomIter, pitRandomIter, tcaRandomIter, dirRandomIter, cols, rows);
                        dirRandomIter.setSample(col, row, 0, FlowNode.OUTLET);
                        deviationRandomIter.setSample(col, row, 0, pLambda * maxSlopeData[6]);
                        pm.done();
                        return;
                    } else {
                        calculateDrainageArea(row, col, maxSlopeData, analizedMatrix, deviationRandomIter, pitRandomIter, tcaRandomIter, dirRandomIter, cols, rows);
                        double sumdev = pLambda * maxSlopeData[6];
                        dirRandomIter.setSample(col, row, 0, flowRandomIter.getSample(col, row, 0));
                        int flow = dirRandomIter.getSample(col, row, 0);
                        int nr = row + order[flow][0];
                        int nc = col + order[flow][1];
                        while (analizedMatrix.isMarked(nc, nr)) {
                            tcaRandomIter.setSample(nc, nr, 0, (tcaRandomIter.getSample(nc, nr, 0) + tcaRandomIter.getSample(col, row, 0)));
                            flow = dirRandomIter.getSample(nc, nr, 0);
                            nr = nr + order[flow][0];
                            nc = nc + order[flow][1];
                        }
                        deviationRandomIter.setSample(col, row, 0, sumdev);
                    }
                }
            }
            pm.worked(1);
        }
        pm.done();
    } finally {
        dirRandomIter.done();
        pitRandomIter.done();
        flowRandomIter.done();
        deviationRandomIter.done();
        tcaRandomIter.done();
    }
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter)

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