/** * Returns the given envelope as an {@code AbstractEnvelope} instance. * If the given envelope is already an instance of {@code AbstractEnvelope}, * then it is returned unchanged. Otherwise the coordinate values and the CRS * of the given envelope are copied in a new envelope. * * @param envelope the envelope to cast, or {@code null}. * @return the values of the given envelope as an {@code AbstractEnvelope} instance. * * @see GeneralEnvelope#castOrCopy(Envelope) * @see ImmutableEnvelope#castOrCopy(Envelope) */ public static AbstractEnvelope castOrCopy(final Envelope envelope) { if (envelope == null || envelope instanceof AbstractEnvelope) { return (AbstractEnvelope) envelope; } return new GeneralEnvelope(envelope); }
/** * Returns the given envelope as a {@code GeneralEnvelope} instance. If the given envelope * is already an instance of {@code GeneralEnvelope}, then it is returned unchanged. * Otherwise the coordinate values and the CRS of the given envelope are * {@linkplain #GeneralEnvelope(Envelope) copied} in a new {@code GeneralEnvelope}. * * @param envelope the envelope to cast, or {@code null}. * @return the values of the given envelope as a {@code GeneralEnvelope} instance. * * @see AbstractEnvelope#castOrCopy(Envelope) * @see ImmutableEnvelope#castOrCopy(Envelope) */ public static GeneralEnvelope castOrCopy(final Envelope envelope) { if (envelope == null || envelope instanceof GeneralEnvelope) { return (GeneralEnvelope) envelope; } return new GeneralEnvelope(envelope); }
/** * Returns the given envelope as a {@code GeneralEnvelope} instance. If the given envelope * is already an instance of {@code GeneralEnvelope}, then it is returned unchanged. * Otherwise the coordinate values and the CRS of the given envelope are * {@linkplain #GeneralEnvelope(Envelope) copied} in a new {@code GeneralEnvelope}. * * @param envelope the envelope to cast, or {@code null}. * @return the values of the given envelope as a {@code GeneralEnvelope} instance. * * @see AbstractEnvelope#castOrCopy(Envelope) * @see ImmutableEnvelope#castOrCopy(Envelope) */ public static GeneralEnvelope castOrCopy(final Envelope envelope) { if (envelope == null || envelope instanceof GeneralEnvelope) { return (GeneralEnvelope) envelope; } return new GeneralEnvelope(envelope); }
/** * Returns the given envelope as an {@code AbstractEnvelope} instance. * If the given envelope is already an instance of {@code AbstractEnvelope}, * then it is returned unchanged. Otherwise the coordinate values and the CRS * of the given envelope are copied in a new envelope. * * @param envelope the envelope to cast, or {@code null}. * @return the values of the given envelope as an {@code AbstractEnvelope} instance. * * @see GeneralEnvelope#castOrCopy(Envelope) * @see ImmutableEnvelope#castOrCopy(Envelope) */ public static AbstractEnvelope castOrCopy(final Envelope envelope) { if (envelope == null || envelope instanceof AbstractEnvelope) { return (AbstractEnvelope) envelope; } return new GeneralEnvelope(envelope); }
/** * If the user wants a clone, copy only the relevant part of the ordinates array. */ @Override @SuppressWarnings("CloneDoesntCallSuperClone") public GeneralEnvelope clone() { final int d = ordinates.length >>> 1; final int dimension = endIndex - beginIndex; final GeneralEnvelope copy = new GeneralEnvelope(endIndex - beginIndex); System.arraycopy(ordinates, beginIndex, copy.ordinates, 0, dimension); System.arraycopy(ordinates, beginIndex + d, copy.ordinates, dimension, dimension); copy.crs = crs; return copy; } }
/** * If the user wants a clone, copy only the relevant part of the ordinates array. */ @Override @SuppressWarnings("CloneDoesntCallSuperClone") public GeneralEnvelope clone() { final int d = ordinates.length >>> 1; final int dimension = endIndex - beginIndex; final GeneralEnvelope copy = new GeneralEnvelope(endIndex - beginIndex); System.arraycopy(ordinates, beginIndex, copy.ordinates, 0, dimension); System.arraycopy(ordinates, beginIndex + d, copy.ordinates, dimension, dimension); copy.crs = crs; return copy; } }
/** * Returns the domain of validity of input coordinates that can be specified to the * {@link #interpolateAt interpolateAt(…)} method. Coordinates outside that domain of * validity will still be accepted, but the extrapolated results may be very wrong. * * <p>The unit of measurement for the coordinate values in the returned envelope is * given by {@link #getCoordinateUnit()}. The envelope CRS is undefined.</p> * * @return the domain covered by this grid. * @throws TransformException if an error occurred while computing the envelope. */ public Envelope getDomainOfValidity() throws TransformException { final GeneralEnvelope env = new GeneralEnvelope(gridSize.length); for (int i=0; i<gridSize.length; i++) { env.setRange(i, -0.5, gridSize[i] - 0.5); } return Envelopes.transform(getCoordinateToGrid().inverse(), env); }
/** * Returns the domain of validity of input coordinates that can be specified to the * {@link #interpolateAt interpolateAt(…)} method. Coordinates outside that domain of * validity will still be accepted, but the extrapolated results may be very wrong. * * <p>The unit of measurement for the coordinate values in the returned envelope is * given by {@link #getCoordinateUnit()}. The envelope CRS is undefined.</p> * * @return the domain covered by this grid. * @throws TransformException if an error occurred while computing the envelope. */ public Envelope getDomainOfValidity() throws TransformException { final GeneralEnvelope env = new GeneralEnvelope(gridSize.length); for (int i=0; i<gridSize.length; i++) { env.setRange(i, -0.5, gridSize[i] - 0.5); } return Envelopes.transform(getCoordinateToGrid().inverse(), env); }
/** * Creates an envelope for the given CRS and coordinate values. */ @Override GeneralEnvelope createFromExtremums(CoordinateReferenceSystem crs, double xmin, double ymin, double xmax, double ymax) { final GeneralEnvelope env = new GeneralEnvelope(crs); env.setRange(0, xmin, xmax); env.setRange(1, ymin, ymax); return env; }
/** * Tests {@link GeneralEnvelope#translate(double...)}. * * @since 0.5 */ @Test public void testTranslate() { final GeneralEnvelope envelope = new GeneralEnvelope(new double[] {4, 5}, new double[] {8, 7}); envelope.translate(2, -4); assertEnvelopeEquals(envelope, 6, 1, 10, 3); }
/** * Creates a new geographic envelope for the given ordinate values. * This method is overridden by {@link SubEnvelopeTest}. */ GeneralEnvelope create(final double xmin, final double ymin, final double xmax, final double ymax) { final GeneralEnvelope envelope = new GeneralEnvelope(2); envelope.setCoordinateReferenceSystem(WGS84); envelope.setEnvelope(xmin, ymin, xmax, ymax); if (!skipValidation) { validate(envelope); } return envelope; }
/** * Creates an envelope for the given coordinate values. */ private static GeneralEnvelope createFromExtremums(boolean latlon, double xmin, double ymin, double xmax, double ymax) { final CommonCRS crs = CommonCRS.WGS84; final GeneralEnvelope env; if (latlon) { env = new GeneralEnvelope(crs.geographic()); env.setRange(1, xmin, xmax); env.setRange(0, ymin, ymax); } else { env = new GeneralEnvelope(crs.normalizedGeographic()); env.setRange(0, xmin, xmax); env.setRange(1, ymin, ymax); } return env; }
/** * Tests the {@link GeneralEnvelope#toString()} method. */ @Test public void testToString() { GeneralEnvelope envelope = new GeneralEnvelope(new double[] {-180, -90}, new double[] {180, 90}); assertEquals("BOX(-180 -90, 180 90)", envelope.toString()); envelope = new GeneralEnvelope(3); envelope.setRange(0, -180, +180); envelope.setRange(1, -90, +90); envelope.setRange(2, 10, 30); assertEquals("BOX3D(-180 -90 10, 180 90 30)", envelope.toString()); }
/** * Tests {@link Envelopes#toPolygonWKT(Envelope)}. */ @Test public void testToPolygonWKT() { final GeneralEnvelope envelope = new GeneralEnvelope(2); envelope.setRange(0, 40, 50); envelope.setRange(1, 20, 25); assertEquals("POLYGON((40 20, 40 25, 50 25, 50 20, 40 20))", Envelopes.toPolygonWKT(envelope)); }
/** * Tests {@link Envelopes#toString(Envelope)}. */ @Test public void testToString() { final GeneralEnvelope envelope = new GeneralEnvelope(2); envelope.setRange(0, 40, 50); envelope.setRange(1, 20, 25); assertEquals("BOX(40 20, 50 25)", Envelopes.toString(envelope)); }
/** * Creates a test envelope with the given CRS and initialized with * [-10 … 70]° of longitude, [-20 … 30]° of latitude, [-40 … 60] metres of elevation * and [51000 … 52000] modified Julian days. */ @SuppressWarnings("fallthrough") private static GeneralEnvelope createEnvelope(final CoordinateReferenceSystem crs) { final GeneralEnvelope envelope = new GeneralEnvelope(crs); switch (crs.getCoordinateSystem().getDimension()) { default: throw new AssertionError(); case 4: envelope.setRange(3, 51000, 52000); // Fall through case 3: envelope.setRange(2, -10, 70); // Fall through case 2: envelope.setRange(1, -20, 30); // Fall through case 1: envelope.setRange(0, -40, 60); case 0: break; } return envelope; }
/** * Tests the {@link GridExtent#GridExtent(AbstractEnvelope, GridRoundingMode, int[], GridExtent, int[])} constructor. */ @Test public void testCreateFromEnvelope() { final GeneralEnvelope env = new GeneralEnvelope(HardCodedCRS.IMAGE); env.setRange(0, -23.01, 30.107); env.setRange(1, 12.97, 18.071); GridExtent extent = new GridExtent(env, GridRoundingMode.NEAREST, null, null, null); assertExtentEquals(extent, 0, -23, 29); assertExtentEquals(extent, 1, 13, 17); assertEquals(DimensionNameType.COLUMN, extent.getAxisType(0).get()); assertEquals(DimensionNameType.ROW, extent.getAxisType(1).get()); }
/** * Asserts that adding the given point to the given envelope produces the given result. * First, this method tests using the {@link Envelope2D} implementation. Then, it tests * using the {@link GeneralEnvelope} implementation. */ private static void assertAddEquals(final GeneralEnvelope e, final DirectPosition2D p, final double xmin, final double ymin, final double xmax, final double ymax) { final Envelope2D r = new Envelope2D(e); r.add(p); assertEnvelopeEquals(r, xmin, ymin, xmax, ymax); // Compares with GeneralEnvelope. final GeneralEnvelope ec = new GeneralEnvelope(e); ec.add(p); assertEnvelopeEquals(ec, xmin, ymin, xmax, ymax); assertTrue("Using GeneralEnvelope.", ec.equals(r, STRICT, false)); }
/** * Implementation of {@link #testAxisRangeChange3D()} and {@link #testAxisRangeChangeWithDatumShift()}. */ private void testAxisRangeChange3D(final GeographicCRS targetCRS) throws FactoryException, TransformException { final GeneralEnvelope envelope = new GeneralEnvelope(new double[] { -0.5, -90, 1000}, new double[] {354.5, +90, 1002}); envelope.setCoordinateReferenceSystem(CRS.compound( HardCodedCRS.WGS84.forConvention(AxesConvention.POSITIVE_RANGE), HardCodedCRS.TIME)); final GeneralEnvelope expected = createFromExtremums(targetCRS, -0.5, -90, -5.5, 90); assertEnvelopeEquals(expected, Envelopes.transform(envelope, targetCRS), STRICT, STRICT); /* * When the envelope to transform span the full longitude range, * target envelope should unconditionally be [-180 … +180]°. */ envelope.setRange(0, -0.5, 359.5); expected.setRange(0, -180, 180); assertEnvelopeEquals(expected, Envelopes.transform(envelope, targetCRS), STRICT, STRICT); }
/** * Tests the {@link GeneralEnvelope#clone()} method. */ @Test public void testClone() { final GeneralEnvelope e1 = new GeneralEnvelope(2); e1.setRange(0, -40, +60); e1.setRange(1, -20, +30); final GeneralEnvelope e2 = e1.clone(); validate(e2); assertNotSame("Expected a new instance.", e1, e2); assertEquals ("The two instances should be equal.", e1, e2); e1.setRange(0, -40, +61); assertFalse("Ordinates array should have been cloned.", e1.equals(e2)); e2.setRange(0, -40, +61); assertEquals(e1, e2); }