Search in sources :

Example 11 with WritableRandomIter

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

the class OmsMorpher method lineendings.

public static void lineendings(WritableRaster inWR, RegionMap regionMap, WritableRaster outWR, int[][] kernels) {
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    WritableRandomIter inIter = RandomIterFactory.createWritable(inWR, null);
    WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
    BinaryFast binaryData = toBinaryFast(cols, rows, inIter);
    new Thin().processLineendings(binaryData, kernels);
    int[] values = binaryData.getValues();
    int index = 0;
    for (int y = 0; y < rows; y++) {
        for (int x = 0; x < cols; x++) {
            double value = (double) values[index];
            if (value == 0) {
                value = doubleNovalue;
            }
            double origValue = inIter.getSampleDouble(x, y, 0);
            if (!isNovalue(origValue) && isNovalue(value)) {
                outIter.setSample(x, y, 0, 1);
            }
            index++;
        }
    }
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) BinaryFast(org.hortonmachine.gears.modules.utils.BinaryFast)

Example 12 with WritableRandomIter

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

the class OmsMorpher method prune.

public static void prune(WritableRaster inWR, RegionMap regionMap, WritableRaster outWR, int[][] kernels, int iterations) {
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    WritableRandomIter inIter = RandomIterFactory.createWritable(inWR, null);
    WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
    BinaryFast binaryData = toBinaryFast(cols, rows, inIter);
    new Thin().processPruning(binaryData, iterations, kernels);
    fromBinaryFast(cols, rows, outIter, binaryData);
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) BinaryFast(org.hortonmachine.gears.modules.utils.BinaryFast)

Example 13 with WritableRandomIter

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

the class OmsRasterNormalizer method process.

