Search in sources :

Example 86 with WritableRandomIter

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

the class OmsMosaic method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outRaster == null, doReset)) {
        return;
    }
    if (inFiles == null) {
        throw new ModelsIllegalargumentException("No input data have been provided.", this, pm);
    }
    if (inFiles != null && inFiles.size() < 2) {
        throw new ModelsIllegalargumentException("The patching module needs at least two maps to be patched.", this, pm);
    }
    GridGeometry2D referenceGridGeometry = null;
    double n = Double.MIN_VALUE;
    double s = Double.MAX_VALUE;
    double e = Double.MIN_VALUE;
    double w = Double.MAX_VALUE;
    int np = Integer.MIN_VALUE;
    int sp = Integer.MAX_VALUE;
    int ep = Integer.MIN_VALUE;
    int wp = Integer.MAX_VALUE;
    pm.beginTask("Calculating final bounds...", inFiles.size());
    for (File coverageFile : inFiles) {
        GridCoverage2D coverage = OmsRasterReader.readRaster(coverageFile.getAbsolutePath());
        if (referenceGridGeometry == null) {
            // take the first as reference
            crs = coverage.getCoordinateReferenceSystem();
            referenceGridGeometry = coverage.getGridGeometry();
            pm.message("Using crs: " + CrsUtilities.getCodeFromCrs(crs));
        }
        Envelope2D worldEnv = coverage.getEnvelope2D();
        GridEnvelope2D pixelEnv = referenceGridGeometry.worldToGrid(worldEnv);
        int minPX = (int) pixelEnv.getMinX();
        int minPY = (int) pixelEnv.getMinY();
        int maxPX = (int) pixelEnv.getMaxX();
        int maxPY = (int) pixelEnv.getMaxY();
        if (minPX < wp)
            wp = minPX;
        if (minPY < sp)
            sp = minPY;
        if (maxPX > ep)
            ep = maxPX;
        if (maxPY > np)
            np = maxPY;
        double minWX = worldEnv.getMinX();
        double minWY = worldEnv.getMinY();
        double maxWX = worldEnv.getMaxX();
        double maxWY = worldEnv.getMaxY();
        if (minWX < w)
            w = minWX;
        if (minWY < s)
            s = minWY;
        if (maxWX > e)
            e = maxWX;
        if (maxWY > n)
            n = maxWY;
        pm.worked(1);
    }
    pm.done();
    int endWidth = ep - wp;
    int endHeight = np - sp;
    pm.message(MessageFormat.format("Output raster will have {0} cols and {1} rows.", endWidth, endHeight));
    WritableRaster outputWR = CoverageUtilities.createWritableRaster(endWidth, endHeight, null, null, HMConstants.doubleNovalue);
    WritableRandomIter outputIter = RandomIterFactory.createWritable(outputWR, null);
    int offestX = Math.abs(wp);
    int offestY = Math.abs(sp);
    int index = 1;
    for (File coverageFile : inFiles) {
        GridCoverage2D coverage = OmsRasterReader.readRaster(coverageFile.getAbsolutePath());
        RenderedImage renderedImage = coverage.getRenderedImage();
        RandomIter randomIter = RandomIterFactory.create(renderedImage, null);
        Envelope2D env = coverage.getEnvelope2D();
        GridEnvelope2D repEnv = referenceGridGeometry.worldToGrid(env);
        GridGeometry2D tmpGG = coverage.getGridGeometry();
        GridEnvelope2D tmpEnv = tmpGG.worldToGrid(env);
        int startX = (int) (repEnv.getMinX() + offestX);
        int startY = (int) (repEnv.getMinY() + offestY);
        double tmpW = tmpEnv.getWidth();
        double tmpH = tmpEnv.getHeight();
        // $NON-NLS-1$
        pm.beginTask("Patch map " + index++, (int) tmpW);
        for (int y = 0; y < tmpH; y++) {
            for (int x = 0; x < tmpW; x++) {
                double value = randomIter.getSampleDouble(x, y, 0);
                outputIter.setSample(x + startX, y + startY, 0, value);
            }
            pm.worked(1);
        }
        pm.done();
        randomIter.done();
    }
    HashMap<String, Double> envelopeParams = new HashMap<String, Double>();
    envelopeParams.put(NORTH, n);
    envelopeParams.put(SOUTH, s);
    envelopeParams.put(WEST, w);
    envelopeParams.put(EAST, e);
    // $NON-NLS-1$
    outRaster = CoverageUtilities.buildCoverage("patch", outputWR, envelopeParams, crs);
}
Also used : GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) HashMap(java.util.HashMap) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) Envelope2D(org.geotools.geometry.Envelope2D) GridEnvelope2D(org.geotools.coverage.grid.GridEnvelope2D) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) GridEnvelope2D(org.geotools.coverage.grid.GridEnvelope2D) WritableRaster(java.awt.image.WritableRaster) RenderedImage(java.awt.image.RenderedImage) File(java.io.File) Execute(oms3.annotations.Execute)

