Search in sources :

Example 21 with WritableRandomIter

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

the class OmsTca3d method area3d.

private WritableRaster area3d(WritableRaster pitImage, WritableRaster flowImage, WritableRaster tca3dImage) {
    int[][] tri = { { 0, 0 }, { 1, 2 }, /* tri 012 */
    { 3, 2 }, /* tri 023 */
    { 3, 4 }, /* tri 034 |4|3|2| */
    { 5, 4 }, /* tri 045 |5|0|1| */
    { 5, 6 }, /* tri 056 |6|7|8| */
    { 7, 6 }, /* tri 067 */
    { 7, 8 }, /* tri 078 */
    { 1, 8 } /* tri 089 */
    };
    int[][] dir = ModelsSupporter.DIR_WITHFLOW_EXITING_INVERTED;
    int nnov = 0;
    double dx = xRes;
    double dy = yRes;
    double semiptr = 0.0, area = 0.0, areamed = 0.0;
    // areatr contains areas of 8 triangles having vertex in the 8 pixel
    // around
    double[] grid = new double[11];
    // grid contains the dimension of pixels according with flow directions
    grid[0] = grid[9] = grid[10] = 0;
    grid[1] = grid[5] = abs(dx);
    grid[3] = grid[7] = abs(dy);
    grid[2] = grid[4] = grid[6] = grid[8] = sqrt(dx * dx + dy * dy);
    // contains the triangle's side
    double[] latitr = new double[3];
    // per ogni lato del triangolo contiene il dislivello e la distanza
    // planimetrica
    double[][] dzdiff = new double[3][2];
    RandomIter pitIter = RandomIterFactory.create(pitImage, null);
    WritableRandomIter tca3dIter = RandomIterFactory.createWritable(tca3dImage, null);
    // $NON-NLS-1$
    pm.beginTask(msg.message("tca3d.woringon"), rows - 2);
    for (int j = 1; j < rows - 1; j++) {
        for (int i = 1; i < cols - 1; i++) {
            double pitAtIJ = pitIter.getSampleDouble(i, j, 0);
            nnov = 0;
            area = 0;
            areamed = 0;
            final double[] areatr = new double[9];
            if (!isNovalue(pitAtIJ)) {
                // calculates the area of the triangle
                for (int k = 1; k <= 8; k++) {
                    double pitAtK0 = pitIter.getSampleDouble(i + dir[tri[k][0]][0], j + dir[tri[k][0]][1], 0);
                    double pitAtK1 = pitIter.getSampleDouble(i + dir[tri[k][1]][0], j + dir[tri[k][1]][1], 0);
                    if (!isNovalue(pitAtK0) && !isNovalue(pitAtK1)) {
                        nnov++;
                        // calcola per ogni lato del triangolo in dislivello
                        // e la distanza planimetrica tra i pixel
                        // considerati.
                        dzdiff[0][0] = abs(pitAtIJ - pitAtK0);
                        dzdiff[0][1] = grid[dir[tri[k][0]][2]];
                        dzdiff[1][0] = abs(pitAtIJ - pitAtK1);
                        dzdiff[1][1] = grid[dir[tri[k][1]][2]];
                        dzdiff[2][0] = abs(pitAtK0 - pitAtK1);
                        dzdiff[2][1] = grid[1];
                        // calcola i lati del tringolo considerato
                        latitr[0] = sqrt(pow(dzdiff[0][0], 2) + pow(dzdiff[0][1], 2));
                        latitr[1] = sqrt(pow(dzdiff[1][0], 2) + pow(dzdiff[1][1], 2));
                        latitr[2] = sqrt(pow(dzdiff[2][0], 2) + pow(dzdiff[2][1], 2));
                        // calcola il semiperimetro del triangolo
                        semiptr = 0.5 * (latitr[0] + latitr[1] + latitr[2]);
                        // calcola l'area di ciascun triangolo
                        areatr[k] = sqrt(semiptr * (semiptr - latitr[0]) * (semiptr - latitr[1]) * (semiptr - latitr[2]));
                    }
                }
                if (nnov == 8) // calcolo l'area del pixel sommando le aree degli 8
                // triangoli.
                {
                    for (int k = 1; k <= 8; k++) {
                        area = area + areatr[k] / 4;
                    }
                    tca3dIter.setSample(i, j, 0, area);
                } else // se il pixel e' circondato da novalue, non e' possibile
                // comporre
                // 8 triangoli, si calcola quindi l'area relativa ai
                // triangoli completi
                // si calcola la media dei loro valori e quindi si spalma il
                // valore
                // ottenuto sul pixel.
                {
                    for (int k = 1; k <= 8; k++) {
                        area = area + areatr[k] / 4;
                    }
                    areamed = area / nnov;
                    tca3dIter.setSample(i, j, 0, areamed * 8);
                }
            } else
                tca3dIter.setSample(i, j, 0, doubleNovalue);
        }
        pm.worked(1);
    }
    pm.done();
    RandomIter flowIter = RandomIterFactory.create(flowImage, null);
    return ModelsEngine.sumDownstream(flowIter, tca3dIter, cols, rows, null, null, pm);
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter)

