Search in sources :

Example 61 with RandomIter

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

the class OmsLW10_CHM_AreaToNetpointAssociator method process.

// VARS DOC END
@Execute
public void process() throws Exception {
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inFlow);
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    GridGeometry2D gridGeometry = inFlow.getGridGeometry();
    GeometryFactory gf = GeometryUtilities.gf();
    /*
         * extract the inundated area from the polygon
         */
    PreparedGeometry preparedFloodingArea = getFloodingArea(inInundationArea);
    /*
         * extract the Canopy Height Model from DTM and DSM
         */
    GridCoverage2D chmGC = getChm(inDsm, inDtm);
    /*
         * extract basins calling netnumbering with in input all the network points
         */
    OmsNetNumbering omsnetnumbering = new OmsNetNumbering();
    omsnetnumbering.inFlow = inFlow;
    omsnetnumbering.inNet = inNet;
    omsnetnumbering.inTca = inTca;
    omsnetnumbering.inPoints = inNetPoints;
    omsnetnumbering.pm = pm;
    omsnetnumbering.process();
    outNetnum = omsnetnumbering.outNetnum;
    outBasins = omsnetnumbering.outBasins;
    RandomIter netnumBasinsIter = CoverageUtilities.getRandomIterator(outBasins);
    RandomIter connectivityIter = CoverageUtilities.getRandomIterator(inConnectivity);
    RandomIter chmIter = CoverageUtilities.getRandomIterator(chmGC);
    RandomIter standIter = CoverageUtilities.getRandomIterator(inStand);
    HashMap<Integer, DescriptiveStatistics> heightBasin2ValueMap = new HashMap<Integer, DescriptiveStatistics>();
    HashMap<Integer, DescriptiveStatistics> standBasin2ValueMap = new HashMap<Integer, DescriptiveStatistics>();
    pm.beginTask("Calculating vegetation stats.", cols);
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            double netnumDouble = netnumBasinsIter.getSampleDouble(c, r, 0);
            if (!isNovalue(netnumDouble)) {
                Integer netNum = (int) netnumDouble;
                Coordinate coordinate = CoverageUtilities.coordinateFromColRow(c, r, gridGeometry);
                Point point = gf.createPoint(coordinate);
                double connectivityDouble = connectivityIter.getSampleDouble(c, r, 0);
                /*
                     * check if the point is connected to the network:
                     * - connectivity index less than the threshold
                     * - point is inside the inundated area
                     * and fill the hashmaps with the correspondent positions.
                     */
                if (connectivityDouble < pConnectivityThreshold || preparedFloodingArea.intersects(point)) {
                    double chmDouble = chmIter.getSampleDouble(c, r, 0);
                    double standDouble = standIter.getSampleDouble(c, r, 0);
                    DescriptiveStatistics summaryHeightStatistics = heightBasin2ValueMap.get(netNum);
                    DescriptiveStatistics summaryStandStatistics = standBasin2ValueMap.get(netNum);
                    if (summaryHeightStatistics == null) {
                        summaryHeightStatistics = new DescriptiveStatistics();
                        summaryStandStatistics = new DescriptiveStatistics();
                        heightBasin2ValueMap.put(netNum, summaryHeightStatistics);
                        standBasin2ValueMap.put(netNum, summaryStandStatistics);
                    }
                    summaryHeightStatistics.addValue(chmDouble);
                    summaryStandStatistics.addValue(standDouble);
                }
            }
        }
        pm.worked(1);
    }
    pm.done();
    /*
         * create the structure for the output attributes and insert the summary statistics
         * as attributes
         */
    FeatureExtender ext = new FeatureExtender(inNetPoints.getSchema(), new String[] { LWFields.VEG_VOL, LWFields.VEG_H }, new Class[] { Double.class, Double.class });
    List<SimpleFeature> inNetworkPointsList = FeatureUtilities.featureCollectionToList(inNetPoints);
    DefaultFeatureCollection finalNetworkPointsFC = new DefaultFeatureCollection();
    final java.awt.Point point = new java.awt.Point();
    for (SimpleFeature inPointFeature : inNetworkPointsList) {
        Geometry geometry = (Geometry) inPointFeature.getDefaultGeometry();
        Coordinate coordinate = geometry.getCoordinate();
        CoverageUtilities.colRowFromCoordinate(coordinate, gridGeometry, point);
        int netnum = netnumBasinsIter.getSample(point.x, point.y, 0);
        DescriptiveStatistics summaryHeightStatistics = heightBasin2ValueMap.get(netnum);
        double medianHeight = 0.0;
        if (summaryHeightStatistics != null) {
            medianHeight = summaryHeightStatistics.getPercentile(50);
        }
        DescriptiveStatistics summaryStandStatistics = standBasin2ValueMap.get(netnum);
        double sumStand = 0.0;
        if (summaryStandStatistics != null) {
            sumStand = summaryStandStatistics.getSum();
        }
        SimpleFeature newPointFeature = ext.extendFeature(inPointFeature, new Object[] { sumStand, medianHeight });
        finalNetworkPointsFC.add(newPointFeature);
    }
    outNetPoints = finalNetworkPointsFC;
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) PreparedGeometryFactory(org.locationtech.jts.geom.prep.PreparedGeometryFactory) HashMap(java.util.HashMap) RegionMap(org.hortonmachine.gears.utils.RegionMap) RandomIter(javax.media.jai.iterator.RandomIter) Point(org.locationtech.jts.geom.Point) Point(org.locationtech.jts.geom.Point) SimpleFeature(org.opengis.feature.simple.SimpleFeature) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) Geometry(org.locationtech.jts.geom.Geometry) FeatureExtender(org.hortonmachine.gears.utils.features.FeatureExtender) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) Coordinate(org.locationtech.jts.geom.Coordinate) OmsNetNumbering(org.hortonmachine.hmachine.modules.network.netnumbering.OmsNetNumbering) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) Execute(oms3.annotations.Execute)

