/** * If the given object is a Java2D geometry and its envelope is non-empty, returns * that envelope as an Apache SIS implementation. Otherwise returns {@code null}. * * @param geometry the geometry from which to get the envelope, or {@code null}. * @return the envelope of the given object, or {@code null} if the object is not * a recognized geometry or its envelope is empty. */ @Override final GeneralEnvelope tryGetEnvelope(final Object geometry) { if (geometry instanceof Shape) { final Rectangle2D bounds = ((Shape) geometry).getBounds2D(); if (!bounds.isEmpty()) { // Test if there is NaN values. final GeneralEnvelope env = new GeneralEnvelope(2); env.setRange(0, bounds.getMinX(), bounds.getMaxX()); env.setRange(1, bounds.getMinY(), bounds.getMaxY()); return env; } } return null; }
/** * 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; }
/** * If the given object is an ESRI geometry and its envelope is non-empty, returns * that envelope as an Apache SIS implementation. Otherwise returns {@code null}. * * @param geometry the geometry from which to get the envelope, or {@code null}. * @return the envelope of the given object, or {@code null} if the object is not * a recognized geometry or its envelope is empty. */ @Override final GeneralEnvelope tryGetEnvelope(final Object geometry) { if (geometry instanceof Geometry) { final Envelope2D bounds = new Envelope2D(); ((Geometry) geometry).queryEnvelope2D(bounds); if (!bounds.isEmpty()) { // Test if there is NaN values. final GeneralEnvelope env = new GeneralEnvelope(2); env.setRange(0, bounds.xmin, bounds.xmax); env.setRange(1, bounds.ymin, bounds.ymax); return env; } } return null; }
/** * If the given object is an ESRI geometry and its envelope is non-empty, returns * that envelope as an Apache SIS implementation. Otherwise returns {@code null}. * * @param geometry the geometry from which to get the envelope, or {@code null}. * @return the envelope of the given object, or {@code null} if the object is not * a recognized geometry or its envelope is empty. */ @Override final GeneralEnvelope tryGetEnvelope(final Object geometry) { if (geometry instanceof Geometry) { final Envelope2D bounds = new Envelope2D(); ((Geometry) geometry).queryEnvelope2D(bounds); if (!bounds.isEmpty()) { // Test if there is NaN values. final GeneralEnvelope env = new GeneralEnvelope(2); env.setRange(0, bounds.xmin, bounds.xmax); env.setRange(1, bounds.ymin, bounds.ymax); return env; } } return null; }
/** * 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; }
/** * 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; }
/** * If the given object is a JTS geometry and its envelope is non-empty, returns * that envelope as an Apache SIS implementation. Otherwise returns {@code null}. * * @param geometry the geometry from which to get the envelope, or {@code null}. * @return the envelope of the given object, or {@code null} if the object is not * a recognized geometry or its envelope is empty. */ @Override final GeneralEnvelope tryGetEnvelope(final Object geometry) { if (geometry instanceof Geometry) { final Envelope bounds = ((Geometry) geometry).getEnvelopeInternal(); final GeneralEnvelope env = new GeneralEnvelope(2); env.setRange(0, bounds.getMinX(), bounds.getMaxX()); env.setRange(1, bounds.getMinY(), bounds.getMaxY()); if (!env.isEmpty()) { return env; } } return null; }
/** * 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); }
/** * 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)); }
/** * 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); }
/** * Tests the rounding performed by the {@link GridExtent#GridExtent(AbstractEnvelope, GridRoundingMode, int[], GridExtent, int[])} constructor. */ @Test public void testRoundings() { final GeneralEnvelope env = new GeneralEnvelope(6); env.setRange(0, 1.49999, 3.49998); // Round to [1…3), stored as [1…2]. env.setRange(1, 1.50001, 3.49998); // Round to [2…3), stored as [1…2] (not [2…2]) because the span is close to 2. env.setRange(2, 1.49998, 3.50001); // Round to [1…4), stored as [1…2] (not [1…3]) because the span is close to 2. env.setRange(3, 1.49999, 3.50002); // Round to [1…4), stored as [2…3] because the upper part is closer to integer. env.setRange(4, 1.2, 3.8); // Round to [1…4), stores as [1…3] because the span is not close enough to integer. GridExtent extent = new GridExtent(env, GridRoundingMode.NEAREST, null, null, null); assertExtentEquals(extent, 0, 1, 2); assertExtentEquals(extent, 1, 1, 2); assertExtentEquals(extent, 2, 1, 2); assertExtentEquals(extent, 3, 2, 3); assertExtentEquals(extent, 4, 1, 3); assertExtentEquals(extent, 5, 0, 0); // Unitialized envelope values were [0…0]. }
/** * 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#findOperation(Envelope, Envelope)}. * * @throws FactoryException if an error occurred while searching the operation. * * @since 1.0 */ @Test public void testFindOperation() throws FactoryException { final GeneralEnvelope source = new GeneralEnvelope(HardCodedCRS.WGS84); final GeneralEnvelope target = new GeneralEnvelope(HardCodedCRS.GEOCENTRIC); source.setRange(0, 20, 30); source.setRange(1, 40, 45); target.setRange(0, 6000, 8000); target.setRange(1, 7000, 9000); target.setRange(2, 4000, 5000); CoordinateOperation op = Envelopes.findOperation(source, target); assertInstanceOf("findOperation", Conversion.class, op); assertEquals("Geographic/geocentric conversions", ((Conversion) op).getMethod().getName().getCode()); }
/** * 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); }
/** * Tests iteration spanning the anti-meridian. * * <div class="note"><b>Tip:</b> in case of test failure, see {@link LocationViewer} as a debugging tool.</div> * * @throws TransformException if an error occurred while computing the coordinate. */ @Test @DependsOnMethod("testEncodeUTM") public void testIteratorOverAntiMeridian() throws TransformException { final GeneralEnvelope areaOfInterest = new GeneralEnvelope(CommonCRS.defaultGeographic()); areaOfInterest.setRange(0, 170, -175); areaOfInterest.setRange(1, 40, 42); testIterator(areaOfInterest, Arrays.asList( "59SME", "59SNE", "59SPE", "59SQE", "60STK", "60SUK", "60SVK", "60SWK", "60SXK", "60SYK", "1SBE", "1SCE", "1SDE", "1SEE", "1SFE", "59TME", "59TNE", "59TPE", "59TQE", "60TTK", "60TUK", "60TVK", "60TWK", "60TXK", "60TYK", "1TBE", "1TCE", "1TDE", "1TEE", "1TFE", "59TMF", "59TNF", "59TPF", "59TQF", "60TTL", "60TUL", "60TVL", "60TWL", "60TXL", "60TYL", "1TBF", "1TCF", "1TDF", "1TEF", "1TFF", "59TMG", "59TNG", "59TPG", "59TQG", "60TTM", "60TUM", "60TVM", "60TWM", "60TXM", "60TYM", "1TBG", "1TCG", "1TDG", "1TEG", "1TFG")); }
/** * Tests {@link GeneralEnvelope#setRange(int, double, double)} with a valid range, * then with a range which is known to be invalid. */ @Test public void testSetRange() { final GeneralEnvelope e = create(2, -4, 3, -3); e.setRange(1, -5, 2); try { e.setRange(1, -10, -20); fail("Invalid range shall not be allowed."); } catch (IllegalArgumentException ex) { // This is the expected exception. final String message = ex.getMessage(); assertTrue(message, message.contains(AxisNames.GEODETIC_LATITUDE)); } // Verify that the envelope still have the old values. assertEnvelopeEquals(e, 2, -5, 3, 2); verifyInvariants(e); }
/** * 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()); }
/** * 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 (indirectly) {@link ServicesForMetadata#setBounds(Envelope, DefaultGeographicBoundingBox, boolean)} * from an envelope spanning the antimeridian. * * @throws TransformException should never happen. */ @Test public void testSetGeographicBoundsSpanningAntimeridian() throws TransformException { final DefaultGeographicBoundingBox box = new DefaultGeographicBoundingBox(); final GeneralEnvelope envelope = createEnvelope(HardCodedCRS.WGS84); envelope.setRange(0, 170, 195); box.setBounds(envelope); assertEquals( 170, box.getWestBoundLongitude(), STRICT); assertEquals(-165, box.getEastBoundLongitude(), STRICT); envelope.setRange(0, 0, 360); box.setBounds(envelope); assertEquals(-180, box.getWestBoundLongitude(), STRICT); assertEquals(+180, box.getEastBoundLongitude(), STRICT); assertEquals( -20, box.getSouthBoundLatitude(), STRICT); assertEquals( 30, box.getNorthBoundLatitude(), STRICT); assertEquals(Boolean.TRUE, box.getInclusion()); }