Search in sources :

Example 1 with GridToEnvelopeMapper

use of org.geotools.referencing.operation.builder.GridToEnvelopeMapper in project geowave by locationtech.

the class RasterUtils method createTransform.

/**
 * Creates a math transform using the information provided.
 *
 * @return The math transform.
 * @throws IllegalStateException if the grid range or the envelope were not set.
 */
public static MathTransform createTransform(final double[] idRangePerDimension, final MultiDimensionalNumericData fullBounds) throws IllegalStateException {
    final GridToEnvelopeMapper mapper = new GridToEnvelopeMapper();
    final boolean swapXY = mapper.getSwapXY();
    final boolean[] reverse = mapper.getReverseAxis();
    final PixelInCell gridType = PixelInCell.CELL_CORNER;
    final int dimension = 2;
    /*
     * Setup the multi-dimensional affine transform for use with OpenGIS. According OpenGIS
     * specification, transforms must map pixel center. This is done by adding 0.5 to grid
     * coordinates.
     */
    final double translate;
    if (PixelInCell.CELL_CENTER.equals(gridType)) {
        translate = 0.5;
    } else if (PixelInCell.CELL_CORNER.equals(gridType)) {
        translate = 0.0;
    } else {
        throw new IllegalStateException(Errors.format(ErrorKeys.ILLEGAL_ARGUMENT_$2, "gridType", gridType));
    }
    final Matrix matrix = MatrixFactory.create(dimension + 1);
    final Double[] minValuesPerDimension = fullBounds.getMinValuesPerDimension();
    final Double[] maxValuesPerDimension = fullBounds.getMaxValuesPerDimension();
    for (int i = 0; i < dimension; i++) {
        // NOTE: i is a dimension in the 'gridRange' space (source
        // coordinates).
        // j is a dimension in the 'userRange' space (target coordinates).
        int j = i;
        if (swapXY) {
            j = 1 - j;
        }
        double scale = idRangePerDimension[j];
        double offset;
        if ((reverse == null) || (j >= reverse.length) || !reverse[j]) {
            offset = minValuesPerDimension[j];
        } else {
            scale = -scale;
            offset = maxValuesPerDimension[j];
        }
        offset -= scale * (-translate);
        matrix.setElement(j, j, 0.0);
        matrix.setElement(j, i, scale);
        matrix.setElement(j, dimension, offset);
    }
    return ProjectiveTransform.create(matrix);
}
Also used : Matrix(org.opengis.referencing.operation.Matrix) GridToEnvelopeMapper(org.geotools.referencing.operation.builder.GridToEnvelopeMapper) PixelInCell(org.opengis.referencing.datum.PixelInCell)

Example 2 with GridToEnvelopeMapper

use of org.geotools.referencing.operation.builder.GridToEnvelopeMapper in project geowebcache by GeoWebCache.

the class GeometryRasterMaskBuilder method getWorldToGridTransform.

private MathTransform getWorldToGridTransform(final BoundingBox coverageBounds, final long[] coverage) {
    // //
    // 
    // Convert the JTS envelope and get the transform
    // 
    // //
    final Envelope2D genvelope = new Envelope2D();
    {
        // genvelope.setCoordinateReferenceSystem(layerCrs);
        double x = coverageBounds.getMinX();
        double y = coverageBounds.getMinY();
        double width = coverageBounds.getWidth();
        double height = coverageBounds.getHeight();
        genvelope.setFrame(x, y, width, height);
    }
    final Rectangle paintArea = new Rectangle();
    {
        int x = (int) coverage[0];
        int y = (int) coverage[1];
        int width = (int) (1 + coverage[2] - x);
        int height = (int) (1 + coverage[3] - y);
        paintArea.setBounds(x, y, width, height);
    // System.out
    // .println("Grid: " + JTS.toGeometry(new Envelope(x, x + width, y, y + height)));
    }
    final MathTransform worldToScreen;
    // //
    // 
    // Get the transform
    // 
    // //
    final GridToEnvelopeMapper mapper = new GridToEnvelopeMapper();
    mapper.setPixelAnchor(PixelInCell.CELL_CORNER);
    mapper.setGridRange(new GridEnvelope2D(paintArea));
    mapper.setEnvelope(genvelope);
    mapper.setSwapXY(false);
    try {
        worldToScreen = mapper.createTransform().inverse();
    } catch (NoninvertibleTransformException | IllegalStateException e) {
        throw new IllegalArgumentException(e);
    }
    return worldToScreen;
}
Also used : NoninvertibleTransformException(org.opengis.referencing.operation.NoninvertibleTransformException) MathTransform(org.opengis.referencing.operation.MathTransform) GridEnvelope2D(org.geotools.coverage.grid.GridEnvelope2D) GridToEnvelopeMapper(org.geotools.referencing.operation.builder.GridToEnvelopeMapper) Rectangle(java.awt.Rectangle) Envelope2D(org.geotools.geometry.Envelope2D) GridEnvelope2D(org.geotools.coverage.grid.GridEnvelope2D)

Aggregations

GridToEnvelopeMapper (org.geotools.referencing.operation.builder.GridToEnvelopeMapper)2 Rectangle (java.awt.Rectangle)1 GridEnvelope2D (org.geotools.coverage.grid.GridEnvelope2D)1 Envelope2D (org.geotools.geometry.Envelope2D)1 PixelInCell (org.opengis.referencing.datum.PixelInCell)1 MathTransform (org.opengis.referencing.operation.MathTransform)1 Matrix (org.opengis.referencing.operation.Matrix)1 NoninvertibleTransformException (org.opengis.referencing.operation.NoninvertibleTransformException)1