Example 62 with RandomIter

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

the class OmsNabla method process.

@Execute
public void process() {
    if (!concatOr(outNabla == null, doReset)) {
        return;
    }
    checkNull(inElev);
    HashMap<String, Double> regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
    nCols = regionMap.get(CoverageUtilities.COLS).intValue();
    nRows = regionMap.get(CoverageUtilities.ROWS).intValue();
    xRes = regionMap.get(CoverageUtilities.XRES);
    yRes = regionMap.get(CoverageUtilities.YRES);
    RenderedImage elevationRI = inElev.getRenderedImage();
    RandomIter elevationIter = RandomIterFactory.create(elevationRI, null);
    WritableRaster gradientWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, doubleNovalue);
    if (pThreshold == null) {
        nabla(elevationIter, gradientWR);
    } else {
        nabla_mask(elevationIter, gradientWR, pThreshold);
    }
    outNabla = CoverageUtilities.buildCoverage("nabla", gradientWR, regionMap, inElev.getCoordinateReferenceSystem());
}
Also used : WritableRaster(java.awt.image.WritableRaster) RandomIter(javax.media.jai.iterator.RandomIter) RenderedImage(java.awt.image.RenderedImage) Execute(oms3.annotations.Execute)

Example 63 with RandomIter

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

the class OmsTca method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outTca == null, doReset)) {
        return;
    }
    checkNull(inFlow);
    if (doLoopCheck) {
        // prepare the loop featurecollection
        SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
        b.setName("loop");
        b.setCRS(inFlow.getCoordinateReferenceSystem());
        b.add("the_geom", LineString.class);
        loopFT = b.buildFeatureType();
        outLoop = new DefaultFeatureCollection();
    }
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inFlow);
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    int novalue = HMConstants.getIntNovalue(inFlow);
    RenderedImage flowRI = inFlow.getRenderedImage();
    WritableRaster tcaWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
    RandomIter flowIter = RandomIterFactory.create(flowRI, null);
    WritableRandomIter tcaIter = RandomIterFactory.createWritable(tcaWR, null);
    boolean loopError = false;
    TreeSet<CheckPoint> passedPoints = null;
    // $NON-NLS-1$
    pm.beginTask("Calculating tca...", rows);
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            FlowNode flowNode = new FlowNode(flowIter, cols, rows, c, r, novalue);
            if (flowNode.isSource()) {
                double previousTcaValue = 0.0;
                if (doLoopCheck)
                    passedPoints = new TreeSet<>();
                int index = 0;
                while (flowNode != null && flowNode.isValid()) {
                    int col = flowNode.col;
                    int row = flowNode.row;
                    if (doLoopCheck && !passedPoints.add(new CheckPoint(col, row, index++))) {
                        // create a shapefile with the loop performed
                        GridGeometry2D gridGeometry = inFlow.getGridGeometry();
                        Iterator<CheckPoint> iterator = passedPoints.iterator();
                        GeometryFactory gf = GeometryUtilities.gf();
                        List<Coordinate> coordinates = new ArrayList<Coordinate>();
                        while (iterator.hasNext()) {
                            CheckPoint checkPoint = (CheckPoint) iterator.next();
                            DirectPosition world = gridGeometry.gridToWorld(new GridCoordinates2D(checkPoint.col, checkPoint.row));
                            double[] coord = world.getCoordinate();
                            coordinates.add(new Coordinate(coord[0], coord[1]));
                        }
                        if (coordinates.size() == 1) {
                            Coordinate first = coordinates.get(0);
                            Coordinate dummy = new Coordinate(first.x + 0.000_000_1, first.y + 0.000_000_1);
                            coordinates.add(dummy);
                        }
                        LineString lineString = gf.createLineString(coordinates.toArray(new Coordinate[0]));
                        SimpleFeatureBuilder builder = new SimpleFeatureBuilder(loopFT);
                        Object[] values = new Object[] { lineString };
                        builder.addAll(values);
                        SimpleFeature feature = builder.buildFeature(null);
                        ((DefaultFeatureCollection) outLoop).add(feature);
                        pm.errorMessage(MessageFormat.format("The downstream sum passed twice through the same position, there might be an error in your flowdirections. col = {0} row = {1}", col, row));
                        loopError = true;
                        break;
                    }
                    double tmpTca = tcaIter.getSampleDouble(col, row, 0);
                    double newTcaValue;
                    /*
                         * cumulate only if first time passing, else just propagate
                         */
                    if (isNovalue(tmpTca)) {
                        tmpTca = 1.0;
                        newTcaValue = tmpTca + previousTcaValue;
                        previousTcaValue = newTcaValue;
                    } else {
                        newTcaValue = tmpTca + previousTcaValue;
                    }
                    tcaIter.setSample(col, row, 0, newTcaValue);
                    flowNode = flowNode.goDownstream();
                }
                if (loopError) {
                    break;
                }
            }
        }
        if (loopError) {
            break;
        }
        pm.worked(1);
    }
    pm.done();
    flowIter.done();
    tcaIter.done();
    if (loopError) {
        outTca = CoverageUtilities.buildDummyCoverage();
    } else {
        outLoop = null;
        outTca = CoverageUtilities.buildCoverageWithNovalue("tca", tcaWR, regionMap, inFlow.getCoordinateReferenceSystem(), doubleNovalue);
    }
}
Also used : DirectPosition(org.opengis.geometry.DirectPosition) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) RegionMap(org.hortonmachine.gears.utils.RegionMap) ArrayList(java.util.ArrayList) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster) TreeSet(java.util.TreeSet) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Coordinate(org.locationtech.jts.geom.Coordinate) LineString(org.locationtech.jts.geom.LineString) GridCoordinates2D(org.geotools.coverage.grid.GridCoordinates2D) RenderedImage(java.awt.image.RenderedImage) FlowNode(org.hortonmachine.gears.libs.modules.FlowNode) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Execute(oms3.annotations.Execute)

