public BoundingBox toBounds(CoordinateReferenceSystem targetCRS) throws TransformException { Envelope transformed = new GeneralEnvelope((BoundingBox) this); transformed = CRS.transform(transformed, targetCRS); return new Envelope2D(transformed); }
public static JGrassRegion getJGrassRegionFromGridCoverage(GridCoverage2D gridCoverage2D) throws InvalidGridGeometryException, TransformException { Envelope2D env = gridCoverage2D.getEnvelope2D(); GridEnvelope2D worldToGrid = gridCoverage2D.getGridGeometry().worldToGrid(env); double xRes = env.getWidth() / worldToGrid.getWidth(); double yRes = env.getHeight() / worldToGrid.getHeight(); JGrassRegion region = new JGrassRegion( env.getMinX(), env.getMaxX(), env.getMinY(), env.getMaxY(), xRes, yRes); return region; }
static GridCoverage2D displace( GridCoverage2D coverage, double tx, double ty, GridCoverageFactory gridCoverageFactory) { // let's compute the new grid geometry GridGeometry2D originalGG = coverage.getGridGeometry(); GridEnvelope gridRange = originalGG.getGridRange(); Envelope2D envelope = originalGG.getEnvelope2D(); double minx = envelope.getMinX() + tx; double miny = envelope.getMinY() + ty; double maxx = envelope.getMaxX() + tx; double maxy = envelope.getMaxY() + ty; ReferencedEnvelope translatedEnvelope = new ReferencedEnvelope( minx, maxx, miny, maxy, envelope.getCoordinateReferenceSystem()); GridGeometry2D translatedGG = new GridGeometry2D(gridRange, translatedEnvelope); GridCoverage2D translatedCoverage = gridCoverageFactory.create( coverage.getName(), coverage.getRenderedImage(), translatedGG, coverage.getSampleDimensions(), new GridCoverage2D[] {coverage}, coverage.getProperties()); return translatedCoverage; }
static Envelope2D doInscribeFullDisk(Envelope2D circumscribed) { double dx = circumscribed.getWidth() / SQRT2; double dy = circumscribed.getHeight() / SQRT2; return new Envelope2D( circumscribed.getCoordinateReferenceSystem(), circumscribed.getCenterX() - dx / 2., circumscribed.getCenterY() - dy / 2., dx, dy); }
/** * A coordinate position consisting of all the minimal ordinates for each dimension for all * points within the {@code Envelope}. * * @return The lower corner. * @todo Change the return type to {@link DirectPosition2D} when we will be allowed to compile * for J2SE 1.5. */ public DirectPosition getLowerCorner() { return new DirectPosition2D(crs, getMinX(), getMinY()); }
public void testTransformLambertAzimuthalEqualAreaWgs84() throws Exception { CoordinateReferenceSystem crs = CRS.decode("EPSG:3574", true); Envelope2D envelope = new Envelope2D(crs); // random bbox that does include the pole envelope.add(-3142000, -3142000); envelope.add(3142000, 3142000); Envelope transformed = CRS.transform(envelope, DefaultGeographicCRS.WGS84); // check we got the whole range of longitudes, since the original bbox contains the pole assertEquals(-180d, transformed.getMinimum(0), 0d); assertEquals(180d, transformed.getMaximum(0), 0d); }
BoundingBox readBoundingBox = new Envelope2D(readEnvelope); for (GridCoverage2D gc : readCoverages) { Envelope2D gce = gc.getEnvelope2D(); if (gce.contains(readBoundingBox)) { cov = gc; break; Envelope2D covEnvelope = cov.getEnvelope2D(); GridCoverage2D cropped = cov; if (covEnvelope.contains(readBoundingBox) && (covEnvelope.getWidth() > readBoundingBox.getWidth() || covEnvelope.getHeight() > readBoundingBox.getHeight())) { cropped = cropOnEnvelope(cov, readEnvelope); final boolean equalsMetadata = CRS.equalsIgnoreMetadata(outputCRS, coverageCRS); boolean sameCRS; try { PixelInCell.CELL_CENTER, scaling); readGG = new GridGeometry2D(PixelInCell.CELL_CENTER, transform, subset, hints); .inverse(), subset) .toRectangle2D()
Envelope e2d = JTS.getEnvelope2D( cropRoi.getEnvelopeInternal(), source.getCoordinateReferenceSystem()); cropEnvelope = new GeneralEnvelope(e2d); final Envelope2D sourceEnvelope = source.getEnvelope2D(); Envelope2D destinationEnvelope = new Envelope2D(cropEnvelope); CoordinateReferenceSystem sourceCRS = sourceEnvelope.getCoordinateReferenceSystem(); CoordinateReferenceSystem destinationCRS = destinationEnvelope.getCoordinateReferenceSystem(); if (destinationCRS == null) { final Envelope2D ge = new Envelope2D(destinationEnvelope); destinationCRS = source.getCoordinateReferenceSystem2D(); ge.setCoordinateReferenceSystem(destinationCRS); destinationEnvelope = ge; if (!CRS.equalsIgnoreMetadata(sourceCRS, destinationCRS)) { throw new CannotCropException( Errors.format( (AffineTransform) ((GridGeometry2D) source.getGridGeometry()) .getGridToCRS(PixelInCell.CELL_CORNER);
/** Circumscribed rectangle (smallest) for full disk earth image */ public static Envelope2D circumscribeFullDisk(CoordinateReferenceSystem geosCRS) throws TransformException, FactoryException { if (!isGeostationaryCRS(geosCRS)) { return null; } MathTransform mt = CRS.findMathTransform(geosCRS, CRS.getProjectedCRS(geosCRS).getBaseCRS(), true); MathTransform imt = mt.inverse(); ParameterValueGroup parameters = CRS.getMapProjection(geosCRS).getParameterValues(); double semiMajorAxis = parameters.parameter("semi_major").doubleValue(); double satelliteHeight = parameters.parameter("satellite_height").doubleValue(); double centralMeridian = parameters.parameter("central_meridian").doubleValue(); DirectPosition2D dp2d = new DirectPosition2D(); double halfFoVRadians = Math.acos(semiMajorAxis / (satelliteHeight + semiMajorAxis)); double halfFoVDegrees = Math.toDegrees(halfFoVRadians); dp2d.setLocation(centralMeridian - halfFoVDegrees, 0.); imt.transform(dp2d, dp2d); double xMin = dp2d.getX(); dp2d.setLocation(centralMeridian + halfFoVDegrees, 0.); imt.transform(dp2d, dp2d); double xMax = dp2d.getX(); dp2d.setLocation(centralMeridian, -halfFoVDegrees); imt.transform(dp2d, dp2d); double yMin = dp2d.getY(); dp2d.setLocation(centralMeridian, halfFoVDegrees); imt.transform(dp2d, dp2d); double yMax = dp2d.getY(); return new Envelope2D(geosCRS, xMin, yMin, xMax - xMin, yMax - yMin); }
@Test public void testUTM() throws Exception { // setup a request large enough to cause severe reprojection deformation CoordinateReferenceSystem crs = CRS.decode("EPSG:32632", true); ReferencedEnvelope mapExtent = new ReferencedEnvelope(-1.5e7, 1.5e7, 0, 1e6, crs); // System.out.println(mapExtent.transform(DefaultGeographicCRS.WGS84, true)); GridCoverageReaderHelper helper = new GridCoverageReaderHelper( reader, new Rectangle(400, 200), mapExtent, Interpolation.getInstance(Interpolation.INTERP_NEAREST)); // read single coverage with no projection handling, we should get the full requested area GridCoverage2D coverage = helper.readCoverage(null); Envelope2D envelope = coverage.getEnvelope2D(); // System.out.println(envelope); assertTrue(envelope.getMinX() < -100); assertTrue(envelope.getMaxX() > 100); // now read via the projection handlers ProjectionHandler handler = ProjectionHandlerFinder.getHandler( mapExtent, reader.getCoordinateReferenceSystem(), true); List<GridCoverage2D> coverages = helper.readCoverages(null, handler); // System.out.println(coverages); assertEquals(1, coverages.size()); envelope = coverages.get(0).getEnvelope2D(); // west/east limited to 45 degrees from the central meridian, plus reading gutter assertEquals(-36, envelope.getMinX(), EPS); assertEquals(54, envelope.getMaxX(), EPS); }
public void testTransformLambertAzimuthalEqualAreaWgs84NonPolar() throws Exception { CoordinateReferenceSystem crs = CRS.decode("EPSG:3035", true); // a bbox that does _not_ include the pole Envelope2D envelope = new Envelope2D(crs); envelope.setFrameFromDiagonal(4029000, 2676000, 4696500, 3567700); Envelope transformed = CRS.transform(envelope, DefaultGeographicCRS.WGS84); // check we did _not_ get the whole range of longitudes assertEquals(5.42, transformed.getMinimum(0), 1e-2); assertEquals(15.88, transformed.getMaximum(0), 1e-2); }
assertThat(inscribed, is(notNullValue())); final DirectPosition2D p = new DirectPosition2D(); p.setLocation(inscribed.getMaxX(), inscribed.getMaxY()); sphericalGeosToGeog.transform(p, p); assertThat(p, is(notNullValue())); geogToSphericalGeos.transform(p, p); assertThat(p, is(notNullValue())); p.setLocation(inscribed.getMaxX(), inscribed.getMinY()); sphericalGeosToGeog.transform(p, p); assertThat(p, is(notNullValue())); geogToSphericalGeos.transform(p, p); assertThat(p, is(notNullValue())); p.setLocation(inscribed.getMinX(), inscribed.getMaxY()); sphericalGeosToGeog.transform(p, p); assertThat(p, is(notNullValue())); assertThat(p, is(notNullValue())); p.setLocation(inscribed.getMinX(), inscribed.getMinY()); sphericalGeosToGeog.transform(p, p); assertThat(p, is(notNullValue()));
new AffineTransform( (AffineTransform) ((GridGeometry2D) source.getGridGeometry()) .getGridToCRS2D(PixelOrientation.UPPER_LEFT)); final MathTransform worldToGridTransform; try { final CoordinateReferenceSystem crs = source.getCoordinateReferenceSystem2D(); final Envelope2D envelope = source.getEnvelope2D(); final DirectPosition2D LLC = new DirectPosition2D(crs, envelope.x, envelope.y); LLC.setCoordinateReferenceSystem(crs); final DirectPosition2D URC = new DirectPosition2D(crs, envelope.x + xPeriod, envelope.y + yPeriod); URC.setCoordinateReferenceSystem(crs); final Envelope2D shrinkedEnvelope = new Envelope2D(LLC, URC); CRS.transform(worldToGridTransform, shrinkedEnvelope).toRectangle2D();
assertThat(circumscribed, is(notNullValue())); final DirectPosition2D p = new DirectPosition2D(); p.setLocation(circumscribed.getCenterX(), circumscribed.getMaxY()); ellipsoidalGeosToGeog.transform(p, p); assertThat(p, is(notNullValue())); geogToEllipsoidalGeos.transform(p, p); assertThat(p, is(notNullValue())); p.setLocation(circumscribed.getCenterX(), circumscribed.getMinY()); ellipsoidalGeosToGeog.transform(p, p); assertThat(p, is(notNullValue())); geogToEllipsoidalGeos.transform(p, p); assertThat(p, is(notNullValue())); p.setLocation(circumscribed.getMaxX(), circumscribed.getCenterY()); ellipsoidalGeosToGeog.transform(p, p); assertThat(p, is(notNullValue())); assertThat(p, is(notNullValue())); p.setLocation(circumscribed.getMinX(), circumscribed.getCenterY()); ellipsoidalGeosToGeog.transform(p, p); assertThat(p, is(notNullValue()));
/** * Transforms a rectangle represented by a GridEnvelope2D object from grid to world coordinates. * The bounds of the Envelope2D object returned correspond to the outer edges of the grid cells * within the input envelope. * * <p>Users needing more control over the nature of the conversion can use the {@code * MathsTransform} provided by {@linkplain GridGeometry2D#getGridToCRS2D(PixelOrientation) } * which is accessed via {@linkplain #getGridGeometry()}. * * @param gridEnv The rectangle of grid coordinates to convert * @return World coordinates of the rectangle as a new Envelope2D object * @throws TransformException if the transformation failed. * @throws IllegalArgumentException if the input rectangle lies outside the coverage * @since 2.6 */ public final Envelope2D gridToWorld(final GridEnvelope2D gridEnv) throws TransformException { MathTransform2D mt = getGridToCRS2D(); if (getGridRange2D().contains(gridEnv)) { GridCoordinates2D low = gridEnv.getLow(); Point2D trLow = mt.transform(new Point2D.Double(low.x - 0.5, low.y - 0.5), null); GridCoordinates2D high = gridEnv.getHigh(); Point2D trHigh = mt.transform(new Point2D.Double(high.x + 0.5, high.y + 0.5), null); return new Envelope2D( new DirectPosition2D(crs2D, trLow.getX(), trLow.getY()), new DirectPosition2D(crs2D, trHigh.getX(), trHigh.getY())); } else { throw new IllegalArgumentException( Errors.format(ErrorKeys.POINT_OUTSIDE_COVERAGE_$1, gridEnv)); } }
CoordinateReferenceSystem worldVanDerGrinten = CRS.parseWKT(wkt); Envelope2D envelope = new Envelope2D(worldVanDerGrinten); envelope.add(-39842778.796051726, -42306552.87521737); envelope.add(40061162.89695589, 37753756.60975308); Envelope transformed = CRS.transform(envelope, DefaultGeographicCRS.WGS84);
if (!CRS.equalsIgnoreMetadata(crs, DefaultGeographicCRS.WGS84)) { GeneralEnvelope env = CRS.transform(envelope, DefaultGeographicCRS.WGS84); if (get2D) { requestedWGS84 = new Envelope2D(env); ((Envelope2D) requestedWGS84) .setCoordinateReferenceSystem(DefaultGeographicCRS.WGS84); } else { requestedWGS84 = env; if (get2D) return new Envelope2D(envelope); else return new GeneralEnvelope(envelope);
private GridGeometry2D applyReadGutter(GridGeometry2D gg) { MathTransform gridToCRS = gg.getGridToCRS(); GridEnvelope2D range = new GridEnvelope2D(gg.getGridRange2D()); applyReadGutter(range); CoordinateReferenceSystem crs = gg.getEnvelope2D().getCoordinateReferenceSystem(); GridGeometry2D result = new GridGeometry2D(range, PixelInCell.CELL_CORNER, gridToCRS, crs, null); return result; }
public void test() throws Exception { p.setLocation(circumscribed.getMaxX(), circumscribed.getMaxY()); sphericalGeosToGeog.transform(p, p); } });