Example 87 with WritableRandomIter

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

the class OmsPointsRasterizer method process.

@Execute
public void process() throws Exception {
    checkNull(inGrid, inVector);
    SimpleFeatureType schema = inVector.getSchema();
    if (!EGeometryType.isPoint(schema.getGeometryDescriptor())) {
        throw new ModelsRuntimeException("The module works only with point vectors.", this);
    }
    RegionMap regionMap = CoverageUtilities.gridGeometry2RegionParamsMap(inGrid);
    double n = regionMap.getNorth();
    double s = regionMap.getSouth();
    double e = regionMap.getEast();
    double w = regionMap.getWest();
    WritableRaster outWR = CoverageUtilities.createWritableRaster(regionMap.getCols(), regionMap.getRows(), null, null, HMConstants.doubleNovalue);
    WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
    List<FeatureMate> matesList = FeatureUtilities.featureCollectionToMatesList(inVector);
    double value = 0;
    pm.beginTask("Rasterizing points...", matesList.size());
    for (FeatureMate featureMate : matesList) {
        Geometry geometry = featureMate.getGeometry();
        if (fCat == null) {
            Double cat = featureMate.getAttribute(fCat, Double.class);
            if (cat != null) {
                value = cat;
            }
        }
        Coordinate[] coordinates = geometry.getCoordinates();
        for (Coordinate coordinate : coordinates) {
            if (!NumericsUtilities.isBetween(coordinate.x, w, e) || !NumericsUtilities.isBetween(coordinate.y, s, n)) {
                continue;
            }
            GridCoordinates2D onGrid = inGrid.worldToGrid(new DirectPosition2D(coordinate.x, coordinate.y));
            outIter.setSample(onGrid.x, onGrid.y, 0, value);
        }
        pm.worked(1);
    }
    pm.done();
    outRaster = CoverageUtilities.buildCoverage("pointsraster", outWR, regionMap, inVector.getSchema().getCoordinateReferenceSystem());
}
Also used : RegionMap(org.hortonmachine.gears.utils.RegionMap) DirectPosition2D(org.geotools.geometry.DirectPosition2D) Geometry(org.locationtech.jts.geom.Geometry) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) FeatureMate(org.hortonmachine.gears.utils.features.FeatureMate) Coordinate(org.locationtech.jts.geom.Coordinate) WritableRaster(java.awt.image.WritableRaster) GridCoordinates2D(org.geotools.coverage.grid.GridCoordinates2D) ModelsRuntimeException(org.hortonmachine.gears.libs.exceptions.ModelsRuntimeException) Execute(oms3.annotations.Execute)

Example 88 with WritableRandomIter

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

