Search in sources :

Example 1 with OmsRasterDiff

use of org.hortonmachine.gears.modules.r.rasterdiff.OmsRasterDiff in project hortonmachine by TheHortonMachine.

the class OmsGeomorphonMaximaFinder method process.

@Execute
public void process() throws Exception {
    checkNull(inDTM, inDSM);
    GridGeometry2D gridGeometry = inDSM.getGridGeometry();
    DummyProgressMonitor pm = new DummyProgressMonitor();
    OmsGeomorphon g = new OmsGeomorphon();
    g.pm = pm;
    g.inElev = inDSM;
    g.pRadius = pRadius;
    g.pThreshold = pThreshold;
    g.process();
    GridCoverage2D geomorphonGC = g.outRaster;
    OmsRasterDiff rasterDiff = new OmsRasterDiff();
    rasterDiff.inRaster1 = inDSM;
    rasterDiff.inRaster2 = inDTM;
    rasterDiff.pm = pm;
    rasterDiff.process();
    GridCoverage2D dsmDtmThresDiff = rasterDiff.outRaster;
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inDSM);
    int rows = regionMap.getRows();
    int cols = regionMap.getCols();
    TreeSet<GridNode> topNodes = new TreeSet<GridNode>(new GridNodePositionComparator());
    peakCode = GeomorphonClassification.PEAK.getCode();
    hollowCode = GeomorphonClassification.HOLLOW.getCode();
    valleyCode = GeomorphonClassification.VALLEY.getCode();
    pitCode = GeomorphonClassification.PIT.getCode();
    spurCode = GeomorphonClassification.SPUR.getCode();
    double geomorphNv = HMConstants.getNovalue(geomorphonGC);
    double elevNv = HMConstants.getNovalue(dsmDtmThresDiff);
    RandomIter geomorphIter = CoverageUtilities.getRandomIterator(geomorphonGC);
    RandomIter elevIter = CoverageUtilities.getRandomIterator(dsmDtmThresDiff);
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            GridNode geomorphNode = new GridNode(geomorphIter, cols, rows, 1, -1, c, r, geomorphNv);
            GridNode elevNode = new GridNode(elevIter, cols, rows, 1, -1, c, r, elevNv);
            if (geomorphNode.elevation == peakCode && !elevNode.touchesBound()) {
                // found peak
                boolean isLocalMaxima = true;
                TreeSet<GridNode> peakNodes = new TreeSet<GridNode>(new GridNodePositionComparator());
                peakNodes.add(geomorphNode);
                gatherNodes(peakNodes, geomorphNode);
                if (peakNodes.size() == 1) {
                    GridNode topNode = peakNodes.first();
                    GridNode topElevNode = new GridNode(elevIter, cols, rows, 1, -1, topNode.col, topNode.row, elevNv);
                    List<GridNode> validSurroundingNodes = topElevNode.getValidSurroundingNodes();
                    if (validSurroundingNodes.size() < 6) {
                        // no more than 2 invalid permitted
                        isLocalMaxima = false;
                    } else {
                        if (!analyzeNeighbors(topNode)) {
                            isLocalMaxima = false;
                        }
                    }
                }
                GridNode topNode = null;
                if (isLocalMaxima) {
                    double maxElev = Double.NEGATIVE_INFINITY;
                    for (GridNode peakNode : peakNodes) {
                        double elev = peakNode.getValueFromMap(elevIter);
                        if (elev > maxElev) {
                            maxElev = elev;
                            topNode = peakNode;
                        }
                    }
                    if (topNode != null) {
                        // check
                        GridNode topElevNode = new GridNode(elevIter, cols, rows, 1, -1, topNode.col, topNode.row, elevNv);
                        double[][] windowValues = topElevNode.getWindow(3, false);
                        double min = Double.POSITIVE_INFINITY;
                        double max = Double.NEGATIVE_INFINITY;
                        for (double[] windowRow : windowValues) {
                            for (double windowValue : windowRow) {
                                if (HMConstants.isNovalue(windowValue)) {
                                    isLocalMaxima = false;
                                    break;
                                } else {
                                    min = Math.min(min, windowValue);
                                    max = Math.max(max, windowValue);
                                }
                            }
                            if (!isLocalMaxima) {
                                break;
                            }
                        }
                        if (max - min > pElevDiffThres) {
                            isLocalMaxima = false;
                        }
                    }
                }
                if (isLocalMaxima && topNode != null) {
                    topNodes.add(topNode);
                }
            }
        }
    }
    outMaxima = new DefaultFeatureCollection();
    SimpleFeatureBuilder builder = getOutBuilder();
    int id = 0;
    for (GridNode topNode : topNodes) {
        Coordinate coordinate = CoverageUtilities.coordinateFromColRow(topNode.col, topNode.row, gridGeometry);
        Point point = gf.createPoint(coordinate);
        double elev = topNode.getValueFromMap(elevIter);
        Object[] values = new Object[] { point, id++, elev };
        try {
            builder.addAll(values);
        } catch (Exception e) {
            e.printStackTrace();
        }
        SimpleFeature newFeature = builder.buildFeature(null);
        ((DefaultFeatureCollection) outMaxima).add(newFeature);
    }
    geomorphIter.done();
    elevIter.done();
    int size = outMaxima.size();
    if (size == 0) {
        pm.message("No tops extracted...");
    } else {
        pm.message("Extracted tops =  " + outMaxima.size());
    }
}
Also used : GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) GridNode(org.hortonmachine.gears.libs.modules.GridNode) RegionMap(org.hortonmachine.gears.utils.RegionMap) OmsRasterDiff(org.hortonmachine.gears.modules.r.rasterdiff.OmsRasterDiff) GridNodePositionComparator(org.hortonmachine.gears.libs.modules.GridNodePositionComparator) TreeSet(java.util.TreeSet) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) RandomIter(javax.media.jai.iterator.RandomIter) Point(org.locationtech.jts.geom.Point) Point(org.locationtech.jts.geom.Point) SimpleFeature(org.opengis.feature.simple.SimpleFeature) OmsGeomorphon(org.hortonmachine.hmachine.modules.geomorphology.geomorphon.OmsGeomorphon) Coordinate(org.locationtech.jts.geom.Coordinate) DummyProgressMonitor(org.hortonmachine.gears.libs.monitor.DummyProgressMonitor) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Execute(oms3.annotations.Execute)

