Search in sources :

Example 1 with AffineTransform2D

use of org.geotools.referencing.operation.transform.AffineTransform2D in project mapfish-print by mapfish.

the class CreateMapProcessor method areaOfInterestInScreenCRS.

private Polygon areaOfInterestInScreenCRS(@Nonnull final MapfishMapContext transformer, @Nullable final AreaOfInterest areaOfInterest) {
    if (areaOfInterest != null) {
        final AffineTransform worldToScreenTransform = worldToScreenTransform(transformer.toReferencedEnvelope(), transformer.getPaintArea());
        MathTransform mathTransform = new AffineTransform2D(worldToScreenTransform);
        final Polygon screenGeometry;
        try {
            screenGeometry = (Polygon) JTS.transform(areaOfInterest.getArea(), mathTransform);
        } catch (TransformException e) {
            throw new RuntimeException(e);
        }
        return screenGeometry;
    }
    return null;
}
Also used : MathTransform(org.opengis.referencing.operation.MathTransform) TransformException(org.opengis.referencing.operation.TransformException) AffineTransform(java.awt.geom.AffineTransform) Polygon(org.locationtech.jts.geom.Polygon) AffineTransform2D(org.geotools.referencing.operation.transform.AffineTransform2D)

Example 2 with AffineTransform2D

use of org.geotools.referencing.operation.transform.AffineTransform2D in project mapfish-print by mapfish.

the class CreateOverviewMapProcessor method rotateExtent.

private Geometry rotateExtent(final Geometry mapExtent, final double rotation, final ReferencedEnvelope originalEnvelope) {
    final Coordinate center = originalEnvelope.centre();
    final AffineTransform affineTransform = AffineTransform.getRotateInstance(rotation, center.x, center.y);
    final MathTransform mathTransform = new AffineTransform2D(affineTransform);
    try {
        return JTS.transform(mapExtent, mathTransform);
    } catch (TransformException e) {
        throw new RuntimeException("Failed to rotate map extent", e);
    }
}
Also used : MathTransform(org.opengis.referencing.operation.MathTransform) Coordinate(org.locationtech.jts.geom.Coordinate) TransformException(org.opengis.referencing.operation.TransformException) AffineTransform(java.awt.geom.AffineTransform) AffineTransform2D(org.geotools.referencing.operation.transform.AffineTransform2D)

Example 3 with AffineTransform2D

use of org.geotools.referencing.operation.transform.AffineTransform2D in project hortonmachine by TheHortonMachine.

the class GeometryUtilities method scaleToUnitaryArea.

/**
 * Scales a {@link Polygon} to have an unitary area.
 *
 * @param polygon the geometry to scale.
 * @return a copy of the scaled geometry.
 * @throws Exception
 */
public static Geometry scaleToUnitaryArea(Geometry polygon) throws Exception {
    double area = polygon.getArea();
    double scale = sqrt(1.0 / area);
    AffineTransform scaleAT = new AffineTransform();
    scaleAT.scale(scale, scale);
    AffineTransform2D scaleTransform = new AffineTransform2D(scaleAT);
    polygon = JTS.transform(polygon, scaleTransform);
    return polygon;
}
Also used : AffineTransform(java.awt.geom.AffineTransform) AffineTransform2D(org.geotools.referencing.operation.transform.AffineTransform2D)

Example 4 with AffineTransform2D

use of org.geotools.referencing.operation.transform.AffineTransform2D 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 5 with AffineTransform2D

use of org.geotools.referencing.operation.transform.AffineTransform2D in project hortonmachine by TheHortonMachine.

the class OmsRasterTransformer method process.

