Search in sources :

Example 1 with DummyProgressMonitor

use of org.hortonmachine.gears.libs.monitor.DummyProgressMonitor in project hortonmachine by TheHortonMachine.

the class OmsRasterSummary method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outMin == null, doReset)) {
        return;
    }
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
    if (printToConsole) {
        pm.message("Bounds and resolution");
        pm.message("---------------------");
        pm.message(regionMap.toStringJGT());
        pm.message("");
        pm.message("Coordinate Reference System");
        pm.message("---------------------------");
        pm.message(inRaster.getCoordinateReferenceSystem().toWKT());
        pm.message("");
    }
    // TODO use the geotools bridge instead of jaitools:
    // http://svn.osgeo.org/geotools/trunk/modules/library/coverage/src/test/java/org/geotools/coverage/processing/operation/ZonalStasTest.java
    RenderedImage inRI = inRaster.getRenderedImage();
    Polygon regionPolygon = CoverageUtilities.getRegionPolygon(inRaster);
    SimpleFeatureCollection regionFC = FeatureUtilities.featureCollectionFromGeometry(inRaster.getCoordinateReferenceSystem(), regionPolygon);
    OmsZonalStats zs = new OmsZonalStats();
    zs.pm = new DummyProgressMonitor();
    zs.inRaster = inRaster;
    zs.inVector = regionFC;
    zs.pPercentageThres = 0;
    zs.process();
    SimpleFeatureCollection outVector = zs.outVector;
    List<SimpleFeature> testList = FeatureUtilities.featureCollectionToList(outVector);
    SimpleFeature feature = testList.get(0);
    if (stats == null) {
        stats = new String[] { Variables.MIN, Variables.MAX, Variables.AVG, Variables.SDEV, Variables.VAR, Variables.SUM };
    }
    for (String statName : stats) {
        Object attribute = feature.getAttribute(statName);
        if (attribute != null) {
            switch(statName) {
                case Variables.MIN:
                    outMin = (Double) attribute;
                    break;
                case Variables.MAX:
                    outMax = (Double) attribute;
                    break;
                case Variables.AVG:
                    outMean = (Double) attribute;
                    break;
                case Variables.SDEV:
                    outSdev = (Double) attribute;
                    break;
                case Variables.SUM:
                    outSum = (Double) attribute;
                    break;
                default:
                    break;
            }
        }
    }
    if (outMin != null && outMax != null) {
        outRange = outMax - outMin;
    }
    if (!doHistogram)
        return;
    double[][] cb = new CoupledFieldsMoments().process(inRI, null, pBins, 1, 2, pm, 1);
    int width = inRI.getWidth();
    int height = inRI.getHeight();
    int pixelsNum = width * height;
    outCb = new double[cb.length + 1][3];
    double sum = 0;
    for (int i = 0; i < outCb.length; i++) {
        if (i < outCb.length - 1) {
            outCb[i][0] = cb[i][0];
            outCb[i][1] = cb[i][1];
            sum = sum + cb[i][1];
            outCb[i][2] = cb[i][1] * 100.0 / pixelsNum;
        } else {
            outCb[i][0] = HMConstants.doubleNovalue;
            double nans = pixelsNum - sum;
            outCb[i][1] = nans;
            outCb[i][2] = nans * 100.0 / pixelsNum;
        }
    }
}
Also used : RegionMap(org.hortonmachine.gears.utils.RegionMap) SimpleFeature(org.opengis.feature.simple.SimpleFeature) CoupledFieldsMoments(org.hortonmachine.gears.utils.math.CoupledFieldsMoments) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) DummyProgressMonitor(org.hortonmachine.gears.libs.monitor.DummyProgressMonitor) RenderedImage(java.awt.image.RenderedImage) Polygon(org.locationtech.jts.geom.Polygon) Execute(oms3.annotations.Execute)

Example 2 with DummyProgressMonitor

use of org.hortonmachine.gears.libs.monitor.DummyProgressMonitor 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 3 with DummyProgressMonitor

