Search in sources :

Example 61 with WritableRandomIter

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

the class OmsMarkoutlets method process.

@Execute
public void process() {
    if (!concatOr(outFlow == null, doReset)) {
        return;
    }
    checkNull(inFlow);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inFlow);
    int nCols = regionMap.getCols();
    int nRows = regionMap.getRows();
    WritableRaster mflowWR = CoverageUtilities.renderedImage2WritableRaster(inFlow.getRenderedImage(), false);
    WritableRandomIter mflowIter = RandomIterFactory.createWritable(mflowWR, null);
    int novalue = HMConstants.getIntNovalue(inFlow);
    // $NON-NLS-1$
    pm.beginTask(msg.message("markoutlets.working"), nRows);
    for (int r = 0; r < nRows; r++) {
        for (int c = 0; c < nCols; c++) {
            FlowNode flowNode = new FlowNode(mflowIter, nCols, nRows, c, r, novalue);
            if (flowNode.isValid() && flowNode.isHeadingOutside()) {
                flowNode.setValueInMap(mflowIter, FlowNode.OUTLET);
            }
        }
        pm.worked(1);
    }
    pm.done();
    outFlow = CoverageUtilities.buildCoverage("markoutlets", mflowWR, regionMap, inFlow.getCoordinateReferenceSystem());
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster) RegionMap(org.hortonmachine.gears.utils.RegionMap) FlowNode(org.hortonmachine.gears.libs.modules.FlowNode) Execute(oms3.annotations.Execute)

Example 62 with WritableRandomIter

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

the class Las2BivariateRasterMosaic method processTile.

