Search in sources :

Example 1 with ROIShape

use of javax.media.jai.ROIShape in project hortonmachine by TheHortonMachine.

the class CoverageUtilities method prepareROI.

/**
 * Utility method for transforming a geometry ROI into the raster space, using the provided affine transformation.
 *
 * @param roi a {@link Geometry} in model space.
 * @param mt2d an {@link AffineTransform} that maps from raster to model space. This is already referred to the pixel corner.
 * @return a {@link ROI} suitable for using with JAI.
 * @throws ProcessException in case there are problems with ivnerting the provided {@link AffineTransform}. Very unlikely to happen.
 */
public static ROI prepareROI(Geometry roi, AffineTransform mt2d) throws Exception {
    // transform the geometry to raster space so that we can use it as a ROI source
    Geometry rasterSpaceGeometry = JTS.transform(roi, new AffineTransform2D(mt2d.createInverse()));
    // simplify the geometry so that it's as precise as the coverage, excess coordinates
    // just make it slower to determine the point in polygon relationship
    Geometry simplifiedGeometry = DouglasPeuckerSimplifier.simplify(rasterSpaceGeometry, 1);
    // build a shape using a fast point in polygon wrapper
    return new ROIShape(new FastLiteShape(simplifiedGeometry));
}
Also used : PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) Geometry(org.locationtech.jts.geom.Geometry) ROIShape(javax.media.jai.ROIShape) FastLiteShape(org.hortonmachine.gears.utils.features.FastLiteShape) AffineTransform2D(org.geotools.referencing.operation.transform.AffineTransform2D)

Example 2 with ROIShape

use of javax.media.jai.ROIShape in project hortonmachine by TheHortonMachine.

the class OmsRangeLookup method process.

@SuppressWarnings("nls")
@Execute
public void process() throws Exception {
    if (!concatOr(outRaster == null, doReset)) {
        return;
    }
    // FIXME remove when the jaitools rangelookup is not pulled fro
    JAIExt.initJAIEXT(true);
    // raster process anymore
    checkNull(inRaster, pRanges, pClasses);
    double novalue = HMConstants.getNovalue(inRaster);
    RenderedImage inRI = inRaster.getRenderedImage();
    RangeLookupTable.Builder<Double, Double> builder = new RangeLookupTable.Builder<Double, Double>();
    String[] rangesSplit = pRanges.trim().split(",");
    String[] classesSplit = pClasses.trim().split(",");
    if (rangesSplit.length != classesSplit.length) {
        throw new ModelsIllegalargumentException("Ranges and classes must be in pairs!", this, pm);
    }
    for (int i = 0; i < rangesSplit.length; i++) {
        String classStr = classesSplit[i].trim();
        double classNum = Double.parseDouble(classStr);
        String range = rangesSplit[i].trim();
        boolean minIncluded = false;
        boolean maxIncluded = false;
        if (range.startsWith("[")) {
            minIncluded = true;
        }
        if (range.endsWith("]")) {
            maxIncluded = true;
        }
        String rangeNoBrac = range.replaceAll("\\[|\\]|\\(|\\)", "");
        String[] split = rangeNoBrac.trim().split("\\s+");
        Double min = null;
        try {
            if (split[0].equals("null")) {
                min = Double.NEGATIVE_INFINITY;
            } else {
                min = Double.parseDouble(split[0]);
            }
        } catch (Exception e) {
        // can be null
        }
        Double max = null;
        try {
            if (split[1].equals("null")) {
                max = Double.POSITIVE_INFINITY;
            } else {
                max = Double.parseDouble(split[1]);
            }
        } catch (Exception e) {
        // can be null
        }
        Range r = RangeFactory.create(min, minIncluded, max, maxIncluded);
        builder.add(r, classNum);
    }
    // List<org.jaitools.numeric.Range> ranges;
    // new RangeLookupProcess().execute(inRaster, 0, ranges, null);
    RangeLookupTable<Double, Double> table = builder.build();
    ROIShape roi = new ROIShape(new Rectangle(0, 0, inRI.getWidth(), inRI.getHeight()));
    ParameterBlockJAI pb = new ParameterBlockJAI("RLookup");
    pb.setSource("source0", inRI);
    pb.setParameter("table", table);
    pb.setParameter("roi", roi);
    pb.setParameter("default", (Double) novalue);
    RenderedImage lookupImg = JAI.create("RLookup", pb);
    HashMap<String, Double> regionMap = CoverageUtilities.getRegionParamsFromGridCoverage(inRaster);
    outRaster = CoverageUtilities.buildCoverageWithNovalue("rangelookup", lookupImg, regionMap, inRaster.getCoordinateReferenceSystem(), novalue);
}
Also used : ParameterBlockJAI(javax.media.jai.ParameterBlockJAI) Rectangle(java.awt.Rectangle) Range(it.geosolutions.jaiext.range.Range) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) RangeLookupTable(it.geosolutions.jaiext.rlookup.RangeLookupTable) ROIShape(javax.media.jai.ROIShape) RenderedImage(java.awt.image.RenderedImage) Execute(oms3.annotations.Execute)

Aggregations

ROIShape (javax.media.jai.ROIShape)2 Range (it.geosolutions.jaiext.range.Range)1 RangeLookupTable (it.geosolutions.jaiext.rlookup.RangeLookupTable)1 Rectangle (java.awt.Rectangle)1 RenderedImage (java.awt.image.RenderedImage)1 ParameterBlockJAI (javax.media.jai.ParameterBlockJAI)1 Execute (oms3.annotations.Execute)1 AffineTransform2D (org.geotools.referencing.operation.transform.AffineTransform2D)1 ModelsIllegalargumentException (org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException)1 FastLiteShape (org.hortonmachine.gears.utils.features.FastLiteShape)1 Geometry (org.locationtech.jts.geom.Geometry)1 PreparedGeometry (org.locationtech.jts.geom.prep.PreparedGeometry)1