Example 2 with OmsRasterDiff

use of org.hortonmachine.gears.modules.r.rasterdiff.OmsRasterDiff in project hortonmachine by TheHortonMachine.

the class TestRasterDiff method testDiff.

public void testDiff() throws Exception {
    OmsRasterDiff cutout = new OmsRasterDiff();
    cutout.pm = pm;
    cutout.inRaster1 = inRaster1;
    cutout.inRaster2 = inRaster2;
    cutout.process();
    GridCoverage2D out = cutout.outRaster;
    int[][] expected = new int[][] { // 
    { intNaN, intNaN, intNaN, intNaN, intNaN, intNaN, intNaN, intNaN, intNaN, intNaN }, // 
    { intNaN, intNaN, 0, 0, 0, 0, 0, 0, 0, intNaN }, // 
    { intNaN, 0, 0, 0, 0, 0, 0, 0, 0, intNaN }, // 
    { intNaN, 0, 0, 0, 0, 0, 0, 0, 0, intNaN }, // 
    { intNaN, 0, 0, 0, 0, 0, 0, 0, 0, intNaN }, // 
    { intNaN, 0, 0, 0, 0, 0, 0, 0, 0, intNaN }, // 
    { intNaN, 0, 0, 0, 0, 0, 0, 0, 0, intNaN }, { intNaN, intNaN, intNaN, intNaN, intNaN, intNaN, intNaN, intNaN, intNaN, intNaN } };
    checkMatrixEqual(out.getRenderedImage(), expected, 0);
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) OmsRasterDiff(org.hortonmachine.gears.modules.r.rasterdiff.OmsRasterDiff)

Example 3 with OmsRasterDiff

use of org.hortonmachine.gears.modules.r.rasterdiff.OmsRasterDiff in project hortonmachine by TheHortonMachine.

the class TestRasterDiff method testDiffWithThres.