@SuppressWarnings({ "unchecked" })
private void processTile(File outFolderFile, CoordinateReferenceSystem crs, ALasDataManager lasData, int x, int y, double w, double e, double s, double n) throws Exception {
    StringBuilder sb = new StringBuilder();
    sb.append("tile_");
    sb.append(x);
    sb.append("_");
    sb.append(y);
    final String tileName = sb.toString();
    sb.append(".tiff");
    File outTileFile = new File(outFolderFile, sb.toString());
    if (outTileFile.exists()) {
        pm.errorMessage("Not overwriting existing tile: " + outTileFile.getName());
        return;
    }
    final int cols = (int) round((e - w) / pRes);
    final int rows = (int) round((n - s) / pRes);
    double xRes = (e - w) / cols;
    double yRes = (n - s) / rows;
    RegionMap regionMap = CoverageUtilities.makeRegionParamsMap(n, s, w, e, xRes, yRes, cols, rows);
    WritableRaster outWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, HMConstants.doubleNovalue);
    GridCoverage2D outputCoverage = CoverageUtilities.buildCoverage("data", outWR, regionMap, crs);
    // pm.message("Reading laspoints for: " + tileName);
    Envelope env = new Envelope(w, e, s, n);
    /*
         * enlarge by buffer value, to solve border issues 
         * 
         * -> outside points are read to aid interpolation at borders
         * -> this forces to keep two gridgeometries active, the 
         *    resulting coverage + the buffered one
         */
    double deltaX = xRes * pBuffer;
    double deltaY = yRes * pBuffer;
    env.expandBy(deltaX, deltaY);
    Polygon roiPolygon = GeometryUtilities.createPolygonFromEnvelope(env);
    List<LasRecord> tileLasPoints = lasData.getPointsInGeometry(roiPolygon, true);
    if (tileLasPoints.size() > 100) {
        final GridGeometry2D gridGeometry = outputCoverage.getGridGeometry();
        int newCols = cols + 2 * pBuffer;
        int newRows = rows + 2 * pBuffer;
        GridGeometry2D bufferedGridGeometry = CoverageUtilities.gridGeometryFromRegionValues(env.getMaxY(), env.getMinY(), env.getMaxX(), env.getMinX(), newCols, newRows, crs);
        ArrayList<LasRecord>[][] lasMatrix = new ArrayList[newCols][newRows];
        for (int r = 0; r < newRows; r++) {
            for (int c = 0; c < newCols; c++) {
                ArrayList<LasRecord> item = new ArrayList<LasRecord>();
                lasMatrix[c][r] = item;
            }
        }
        // Splitting las into cells
        final Point point = new Point();
        for (LasRecord dot : tileLasPoints) {
            if (doIntensity) {
                if (dot.intensity == NOINTENSITY) {
                    continue;
                }
                minValue = min(dot.intensity, minValue);
                maxValue = max(dot.intensity, maxValue);
            } else {
                minValue = min(dot.z, minValue);
                maxValue = max(dot.z, maxValue);
            }
            CoverageUtilities.colRowFromCoordinate(new Coordinate(dot.x, dot.y), bufferedGridGeometry, point);
            lasMatrix[point.x][point.y].add(dot);
        }
        WritableRandomIter outWIter = null;
        try {
            outWIter = CoverageUtilities.getWritableRandomIterator(outWR);
            for (int c = pBuffer; c < newCols - pBuffer; c++) {
                if (c % 100 == 0) {
                    StringBuilder sb1 = new StringBuilder();
                    sb1.append(tileName);
                    sb1.append(": ");
                    sb1.append(c);
                    sb1.append(" of ");
                    sb1.append(newCols);
                    pm.message(sb1.toString());
                }
                for (int r = pBuffer; r < newRows - pBuffer; r++) {
                    Coordinate coordinate = CoverageUtilities.coordinateFromColRow(c, r, bufferedGridGeometry);
                    List<LasRecord> currentPoints = new ArrayList<LasRecord>();
                    for (int tmpC = c - pBuffer; tmpC <= c + pBuffer; tmpC++) {
                        for (int tmpR = r - pBuffer; tmpR <= r + pBuffer; tmpR++) {
                            currentPoints.addAll(lasMatrix[tmpC][tmpR]);
                        }
                    }
                    int size = currentPoints.size();
                    if (size >= pMinpoints) {
                        // need at least as many samples as parameters
                        try {
                            double[] parameters = calculateParameters(currentPoints);
                            double interpolatedValue = getInterpolatedValue(parameters, coordinate.x, coordinate.y);
                            // limit by min/max
                            if (interpolatedValue < minValue) {
                                interpolatedValue = minValue;
                            }
                            if (interpolatedValue > maxValue) {
                                interpolatedValue = maxValue;
                            }
                            CoverageUtilities.colRowFromCoordinate(coordinate, gridGeometry, point);
                            outWIter.setSample(point.x, point.y, 0, interpolatedValue);
                        } catch (SingularMatrixException ex) {
                        // we ignore the singular matrix leaving the cell undefined
                        }
                    }
                }
            }
        } finally {
            if (outWIter != null)
                outWIter.done();
        }
        dumpRaster(outputCoverage, outTileFile.getAbsolutePath());
    } else {
        pm.message(tileName + " ignored because of no points there.");
    }
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) RegionMap(org.hortonmachine.gears.utils.RegionMap) ArrayList(java.util.ArrayList) Point(java.awt.Point) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Envelope(org.locationtech.jts.geom.Envelope) Point(java.awt.Point) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) LasRecord(org.hortonmachine.gears.io.las.core.LasRecord) Coordinate(org.locationtech.jts.geom.Coordinate) WritableRaster(java.awt.image.WritableRaster) SingularMatrixException(org.apache.commons.math3.linear.SingularMatrixException) Polygon(org.locationtech.jts.geom.Polygon) File(java.io.File)

Example 63 with WritableRandomIter

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

the class LasOnRasterMapper method process.

