Search in sources :

Example 11 with RandomIter

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

the class OmsRasterValueRounder method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outRaster == null, doReset)) {
        return;
    }
    checkNull(inRaster, pPattern);
    formatter = new DecimalFormat(pPattern);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
    int rows = regionMap.getRows();
    int cols = regionMap.getCols();
    WritableRaster outWR = CoverageUtilities.createWritableRaster(cols, rows, null, null, null);
    RandomIter inRasterIter = CoverageUtilities.getRandomIterator(inRaster);
    WritableRandomIter outIter = CoverageUtilities.getWritableRandomIterator(outWR);
    pm.beginTask("Rounding data...", rows);
    for (int r = 0; r < rows; r++) {
        if (isCanceled(pm)) {
            return;
        }
        for (int c = 0; c < cols; c++) {
            double value = inRasterIter.getSampleDouble(c, r, 0);
            if (!isNovalue(value)) {
                String formatted = formatter.format(value);
                value = Double.parseDouble(formatted);
                outIter.setSample(c, r, 0, value);
            } else {
                outIter.setSample(c, r, 0, doubleNovalue);
            }
        }
        pm.worked(1);
    }
    pm.done();
    outIter.done();
    outRaster = CoverageUtilities.buildCoverage("rounded", outWR, regionMap, inRaster.getCoordinateReferenceSystem());
}
Also used : WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) WritableRaster(java.awt.image.WritableRaster) DecimalFormat(java.text.DecimalFormat) RegionMap(org.hortonmachine.gears.utils.RegionMap) RandomIter(javax.media.jai.iterator.RandomIter) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) Execute(oms3.annotations.Execute)

Example 12 with RandomIter

use of javax.media.jai.iterator.RandomIter 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 13 with RandomIter

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

the class OmsRaster2Xyz method process.

@SuppressWarnings("nls")
@Execute
public void process() throws Exception {
    checkNull(inRaster);
    RandomIter rasterIter = CoverageUtilities.getRandomIterator(inRaster);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
    GridGeometry2D gridGeometry = inRaster.getGridGeometry();
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(inFile));
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                double elevation = rasterIter.getSampleDouble(c, r, 0);
                if (doRemovenv && HMConstants.isNovalue(elevation)) {
                    continue;
                }
                DirectPosition position = gridGeometry.gridToWorld(new GridCoordinates2D(c, r));
                double[] coordinate = position.getCoordinate();
                StringBuilder sb = new StringBuilder();
                sb.append(coordinate[0]);
                sb.append("\t");
                sb.append(coordinate[1]);
                sb.append("\t");
                sb.append(elevation);
                sb.append("\n");
                writer.write(sb.toString());
            }
        }
    } finally {
        if (writer != null)
            writer.close();
    }
}
Also used : DirectPosition(org.opengis.geometry.DirectPosition) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) FileWriter(java.io.FileWriter) RegionMap(org.hortonmachine.gears.utils.RegionMap) GridCoordinates2D(org.geotools.coverage.grid.GridCoordinates2D) RandomIter(javax.media.jai.iterator.RandomIter) BufferedWriter(java.io.BufferedWriter) Execute(oms3.annotations.Execute)

Example 14 with RandomIter

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

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

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