Search in sources :

Example 1 with GridNodeElevationToLeastComparator

use of org.hortonmachine.gears.libs.modules.GridNodeElevationToLeastComparator in project hortonmachine by TheHortonMachine.

the class TestFlowUtils method testElevationSort.

public void testElevationSort() throws Exception {
    TreeSet<GridNode> set = new TreeSet<GridNode>(new GridNodeElevationToLeastComparator());
    for (int r = 0; r < nRows; r++) {
        for (int c = 0; c < nCols; c++) {
            GridNode node = new GridNode(elevationIter, nCols, nRows, xRes, yRes, c, r, NaN);
            if (node.isValid()) {
                boolean added = set.add(node);
                assertTrue(added);
            }
        }
    }
    GridNode first = set.first();
    assertEquals(first.col, 0);
    assertEquals(first.row, 3);
    GridNode last = set.last();
    assertEquals(last.col, 9);
    assertEquals(last.row, 0);
}
Also used : GridNode(org.hortonmachine.gears.libs.modules.GridNode) TreeSet(java.util.TreeSet) GridNodeElevationToLeastComparator(org.hortonmachine.gears.libs.modules.GridNodeElevationToLeastComparator)

Example 2 with GridNodeElevationToLeastComparator

use of org.hortonmachine.gears.libs.modules.GridNodeElevationToLeastComparator in project hortonmachine by TheHortonMachine.

the class OmsLeastCostFlowDirections method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outFlow == null, doReset)) {
        return;
    }
    checkNull(inElev);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
    cols = regionMap.getCols();
    rows = regionMap.getRows();
    double xRes = regionMap.getXres();
    double yRes = regionMap.getYres();
    RandomIter elevationIter = CoverageUtilities.getRandomIterator(inElev);
    WritableRaster flowWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
    flowIter = CoverageUtilities.getWritableRandomIterator(flowWR);
    WritableRaster tcaWR = null;
    if (doTca) {
        tcaWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
        tcaIter = CoverageUtilities.getWritableRandomIterator(tcaWR);
    }
    WritableRaster slopeWR = null;
    if (doSlope) {
        slopeWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
        slopeIter = CoverageUtilities.getWritableRandomIterator(slopeWR);
    }
    WritableRaster aspectWR = null;
    if (doAspect) {
        aspectWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
        aspectIter = CoverageUtilities.getWritableRandomIterator(aspectWR);
    }
    orderedNodes = new TreeSet<GridNode>(new GridNodeElevationToLeastComparator());
    assignedFlowsMap = new BitMatrix(cols, rows);
    double novalue = HMConstants.getNovalue(inElev);
    pm.beginTask("Check for potential outlets...", rows);
    int nonValidCellsNum = 0;
    for (int r = 0; r < rows; r++) {
        if (pm.isCanceled()) {
            return;
        }
        for (int c = 0; c < cols; c++) {
            GridNode node = new GridNode(elevationIter, cols, rows, xRes, yRes, c, r, novalue);
            if (!node.isValid()) {
                nonValidCellsNum++;
                assignedFlowsMap.mark(c, r);
                continue;
            }
            if (node.touchesBound()) {
                orderedNodes.add(node);
                if (doExcludeBorder) {
                    assignedFlowsMap.mark(c, r);
                } else {
                    flowIter.setSample(c, r, 0, Direction.getOutletValue());
                }
            }
        }
        pm.worked(1);
    }
    pm.done();
    pm.beginTask("Extract flowdirections...", (rows * cols - nonValidCellsNum));
    GridNode lowestNode = null;
    while ((lowestNode = orderedNodes.pollFirst()) != null) {
        /*
             * set the current cell as marked. If it is an alone one,
             * it will stay put as an outlet (if we do not mark it, it 
             * might get overwritten. Else il will be redundantly set 
             * later again.
             */
        assignedFlowsMap.mark(lowestNode.col, lowestNode.row);
        List<GridNode> surroundingNodes = lowestNode.getSurroundingNodes();
        /*
             * vertical and horiz cells, if they exist, are 
             * set to flow inside the current cell and added to the 
             * list of cells to process.
             */
        GridNode e = surroundingNodes.get(0);
        if (nodeOk(e)) {
            // flow in current and get added to the list of nodes to process by elevation
            // order
            setNodeValues(e, E.getEnteringFlow());
        }
        GridNode n = surroundingNodes.get(2);
        if (nodeOk(n)) {
            setNodeValues(n, N.getEnteringFlow());
        }
        GridNode w = surroundingNodes.get(4);
        if (nodeOk(w)) {
            setNodeValues(w, W.getEnteringFlow());
        }
        GridNode s = surroundingNodes.get(6);
        if (nodeOk(s)) {
            setNodeValues(s, S.getEnteringFlow());
        }
        /*
             * diagonal cells are processed only if they are valid and 
             * they are not steeper than their attached vertical and horiz cells.
             */
        GridNode en = surroundingNodes.get(1);
        if (nodeOk(en) && assignFlowDirection(lowestNode, en, e, n)) {
            setNodeValues(en, EN.getEnteringFlow());
        }
        GridNode nw = surroundingNodes.get(3);
        if (nodeOk(nw) && assignFlowDirection(lowestNode, nw, n, w)) {
            setNodeValues(nw, NW.getEnteringFlow());
        }
        GridNode ws = surroundingNodes.get(5);
        if (nodeOk(ws) && assignFlowDirection(lowestNode, ws, w, s)) {
            setNodeValues(ws, WS.getEnteringFlow());
        }
        GridNode se = surroundingNodes.get(7);
        if (nodeOk(se) && assignFlowDirection(lowestNode, se, s, e)) {
            setNodeValues(se, SE.getEnteringFlow());
        }
    }
    pm.done();
    CoordinateReferenceSystem crs = inElev.getCoordinateReferenceSystem();
    outFlow = CoverageUtilities.buildCoverageWithNovalue("flowdirections", flowWR, regionMap, crs, doubleNovalue);
    if (doTca)
        outTca = CoverageUtilities.buildCoverageWithNovalue("tca", tcaWR, regionMap, crs, doubleNovalue);
    if (doSlope)
        outSlope = CoverageUtilities.buildCoverageWithNovalue("slope", slopeWR, regionMap, crs, doubleNovalue);
    if (doAspect)
        outAspect = CoverageUtilities.buildCoverageWithNovalue("aspect", aspectWR, regionMap, crs, doubleNovalue);
}
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) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) BitMatrix(org.hortonmachine.gears.utils.BitMatrix) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GridNodeElevationToLeastComparator(org.hortonmachine.gears.libs.modules.GridNodeElevationToLeastComparator) Execute(oms3.annotations.Execute)

Aggregations

GridNode (org.hortonmachine.gears.libs.modules.GridNode)2 GridNodeElevationToLeastComparator (org.hortonmachine.gears.libs.modules.GridNodeElevationToLeastComparator)2 WritableRaster (java.awt.image.WritableRaster)1 TreeSet (java.util.TreeSet)1 RandomIter (javax.media.jai.iterator.RandomIter)1 WritableRandomIter (javax.media.jai.iterator.WritableRandomIter)1 Execute (oms3.annotations.Execute)1 BitMatrix (org.hortonmachine.gears.utils.BitMatrix)1 RegionMap (org.hortonmachine.gears.utils.RegionMap)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1