Search in sources :

Example 66 with WritableRandomIter

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

the class OmsDePitter method process.

@Execute
public void process() throws Exception {
    checkNull(inElev);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
    cols = regionMap.getCols();
    rows = regionMap.getRows();
    xRes = regionMap.getXres();
    yRes = regionMap.getYres();
    novalue = HMConstants.getNovalue(inElev);
    // output raster
    WritableRaster pitRaster = CoverageUtilities.renderedImage2DoubleWritableRaster(inElev.getRenderedImage(), false);
    WritableRandomIter pitIter = CoverageUtilities.getWritableRandomIterator(pitRaster);
    try {
        ConcurrentLinkedQueue<GridNode> pitsList = getPitsList(cols, rows, xRes, yRes, pitIter);
        AtomicInteger count = new AtomicInteger();
        // int count = 0;
        int iteration = 1;
        while (pitsList.size() > 0) {
            if (pm.isCanceled()) {
                return;
            }
            int pitCount = pitsList.size();
            BitMatrix allPitsPositions = new BitMatrix(cols, rows);
            // List<GridNode> processedNodesInPit = new ArrayList<>();
            int shownCount = pitCount;
            if (!verbose) {
                shownCount = IHMProgressMonitor.UNKNOWN;
            }
            pm.beginTask("Processing " + pitCount + " pits (iteration N." + iteration++ + ")... ", shownCount);
            if (doParallel) {
                pitsList.parallelStream().forEach(originalPitNode -> {
                    int _count = count.incrementAndGet();
                    if (pm.isCanceled()) {
                        return;
                    }
                    processPitNode(originalPitNode, allPitsPositions, _count, pitIter);
                });
            } else {
                pitsList.stream().forEach(originalPitNode -> {
                    int _count = count.incrementAndGet();
                    if (pm.isCanceled()) {
                        return;
                    }
                    processPitNode(originalPitNode, allPitsPositions, _count, pitIter);
                });
            }
            // for( GridNode originalPitNode : pitsList ) {
            // count++;
            // if (pm.isCanceled()) {
            // return;
            // }
            // processPitNode(originalPitNode, allPitsPositions, count, pitIter);
            // }
            pm.done();
            // if (true && iteration < 3) {
            // outPit = CoverageUtilities.buildCoverage("pitfiller", pitRaster, regionMap,
            // inElev.getCoordinateReferenceSystem());
            // OmsRasterWriter.writeRaster("/home/hydrologis/Dropbox/hydrologis/lavori/2017_06_mapzone/test/dtm_test2_pit_"
            // + iteration + ".tiff", outPit);
            // }
            pitsList = getPitsList(cols, rows, xRes, yRes, pitIter, allPitsPositions);
            int size = pitsList.size();
            if (verbose) {
                pm.message("Left pits: " + size);
                pm.message("---------------------------------------------------------------------");
            }
            if (size < 10000) {
                verbose = false;
            }
        }
        outPit = CoverageUtilities.buildCoverageWithNovalue("pitfiller", pitRaster, regionMap, inElev.getCoordinateReferenceSystem(), novalue);
        if (doFlow) {
            WritableRaster flowRaster = CoverageUtilities.createWritableRaster(cols, rows, Short.class, null, null);
            WritableRandomIter flowIter = CoverageUtilities.getWritableRandomIterator(flowRaster);
            try {
                pm.beginTask("Calculating flowdirections...", rows * cols);
                processGrid(cols, rows, false, (c, r) -> {
                    if (pm.isCanceled()) {
                        return;
                    }
                    GridNode node = new GridNode(pitIter, cols, rows, xRes, yRes, c, r, novalue);
                    boolean isValid = node.isValid();
                    if (!isValid || node.touchesBound() || node.touchesNovalue()) {
                        flowIter.setSample(c, r, 0, HMConstants.intNovalue);
                    } else {
                        GridNode nextDown = node.goDownstreamSP();
                        if (nextDown == null) {
                            flowIter.setSample(c, r, 0, HMConstants.intNovalue);
                        } else {
                            int newFlow = node.getFlow();
                            flowIter.setSample(c, r, 0, newFlow);
                        }
                    }
                    pm.worked(1);
                });
                pm.done();
                outFlow = CoverageUtilities.buildCoverageWithNovalue("flow", flowRaster, regionMap, inElev.getCoordinateReferenceSystem(), HMConstants.intNovalue);
            } finally {
                flowIter.done();
            }
        }
    } finally {
        pitIter.done();
    }
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) GridNode(org.hortonmachine.gears.libs.modules.GridNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WritableRaster(java.awt.image.WritableRaster) RegionMap(org.hortonmachine.gears.utils.RegionMap) BitMatrix(org.hortonmachine.gears.utils.BitMatrix) Execute(oms3.annotations.Execute)

Example 67 with WritableRandomIter

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

the class OmsSplitSubbasins method process.

@Execute
public void process() {
    if (!concatOr(outSubbasins == null, doReset)) {
        return;
    }
    checkNull(inFlow, inHack, pHackorder);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inFlow);
    nCols = regionMap.getCols();
    nRows = regionMap.getRows();
    hackOrder = pHackorder;
    int flowNovalue = HMConstants.getIntNovalue(inFlow);
    RenderedImage flowRI = inFlow.getRenderedImage();
    WritableRaster flowWR = CoverageUtilities.renderedImage2WritableRaster(flowRI, true);
    RenderedImage hacksRI = inHack.getRenderedImage();
    WritableRaster hackWR = CoverageUtilities.renderedImage2WritableRaster(hacksRI, true);
    WritableRandomIter flowIter = RandomIterFactory.createWritable(flowWR, null);
    WritableRandomIter hacksIter = RandomIterFactory.createWritable(hackWR, null);
    double outNv = HMConstants.doubleNovalue;
    WritableRaster netImage = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, outNv);
    WritableRandomIter netIter = RandomIterFactory.createWritable(netImage, null);
    net(hacksIter, netIter);
    WritableRaster netNumberWR = netNumber(flowIter, hacksIter, netIter);
    WritableRandomIter netNumberIter = RandomIterFactory.createWritable(netNumberWR, null);
    WritableRaster subbasinWR = ModelsEngine.extractSubbasins(flowIter, flowNovalue, netIter, netNumberIter, nRows, nCols, pm);
    outNetnum = // $NON-NLS-1$
    CoverageUtilities.buildCoverageWithNovalue(// $NON-NLS-1$
    "netnum", // $NON-NLS-1$
    netNumberWR, // $NON-NLS-1$
    regionMap, inFlow.getCoordinateReferenceSystem(), outNv);
    outSubbasins = // $NON-NLS-1$
    CoverageUtilities.buildCoverageWithNovalue(// $NON-NLS-1$
    "subbasins", // $NON-NLS-1$
    subbasinWR, // $NON-NLS-1$
    regionMap, inFlow.getCoordinateReferenceSystem(), outNv);
}
Also used : 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 68 with WritableRandomIter

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