@Execute
public void process() throws Exception {
    checkNull(inLas, inDtm, outRaster);
    GridCoverage2D inDtmGC = getRaster(inDtm);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inDtmGC);
    double north = regionMap.getNorth();
    double south = regionMap.getSouth();
    double east = regionMap.getEast();
    double west = regionMap.getWest();
    if (pXres == null || pYres == null) {
        pXres = regionMap.getXres();
        pYres = regionMap.getYres();
    }
    CoordinateReferenceSystem crs = null;
    Polygon polygon = CoverageUtilities.getRegionPolygon(inDtmGC);
    crs = inDtmGC.getCoordinateReferenceSystem();
    GridGeometry2D dtmGridGeometry = inDtmGC.getGridGeometry();
    final int newRows = (int) round((north - south) / pYres);
    int newCols = (int) round((east - west) / pXres);
    final GridGeometry2D newGridGeometry2D = CoverageUtilities.gridGeometryFromRegionValues(north, south, east, west, newCols, newRows, crs);
    RegionMap newRegionMap = CoverageUtilities.gridGeometry2RegionParamsMap(newGridGeometry2D);
    final WritableRaster newWR = CoverageUtilities.createWritableRaster(newCols, newRows, null, null, HMConstants.doubleNovalue);
    RandomIter dtmIter = CoverageUtilities.getRandomIterator(inDtmGC);
    try (ALasDataManager lasData = ALasDataManager.getDataManager(new File(inLas), null, 0.0, crs)) {
        lasData.open();
        pm.beginTask("Reading points on region...", IHMProgressMonitor.UNKNOWN);
        List<LasRecord> lasPoints = lasData.getPointsInGeometry(polygon, false);
        pm.done();
        pm.beginTask("Setting raster points...", lasPoints.size());
        final Point gridPoint = new Point();
        final Point dtmPoint = new Point();
        for (LasRecord lasRecord : lasPoints) {
            double dotZ = lasRecord.z;
            Coordinate coordinate = new Coordinate(lasRecord.x, lasRecord.y, dotZ);
            CoverageUtilities.colRowFromCoordinate(coordinate, newGridGeometry2D, gridPoint);
            double newRasterValue = newWR.getSampleDouble(gridPoint.x, gridPoint.y, 0);
            CoverageUtilities.colRowFromCoordinate(coordinate, dtmGridGeometry, dtmPoint);
            double dtmValue = dtmIter.getSampleDouble(dtmPoint.x, dtmPoint.y, 0);
            if (doMax) {
                if (HMConstants.isNovalue(newRasterValue) || newRasterValue < dotZ) {
                    if (!HMConstants.isNovalue(dtmValue) && dtmValue > dotZ) {
                        dotZ = dtmValue;
                    }
                    newWR.setSample(gridPoint.x, gridPoint.y, 0, dotZ);
                }
            } else {
                if (HMConstants.isNovalue(newRasterValue) || newRasterValue > dotZ) {
                    if (!HMConstants.isNovalue(dtmValue) && dtmValue > dotZ) {
                        dotZ = dtmValue;
                    }
                    newWR.setSample(gridPoint.x, gridPoint.y, 0, dotZ);
                }
            }
            pm.worked(1);
        }
        pm.done();
    }
    GridCoverage2D outRasterGC = CoverageUtilities.buildCoverage("outraster", newWR, newRegionMap, crs);
    dumpRaster(outRasterGC, outRaster);
    if (pXres != null && pYres != null && outDtm != null) {
        WritableRaster[] holder = new WritableRaster[1];
        GridCoverage2D outDtmGC = CoverageUtilities.createCoverageFromTemplate(outRasterGC, HMConstants.doubleNovalue, holder);
        GridGeometry2D outGridGeometry = outDtmGC.getGridGeometry();
        WritableRandomIter outIter = CoverageUtilities.getWritableRandomIterator(holder[0]);
        RegionMap outRegionMap = CoverageUtilities.getRegionParamsFromGridCoverage(outDtmGC);
        int cols = outRegionMap.getCols();
        int rows = outRegionMap.getRows();
        final Point p = new Point();
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                Coordinate coordinate = CoverageUtilities.coordinateFromColRow(c, r, outGridGeometry);
                CoverageUtilities.colRowFromCoordinate(coordinate, dtmGridGeometry, p);
                double dtmValue = dtmIter.getSampleDouble(p.x, p.y, 0);
                outIter.setSample(c, r, 0, dtmValue);
            }
        }
        outIter.done();
        dumpRaster(outDtmGC, outDtm);
    }
    dtmIter.done();
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) RegionMap(org.hortonmachine.gears.utils.RegionMap) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) Point(java.awt.Point) Point(java.awt.Point) ALasDataManager(org.hortonmachine.gears.io.las.ALasDataManager) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) LasRecord(org.hortonmachine.gears.io.las.core.LasRecord) Coordinate(org.locationtech.jts.geom.Coordinate) WritableRaster(java.awt.image.WritableRaster) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Polygon(org.locationtech.jts.geom.Polygon) File(java.io.File) Execute(oms3.annotations.Execute)