Example 22 with WritableRandomIter

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

the class OmsH2cA method process.

@Execute
public void process() {
    if (!concatOr(outAttribute == null, doReset)) {
        return;
    }
    checkNull(inFlow, inNet, inAttribute);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inFlow);
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    int novalue = HMConstants.getIntNovalue(inFlow);
    RenderedImage flowRI = inFlow.getRenderedImage();
    WritableRaster flowWR = CoverageUtilities.renderedImage2WritableRaster(flowRI, true);
    WritableRandomIter flowIter = RandomIterFactory.createWritable(flowWR, null);
    RandomIter attributeIter = CoverageUtilities.getRandomIterator(inAttribute);
    RandomIter netIter = CoverageUtilities.getRandomIterator(inNet);
    // $NON-NLS-1$
    pm.beginTask("Marking the network...", rows);
    /*
         * mark network as outlet, in order to easier stop on the net 
         * while going downstream
         */
    for (int j = 0; j < rows; j++) {
        for (int i = 0; i < cols; i++) {
            if (netIter.getSampleDouble(i, j, 0) == FlowNode.NETVALUE)
                flowIter.setSample(i, j, 0, FlowNode.OUTLET);
        }
        pm.worked(1);
    }
    pm.done();
    netIter.done();
    WritableRaster h2caWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
    WritableRandomIter h2caIter = RandomIterFactory.createWritable(h2caWR, null);
    ModelsEngine.markHillSlopeWithLinkValue(flowIter, novalue, attributeIter, h2caIter, cols, rows, pm);
    h2caIter.done();
    attributeIter.done();
    flowIter.done();
    outAttribute = CoverageUtilities.buildCoverage("h2ca", h2caWR, regionMap, inFlow.getCoordinateReferenceSystem());
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) 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)

Example 23 with WritableRandomIter

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

the class OmsTopIndex method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outTopindex == null, doReset)) {
        return;
    }
    checkNull(inTca, inSlope);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inTca);
    int nCols = regionMap.getCols();
    int nRows = regionMap.getRows();
    RandomIter tcaIter = CoverageUtilities.getRandomIterator(inTca);
    RandomIter slopeIter = CoverageUtilities.getRandomIterator(inSlope);
    WritableRaster topindexWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, doubleNovalue);
    WritableRandomIter topindexIter = RandomIterFactory.createWritable(topindexWR, null);
    try {
        pm.beginTask(msg.message("topindex.calculating"), nRows * nCols);
        processGrid(nCols, nRows, (c, r) -> {
            if (pm.isCanceled()) {
                return;
            }
            int tcaValue = tcaIter.getSample(c, r, 0);
            if (!isNovalue(tcaValue)) {
                if (slopeIter.getSampleDouble(c, r, 0) != 0) {
                    topindexIter.setSample(c, r, 0, Math.log(tcaValue / slopeIter.getSampleDouble(c, r, 0)));
                }
            }
            pm.worked(1);
        });
        pm.done();
        outTopindex = CoverageUtilities.buildCoverage("topindex", topindexWR, regionMap, inTca.getCoordinateReferenceSystem());
    } finally {
        tcaIter.done();
        slopeIter.done();
        topindexIter.done();
    }
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster) RegionMap(org.hortonmachine.gears.utils.RegionMap) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) Execute(oms3.annotations.Execute)