the class OmsSplitSubbasins method netNumber.

private WritableRaster netNumber(RandomIter flowIter, RandomIter hacksIter, RandomIter netIter) {
    int drainingPixelNum = 0;
    int[] flowColRow = new int[2];
    WritableRaster netNumberingImage = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, 0.0);
    WritableRandomIter netNumberRandomIter = RandomIterFactory.createWritable(netNumberingImage, null);
    int n = 0;
    pm.beginTask("Numbering network...", nRows);
    for (int r = 0; r < nRows; r++) {
        for (int c = 0; c < nCols; c++) {
            flowColRow[0] = c;
            flowColRow[1] = r;
            if (!isNovalue(netIter.getSampleDouble(c, r, 0)) && flowIter.getSampleDouble(c, r, 0) != 10.0 && NumericsUtilities.dEq(netNumberRandomIter.getSampleDouble(c, r, 0), 0.0)) {
                boolean isSource = true;
                for (int k = 1; k <= 8; k++) {
                    boolean isDraining = flowIter.getSampleDouble(flowColRow[0] + dir[k][1], flowColRow[1] + dir[k][0], 0) == dir[k][2];
                    boolean isOnNet = !isNovalue(netIter.getSampleDouble(flowColRow[0] + dir[k][1], flowColRow[1] + dir[k][0], 0));
                    if (isDraining && isOnNet) {
                        isSource = false;
                        break;
                    }
                }
                /*
                     * if it is source pixel, go down
                     */
                if (isSource) {
                    n++;
                    netNumberRandomIter.setSample(c, r, 0, n);
                    if (!ModelsEngine.go_downstream(flowColRow, flowIter.getSampleDouble(flowColRow[0], flowColRow[1], 0)))
                        throw new ModelsIllegalargumentException("go_downstream failure...", this, pm);
                    /*
                         * while it is on the network, go downstream
                         */
                    while (!isNovalue(flowIter.getSampleDouble(flowColRow[0], flowColRow[1], 0)) && netNumberRandomIter.getSampleDouble(flowColRow[0], flowColRow[1], 0) == 0) {
                        /*
                             * calculate how many pixels drain into the current pixel.
                             */
                        drainingPixelNum = 0;
                        for (int k = 1; k <= 8; k++) {
                            if (!isNovalue(netIter.getSampleDouble(flowColRow[0] + dir[k][1], flowColRow[1] + dir[k][0], 0)) && flowIter.getSampleDouble(flowColRow[0] + dir[k][1], flowColRow[1] + dir[k][0], 0) == dir[k][2]) {
                                drainingPixelNum++;
                            }
                        }
                        if (drainingPixelNum > 1) {
                            n++;
                        }
                        netNumberRandomIter.setSample(flowColRow[0], flowColRow[1], 0, n);
                        if (!ModelsEngine.go_downstream(flowColRow, flowIter.getSampleDouble(flowColRow[0], flowColRow[1], 0)))
                            throw new ModelsIllegalargumentException("go_downstream failure...", this, pm);
                    }
                }
            }
        }
        pm.worked(1);
    }
    pm.done();
    return netNumberingImage;
}
Also used : ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster)