use of org.hortonmachine.gears.libs.monitor.DummyProgressMonitor in project hortonmachine by TheHortonMachine.

the class GeometryUtilities method sortHorizontal.

public static void sortHorizontal(Coordinate[] coordinates) {
    QuickSortAlgorithm sorter = new QuickSortAlgorithm(new DummyProgressMonitor());
    double[] x = new double[coordinates.length];
    double[] y = new double[coordinates.length];
    for (int i = 0; i < x.length; i++) {
        x[i] = coordinates[i].x;
        y[i] = coordinates[i].y;
    }
    sorter.sort(x, y);
    for (int i = 0; i < x.length; i++) {
        coordinates[i].x = x[i];
        coordinates[i].y = y[i];
    }
}
Also used : DummyProgressMonitor(org.hortonmachine.gears.libs.monitor.DummyProgressMonitor) QuickSortAlgorithm(org.hortonmachine.gears.utils.sorting.QuickSortAlgorithm) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint)

Example 4 with DummyProgressMonitor

use of org.hortonmachine.gears.libs.monitor.DummyProgressMonitor in project hortonmachine by TheHortonMachine.

the class HM method makeSldStyleForRaster.

public static void makeSldStyleForRaster(String tableName, String rasterPath, double opacity) throws Exception {
    RasterSummary s = new RasterSummary();
    s.pm = new DummyProgressMonitor();
    s.inRaster = rasterPath;
    s.process();
    double min = s.outMin;
    double max = s.outMax;
    String style = RasterStyleUtilities.styleToString(RasterStyleUtilities.createStyleForColortable(tableName, min, max, opacity));
    File styleFile = FileUtilities.substituteExtention(new File(rasterPath), "sld");
    FileUtilities.writeFile(style, styleFile);
}
Also used : RasterSummary(org.hortonmachine.modules.RasterSummary) DummyProgressMonitor(org.hortonmachine.gears.libs.monitor.DummyProgressMonitor) LineString(org.locationtech.jts.geom.LineString) File(java.io.File)

Example 5 with DummyProgressMonitor

use of org.hortonmachine.gears.libs.monitor.DummyProgressMonitor in project hortonmachine by TheHortonMachine.

the class HM method makeQgisStyleForRaster.

public static void makeQgisStyleForRaster(String tableName, String rasterPath, int labelDecimals) throws Exception {
    RasterSummary s = new RasterSummary();
    s.pm = new DummyProgressMonitor();
    s.inRaster = rasterPath;
    s.process();
    double min = s.outMin;
    double max = s.outMax;
    String style = RasterStyleUtilities.createQGISRasterStyle(tableName, min, max, null, labelDecimals);
    File styleFile = FileUtilities.substituteExtention(new File(rasterPath), "qml");
    FileUtilities.writeFile(style, styleFile);
}
Also used : RasterSummary(org.hortonmachine.modules.RasterSummary) DummyProgressMonitor(org.hortonmachine.gears.libs.monitor.DummyProgressMonitor) LineString(org.locationtech.jts.geom.LineString) File(java.io.File)

Aggregations

DummyProgressMonitor (org.hortonmachine.gears.libs.monitor.DummyProgressMonitor)16 GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)7 RegionMap (org.hortonmachine.gears.utils.RegionMap)6 File (java.io.File)4 RandomIter (javax.media.jai.iterator.RandomIter)4 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)4 WritableRandomIter (javax.media.jai.iterator.WritableRandomIter)3 Execute (oms3.annotations.Execute)3 GridGeometry2D (org.geotools.coverage.grid.GridGeometry2D)3 Coordinate (org.locationtech.jts.geom.Coordinate)3 Geometry (org.locationtech.jts.geom.Geometry)3 SimpleFeature (org.opengis.feature.simple.SimpleFeature)3 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)3 Point (java.awt.Point)2 Point2D (java.awt.geom.Point2D)2 Raster (java.awt.image.Raster)2 RenderedImage (java.awt.image.RenderedImage)2 WritableRaster (java.awt.image.WritableRaster)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2