Example 24 with WritableRandomIter

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

the class LasHeightDistribution method process.

@Execute
public void process() throws Exception {
    checkNull(inIndexFile, inVector, inDem);
    // if (outChartsFolder != null) {
    // outChartsFolderFile = new File(outChartsFolder);
    // if (outChartsFolderFile.exists()) {
    // doChart = true;
    // }
    // }
    double percentageOverlap = pOverlapPerc / 100.0;
    File indexFile = new File(inIndexFile);
    SimpleFeatureCollection tilesFC = getVector(inVector);
    List<FeatureMate> tilesMates = FeatureUtilities.featureCollectionToMatesList(tilesFC);
    GridCoverage2D inDemGC = getRaster(inDem);
    CoordinateReferenceSystem crs = inDemGC.getCoordinateReferenceSystem();
    WritableRaster[] finalCoverageWRH = new WritableRaster[1];
    GridCoverage2D outCatsGC = CoverageUtilities.createCoverageFromTemplate(inDemGC, HMConstants.doubleNovalue, finalCoverageWRH);
    WritableRandomIter finalIter = CoverageUtilities.getWritableRandomIterator(finalCoverageWRH[0]);
    try (ALasDataManager dataManager = ALasDataManager.getDataManager(indexFile, inDemGC, pThres, null)) {
        dataManager.open();
        for (int i = 0; i < tilesMates.size(); i++) {
            pm.message("Processing tile: " + i + "/" + tilesMates.size());
            FeatureMate tileMate = tilesMates.get(i);
            String id = tileMate.getAttribute(fId, String.class);
            Geometry tileGeom = tileMate.getGeometry();
            Envelope geomEnvelope = tileGeom.getEnvelopeInternal();
            ReferencedEnvelope refEnvelope = new ReferencedEnvelope(geomEnvelope, crs);
            Envelope2D tileEnvelope = new Envelope2D(refEnvelope);
            WritableRaster[] tmpWrH = new WritableRaster[1];
            GridCoverage2D tmp = CoverageUtilities.createSubCoverageFromTemplate(inDemGC, tileEnvelope, doubleNovalue, tmpWrH);
            RegionMap tileRegionMap = CoverageUtilities.getRegionParamsFromGridCoverage(tmp);
            GridGeometry2D tileGridGeometry = tmp.getGridGeometry();
            List<LasRecord> pointsListForTile = dataManager.getPointsInGeometry(tileGeom, true);
            if (pointsListForTile.size() == 0) {
                pm.errorMessage("No points found in tile: " + id);
                continue;
            }
            if (pointsListForTile.size() < 2) {
                pm.errorMessage("Not enough points found in tile: " + id);
                continue;
            }
            List<double[]> negativeRanges = analyseNegativeLayerRanges(id, pointsListForTile);
            List<GridCoverage2D> rangeCoverages = new ArrayList<GridCoverage2D>();
            for (double[] range : negativeRanges) {
                List<LasRecord> pointsInVerticalRange = ALasDataManager.getPointsInVerticalRange(pointsListForTile, range[0], range[1], true);
                WritableRaster[] wrH = new WritableRaster[1];
                GridCoverage2D tmpCoverage = CoverageUtilities.createSubCoverageFromTemplate(inDemGC, tileEnvelope, doubleNovalue, wrH);
                rangeCoverages.add(tmpCoverage);
                WritableRandomIter tmpIter = CoverageUtilities.getWritableRandomIterator(wrH[0]);
                final DirectPosition2D wp = new DirectPosition2D();
                for (LasRecord lasRecord : pointsInVerticalRange) {
                    wp.setLocation(lasRecord.x, lasRecord.y);
                    GridCoordinates2D gp = tileGridGeometry.worldToGrid(wp);
                    double count = tmpIter.getSampleDouble(gp.x, gp.y, 0);
                    if (isNovalue(count)) {
                        count = 0;
                    }
                    tmpIter.setSample(gp.x, gp.y, 0, count + 1);
                }
                tmpIter.done();
            }
            /*
                 * categorize in non forest/single/double layer
                 */
            /*
                 * 1) check if the multiple layers are double or
                 * single at variable heights. 
                 */
            boolean isDoubleLayered = false;
            if (rangeCoverages.size() > 1) {
                for (int j = 0; j < rangeCoverages.size() - 1; j++) {
                    GridCoverage2D cov1 = rangeCoverages.get(j);
                    GridCoverage2D cov2 = rangeCoverages.get(j + 1);
                    if (overlapForPercentage(cov1, cov2, percentageOverlap)) {
                        isDoubleLayered = true;
                        break;
                    }
                }
            }
            /*
                 * 2) define each pixel of the end map
                 * - 0 = no forest
                 * - 1 = single layer
                 * - 2 = single with variable height
                 * - 3 = double layer
                 */
            GridGeometry2D gridGeometry = outCatsGC.getGridGeometry();
            // RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(outCats);
            double[] gridValue = new double[1];
            int cols = tileRegionMap.getCols();
            int rows = tileRegionMap.getRows();
            for (int r = 0; r < rows; r++) {
                for (int c = 0; c < cols; c++) {
                    int value = 0;
                    GridCoordinates2D gridPosition = new GridCoordinates2D(c, r);
                    for (int j = 0; j < rangeCoverages.size(); j++) {
                        GridCoverage2D cov = rangeCoverages.get(j);
                        cov.evaluate(gridPosition, gridValue);
                        if (!isNovalue(gridValue[0])) {
                            value++;
                        }
                    }
                    // set final value in the grid
                    if (value > 1) {
                        // multilayer
                        if (isDoubleLayered) {
                            value = 3;
                        } else {
                            value = 2;
                        }
                    }
                    DirectPosition worldPosition = tileGridGeometry.gridToWorld(gridPosition);
                    GridCoordinates2D worldPositionCats = gridGeometry.worldToGrid(worldPosition);
                    finalIter.setSample(worldPositionCats.x, worldPositionCats.y, 0, value);
                }
            }
        }
    }
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(outCatsGC);
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            double value = finalIter.getSampleDouble(c, r, 0);
            if (isNovalue(value)) {
                finalIter.setSample(c, r, 0, 0.0);
            }
        }
    }
    finalIter.done();
    dumpRaster(outCatsGC, outCats);
}
Also used : DirectPosition(org.opengis.geometry.DirectPosition) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) RegionMap(org.hortonmachine.gears.utils.RegionMap) ArrayList(java.util.ArrayList) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Envelope(org.locationtech.jts.geom.Envelope) DirectPosition2D(org.geotools.geometry.DirectPosition2D) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) FeatureMate(org.hortonmachine.gears.utils.features.FeatureMate) LasRecord(org.hortonmachine.gears.io.las.core.LasRecord) WritableRaster(java.awt.image.WritableRaster) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) Envelope2D(org.geotools.geometry.Envelope2D) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) ALasDataManager(org.hortonmachine.gears.io.las.ALasDataManager) Geometry(org.locationtech.jts.geom.Geometry) GridCoordinates2D(org.geotools.coverage.grid.GridCoordinates2D) File(java.io.File) Execute(oms3.annotations.Execute)

Example 25 with WritableRandomIter

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

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