Example 69 with WritableRandomIter

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

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

the class OmsAspect method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outAspect == null, doReset)) {
        return;
    }
    checkNull(inElev);
    if (doRadiants) {
        radtodeg = 1.0;
    }
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    WritableRaster aspectWR;
    if (doRound) {
        aspectWR = CoverageUtilities.createWritableRaster(cols, rows, Short.class, null, null);
    } else {
        aspectWR = CoverageUtilities.createWritableRaster(cols, rows, Float.class, null, null);
    }
    WritableRandomIter aspectIter = RandomIterFactory.createWritable(aspectWR, null);
    try {
        pm.beginTask(msg.message("aspect.calculating"), rows * cols);
        processGridNodes(inElev, gridNode -> {
            if (pm.isCanceled()) {
                return;
            }
            double aspect = calculateAspect(gridNode, radtodeg, doRound);
            int col = gridNode.col;
            int row = gridNode.row;
            if (col == 0 || row == 0 || col == cols - 1 || row == rows - 1) {
                aspectIter.setSample(col, row, 0, HMConstants.shortNovalue);
            } else {
                if (doRound) {
                    aspectIter.setSample(col, row, 0, (short) aspect);
                } else {
                    aspectIter.setSample(col, row, 0, aspect);
                }
            }
            pm.worked(1);
        });
        pm.done();
    } finally {
        aspectIter.done();
    }
    outAspect = CoverageUtilities.buildCoverage("aspect", aspectWR, regionMap, inElev.getCoordinateReferenceSystem());
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster) RegionMap(org.hortonmachine.gears.utils.RegionMap) 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