Search in sources :

Example 1 with ISurfaceInterpolator

use of org.hortonmachine.gears.modules.r.interpolation2d.core.ISurfaceInterpolator in project hortonmachine by TheHortonMachine.

the class OmsHoleFiller method process.

@Execute
public void process() throws Exception {
    checkNull(inRaster);
    ISurfaceInterpolator interpolator;
    if (pMode.equals(IDW)) {
        interpolator = new IDWInterpolator(pBuffer);
    } else {
        interpolator = new TPSInterpolator(pBuffer);
    }
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
    int rows = regionMap.getRows();
    int cols = regionMap.getCols();
    WritableRaster outWR = CoverageUtilities.renderedImage2WritableRaster(inRaster.getRenderedImage(), false);
    WritableRandomIter outIter = CoverageUtilities.getWritableRandomIterator(outWR);
    GridGeometry2D gridGeometry = inRaster.getGridGeometry();
    PreparedGeometry preparedRoi = null;
    if (inROI != null) {
        List<Geometry> roiList = FeatureUtilities.featureCollectionToGeometriesList(inROI, false, null);
        GeometryCollection gc = new GeometryCollection(roiList.toArray(GeometryUtilities.TYPE_GEOMETRY), gf);
        preparedRoi = PreparedGeometryFactory.prepare(gc);
    }
    pm.beginTask("Filling holes...", cols - 2);
    for (int r = 1; r < rows - 1; r++) {
        for (int c = 1; c < cols - 1; c++) {
            if (pm.isCanceled()) {
                return;
            }
            double value = outIter.getSampleDouble(c, r, 0);
            if (isNovalue(value)) {
                DirectPosition worldPosition = gridGeometry.gridToWorld(new GridCoordinates2D(c, r));
                double[] coordinate = worldPosition.getCoordinate();
                Coordinate pointCoordinate = new Coordinate(coordinate[0], coordinate[1]);
                Point point = gf.createPoint(pointCoordinate);
                if (preparedRoi == null || preparedRoi.intersects(point)) {
                    // TODO this could be done considering more points and more far away points.
                    // For now, this works.
                    List<Coordinate> surroundingValids = getValidSurroundingPoints(outIter, gridGeometry, c, r);
                    if (surroundingValids.size() > 3) {
                        double newValue = interpolator.getValue(surroundingValids.toArray(new Coordinate[0]), pointCoordinate);
                        outIter.setSample(c, r, 0, newValue);
                    }
                }
            }
        }
        pm.worked(1);
    }
    pm.done();
    outIter.done();
    outRaster = CoverageUtilities.buildCoverage("nulled", outWR, regionMap, inRaster.getCoordinateReferenceSystem());
}
Also used : ISurfaceInterpolator(org.hortonmachine.gears.modules.r.interpolation2d.core.ISurfaceInterpolator) DirectPosition(org.opengis.geometry.DirectPosition) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) RegionMap(org.hortonmachine.gears.utils.RegionMap) Point(org.locationtech.jts.geom.Point) Point(org.locationtech.jts.geom.Point) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) Geometry(org.locationtech.jts.geom.Geometry) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) WritableRandomIter(javax.media.jai.iterator.WritableRandomIter) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) Coordinate(org.locationtech.jts.geom.Coordinate) WritableRaster(java.awt.image.WritableRaster) TPSInterpolator(org.hortonmachine.gears.modules.r.interpolation2d.core.TPSInterpolator) GridCoordinates2D(org.geotools.coverage.grid.GridCoordinates2D) IDWInterpolator(org.hortonmachine.gears.modules.r.interpolation2d.core.IDWInterpolator) Execute(oms3.annotations.Execute)

Aggregations

WritableRaster (java.awt.image.WritableRaster)1 WritableRandomIter (javax.media.jai.iterator.WritableRandomIter)1 Execute (oms3.annotations.Execute)1 GridCoordinates2D (org.geotools.coverage.grid.GridCoordinates2D)1 GridGeometry2D (org.geotools.coverage.grid.GridGeometry2D)1 IDWInterpolator (org.hortonmachine.gears.modules.r.interpolation2d.core.IDWInterpolator)1 ISurfaceInterpolator (org.hortonmachine.gears.modules.r.interpolation2d.core.ISurfaceInterpolator)1 TPSInterpolator (org.hortonmachine.gears.modules.r.interpolation2d.core.TPSInterpolator)1 RegionMap (org.hortonmachine.gears.utils.RegionMap)1 Coordinate (org.locationtech.jts.geom.Coordinate)1 Geometry (org.locationtech.jts.geom.Geometry)1 GeometryCollection (org.locationtech.jts.geom.GeometryCollection)1 Point (org.locationtech.jts.geom.Point)1 PreparedGeometry (org.locationtech.jts.geom.prep.PreparedGeometry)1 DirectPosition (org.opengis.geometry.DirectPosition)1