@Execute
public void process() throws Exception {
    if (!concatOr(outRaster == null, doReset)) {
        return;
    }
    Interpolation interpolation = Interpolation.getInstance(Interpolation.INTERP_NEAREST);
    if (pInterpolation.equals(BILINEAR)) {
        interpolation = Interpolation.getInstance(Interpolation.INTERP_BILINEAR);
    } else if (pInterpolation.equals(BICUBIC)) {
        interpolation = Interpolation.getInstance(Interpolation.INTERP_BICUBIC);
    }
    RenderedImage inRasterRI = inRaster.getRenderedImage();
    RegionMap sourceRegion = CoverageUtilities.gridGeometry2RegionParamsMap(inRaster.getGridGeometry());
    Envelope2D envelope2d = inRaster.getEnvelope2D();
    Envelope targetEnvelope = new Envelope(envelope2d.getMinX(), envelope2d.getMaxX(), envelope2d.getMinY(), envelope2d.getMaxY());
    Geometry targetGeometry = null;
    GeometryFactory gf = GeometryUtilities.gf();
    RenderedOp finalImg = null;
    if (pAngle != null) {
        pm.beginTask("Rotate raster by angle: " + pAngle, IHMProgressMonitor.UNKNOWN);
        float centerX = 0f;
        float centerY = 0f;
        if (pEast == null) {
            centerX = (float) envelope2d.getCenterX();
        } else {
            centerX = pEast.floatValue();
        }
        if (pNorth == null) {
            centerY = (float) envelope2d.getCenterY();
        } else {
            centerY = pNorth.floatValue();
        }
        finalImg = RotateDescriptor.create(inRasterRI, centerX, centerY, (float) Math.toRadians(pAngle), interpolation, null, null);
        // also keep track of the transforming envelope
        AffineTransform rotationAT = new AffineTransform();
        rotationAT.translate(centerX, centerY);
        rotationAT.rotate(Math.toRadians(-pAngle));
        rotationAT.translate(-centerX, -centerY);
        MathTransform rotationTransform = new AffineTransform2D(rotationAT);
        Envelope jtsEnv = new Envelope(targetEnvelope.getMinX(), targetEnvelope.getMaxX(), targetEnvelope.getMinY(), targetEnvelope.getMaxY());
        targetEnvelope = JTS.transform(jtsEnv, rotationTransform);
        Geometry rotGeometry = gf.toGeometry(jtsEnv);
        targetGeometry = JTS.transform(rotGeometry, rotationTransform);
        pm.done();
    }
    if (doFlipHorizontal) {
        pm.beginTask("Flip horizontally...", IHMProgressMonitor.UNKNOWN);
        if (finalImg != null) {
            finalImg = TransposeDescriptor.create(finalImg, TransposeDescriptor.FLIP_HORIZONTAL, null);
        } else {
            finalImg = TransposeDescriptor.create(inRasterRI, TransposeDescriptor.FLIP_HORIZONTAL, null);
        }
        Envelope jtsEnv = new Envelope(targetEnvelope.getMinX(), targetEnvelope.getMaxX(), targetEnvelope.getMinY(), targetEnvelope.getMaxY());
        targetGeometry = gf.toGeometry(jtsEnv);
        pm.done();
    }
    if (doFlipVertical) {
        pm.beginTask("Flip vertically...", IHMProgressMonitor.UNKNOWN);
        if (finalImg != null) {
            finalImg = TransposeDescriptor.create(finalImg, TransposeDescriptor.FLIP_VERTICAL, null);
        } else {
            finalImg = TransposeDescriptor.create(inRasterRI, TransposeDescriptor.FLIP_VERTICAL, null);
        }
        Envelope jtsEnv = new Envelope(targetEnvelope.getMinX(), targetEnvelope.getMaxX(), targetEnvelope.getMinY(), targetEnvelope.getMaxY());
        targetGeometry = gf.toGeometry(jtsEnv);
        pm.done();
    }
    if (pScaleX != null || pScaleY != null) {
        float scaleX = 1f;
        float scaleY = 1f;
        if (pScaleX == null) {
            scaleX = 1f;
        } else {
            scaleX = pScaleX.floatValue();
        }
        if (pScaleY == null) {
            scaleY = 1f;
        } else {
            scaleY = pScaleY.floatValue();
        }
        pm.beginTask("Scale raster by: " + scaleX + " and " + scaleY, IHMProgressMonitor.UNKNOWN);
        // float centerY = (float) envelope2d.getCenterY();
        if (finalImg != null) {
            finalImg = ScaleDescriptor.create(finalImg, new Float(scaleX), new Float(scaleY), new Float(0.0f), new Float(0.0f), interpolation, null);
        } else {
            finalImg = ScaleDescriptor.create(inRasterRI, new Float(scaleX), new Float(scaleY), new Float(0.0f), new Float(0.0f), interpolation, null);
        }
        // also keep track of the transforming envelope
        AffineTransform scaleAT = new AffineTransform();
        // scaleAT.translate(centerX, centerY);
        scaleAT.scale(scaleX, scaleY);
        // scaleAT.translate(-centerX, -centerY);
        MathTransform scaleTransform = new AffineTransform2D(scaleAT);
        Envelope jtsEnv = new Envelope(targetEnvelope.getMinX(), targetEnvelope.getMaxX(), targetEnvelope.getMinY(), targetEnvelope.getMaxY());
        targetEnvelope = JTS.transform(jtsEnv, scaleTransform);
        Geometry scaledGeometry = gf.toGeometry(jtsEnv);
        targetGeometry = JTS.transform(scaledGeometry, scaleTransform);
        pm.done();
    }
    if (pTransX != null || pTransY != null) {
        float transX = 1f;
        float transY = 1f;
        if (pTransX == null) {
            transX = 1f;
        } else {
            transX = pTransX.floatValue();
        }
        if (pTransY == null) {
            transY = 1f;
        } else {
            transY = pTransY.floatValue();
        }
        pm.beginTask("Translate raster by: " + transX + " and " + transY, IHMProgressMonitor.UNKNOWN);
        if (finalImg != null) {
            finalImg = TranslateDescriptor.create(finalImg, transX, transY, interpolation, null);
        } else {
            finalImg = TranslateDescriptor.create(inRasterRI, transX, transY, interpolation, null);
        }
        // also keep track of the transforming envelope
        AffineTransform translationAT = new AffineTransform();
        translationAT.translate(transX, transY);
        MathTransform translateTransform = new AffineTransform2D(translationAT);
        if (targetGeometry == null) {
            targetGeometry = gf.toGeometry(targetEnvelope);
        }
        targetEnvelope = JTS.transform(targetEnvelope, translateTransform);
        targetGeometry = JTS.transform(targetGeometry, translateTransform);
        pm.done();
    }
    if (finalImg != null) {
        RegionMap targetRegion = new RegionMap();
        targetRegion.put(NORTH, targetEnvelope.getMaxY());
        targetRegion.put(SOUTH, targetEnvelope.getMinY());
        targetRegion.put(WEST, targetEnvelope.getMinX());
        targetRegion.put(EAST, targetEnvelope.getMaxX());
        targetRegion.put(XRES, sourceRegion.getXres());
        targetRegion.put(YRES, sourceRegion.getYres());
        // targetRegion.put(ROWS, (double) height);
        // targetRegion.put(COLS, (double) width);
        CoordinateReferenceSystem crs = inRaster.getCoordinateReferenceSystem();
        outRaster = CoverageUtilities.buildCoverage("out", finalImg, targetRegion, crs);
        outBounds = FeatureUtilities.featureCollectionFromGeometry(crs, targetGeometry);
    }
    pm.done();
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) MathTransform(org.opengis.referencing.operation.MathTransform) RegionMap(org.hortonmachine.gears.utils.RegionMap) Envelope(org.locationtech.jts.geom.Envelope) Envelope2D(org.geotools.geometry.Envelope2D) Geometry(org.locationtech.jts.geom.Geometry) Interpolation(javax.media.jai.Interpolation) RenderedOp(javax.media.jai.RenderedOp) AffineTransform(java.awt.geom.AffineTransform) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) RenderedImage(java.awt.image.RenderedImage) AffineTransform2D(org.geotools.referencing.operation.transform.AffineTransform2D) Execute(oms3.annotations.Execute)

Aggregations

AffineTransform2D (org.geotools.referencing.operation.transform.AffineTransform2D)8 AffineTransform (java.awt.geom.AffineTransform)7 MathTransform (org.opengis.referencing.operation.MathTransform)5 Geometry (org.locationtech.jts.geom.Geometry)4 TransformException (org.opengis.referencing.operation.TransformException)3 Rectangle (java.awt.Rectangle)2 Execute (oms3.annotations.Execute)2 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)2 Coordinate (org.locationtech.jts.geom.Coordinate)2 Point (java.awt.Point)1 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)1 BufferedImage (java.awt.image.BufferedImage)1 ColorModel (java.awt.image.ColorModel)1 DataBuffer (java.awt.image.DataBuffer)1 RenderedImage (java.awt.image.RenderedImage)1 SampleModel (java.awt.image.SampleModel)1 WritableRaster (java.awt.image.WritableRaster)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1