Example 64 with WritableRandomIter

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

the class OmsKrigingRasterMode method storeResult.

private void storeResult(double[] interpolatedValues, HashMap<Integer, Coordinate> interpolatedCoordinatesMap) throws MismatchedDimensionException, Exception {
    WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
    Set<Integer> pointsToInterpolateIdSett = interpolatedCoordinatesMap.keySet();
    Iterator<Integer> idIterator = pointsToInterpolateIdSett.iterator();
    int c = 0;
    MathTransform transf = inInterpolationGrid.getCRSToGrid2D();
    final DirectPosition gridPoint = new DirectPosition2D();
    while (idIterator.hasNext()) {
        int id = idIterator.next();
        Coordinate coordinate = (Coordinate) interpolatedCoordinatesMap.get(id);
        DirectPosition point = new DirectPosition2D(inInterpolationGrid.getCoordinateReferenceSystem(), coordinate.x, coordinate.y);
        transf.transform(point, gridPoint);
        double[] gridCoord = gridPoint.getCoordinate();
        int x = (int) gridCoord[0];
        int y = (int) gridCoord[1];
        outIter.setSample(x, y, 0, interpolatedValues[c]);
        c++;
    }
    RegionMap regionMap = CoverageUtilities.gridGeometry2RegionParamsMap(inInterpolationGrid);
    outGrid = CoverageUtilities.buildCoverage("gridded", outWR, regionMap, inInterpolationGrid.getCoordinateReferenceSystem());
}
Also used : DirectPosition(org.opengis.geometry.DirectPosition) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) MathTransform(org.opengis.referencing.operation.MathTransform) Coordinate(org.locationtech.jts.geom.Coordinate) RegionMap(org.hortonmachine.gears.utils.RegionMap) DirectPosition2D(org.geotools.geometry.DirectPosition2D)

Example 65 with WritableRandomIter

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

the class OmsKriging method storeResult.

private void storeResult(double[] interpolatedValues, HashMap<Integer, Coordinate> interpolatedCoordinatesMap) throws MismatchedDimensionException, Exception {
    WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
    Set<Integer> pointsToInterpolateIdSett = interpolatedCoordinatesMap.keySet();
    Iterator<Integer> idIterator = pointsToInterpolateIdSett.iterator();
    int c = 0;
    MathTransform transf = inInterpolationGrid.getCRSToGrid2D();
    final DirectPosition gridPoint = new DirectPosition2D();
    while (idIterator.hasNext()) {
        int id = idIterator.next();
        Coordinate coordinate = (Coordinate) interpolatedCoordinatesMap.get(id);
        DirectPosition point = new DirectPosition2D(inInterpolationGrid.getCoordinateReferenceSystem(), coordinate.x, coordinate.y);
        transf.transform(point, gridPoint);
        double[] gridCoord = gridPoint.getCoordinate();
        int x = (int) gridCoord[0];
        int y = (int) gridCoord[1];
        outIter.setSample(x, y, 0, checkResultValue(interpolatedValues[c]));
        c++;
    }
    RegionMap regionMap = CoverageUtilities.gridGeometry2RegionParamsMap(inInterpolationGrid);
    outGrid = CoverageUtilities.buildCoverage("gridded", outWR, regionMap, inInterpolationGrid.getCoordinateReferenceSystem());
}
Also used : DirectPosition(org.opengis.geometry.DirectPosition) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) MathTransform(org.opengis.referencing.operation.MathTransform) Coordinate(org.locationtech.jts.geom.Coordinate) RegionMap(org.hortonmachine.gears.utils.RegionMap) DirectPosition2D(org.geotools.geometry.DirectPosition2D)

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