CoordinateReferenceSystem destCrs = CRS.decode("EPSG:4326"); Boolean latLon = null; if (crs != null) { CoordinateReferenceSystem sourceCrs = CRS.decode(crs); if (CRS.getAxisOrder(destCrs) == CRS.AxisOrder.NORTH_EAST) latLon = true; else if (CRS.getAxisOrder(destCrs) == CRS.AxisOrder.EAST_NORTH) DirectPosition2D orig = new DirectPosition2D(x, y); DirectPosition2D transformed = new DirectPosition2D(); mathTransform.transform(orig, transformed); lon = transformed.getY(); lat = transformed.getX();
try { GeneralEnvelope requestedEnvelope = new GeneralEnvelope(gridGeometry.getEnvelope()); final CoordinateReferenceSystem requestCRS = requestedEnvelope.getCoordinateReferenceSystem(); final CoordinateReferenceSystem nativeCRS = reader.getCoordinateReferenceSystem(); if (!CRS.equalsIgnoreMetadata(requestCRS, nativeCRS)) { requestedEnvelope = CRS.transform(requestedEnvelope, nativeCRS); requestedEnvelope.intersect(reader.getOriginalEnvelope()); if (!requestedEnvelope.isEmpty()) { MathTransform crsToGrid = meta.getGrid().getGridToCRS().inverse(); GeneralEnvelope requestedGrid = CRS.transform(crsToGrid, requestedEnvelope); double[] spans = new double[requestedGrid.getDimension()]; double[] resolutions = new double[requestedGrid.getDimension()];
if (!envelope.equals(this.envelope, DELTA, true)) { if (!envelope.intersects(this.envelope, true)) { throw new IllegalArgumentException( "The coverage envelopes need to intersect each other"); if (!CRS.equalsIgnoreMetadata(crs, this.crs)) { try { destinationToSourceTransform = CRS.findMathTransform(crs, this.crs, true); } catch (FactoryException e) { throw new DataSourceException("Unable to inspect request CRS", e); && !destinationToSourceTransform.isIdentity()) { throw new IllegalArgumentException( "The coverage coordinateReferenceSystem should be the same for all coverages");
/** See if we can use the stgeorge grid shift files as the ESPG db would like us to */ @Test public void testNadCon() throws Exception { CoordinateReferenceSystem crs4138 = CRS.decode("EPSG:4138"); CoordinateReferenceSystem crs4326 = CRS.decode("EPSG:4326"); MathTransform mt = CRS.findMathTransform(crs4138, crs4326); assertTrue(mt.toWKT().contains("NADCON")); double[] src = new double[] {-169.625, 56.575}; double[] expected = new double[] {-169.62744, 56.576034}; double[] p = new double[2]; mt.transform(src, 0, p, 0, 1); assertEquals(expected[0], p[0], 1e-6); assertEquals(expected[1], p[1], 1e-6); } }
/** Transforms the specified value. */ public double transform(final double value) throws TransformException { final double[] values = new double[] {value}; final double[] buffer = new double[] {transform1.getTargetDimensions()}; transform1.transform(values, 0, buffer, 0, 1); transform2.transform(buffer, 0, values, 0, 1); return values[0]; }
/** * Checks if transforms are compatibles. The default implementation check if transfert dimension * match. */ boolean isValid() { return transform1.getTargetDimensions() == transform2.getSourceDimensions(); }
private void inspectCoordinateReferenceSystems() throws DataSourceException { requestCRS = CRS.getHorizontalCRS(requestedBBox.getCoordinateReferenceSystem()); if (!CRS.equalsIgnoreMetadata(requestCRS, coverageProperties.crs2D)) try { destinationToSourceTransform = CRS.findMathTransform(requestCRS, coverageProperties.crs2D, true); } catch (FactoryException e) { throw new DataSourceException("Unable to inspect request CRS", e); if (destinationToSourceTransform.isIdentity()) { new ReferencedEnvelope(requestedBBox, coverageProperties.crs2D); } else { PixelInCell.CELL_CORNER); requestedBBox = new ReferencedEnvelope( CRS.transform( tempTransform, new GeneralEnvelope(requestedRasterArea)));
@Test public void testUTMDatelineWrapping() throws Exception { CoordinateReferenceSystem crs = CRS.decode("EPSG:32601", true); ReferencedEnvelope re = new ReferencedEnvelope(300000, 409800, 5890200, 6000000, crs); MathTransform mt = CRS.findMathTransform(crs, WGS84); Geometry geom = JTS.toGeometry(re); ReferencedEnvelope targetReferenceEnvelope = new ReferencedEnvelope(-180, 180, -90, 90, WGS84); ProjectionHandler ph = ProjectionHandlerFinder.getHandler(targetReferenceEnvelope, crs, true); Geometry preProcessed = ph.preProcess(geom); Geometry transformed = JTS.transform(preProcessed, mt); Geometry postProcessed = ph.postProcess(mt.inverse(), transformed); // sits across the dateline and it's "small" (used to cover the entire planet) Envelope ppEnvelope = postProcessed.getGeometryN(0).getEnvelopeInternal(); assertTrue(ppEnvelope.contains(180, 54)); // the original width is 109km, at this latitude one degree of longitude is only 65km assertEquals(1.7, ppEnvelope.getWidth(), 0.1); }
if (!CRS.equalsIgnoreMetadata(targetCRS, targetGGCRS) && !CRS.findMathTransform(targetCRS, targetGGCRS).isIdentity()) { throw new IllegalArgumentException( Errors.format( step2 = IdentityTransform.create(step1.getTargetDimensions()); step3 = step1.inverse(); allSteps = IdentityTransform.create(step1.getSourceDimensions()); step2 = IdentityTransform.create(step1.getTargetDimensions()); step3 = sourceGG.getGridToCRS(CORNER).inverse(); gridRange = CRS.transform(allSteps.inverse(), gridRange); .inverse(); GeneralEnvelope gridRange = CRS.transform(step1.inverse(), targetEnvelope); final Map<String, Object> imageProperties = new HashMap<String, Object>(); Warp warp = null; if (allSteps.isIdentity() || (allSteps instanceof AffineTransform && XAffineTransform.isIdentity((AffineTransform) allSteps, EPS))) { mtr = mtFactory.createConcatenatedTransform(mtr, step2.inverse()); final AffineTransform affine = (AffineTransform) allSteps.inverse(); w.affine(affine, interpolation, backgroundValues); operation = "Affine";
requestCRS = CRS.getHorizontalCRS(requestedBBox.getCoordinateReferenceSystem()); try { && !destinationToSourceTransform.isIdentity()) { final GeneralEnvelope temp = new GeneralEnvelope(CRS.transform(requestedBBox, coverageProperties.crs2D)); temp.setCoordinateReferenceSystem(coverageProperties.crs2D); cropBBox = new ReferencedEnvelope(temp); needsReprojection = true; new ReferencedEnvelope( requestedBBox.getMinX(), requestedBBox.getMaxX(), new ReferencedEnvelope( ((ReferencedEnvelope) cropBBox).intersection(coverageProperties.bbox), coverageProperties.crs2D); if (!CRS.equalsIgnoreMetadata(coverageProperties.geographicCRS2D, requestCRS)) { CRS.findMathTransform( requestCRS, coverageProperties.geographicCRS2D, true); if (!requestCRSToCoverageGeographicCRS2D.isIdentity()) { requestedBBOXInCoverageGeographicCRS = CRS.transform(requestedBBox, coverageProperties.geographicCRS2D); requestedBBOXInCoverageGeographicCRS.setCoordinateReferenceSystem( coverageProperties.geographicCRS2D);
@Test public void testWrapGeometryMercator() throws Exception { ReferencedEnvelope world = new ReferencedEnvelope(160, 180, -40, 40, WGS84); ReferencedEnvelope mercatorEnvelope = world.transform(MERCATOR, true); // move it so that it crosses the dateline (measures are still accurate for something // crossing the dateline mercatorEnvelope.translate(mercatorEnvelope.getWidth() / 2, 0); // a geometry that will cross the dateline and sitting in the same area as the // rendering envelope Geometry g = new WKTReader().read("LINESTRING(170 -40, 190 40)"); // make sure the geometry is not wrapped ProjectionHandler handler = ProjectionHandlerFinder.getHandler(mercatorEnvelope, WGS84, true); assertTrue(handler.requiresProcessing(g)); Geometry preProcessed = handler.preProcess(g); // no cutting expected assertEquals(g, preProcessed); // transform and post process MathTransform mt = CRS.findMathTransform(WGS84, MERCATOR, true); Geometry transformed = JTS.transform(g, mt); Geometry postProcessed = handler.postProcess(mt.inverse(), transformed); Envelope env = postProcessed.getEnvelopeInternal(); // check the geometry is in the same area as the rendering envelope assertEquals(mercatorEnvelope.getMinX(), env.getMinX(), EPS); assertEquals(mercatorEnvelope.getMaxX(), env.getMaxX(), EPS); }
/** * Test method for {@link CoordinateOperationFactoryUsingWKT#createCoordinateOperation}. * * @throws TransformException */ @Test public void testCreateOperationFromCustomCodes() throws Exception { // Test CRSs CoordinateReferenceSystem source = CRS.decode(SOURCE_CRS); CoordinateReferenceSystem target = CRS.decode(TARGET_CRS); MathTransform mt = CRS.findMathTransform(source, target, true); // Test MathTransform double[] p = new double[2]; mt.transform(SRC_TEST_POINT, 0, p, 0, 1); assertEquals(p[0], DST_TEST_POINT[0], 1e-8); assertEquals(p[1], DST_TEST_POINT[1], 1e-8); }
MathTransform tr; try { final CoordinateReferenceSystem WGS84 = CRS.decode("EPSG:4326", true); tr = CRS.findMathTransform(coverageCRS, WGS84); } catch (Exception e) { LOG.error("error creating CRS transform.", e); try { DirectPosition sourcePos = gridGeometry.gridToWorld(coord); DirectPosition targetPos = tr.transform(sourcePos, null); double lon = targetPos.getOrdinate(0); double lat = targetPos.getOrdinate(1);
throws FactoryException, TransformException { ProjectionHandler handler; CoordinateReferenceSystem sourceCRS = sourceEnvelope.getCoordinateReferenceSystem(); if (targetReferenceEnvelope == null) { targetReferenceEnvelope = ReferencedEnvelope.reference(getCRSEnvelope(targetCRS)); ReferencedEnvelope reference = new ReferencedEnvelope(targetCRS); handler = ProjectionHandlerFinder.getHandler(reference, sourceCRS, false); MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS); Geometry footprint = JTS.toGeometry(sourceEnvelope); Geometry preProcessed = handler.preProcess(footprint); Geometry postProcessed = handler.postProcess(transform.inverse(), transformed); if (postProcessed == null) { return null; sourceEnvelope = new ReferencedEnvelope(CRS.transform(sourceEnvelope, targetCRS));
/** Makes sure that the transform between two EPSG:4326 is the identity transform. */ public void testFindMathTransformIdentity() throws FactoryException { CoordinateReferenceSystem crs1default = CRS.decode("EPSG:4326"); CoordinateReferenceSystem crs2default = CRS.decode("EPSG:4326"); MathTransform tDefault = CRS.findMathTransform(crs1default, crs2default); assertTrue("WSG84 transformed to WSG84 should be Identity", tDefault.isIdentity()); CoordinateReferenceSystem crs1force = CRS.decode("EPSG:4326", true); CoordinateReferenceSystem crs2force = CRS.decode("EPSG:4326", true); MathTransform tForce = CRS.findMathTransform(crs1force, crs2force); assertTrue("WSG84 transformed to WSG84 should be Identity", tForce.isIdentity()); }
throws TransformException, NoninvertibleTransformException, FactoryException { final boolean isReprojected = !CRS.equalsIgnoreMetadata( readBBox.getCoordinateReferenceSystem(), requestedBBox.getCoordinateReferenceSystem()); if (isReprojected) { readBBox = readBBox.transform(requestedBBox.getCoordinateReferenceSystem(), true); CRS.transform(readBBox, requestedBBox.getCoordinateReferenceSystem()); final int NPOINTS = 36; double[] points = new double[NPOINTS * 2]; double x; if (i == 0) { x = cropBboxTarget.getMinimum(0) + resX / 2; } else if (i == 1) { x = cropBboxTarget.getMedian(0); } else { x = cropBboxTarget.getMaximum(0) - resX / 2; destinationToSourceTransform.transform(points, 0, points, 0, NPOINTS); GeneralEnvelope envelope = new GeneralEnvelope(new double[] {x0, y0}, new double[] {x1, y1}); envelope = CRS.transform(destinationToSourceTransform, envelope); GridToEnvelopeMapper mapper = new GridToEnvelopeMapper(new GridEnvelope2D(0, 0, 1, 1), envelope);
/** Tests the transformations of an envelope. */ @Test public void testEnvelopeTransformation() throws FactoryException, TransformException { final CoordinateReferenceSystem mapCRS = CRS.parseWKT(WKT.UTM_10N); final CoordinateReferenceSystem WGS84 = DefaultGeographicCRS.WGS84; final MathTransform crsTransform = CRS.findMathTransform(WGS84, mapCRS, true); assertFalse(crsTransform.isIdentity()); final GeneralEnvelope firstEnvelope, transformedEnvelope, oldEnvelope; firstEnvelope = new GeneralEnvelope(new double[] {-124, 42}, new double[] {-122, 43}); firstEnvelope.setCoordinateReferenceSystem(WGS84); transformedEnvelope = CRS.transform(crsTransform, firstEnvelope); transformedEnvelope.setCoordinateReferenceSystem(mapCRS); oldEnvelope = CRS.transform(crsTransform.inverse(), transformedEnvelope); oldEnvelope.setCoordinateReferenceSystem(WGS84); assertTrue(oldEnvelope.contains(firstEnvelope, true)); assertTrue(oldEnvelope.equals(firstEnvelope, 0.02, true)); }
/** Should fails unless GEOT-352 has been fixed. */ @Test(expected = OperationNotFoundException.class) public void testHtoH() throws Exception { final CoordinateReferenceSystem sourceCRS = crsFactory.createFromWKT(NAD27_H); final CoordinateReferenceSystem targetCRS = crsFactory.createFromWKT(WGS84_H); final CoordinateOperation op = opFactory.createOperation(sourceCRS, targetCRS); final MathTransform mt = op.getMathTransform(); assertNotSame(sourceCRS, op.getSourceCRS()); assertNotSame(targetCRS, op.getTargetCRS()); assertFalse(mt.isIdentity()); assertInterfaced(mt); }
/** * Tests the transformations of an envelope when the two CRS have identify transforms but * different datum names */ @Test public void testEnvelopeTransformation2() throws FactoryException, TransformException { final CoordinateReferenceSystem WGS84Altered = CRS.parseWKT(WKT.WGS84_ALTERED); final CoordinateReferenceSystem WGS84 = DefaultGeographicCRS.WGS84; final MathTransform crsTransform = CRS.findMathTransform(WGS84, WGS84Altered, true); assertTrue(crsTransform.isIdentity()); final GeneralEnvelope firstEnvelope; firstEnvelope = new GeneralEnvelope(new double[] {-124, 42}, new double[] {-122, 43}); firstEnvelope.setCoordinateReferenceSystem(WGS84); // this triggered a assertion error in GEOT-2934 Envelope transformed = CRS.transform(firstEnvelope, WGS84Altered); // check the envelope is what we expect assertEquals(transformed.getCoordinateReferenceSystem(), WGS84Altered); double EPS = 1e-9; assertEquals(transformed.getMinimum(0), firstEnvelope.getMinimum(0), EPS); assertEquals(transformed.getMinimum(1), firstEnvelope.getMinimum(1), EPS); assertEquals(transformed.getMaximum(0), firstEnvelope.getMaximum(0), EPS); assertEquals(transformed.getMaximum(1), firstEnvelope.getMaximum(1), EPS); }
if (name.equals(AbstractGridFormat.READ_GRIDGEOMETRY2D.getName().toString())) { final GridGeometry2D gg = (GridGeometry2D) param.getValue(); reqEnvelope = new GeneralEnvelope((Envelope) gg.getEnvelope2D()); coverageEnvelope.getCoordinateReferenceSystem(); CoordinateReferenceSystem requestCrs = reqEnvelope.getCoordinateReferenceSystem(); if (!CRS.equalsIgnoreMetadata(nativeCrs, requestCrs)) { LOGGER.fine( "Request CRS and native CRS differ, " dim = gg.getGridRange2D(); continue; try { MathTransform gridToWorld = getOriginalGridToWorld(PixelInCell.CELL_CENTER); MathTransform worldToGrid = gridToWorld.inverse(); adjustedGRange = CRS.transform(worldToGrid, reqEnvelope); } catch (Exception e) { throw new RuntimeException(e);