Java Code Examples for org.opengis.geometry.Envelope#getUpperCorner()

The following examples show how to use org.opengis.geometry.Envelope#getUpperCorner() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Get the array of region parameters covered by the {@link GridCoverage2D coverage}. 
 * 
 * @param gridCoverage the coverage.
 * @return the array of region parameters as [n, s, w, e, xres, yres, cols, rows]
 */
public static double[] getRegionArrayFromGridCoverage( GridCoverage2D gridCoverage ) {
    Envelope envelope = gridCoverage.getEnvelope();
    DirectPosition lowerCorner = envelope.getLowerCorner();
    double[] westSouth = lowerCorner.getCoordinate();
    DirectPosition upperCorner = envelope.getUpperCorner();
    double[] eastNorth = upperCorner.getCoordinate();

    GridGeometry2D gridGeometry = gridCoverage.getGridGeometry();
    GridEnvelope2D gridRange = gridGeometry.getGridRange2D();
    int height = gridRange.height;
    int width = gridRange.width;

    AffineTransform gridToCRS = (AffineTransform) gridGeometry.getGridToCRS();
    double xRes = XAffineTransform.getScaleX0(gridToCRS);
    double yRes = XAffineTransform.getScaleY0(gridToCRS);

    double[] params = new double[]{eastNorth[1], westSouth[1], westSouth[0], eastNorth[0], xRes, yRes, width, height};

    return params;
}
 
Example 2
Source File: ArrayEnvelope.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a new envelope with the same data than the specified envelope.
 *
 * @param envelope  the envelope to copy.
 */
public ArrayEnvelope(final Envelope envelope) {
    ensureNonNull("envelope", envelope);
    /*
     * Do not optimize with `if (envelope instanceof ArrayEnvelope)` because subclasses may change the semantic.
     * In particular the SubEnvelope subclass uses only a subrange of this array. If we still want to optimize,
     * we would have to test for specific classes. It is probably not worth at this stage.
     */
    crs = envelope.getCoordinateReferenceSystem();
    final int dimension = envelope.getDimension();
    coordinates = new double[dimension * 2];
    final DirectPosition lowerCorner = envelope.getLowerCorner();
    final DirectPosition upperCorner = envelope.getUpperCorner();
    for (int i=0; i<dimension; i++) {
        coordinates[i]           = lowerCorner.getOrdinate(i);
        coordinates[i+dimension] = upperCorner.getOrdinate(i);
    }
    verifyRanges(crs, coordinates);
}
 
Example 3
Source File: ReferencingAssert.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts that two envelopes have the same minimum and maximum coordinates.
 * This method ignores the envelope type (i.e. the implementation class) and the CRS.
 *
 * @param expected    the expected envelope.
 * @param actual      the envelope to compare with the expected one.
 * @param tolerances  the tolerance threshold on location along each axis. If this array length is shorter
 *                    than the number of dimensions, then the last tolerance is reused for all remaining axes.
 *                    If this array is empty, then the tolerance threshold is zero.
 *
 * @since 0.7
 */
public static void assertEnvelopeEquals(final Envelope expected, final Envelope actual, final double... tolerances) {
    final int dimension = expected.getDimension();
    assertEquals("dimension", dimension, actual.getDimension());
    final DirectPosition expectedLower = expected.getLowerCorner();
    final DirectPosition expectedUpper = expected.getUpperCorner();
    final DirectPosition actualLower   = actual  .getLowerCorner();
    final DirectPosition actualUpper   = actual  .getUpperCorner();
    double tolerance = 0;
    for (int i=0; i<dimension; i++) {
        if (i < tolerances.length) {
            tolerance = tolerances[i];
        }
        if (abs(expectedLower.getOrdinate(i) - actualLower.getOrdinate(i)) > tolerance ||
            abs(expectedUpper.getOrdinate(i) - actualUpper.getOrdinate(i)) > tolerance)
        {
            fail("Envelopes are not equal in dimension " + i + ":\n"
                    + "expected " + Envelopes.toString(expected) + "\n"
                    + " but got " + Envelopes.toString(actual));
        }
    }
}
 
