Search in sources :

Example 26 with RandomIter

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

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

the class LasHeightDistribution method overlapForPercentage.

private boolean overlapForPercentage(GridCoverage2D cov1, GridCoverage2D cov2, double forPercentage) {
    RandomIter cov1Iter = CoverageUtilities.getRandomIterator(cov1);
    RandomIter cov2Iter = CoverageUtilities.getRandomIterator(cov2);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(cov1);
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    int valid1 = 0;
    int valid2 = 0;
    int overlapping = 0;
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            double v1 = cov1Iter.getSampleDouble(c, r, 0);
            double v2 = cov2Iter.getSampleDouble(c, r, 0);
            if (!isNovalue(v1)) {
                valid1++;
            }
            if (!isNovalue(v2)) {
                valid2++;
            }
            if (!isNovalue(v1) && !isNovalue(v2)) {
                overlapping++;
            }
        }
    }
    cov1Iter.done();
    cov2Iter.done();
    if (overlapping == 0) {
        return false;
    }
    double perc1 = overlapping / valid1;
    if (perc1 > forPercentage) {
        return true;
    }
    double perc2 = overlapping / valid2;
    if (perc2 > forPercentage) {
        return true;
    }
    return false;
}
Also used : RegionMap(org.hortonmachine.gears.utils.RegionMap) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter)

Example 28 with RandomIter

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

the class TestGeomorphon method testLastVisible2.

public void testLastVisible2() throws Exception {
    double[][] mapData = new double[][] { // 
    { 500, 500, 500, 500, 500, 500, 500, 500, 500, 500 }, // 
    { 500, 600, 600, 600, 600, 700, 800, 1100, 1500, 1500 }, // 
    { 500, 600, 700, 600, 600, 700, 800, 1100, 1500, 1500 }, // 
    { 500, 600, 800, 600, 600, 700, 800, 1100, 1500, 1500 }, // 
    { 500, 600, 800, 600, 600, 700, 800, 1100, 1500, 1500 }, // 
    { 500, 600, 700, 600, 600, 700, 800, 1100, 1500, 2500 }, // 
    { 500, 600, 600, 600, 600, 700, 800, 1100, 1500, 1500 }, { 500, 500, 500, 500, 500, 500, 500, 500, 500, 500 } };
    HashMap<String, Double> eP = HMTestMaps.getEnvelopeparams();
    CoordinateReferenceSystem crs = HMTestMaps.getCrs();
    GridCoverage2D inElev = CoverageUtilities.buildCoverage("elevation", mapData, eP, crs, true);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
    RandomIter elevIter = CoverageUtilities.getRandomIterator(inElev);
    GridGeometry2D gridGeometry = inElev.getGridGeometry();
    // up
    DirectPosition startPosition = gridGeometry.gridToWorld(new GridCoordinates2D(0, 3));
    double[] coordinateArray = startPosition.getCoordinate();
    Coordinate startCoord = new Coordinate(coordinateArray[0], coordinateArray[1]);
    DirectPosition endPosition = gridGeometry.gridToWorld(new GridCoordinates2D(9, 5));
    coordinateArray = endPosition.getCoordinate();
    Coordinate endCoord = new Coordinate(coordinateArray[0], coordinateArray[1]);
    ProfilePoint lastVisiblePoint = OmsGeomorphon.getLastVisiblePoint(regionMap, elevIter, gridGeometry, startCoord, endCoord);
    assertEquals(2500.0, lastVisiblePoint.getElevation(), DELTA);
    // down
    startPosition = gridGeometry.gridToWorld(new GridCoordinates2D(9, 5));
    coordinateArray = startPosition.getCoordinate();
    startCoord = new Coordinate(coordinateArray[0], coordinateArray[1]);
    endPosition = gridGeometry.gridToWorld(new GridCoordinates2D(0, 3));
    coordinateArray = endPosition.getCoordinate();
    endCoord = new Coordinate(coordinateArray[0], coordinateArray[1]);
    lastVisiblePoint = OmsGeomorphon.getLastVisiblePoint(regionMap, elevIter, gridGeometry, startCoord, endCoord);
    assertEquals(500.0, lastVisiblePoint.getElevation(), DELTA);
    // middle
    startPosition = gridGeometry.gridToWorld(new GridCoordinates2D(5, 3));
    coordinateArray = startPosition.getCoordinate();
    startCoord = new Coordinate(coordinateArray[0], coordinateArray[1]);
    endPosition = gridGeometry.gridToWorld(new GridCoordinates2D(0, 3));
    coordinateArray = endPosition.getCoordinate();
    endCoord = new Coordinate(coordinateArray[0], coordinateArray[1]);
    lastVisiblePoint = OmsGeomorphon.getLastVisiblePoint(regionMap, elevIter, gridGeometry, startCoord, endCoord);
    assertEquals(800.0, lastVisiblePoint.getElevation(), DELTA);
}
Also used : DirectPosition(org.opengis.geometry.DirectPosition) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) RegionMap(org.hortonmachine.gears.utils.RegionMap) RandomIter(javax.media.jai.iterator.RandomIter) ProfilePoint(org.hortonmachine.gears.utils.coverage.ProfilePoint) Coordinate(org.locationtech.jts.geom.Coordinate) GridCoordinates2D(org.geotools.coverage.grid.GridCoordinates2D) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 29 with RandomIter

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