@Execute
public void process() throws Exception {
    checkNull(inRaster);
    double[] minMax = OmsRasterSummary.getMinMax(inRaster);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
    int nCols = regionMap.getCols();
    int nRows = regionMap.getRows();
    RandomIter rasterIter = CoverageUtilities.getRandomIterator(inRaster);
    WritableRaster outWR = CoverageUtilities.createWritableRaster(nCols, nRows, null, null, doubleNovalue);
    WritableRandomIter outIter = RandomIterFactory.createWritable(outWR, null);
    pm.beginTask("Normalizing...", nRows);
    for (int r = 0; r < nRows; r++) {
        if (pm.isCanceled()) {
            return;
        }
        for (int c = 0; c < nCols; c++) {
            double value = rasterIter.getSampleDouble(c, r, 0);
            if (isNovalue(value)) {
                if (doSetnovalues) {
                    outIter.setSample(c, r, 0, 0.0);
                }
                continue;
            }
            double normalizedValue = NumericsUtilities.normalize(minMax[1], minMax[0], value, pNValue);
            outIter.setSample(c, r, 0, normalizedValue);
        }
        pm.worked(1);
    }
    pm.done();
    outRaster = CoverageUtilities.buildCoverage("normalized", 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) Execute(oms3.annotations.Execute)

Example 14 with WritableRandomIter

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

the class OmsRasterConverter method process.

@Execute
public void process() throws Exception {
    checkNull(inRaster);
    if (pOutType == null || pOutType.length() == 0) {
        outRaster = inRaster;
    } else {
        // convert type
        RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
        int nCols = regionMap.getCols();
        int nRows = regionMap.getRows();
        RandomIter inIter = CoverageUtilities.getRandomIterator(inRaster);
        WritableRaster outWritableRaster = null;
        WritableRandomIter outIter = null;
        try {
            switch(pOutType) {
                case TYPE_INT:
                    outWritableRaster = CoverageUtilities.createWritableRaster(nCols, nRows, Integer.class, null, null);
                    outIter = CoverageUtilities.getWritableRandomIterator(outWritableRaster);
                    pm.beginTask("Converting...", nRows);
                    for (int r = 0; r < nRows; r++) {
                        if (pm.isCanceled()) {
                            return;
                        }
                        for (int c = 0; c < nCols; c++) {
                            int value = inIter.getSample(c, r, 0);
                            outIter.setSample(c, r, 0, value);
                        }
                        pm.worked(1);
                    }
                    pm.done();
                    break;
                case TYPE_FLOAT:
                    outWritableRaster = CoverageUtilities.createWritableRaster(nCols, nRows, Float.class, null, null);
                    outIter = CoverageUtilities.getWritableRandomIterator(outWritableRaster);
                    try {
                        pm.beginTask("Converting...", nRows);
                        for (int r = 0; r < nRows; r++) {
                            if (pm.isCanceled()) {
                                return;
                            }
                            for (int c = 0; c < nCols; c++) {
                                float value = inIter.getSampleFloat(c, r, 0);
                                outIter.setSample(c, r, 0, value);
                            }
                            pm.worked(1);
                        }
                        pm.done();
                    } finally {
                        outIter.done();
                    }
                    break;
                case TYPE_DOUBLE:
                default:
                    outWritableRaster = CoverageUtilities.createWritableRaster(nCols, nRows, Double.class, null, null);
                    outIter = CoverageUtilities.getWritableRandomIterator(outWritableRaster);
                    try {
                        pm.beginTask("Converting...", nRows);
                        for (int r = 0; r < nRows; r++) {
                            if (pm.isCanceled()) {
                                return;
                            }
                            for (int c = 0; c < nCols; c++) {
                                double value = inIter.getSampleDouble(c, r, 0);
                                outIter.setSample(c, r, 0, value);
                            }
                            pm.worked(1);
                        }
                        pm.done();
                    } finally {
                        outIter.done();
                    }
                    break;
            }
            outRaster = CoverageUtilities.buildCoverage("converted", outWritableRaster, regionMap, inRaster.getCoordinateReferenceSystem());
        } finally {
            outIter.done();
            inIter.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 15 with WritableRandomIter

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

the class OmsBobTheBuilder method process.

@Execute
public void process() throws Exception {
    checkNull(inRaster, inArea, inElevations, fElevation);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    double west = regionMap.getWest();
    double east = regionMap.getEast();
    double south = regionMap.getSouth();
    double north = regionMap.getNorth();
    ReferencedEnvelope vectorBounds = inArea.getBounds();
    if (!isBetween(vectorBounds.getMaxX(), west, east) || !isBetween(vectorBounds.getMinX(), west, east) || !isBetween(vectorBounds.getMaxY(), south, north) || !isBetween(vectorBounds.getMinY(), south, north)) {
        throw new ModelsIllegalargumentException("The vector map has to be within the raster map boundaries.", this, pm);
    }
    List<FeatureMate> polygonMates = FeatureUtilities.featureCollectionToMatesList(inArea);
    String polygonMessage = "This operation can be applied only to a single polygon.";
    if (polygonMates.size() != 1) {
        throw new ModelsIllegalargumentException(polygonMessage, this, pm);
    }
    FeatureMate polygonMate = polygonMates.get(0);
    Geometry polygon = polygonMate.getGeometry();
    if (polygon instanceof MultiPolygon) {
        polygon = polygon.getGeometryN(0);
    }
    if (!(polygon instanceof Polygon)) {
        throw new ModelsIllegalargumentException(polygonMessage, this, pm);
    }
    List<FeatureMate> pointsMates = FeatureUtilities.featureCollectionToMatesList(inElevations);
    if (doUseOnlyInternal) {
        PreparedGeometry preparedPolygon = PreparedGeometryFactory.prepare(polygon);
        List<FeatureMate> tmpPointsMates = new ArrayList<FeatureMate>();
        for (FeatureMate pointMate : pointsMates) {
            Geometry geometry = pointMate.getGeometry();
            if (preparedPolygon.covers(geometry)) {
                tmpPointsMates.add(pointMate);
            }
        }
        pointsMates = tmpPointsMates;
    }
    if (pointsMates.size() < 4) {
        throw new ModelsIllegalargumentException("You need at least 4 elevation points (the more, the better) to gain a decent interpolation.", this, pm);
    }
    List<Coordinate> controlPointsList = new ArrayList<Coordinate>();
    if (doPolygonborder) {
        pm.beginTask("Extract polygon border...", IHMProgressMonitor.UNKNOWN);
        Coordinate[] polygonCoordinates = polygon.getCoordinates();
        List<ProfilePoint> profile = CoverageUtilities.doProfile(inRaster, polygonCoordinates);
        for (ProfilePoint profilePoint : profile) {
            Coordinate position = profilePoint.getPosition();
            double elevation = profilePoint.getElevation();
            Coordinate coord = new Coordinate(position.x, position.y, elevation);
            controlPointsList.add(coord);
        }
        pm.done();
    }
    for (FeatureMate pointsMate : pointsMates) {
        Coordinate coordinate = pointsMate.getGeometry().getCoordinate();
        double elev = pointsMate.getAttribute(fElevation, Double.class);
        Coordinate coord = new Coordinate(coordinate.x, coordinate.y, elev);
        controlPointsList.add(coord);
    }
    Coordinate[] controlPoints = controlPointsList.toArray(new Coordinate[0]);
    GridGeometry2D gridGeometry = inRaster.getGridGeometry();
    RandomIter elevIter = CoverageUtilities.getRandomIterator(inRaster);
    WritableRaster outputWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, HMConstants.doubleNovalue);
    WritableRandomIter outputIter = RandomIterFactory.createWritable(outputWR, null);
    DefaultFeatureCollection newCollection = new DefaultFeatureCollection();
    newCollection.add(polygonMate.getFeature());
    OmsScanLineRasterizer slRasterizer = new OmsScanLineRasterizer();
    slRasterizer.pm = pm;
    slRasterizer.inVector = newCollection;
    slRasterizer.pCols = cols;
    slRasterizer.pRows = rows;
    slRasterizer.pNorth = north;
    slRasterizer.pSouth = south;
    slRasterizer.pEast = east;
    slRasterizer.pWest = west;
    slRasterizer.pValue = 1.0;
    slRasterizer.process();
    GridCoverage2D outRasterized = slRasterizer.outRaster;
    if (pMaxbuffer < 0)
        pMaxbuffer = Math.max(vectorBounds.getWidth(), vectorBounds.getHeight());
    IDWInterpolator interpolator = new IDWInterpolator(pMaxbuffer);
    final GridCoordinates2D gridCoord = new GridCoordinates2D();
    RandomIter rasterizedIter = CoverageUtilities.getRandomIterator(outRasterized);
    pm.beginTask("Interpolating...", cols);
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            double probValue = rasterizedIter.getSampleDouble(c, r, 0);
            if (isNovalue(probValue)) {
                continue;
            }
            gridCoord.setLocation(c, r);
            DirectPosition world = gridGeometry.gridToWorld(gridCoord);
            double[] coordinate = world.getCoordinate();
            double interpolated = interpolator.getValue(controlPoints, new Coordinate(coordinate[0], coordinate[1]));
            outputIter.setSample(c, r, 0, interpolated);
        }
        pm.worked(1);
    }
    pm.done();
    pm.beginTask("Merging with original raster...", cols);
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            double interpolatedValue = outputIter.getSampleDouble(c, r, 0);
            double rasterValue = elevIter.getSampleDouble(c, r, 0);
            if (isNovalue(interpolatedValue)) {
                if (!isNovalue(rasterValue))
                    outputIter.setSample(c, r, 0, rasterValue);
            } else {
                if (doErode) {
                    // any value generated is ok
                    outputIter.setSample(c, r, 0, interpolatedValue);
                } else {
                    // only values higher than the raster are ok
                    if (!isNovalue(rasterValue)) {
                        if (rasterValue < interpolatedValue) {
                            outputIter.setSample(c, r, 0, interpolatedValue);
                        } else {
                            outputIter.setSample(c, r, 0, rasterValue);
                        }
                    } else {
                        outputIter.setSample(c, r, 0, interpolatedValue);
                    }
                }
            }
        }
        pm.worked(1);
    }
    pm.done();
    outRaster = CoverageUtilities.buildCoverage("raster", outputWR, regionMap, inRaster.getCoordinateReferenceSystem());
}
Also used : DirectPosition(org.opengis.geometry.DirectPosition) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) RegionMap(org.hortonmachine.gears.utils.RegionMap) ArrayList(java.util.ArrayList) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) FeatureMate(org.hortonmachine.gears.utils.features.FeatureMate) WritableRaster(java.awt.image.WritableRaster) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) ProfilePoint(org.hortonmachine.gears.utils.coverage.ProfilePoint) ProfilePoint(org.hortonmachine.gears.utils.coverage.ProfilePoint) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) Geometry(org.locationtech.jts.geom.Geometry) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Coordinate(org.locationtech.jts.geom.Coordinate) OmsScanLineRasterizer(org.hortonmachine.gears.modules.r.scanline.OmsScanLineRasterizer) GridCoordinates2D(org.geotools.coverage.grid.GridCoordinates2D) IDWInterpolator(org.hortonmachine.gears.modules.r.interpolation2d.core.IDWInterpolator) 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