Search in sources :

Example 1 with OmsScanLineRasterizer

use of org.hortonmachine.gears.modules.r.scanline.OmsScanLineRasterizer in project hortonmachine by TheHortonMachine.

the class OmsRasterVectorIntersector method process.

@Execute
public void process() throws Exception {
    checkNull(inRaster, inVector);
    SimpleFeatureType schema = inVector.getSchema();
    if (!EGeometryType.isPolygon(schema.getGeometryDescriptor())) {
        throw new ModelsRuntimeException("The module works only with polygon vectors.", this);
    }
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
    OmsScanLineRasterizer raster = new OmsScanLineRasterizer();
    raster.inVector = inVector;
    raster.pCols = regionMap.getCols();
    raster.pRows = regionMap.getRows();
    raster.pNorth = regionMap.getNorth();
    raster.pSouth = regionMap.getSouth();
    raster.pEast = regionMap.getEast();
    raster.pWest = regionMap.getWest();
    raster.pValue = 1.0;
    raster.process();
    GridCoverage2D rasterizedVector = raster.outRaster;
    OmsCutOut cutout = new OmsCutOut();
    cutout.pm = pm;
    cutout.inRaster = inRaster;
    cutout.inMask = rasterizedVector;
    cutout.doInverse = doInverse;
    cutout.process();
    outRaster = cutout.outRaster;
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) OmsScanLineRasterizer(org.hortonmachine.gears.modules.r.scanline.OmsScanLineRasterizer) OmsCutOut(org.hortonmachine.gears.modules.r.cutout.OmsCutOut) RegionMap(org.hortonmachine.gears.utils.RegionMap) ModelsRuntimeException(org.hortonmachine.gears.libs.exceptions.ModelsRuntimeException) Execute(oms3.annotations.Execute)

Example 2 with OmsScanLineRasterizer

use of org.hortonmachine.gears.modules.r.scanline.OmsScanLineRasterizer 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)

Example 3 with OmsScanLineRasterizer

use of org.hortonmachine.gears.modules.r.scanline.OmsScanLineRasterizer in project hortonmachine by TheHortonMachine.

the class ScanLineRasterizer method process.

@Execute
public void process() throws Exception {
    OmsScanLineRasterizer scanlinerasterizer = new OmsScanLineRasterizer();
    scanlinerasterizer.inVector = getVector(inVector);
    scanlinerasterizer.inRaster = getRaster(inRaster);
    scanlinerasterizer.pValue = pValue;
    scanlinerasterizer.fCat = fCat;
    scanlinerasterizer.pNorth = pNorth;
    scanlinerasterizer.pSouth = pSouth;
    scanlinerasterizer.pWest = pWest;
    scanlinerasterizer.pEast = pEast;
    scanlinerasterizer.pRows = pRows;
    scanlinerasterizer.pCols = pCols;
    scanlinerasterizer.pUsePointInPolygon = pUsePointInPolygon;
    scanlinerasterizer.pm = pm;
    scanlinerasterizer.doProcess = doProcess;
    scanlinerasterizer.doReset = doReset;
    scanlinerasterizer.process();
    dumpRaster(scanlinerasterizer.outRaster, outRaster);
}
Also used : OmsScanLineRasterizer(org.hortonmachine.gears.modules.r.scanline.OmsScanLineRasterizer) Execute(oms3.annotations.Execute)

Example 4 with OmsScanLineRasterizer

use of org.hortonmachine.gears.modules.r.scanline.OmsScanLineRasterizer in project hortonmachine by TheHortonMachine.

the class RasterExtruder method process.

@Execute
public void process() throws Exception {
    checkNull(inRaster, inVector, pAddingField);
    GridCoverage2D dtm = getRaster(inRaster);
    SimpleFeatureCollection vector = getVector(inVector);
    OmsScanLineRasterizer r = new OmsScanLineRasterizer();
    r.inRaster = dtm;
    r.inVector = vector;
    r.fCat = pAddingField;
    r.process();
    GridCoverage2D outGC = r.outRaster;
    GridCoverage2D mergedGC = CoverageUtilities.mergeCoverages(outGC, dtm);
    dumpRaster(mergedGC, outRaster);
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) OmsScanLineRasterizer(org.hortonmachine.gears.modules.r.scanline.OmsScanLineRasterizer) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Execute(oms3.annotations.Execute)

Example 5 with OmsScanLineRasterizer