Example 64 with RandomIter

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

the class OmsH2cd method process.

@Execute
public void process() throws Exception {
    checkNull(inFlow, inNet);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inFlow);
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    RenderedImage flowRI = inFlow.getRenderedImage();
    WritableRaster flowWR = CoverageUtilities.renderedImage2WritableRaster(flowRI, false);
    WritableRandomIter flowIter = RandomIterFactory.createWritable(flowWR, null);
    RenderedImage netRI = inNet.getRenderedImage();
    RandomIter netIter = RandomIterFactory.create(netRI, null);
    RandomIter elevIter = null;
    if (inElev != null && pMode == 1) {
        RenderedImage elevRI = inElev.getRenderedImage();
        elevIter = RandomIterFactory.create(elevRI, null);
    }
    WritableRaster h2cdWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, 0.0);
    WritableRandomIter h2cdIter = RandomIterFactory.createWritable(h2cdWR, null);
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            double value = netIter.getSampleDouble(c, r, 0);
            if (!isNovalue(value))
                flowIter.setSample(c, r, 0, 10.0);
        }
    }
    if (pMode == 1) {
        ModelsEngine.topologicalOutletdistance(flowIter, elevIter, h2cdIter, regionMap, pm);
    } else {
        ModelsEngine.outletdistance(flowIter, h2cdIter, regionMap, pm);
    }
    for (int j = 0; j < rows; j++) {
        for (int i = 0; i < cols; i++) {
            if (!isNovalue(netIter.getSampleDouble(i, j, 0)) && !isNovalue(flowIter.getSampleDouble(i, j, 0))) {
                h2cdIter.setSample(i, j, 0, 0.0);
            } else if (isNovalue(flowIter.getSampleDouble(i, j, 0))) {
                h2cdIter.setSample(i, j, 0, HMConstants.doubleNovalue);
            }
        }
    }
    // $NON-NLS-1$
    outH2cd = CoverageUtilities.buildCoverage("h2cd", h2cdWR, 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 65 with RandomIter

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

the class OmsTc method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outTc3 == null, outTc9 == null, doReset)) {
        return;
    }
    checkNull(inProf, inTan);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inProf);
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    WritableRaster tc3WR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
    WritableRaster tc9WR = CoverageUtilities.createWritableRaster(cols, rows, null, null, doubleNovalue);
    WritableRandomIter tc3Iter = RandomIterFactory.createWritable(tc3WR, null);
    WritableRandomIter tc9Iter = RandomIterFactory.createWritable(tc9WR, null);
    RandomIter profIter = CoverageUtilities.getRandomIterator(inProf);
    RandomIter tangIter = CoverageUtilities.getRandomIterator(inTan);
    // calculate ...
    // $NON-NLS-1$ //$NON-NLS-2$
    pm.beginTask(msg.message("working") + "tc9...", rows);
    for (int j = 0; j < rows; j++) {
        for (int i = 0; i < cols; i++) {
            double tangValue = tangIter.getSampleDouble(i, j, 0);
            if (isNovalue(tangValue)) {
                tc9Iter.setSample(i, j, 0, HMConstants.doubleNovalue);
            } else {
                double profValue = profIter.getSampleDouble(i, j, 0);
                if (Math.abs(tangValue) <= pTanthres) {
                    if (Math.abs(profValue) <= pProfthres) {
                        tc9Iter.setSample(i, j, 0, 10);
                    } else if (profValue < -pProfthres) {
                        tc9Iter.setSample(i, j, 0, 20);
                    } else if (profValue > pProfthres) {
                        tc9Iter.setSample(i, j, 0, 30);
                    }
                } else if (tangValue < -pTanthres) {
                    if (Math.abs(profValue) <= pProfthres) {
                        tc9Iter.setSample(i, j, 0, 40);
                    } else if (profValue < -pProfthres) {
                        tc9Iter.setSample(i, j, 0, 50);
                    } else if (profValue > pProfthres) {
                        tc9Iter.setSample(i, j, 0, 60);
                    }
                } else if (tangValue > pTanthres) {
                    if (Math.abs(profValue) <= pProfthres) {
                        tc9Iter.setSample(i, j, 0, 70);
                    } else if (profValue < -pProfthres) {
                        tc9Iter.setSample(i, j, 0, 80);
                    } else if (profValue > pProfthres) {
                        tc9Iter.setSample(i, j, 0, 90);
                    }
                }
            }
        }
        pm.worked(1);
    }
    pm.done();
    profIter.done();
    tangIter.done();
    // $NON-NLS-1$ //$NON-NLS-2$
    pm.beginTask(msg.message("working") + "tc3...", rows);
    for (int j = 0; j < rows; j++) {
        for (int i = 0; i < cols; i++) {
            double cp9Value = tc9Iter.getSampleDouble(i, j, 0);
            if (!isNovalue(cp9Value)) {
                if (cp9Value == 70 || cp9Value == 90 || cp9Value == 30) {
                    tc3Iter.setSample(i, j, 0, 15);
                } else if (cp9Value == 10) {
                    tc3Iter.setSample(i, j, 0, 25);
                } else {
                    tc3Iter.setSample(i, j, 0, 35);
                }
            } else {
                tc3Iter.setSample(i, j, 0, cp9Value);
            }
        }
        pm.worked(1);
    }
    pm.done();
    // $NON-NLS-1$
    outTc3 = CoverageUtilities.buildCoverage("tc3", tc3WR, regionMap, inProf.getCoordinateReferenceSystem());
    // $NON-NLS-1$
    outTc9 = CoverageUtilities.buildCoverage("tc9", tc9WR, regionMap, inProf.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) 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