Example 4
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the parameters of the region covered by the {@link GridCoverage2D coverage}. 
 * 
 * @param gridCoverage the coverage.
 * @return the {@link HashMap map} of parameters. ( {@link #NORTH} and the 
 *          other static vars can be used to retrieve them.
 */
public static RegionMap getRegionParamsFromGridCoverage( GridCoverage2D gridCoverage ) {
    RegionMap envelopeParams = new RegionMap();

    Envelope envelope = gridCoverage.getEnvelope();

    DirectPosition lowerCorner = envelope.getLowerCorner();
    double[] westSouth = lowerCorner.getCoordinate();
    DirectPosition upperCorner = envelope.getUpperCorner();
    double[] eastNorth = upperCorner.getCoordinate();

    GridGeometry2D gridGeometry = gridCoverage.getGridGeometry();
    GridEnvelope2D gridRange = gridGeometry.getGridRange2D();
    int height = gridRange.height;
    int width = gridRange.width;

    AffineTransform gridToCRS = (AffineTransform) gridGeometry.getGridToCRS();
    double xRes = XAffineTransform.getScaleX0(gridToCRS);
    double yRes = XAffineTransform.getScaleY0(gridToCRS);

    envelopeParams.put(NORTH, eastNorth[1]);
    envelopeParams.put(SOUTH, westSouth[1]);
    envelopeParams.put(WEST, westSouth[0]);
    envelopeParams.put(EAST, eastNorth[0]);
    envelopeParams.put(XRES, xRes);
    envelopeParams.put(YRES, yRes);
    envelopeParams.put(ROWS, (double) height);
    envelopeParams.put(COLS, (double) width);

    return envelopeParams;
}
 
Example 5
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the parameters of the region covered by the {@link ImageMosaicReader}. 
 * 
 * @param reader the ImageMosaicReader.
 * @return the {@link HashMap map} of parameters. ( {@link #NORTH} and the 
 *          other static vars can be used to retrieve them.
 */
public static RegionMap getRegionParamsFromImageMosaicReader( ImageMosaicReader reader ) throws IOException {
    RegionMap envelopeParams = new RegionMap();

    Envelope envelope = reader.getOriginalEnvelope();

    DirectPosition lowerCorner = envelope.getLowerCorner();
    double[] westSouth = lowerCorner.getCoordinate();
    DirectPosition upperCorner = envelope.getUpperCorner();
    double[] eastNorth = upperCorner.getCoordinate();

    GridEnvelope2D gridRange = (GridEnvelope2D) reader.getOriginalGridRange();
    int height = gridRange.height;
    int width = gridRange.width;
    double[][] resolutionLevels = reader.getResolutionLevels();
    double xRes = resolutionLevels[0][0];
    double yRes = resolutionLevels[0][1];

    envelopeParams.put(NORTH, eastNorth[1]);
    envelopeParams.put(SOUTH, westSouth[1]);
    envelopeParams.put(WEST, westSouth[0]);
    envelopeParams.put(EAST, eastNorth[0]);
    envelopeParams.put(XRES, xRes);
    envelopeParams.put(YRES, yRes);
    envelopeParams.put(ROWS, (double) height);
    envelopeParams.put(COLS, (double) width);

    return envelopeParams;
}
 
Example 6
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public static RegionMap gridGeometry2RegionParamsMap( GridGeometry2D gridGeometry ) {
    RegionMap envelopeParams = new RegionMap();

    Envelope envelope = gridGeometry.getEnvelope2D();
    DirectPosition lowerCorner = envelope.getLowerCorner();
    double[] westSouth = lowerCorner.getCoordinate();
    DirectPosition upperCorner = envelope.getUpperCorner();
    double[] eastNorth = upperCorner.getCoordinate();

    GridEnvelope2D gridRange = gridGeometry.getGridRange2D();
    int height = gridRange.height;
    int width = gridRange.width;

    AffineTransform gridToCRS = (AffineTransform) gridGeometry.getGridToCRS();
    double xRes = XAffineTransform.getScaleX0(gridToCRS);
    double yRes = XAffineTransform.getScaleY0(gridToCRS);

    envelopeParams.put(NORTH, eastNorth[1]);
    envelopeParams.put(SOUTH, westSouth[1]);
    envelopeParams.put(WEST, westSouth[0]);
    envelopeParams.put(EAST, eastNorth[0]);
    envelopeParams.put(XRES, xRes);
    envelopeParams.put(YRES, yRes);
    envelopeParams.put(ROWS, (double) height);
    envelopeParams.put(COLS, (double) width);

    return envelopeParams;
}
 
Example 7
Source File: AbstractEnvelope.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Implementation of the public {@link #toString()} and {@link Envelopes#toString(Envelope)}
 * methods for formatting a {@code BOX} element from an envelope.
 *
 * @param  envelope           the envelope to format.
 * @param  isSinglePrecision  {@code true} if every lower and upper corner values can be casted to {@code float}.
 * @return this envelope as a {@code BOX} or {@code BOX3D} (most typical dimensions) element.
 *
 * @see GeneralEnvelope#GeneralEnvelope(CharSequence)
 * @see CoordinateFormat
 * @see org.apache.sis.io.wkt
 */
static String toString(final Envelope envelope, final boolean isSinglePrecision) {
    final int dimension = envelope.getDimension();
    final StringBuilder buffer = new StringBuilder(64).append("BOX");
    if (dimension != 2) {
        buffer.append(dimension).append('D');
    }
    if (dimension == 0) {
        buffer.append("()");
    } else {
        final DirectPosition lowerCorner = envelope.getLowerCorner();
        final DirectPosition upperCorner = envelope.getUpperCorner();
        boolean isUpper = false;
        do {                                                        // Executed exactly twice.
            for (int i=0; i<dimension; i++) {
                buffer.append(i == 0 && !isUpper ? '(' : ' ');
                final double coordinate = (isUpper ? upperCorner : lowerCorner).getOrdinate(i);
                if (isSinglePrecision) {
                    buffer.append((float) coordinate);
                } else {
                    buffer.append(coordinate);
                }
                trimFractionalPart(buffer);
            }
            buffer.append(isUpper ? ')' : ',');
        } while ((isUpper = !isUpper) == true);
    }
    return buffer.toString();
}
 
Example 8
Source File: GeneralEnvelopeTest.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that the given two-dimensional envelope is equals to the given rectangle.
 * The {@code xLower} and {@code xUpper} arguments are the <var>x</var> coordinate values
 * for the lower and upper corners respectively. The actual {@code xmin} and {@code ymin}
 * values will be inferred from those corners.
 *
 * <p>This method assumes that only the <var>x</var> axis may be a wraparound axis.</p>
 *
 * @param  test    the actual envelope to verify.
 * @param  xLower  the expected first   <var>x</var> coordinate value. May  be greater than {@code xUpper}.
 * @param  xUpper  the expected last    <var>x</var> coordinate value. May  be less    than {@code xLower}.
 * @param  ymin    the expected minimal <var>y</var> coordinate value. Must be less    than {@code ymax}.
 * @param  ymax    the expected maximal <var>y</var> coordinate value. Must be greater than {@code ymax}.
 */
private static void assertEnvelopeEquals(final Envelope test,
        final double xLower, final double ymin, final double xUpper, final double ymax)
{
    final double xmin, xmax;
    if (MathFunctions.isNegative(xUpper - xLower)) {                // Check for anti-meridian spanning.
        xmin = -180;
        xmax = +180;
    } else {
        xmin = xLower;
        xmax = xUpper;
    }
    final DirectPosition lower = test.getLowerCorner();
    final DirectPosition upper = test.getUpperCorner();
    assertEquals("lower", xLower, lower.getOrdinate(0), STRICT);
    assertEquals("upper", xUpper, upper.getOrdinate(0), STRICT);
    assertEquals("xmin",  xmin,   test .getMinimum (0), STRICT);
    assertEquals("xmax",  xmax,   test .getMaximum (0), STRICT);
    assertEquals("ymin",  ymin,   test .getMinimum (1), STRICT);
    assertEquals("ymax",  ymax,   test .getMaximum (1), STRICT);
    assertEquals("ymin",  ymin,   lower.getOrdinate(1), STRICT);
    assertEquals("ymax",  ymax,   upper.getOrdinate(1), STRICT);
    if (test instanceof Envelope2D) {
        final Envelope2D ri = (Envelope2D) test;
        assertEquals("xmin", xmin, ri.getMinX(), STRICT);
        assertEquals("xmax", xmax, ri.getMaxX(), STRICT);
        assertEquals("ymin", ymin, ri.getMinY(), STRICT);
        assertEquals("ymax", ymax, ri.getMaxY(), STRICT);
    }
}
 
Example 9
Source File: Matrices.java    From sis with Apache License 2.0 4 votes vote down vote up
/**
 * Implementation of {@code createTransform(…)} public methods expecting envelopes and/or axis directions.
 * Argument validity shall be verified by the caller.
 *
 * @param useEnvelopes {@code true} if source and destination envelopes shall be taken in account.
 *        If {@code false}, then source and destination envelopes will be ignored and can be null.
 */
@SuppressWarnings("null")
private static MatrixSIS createTransform(final Envelope srcEnvelope, final AxisDirection[] srcAxes,
                                         final Envelope dstEnvelope, final AxisDirection[] dstAxes,
                                         final boolean useEnvelopes)
{
    final DirectPosition dstCorner, srcCorner, srcOppositeCorner;
    if (useEnvelopes) {
        dstCorner         = dstEnvelope.getLowerCorner();
        srcCorner         = srcEnvelope.getLowerCorner();
        srcOppositeCorner = srcEnvelope.getUpperCorner();
    } else {
        dstCorner = srcCorner = srcOppositeCorner = null;
    }
    /*
     * Unconditionally create extended precision matrix even if standard precision would be
     * enough because callers in other package may perform additional arithmetic operations
     * on it (for example org.apache.sis.referencing.cs.CoordinateSystems.swapAndScaleAxes).
     */
    final MatrixSIS matrix = new GeneralMatrix(dstAxes.length+1, srcAxes.length+1, false, 2);
    /*
     * Maps source axes to destination axes. If no axis is moved (for example if the user
     * want to transform (NORTH,EAST) to (SOUTH,EAST)), then source and destination index
     * will be equal. If some axes are moved (for example if the user want to transform
     * (NORTH,EAST) to (EAST,NORTH)), then coordinates at index {@code srcIndex} will have
     * to be moved at index {@code dstIndex}.
     */
    for (int dstIndex = 0; dstIndex < dstAxes.length; dstIndex++) {
        boolean hasFound = false;
        final AxisDirection dstDir = dstAxes[dstIndex];
        final AxisDirection search = AxisDirections.absolute(dstDir);
        for (int srcIndex = 0; srcIndex < srcAxes.length; srcIndex++) {
            final AxisDirection srcDir = srcAxes[srcIndex];
            if (search.equals(AxisDirections.absolute(srcDir))) {
                if (hasFound) {
                    throw new IllegalArgumentException(Resources.format(
                            Resources.Keys.ColinearAxisDirections_2, srcDir, dstDir));
                }
                hasFound = true;
                /*
                 * Set the matrix elements. Some matrix elements will never be set.
                 * They will be left to zero, which is their desired value.
                 */
                final boolean same = srcDir.equals(dstDir);
                if (useEnvelopes) {
                    /*
                     * See the comment in transform(Envelope, Envelope) for an explanation about why
                     * we use the lower/upper corners instead than getMinimum()/getMaximum() methods.
                     */
                    final DoubleDouble scale = new DoubleDouble(same ? +1d : -1d);
                    scale.multiplyGuessError(dstEnvelope.getSpan(dstIndex));
                    scale.divideGuessError  (srcEnvelope.getSpan(srcIndex));

                    final DoubleDouble translate = new DoubleDouble(scale);
                    translate.multiplyGuessError((same ? srcCorner : srcOppositeCorner).getOrdinate(srcIndex));
                    translate.negate();
                    translate.addGuessError(dstCorner.getOrdinate(dstIndex));

                    matrix.setNumber(dstIndex, srcIndex,       scale);
                    matrix.setNumber(dstIndex, srcAxes.length, translate);
                } else {
                    matrix.setElement(dstIndex, srcIndex, same ? +1 : -1);
                }
            }
        }
        if (!hasFound) {
            throw new IllegalArgumentException(Resources.format(
                    Resources.Keys.CanNotMapAxisToDirection_1, dstAxes[dstIndex]));
        }
    }
    matrix.setElement(dstAxes.length, srcAxes.length, 1);
    return matrix;
}
 
Example 10
Source File: AbstractEnvelope.java    From sis with Apache License 2.0 3 votes vote down vote up
/**
 * Compares to the specified envelope for equality up to the specified tolerance value.
 * The tolerance value {@code eps} can be either relative to the {@linkplain #getSpan(int)
 * envelope span} along each dimension or can be an absolute value (as for example some
 * ground resolution of a {@linkplain org.apache.sis.coverage.grid.GridCoverage grid coverage}).
 *
 * <ul>
 *   <li>If {@code epsIsRelative} is set to {@code true}, the actual tolerance value for a
 *       given dimension <var>i</var> is {@code eps} × {@code span} where {@code span}
 *       is the maximum of {@linkplain #getSpan(int) this envelope span} and the specified
 *       envelope span along dimension <var>i</var>.</li>
 *   <li>If {@code epsIsRelative} is set to {@code false}, the actual tolerance value for a
 *       given dimension <var>i</var> is {@code eps}.</li>
 * </ul>
 *
 * <div class="note"><b>Note:</b>
 * Relative tolerance values (as opposed to absolute tolerance values) help to workaround the
 * fact that tolerance value are CRS dependent. For example the tolerance value need to be
 * smaller for geographic CRS than for UTM projections, because the former typically has a
 * [-180…180]° range while the later can have a range of thousands of meters.</div>
 *
 * <h4>Coordinate Reference System</h4>
 * To be considered equal, the two envelopes must have the same {@linkplain #getDimension() dimension}
 * and their CRS must be {@linkplain org.apache.sis.util.Utilities#equalsIgnoreMetadata equals,
 * ignoring metadata}. If at least one envelope has a null CRS, then the CRS are ignored and the
 * coordinate values are compared as if the CRS were equal.
 *
 * @param  other          the envelope to compare with.
 * @param  eps            the tolerance value to use for numerical comparisons.
 * @param  epsIsRelative  {@code true} if the tolerance value should be relative to axis length,
 *                        or {@code false} if it is an absolute value.
 * @return {@code true} if the given object is equal to this envelope up to the given tolerance value.
 *
 * @see #contains(Envelope)
 * @see #intersects(Envelope)
 */
public boolean equals(final Envelope other, final double eps, final boolean epsIsRelative) {
    ensureNonNull("other", other);
    final int dimension = getDimension();
    if (other.getDimension() != dimension || !equalsIgnoreMetadata(
            getCoordinateReferenceSystem(), other.getCoordinateReferenceSystem(), false))
    {
        return false;
    }
    final DirectPosition lowerCorner = other.getLowerCorner();
    final DirectPosition upperCorner = other.getUpperCorner();
    for (int i=0; i<dimension; i++) {
        double ε = eps;
        if (epsIsRelative) {
            final double span = Math.max(getSpan(i), other.getSpan(i));
            if (span > 0 && span < Double.POSITIVE_INFINITY) {
                ε *= span;
            }
        }
        if (!epsilonEqual(getLower(i), lowerCorner.getOrdinate(i), ε) ||
            !epsilonEqual(getUpper(i), upperCorner.getOrdinate(i), ε))
        {
            return false;
        }
    }
    return true;
}
 
Example 11
Source File: Envelope2D.java    From sis with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs a two-dimensional envelope defined by an other {@link Envelope}.
 *
 * @param  envelope  the envelope to copy (can not be {@code null}).
 * @throws MismatchedDimensionException if the given envelope is not two-dimensional.
 */
public Envelope2D(final Envelope envelope) throws MismatchedDimensionException {
    this(envelope.getCoordinateReferenceSystem(), envelope.getLowerCorner(), envelope.getUpperCorner());
}