use of org.hortonmachine.gears.modules.r.scanline.OmsScanLineRasterizer in project hortonmachine by TheHortonMachine.

the class OmsMeltonNumber method process.

@Execute
public void process() throws Exception {
    checkNull(inElev, inFans);
    RegionMap regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inElev);
    int cols = regionMap.getCols();
    int rows = regionMap.getRows();
    double west = regionMap.getWest();
    double east = regionMap.getEast();
    double south = regionMap.getSouth();
    double north = regionMap.getNorth();
    AttributeType type = inFans.getSchema().getType(fId);
    if (type == null) {
        throw new ModelsIllegalargumentException(MessageFormat.format("The attribute {0} does not exist in the vector map.", fId), this, pm);
    }
    List<SimpleFeature> fansList = FeatureUtilities.featureCollectionToList(inFans);
    outMelton = new String[fansList.size()][2];
    int index = 0;
    pm.beginTask("Calculating Melton number for fans...", fansList.size());
    for (SimpleFeature fan : fansList) {
        Object attribute = fan.getAttribute(fId);
        // rasterize the fan
        DefaultFeatureCollection newCollection = new DefaultFeatureCollection();
        newCollection.add(fan);
        OmsScanLineRasterizer rasterizer = new OmsScanLineRasterizer();
        rasterizer.inVector = newCollection;
        rasterizer.pCols = cols;
        rasterizer.pRows = rows;
        rasterizer.pNorth = north;
        rasterizer.pSouth = south;
        rasterizer.pEast = east;
        rasterizer.pWest = west;
        rasterizer.pValue = 1.0;
        rasterizer.pm = new DummyProgressMonitor();
        rasterizer.process();
        GridCoverage2D rasterizedFan = rasterizer.outRaster;
        GridCoverage2D fanElev = CoverageUtilities.coverageValuesMapper(inElev, rasterizedFan);
        // extract min and max
        OmsRasterSummary summary = new OmsRasterSummary();
        summary.pm = new DummyProgressMonitor();
        summary.inRaster = fanElev;
        summary.process();
        double min = summary.outMin;
        double max = summary.outMax;
        // get the suface of the fan
        Geometry geometry = (Geometry) fan.getDefaultGeometry();
        double area = geometry.getArea();
        // calculate Melton
        double melton = (max - min) / sqrt(area);
        outMelton[index][0] = attribute.toString();
        outMelton[index][1] = String.valueOf(melton);
        index++;
        pm.message(MessageFormat.format("id: {0} gave Melton number: {1}", attribute.toString(), melton));
        pm.message("Based on max: " + max + " min: " + min + " and area: " + area);
        pm.worked(1);
    }
    pm.done();
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) RegionMap(org.hortonmachine.gears.utils.RegionMap) SimpleFeature(org.opengis.feature.simple.SimpleFeature) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) Geometry(org.locationtech.jts.geom.Geometry) OmsScanLineRasterizer(org.hortonmachine.gears.modules.r.scanline.OmsScanLineRasterizer) OmsRasterSummary(org.hortonmachine.gears.modules.r.summary.OmsRasterSummary) AttributeType(org.opengis.feature.type.AttributeType) DummyProgressMonitor(org.hortonmachine.gears.libs.monitor.DummyProgressMonitor) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) Execute(oms3.annotations.Execute)

Aggregations

OmsScanLineRasterizer (org.hortonmachine.gears.modules.r.scanline.OmsScanLineRasterizer)8 GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)7 Execute (oms3.annotations.Execute)6 ModelsIllegalargumentException (org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException)4 RegionMap (org.hortonmachine.gears.utils.RegionMap)4 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)3 DefaultFeatureCollection (org.geotools.feature.DefaultFeatureCollection)3 Geometry (org.locationtech.jts.geom.Geometry)3 WritableRaster (java.awt.image.WritableRaster)2 RandomIter (javax.media.jai.iterator.RandomIter)2 WritableRandomIter (javax.media.jai.iterator.WritableRandomIter)2 Coordinate (org.locationtech.jts.geom.Coordinate)2 SimpleFeature (org.opengis.feature.simple.SimpleFeature)2 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)2 RenderedImage (java.awt.image.RenderedImage)1 ArrayList (java.util.ArrayList)1 GridCoordinates2D (org.geotools.coverage.grid.GridCoordinates2D)1 GridGeometry2D (org.geotools.coverage.grid.GridGeometry2D)1 Envelope2D (org.geotools.geometry.Envelope2D)1 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)1