the class OmsCutOut method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outRaster == null, doReset)) {
        return;
    }
    // do autoboxing only once
    if (pMax != null) {
        max = pMax;
        doMax = true;
    }
    if (pMin != null) {
        min = pMin;
        doMin = true;
    }
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
    int nCols = regionMap.getCols();
    int nRows = regionMap.getRows();
    RenderedImage geodataRI = inRaster.getRenderedImage();
    RandomIter geodataIter = RandomIterFactory.create(geodataRI, null);
    double maskNv = 0;
    if (inMask != null) {
        RenderedImage maskRI = inMask.getRenderedImage();
        maskNv = HMConstants.getNovalue(inMask);
        maskIter = RandomIterFactory.create(maskRI, null);
    }
    WritableRaster outWR = CoverageUtilities.renderedImage2WritableRaster(geodataRI, false);
    WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
    try {
        double _maskNv = maskNv;
        pm.beginTask("Processing map...", nRows * nCols);
        processGrid(nCols, nRows, false, (c, r) -> {
            if (pm.isCanceled()) {
                return;
            }
            pm.worked(1);
            if (maskIter != null) {
                double maskValue = maskIter.getSampleDouble(c, r, 0);
                if (!doInverse) {
                    if (isNovalue(maskValue, _maskNv)) {
                        outIter.setSample(c, r, 0, HMConstants.doubleNovalue);
                        return;
                    }
                } else {
                    if (!isNovalue(maskValue, _maskNv)) {
                        outIter.setSample(c, r, 0, HMConstants.doubleNovalue);
                        return;
                    }
                }
            }
            double value = geodataIter.getSampleDouble(c, r, 0);
            if (doMax && value > max) {
                outIter.setSample(c, r, 0, HMConstants.doubleNovalue);
                return;
            }
            if (doMin && value < min) {
                outIter.setSample(c, r, 0, HMConstants.doubleNovalue);
            }
        });
        pm.done();
    } finally {
        geodataIter.done();
        outIter.done();
        if (maskIter != null) {
            maskIter.done();
        }
    }
    outRaster = CoverageUtilities.buildCoverage("cutout", outWR, regionMap, inRaster.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 89 with WritableRandomIter

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

the class OmsBiasedSigmaFilter method process.

@Execute
public void process() throws Exception {
    checkNull(inGeodata);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inGeodata);
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    double xres = regionMap.getXres();
    double yres = regionMap.getYres();
    RandomIter inIter = CoverageUtilities.getRandomIterator(inGeodata);
    double novalue = HMConstants.getNovalue(inGeodata);
    WritableRaster outWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, novalue);
    WritableRandomIter outIter = CoverageUtilities.getWritableRandomIterator(outWR);
    int part = (pWindow - 1) / 2;
    pm.beginTask("Processing filter...", cols - (pWindow - 1));
    for (int r = part; r < rows - part; r++) {
        for (int c = part; c < cols - part; c++) {
            GridNode node = new GridNode(inIter, cols, rows, xres, yres, c, r, novalue);
            if (node.isValid() && !node.touchesBound()) {
                double[][] window = node.getWindow(pWindow, false);
                double elevation = node.elevation;
                double sumUpper = 0;
                double countUpper = 0;
                double sumLower = 0;
                double countLower = 0;
                for (int i = 0; i < window.length; i++) {
                    for (int j = 0; j < window[0].length; j++) {
                        if (i == 1 && j == 1) {
                            continue;
                        }
                        if (window[i][j] >= elevation) {
                            sumUpper = sumUpper + window[i][j];
                            countUpper++;
                        } else {
                            sumLower = sumLower + window[i][j];
                            countLower++;
                        }
                    }
                }
                double avgUpper = sumUpper / countUpper;
                double avgLower = sumLower / countLower;
                double value = 0;
                if (countUpper == 0) {
                    value = avgLower;
                } else if (countLower == 0) {
                    value = avgUpper;
                } else {
                    double deltaUpper = abs(elevation - avgUpper);
                    double deltaLower = abs(elevation - avgLower);
                    if (deltaUpper < deltaLower) {
                        value = avgUpper;
                    } else {
                        value = avgLower;
                    }
                }
                if (!isNovalue(value)) {
                    outIter.setSample(c, r, 0, value);
                }
            }
        }
        pm.worked(1);
    }
    pm.done();
    outIter.done();
    outGeodata = CoverageUtilities.buildCoverage("sigma", outWR, regionMap, inGeodata.getCoordinateReferenceSystem());
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) GridNode(org.hortonmachine.gears.libs.modules.GridNode) 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 90 with WritableRandomIter

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

the class OmsKernelFilter method process.

public void process() throws Exception {
    checkNull(inRaster);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    ValueType type = getKernelType(pKernel);
    KernelJAI kernel = KernelFactory.createCircle(pRadius, type);
    RenderedImage inImg = inRaster.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("Processing...", cols - 2 * pRadius);
    for (int r = pRadius; r < rows - pRadius; r++) {
        for (int c = pRadius; c < cols - pRadius; c++) {
            double kernelSum = 0.0;
            int k = 0;
            double outputValue = 0.0;
            for (int kr = -pRadius; kr <= pRadius; kr++, k++) {
                for (int kc = -pRadius; kc <= pRadius; kc++) {
                    double value = inIter.getSampleDouble(c + kc, r + kr, 0);
                    if (!isNovalue(value)) {
                        outputValue = outputValue + value * kernelData[k];
                        kernelSum = kernelSum + kernelData[k];
                    }
                }
            }
            outIter.setSample(c, r, 0, outputValue / kernelSum);
        }
        pm.worked(1);
    }
    pm.done();
    outRaster = CoverageUtilities.buildCoverage("filtered", outWR, regionMap, inRaster.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)

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