the class TestScanLineRasterizer method testScanLineRasterizerUtilsMethod.

public void testScanLineRasterizerUtilsMethod() throws Exception {
    RandomIter rasterIter = CoverageUtilities.getRandomIterator(elevationCoverage);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(elevationCoverage);
    List<Coordinate> coordinates = CoverageUtilities.extractPolygonOnCoverage(rasterIter, regionMap.getCols(), regionMap.getRows(), regionMap.getXres(), elevationCoverage.getGridGeometry(), polygon, 0);
    int index = 0;
    for (int r = 0; r < elevationData.length; r++) {
        for (int c = 0; c < elevationData[0].length; c++) {
            Coordinate coordinate = coordinates.get(index++);
            assertEquals(elevationData[r][c], coordinate.z, DELTA);
        }
    }
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) RegionMap(org.hortonmachine.gears.utils.RegionMap) RandomIter(javax.media.jai.iterator.RandomIter)

Example 30 with RandomIter

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

the class OmsKernelDensity method process.

@SuppressWarnings("nls")
@Execute
public void process() throws Exception {
    checkNull(inMap);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inMap);
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    ValueType type = KernelFactory.ValueType.EPANECHNIKOV;
    switch(pKernel) {
        case 0:
            type = KernelFactory.ValueType.BINARY;
            break;
        case 1:
            type = KernelFactory.ValueType.COSINE;
            break;
        case 2:
            type = KernelFactory.ValueType.DISTANCE;
            break;
        case 4:
            type = KernelFactory.ValueType.GAUSSIAN;
            break;
        case 5:
            type = KernelFactory.ValueType.INVERSE_DISTANCE;
            break;
        case 6:
            type = KernelFactory.ValueType.QUARTIC;
            break;
        case 7:
            type = KernelFactory.ValueType.TRIANGULAR;
            break;
        case 8:
            type = KernelFactory.ValueType.TRIWEIGHT;
            break;
    }
    KernelJAI kernel = KernelFactory.createCircle(pRadius, type);
    RenderedImage inImg = inMap.getRenderedImage();
    RandomIter inIter = RandomIterFactory.create(inImg, null);
    WritableRaster outWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, HMConstants.doubleNovalue);
    WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
    float[] kernelData = kernel.getKernelData();
    pm.beginTask("Estimating kernel density...", cols - 2 * pRadius);
    for (int r = pRadius; r < rows - pRadius; r++) {
        for (int c = pRadius; c < cols - pRadius; c++) {
            double inputValue = inIter.getSampleDouble(c, r, 0);
            if (isNovalue(inputValue)) {
                continue;
            }
            if (doConstant)
                inputValue = 1.0;
            int k = 0;
            for (int kr = -pRadius; kr <= pRadius; kr++, k++) {
                for (int kc = -pRadius; kc <= pRadius; kc++) {
                    // data[gridCoords.y + j][gridCoords.x + i] += cdata[k] * centreValue;
                    double outputValue = outIter.getSampleDouble(c + kc, r + kr, 0);
                    if (isNovalue(outputValue)) {
                        outputValue = 0;
                    }
                    outputValue = outputValue + kernelData[k] * inputValue;
                    outIter.setSample(c + kc, r + kr, 0, outputValue);
                }
            }
        }
        pm.worked(1);
    }
    pm.done();
    pm.beginTask("Finalizing...", cols);
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            double outputValue = outIter.getSampleDouble(c, r, 0);
            if (isNovalue(outputValue)) {
                outIter.setSample(c, r, 0, 0.0);
            }
        }
        pm.worked(1);
    }
    pm.done();
    outDensity = CoverageUtilities.buildCoverage("kerneldensity", outWR, regionMap, inMap.getCoordinateReferenceSystem());
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) ValueType(org.jaitools.media.jai.kernel.KernelFactory.ValueType) KernelJAI(javax.media.jai.KernelJAI) WritableRaster(java.awt.image.WritableRaster) RegionMap(org.hortonmachine.gears.utils.RegionMap) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) RenderedImage(java.awt.image.RenderedImage) Execute(oms3.annotations.Execute)

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