public void testDiffWithThres() throws Exception {
    OmsRasterDiff cutout = new OmsRasterDiff();
    cutout.pm = pm;
    cutout.inRaster1 = inRaster1;
    cutout.inRaster2 = inRaster2;
    cutout.pThreshold = 0.0;
    cutout.process();
    GridCoverage2D out = cutout.outRaster;
    int[][] expected = new int[][] { // 
    { intNaN, intNaN, intNaN, intNaN, intNaN, intNaN, intNaN, intNaN, intNaN, intNaN }, // 
    { intNaN, intNaN, 0, 0, 0, 0, 0, 0, 0, intNaN }, // 
    { intNaN, 0, 0, 0, 0, 0, 0, 0, 0, intNaN }, // 
    { intNaN, 0, 0, 0, 0, 0, 0, 0, 0, intNaN }, // 
    { intNaN, 0, 0, 0, 0, 0, 0, 0, 0, intNaN }, // 
    { intNaN, 0, 0, 0, 0, 0, 0, 0, 0, intNaN }, // 
    { intNaN, 0, 0, 0, 0, 0, 0, 0, 0, intNaN }, { intNaN, intNaN, intNaN, intNaN, intNaN, intNaN, intNaN, intNaN, intNaN, intNaN } };
    checkMatrixEqual(out.getRenderedImage(), expected, 0);
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) OmsRasterDiff(org.hortonmachine.gears.modules.r.rasterdiff.OmsRasterDiff)

Example 4 with OmsRasterDiff

use of org.hortonmachine.gears.modules.r.rasterdiff.OmsRasterDiff in project hortonmachine by TheHortonMachine.

the class OmsLW10_CHM_AreaToNetpointAssociator method getChm.

/*
    * extract the Canopy Height Model from DTM and DSM
    */
private GridCoverage2D getChm(GridCoverage2D inDsm, GridCoverage2D inDtm) throws Exception {
    OmsRasterDiff rasterDiff = new OmsRasterDiff();
    rasterDiff.pm = pm;
    rasterDiff.inRaster1 = inDsm;
    rasterDiff.inRaster2 = inDtm;
    rasterDiff.pThreshold = 0.0;
    rasterDiff.doNegatives = false;
    rasterDiff.process();
    GridCoverage2D out = rasterDiff.outRaster;
    return out;
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) OmsRasterDiff(org.hortonmachine.gears.modules.r.rasterdiff.OmsRasterDiff)

Example 5 with OmsRasterDiff

use of org.hortonmachine.gears.modules.r.rasterdiff.OmsRasterDiff in project hortonmachine by TheHortonMachine.

the class RasterDiff method process.

@Execute
public void process() throws Exception {
    OmsRasterDiff rasterdiff = new OmsRasterDiff();
    rasterdiff.inRaster1 = getRaster(inRaster1);
    rasterdiff.inRaster2 = getRaster(inRaster2);
    rasterdiff.pThreshold = pThreshold;
    rasterdiff.doNegatives = doNegatives;
    rasterdiff.pm = pm;
    rasterdiff.doProcess = doProcess;
    rasterdiff.doReset = doReset;
    rasterdiff.process();
    dumpRaster(rasterdiff.outRaster, outRaster);
}
Also used : OmsRasterDiff(org.hortonmachine.gears.modules.r.rasterdiff.OmsRasterDiff) Execute(oms3.annotations.Execute)

Aggregations

OmsRasterDiff (org.hortonmachine.gears.modules.r.rasterdiff.OmsRasterDiff)5 GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)4 Execute (oms3.annotations.Execute)2 TreeSet (java.util.TreeSet)1 RandomIter (javax.media.jai.iterator.RandomIter)1 GridGeometry2D (org.geotools.coverage.grid.GridGeometry2D)1 DefaultFeatureCollection (org.geotools.feature.DefaultFeatureCollection)1 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)1 GridNode (org.hortonmachine.gears.libs.modules.GridNode)1 GridNodePositionComparator (org.hortonmachine.gears.libs.modules.GridNodePositionComparator)1 DummyProgressMonitor (org.hortonmachine.gears.libs.monitor.DummyProgressMonitor)1 RegionMap (org.hortonmachine.gears.utils.RegionMap)1 OmsGeomorphon (org.hortonmachine.hmachine.modules.geomorphology.geomorphon.OmsGeomorphon)1 Coordinate (org.locationtech.jts.geom.Coordinate)1 Point (org.locationtech.jts.geom.Point)1 SimpleFeature (org.opengis.feature.simple.SimpleFeature)1