/** * Check we are actually using the EPSG database for anything not in override * * @throws TransformException */ @Test public void testFallbackOnEPSGDatabaseStd() throws Exception { // Test CRSs CoordinateReferenceSystem source = CRS.decode("EPSG:3002"); CoordinateReferenceSystem target = CRS.decode("EPSG:4326"); CoordinateOperation co = CRS.getCoordinateOperationFactory(true).createOperation(source, target); ConcatenatedOperation cco = (ConcatenatedOperation) co; // the EPSG one only has two steps, the non EPSG one 4 assertEquals(2, cco.getOperations().size()); }
public ReprojectingFeatureCollection( SimpleFeatureCollection delegate, CoordinateReferenceSystem target) throws SchemaException, OperationNotFoundException, FactoryRegistryException, FactoryException { super(delegate); this.target = target; this.schema = FeatureTypes.transform(delegate.getSchema(), target); // create transform cache transformers = new HashMap(); // cache "default" transform CoordinateReferenceSystem source = delegate.getSchema().getCoordinateReferenceSystem(); if (source != null) { MathTransform tx = ReferencingFactoryFinder.getCoordinateOperationFactory(hints) .createOperation(source, target) .getMathTransform(); GeometryCoordinateSequenceTransformer transformer = new GeometryCoordinateSequenceTransformer(); transformer.setMathTransform(tx); transformers.put(source, transformer); } else { throw new RuntimeException( "Source was null in trying to create a reprojected feature collection!"); } }
/** * Returns {@code true} if the specified operation is an identity conversion. This method always * returns {@code false} for transformations even if their associated math transform is an * identity one, because such transformations are usually datum shift and must be visible. */ private static boolean isIdentity(final CoordinateOperation operation) { return (operation instanceof Conversion) && operation.getMathTransform().isIdentity(); }
double utmZoneCenterLongitude = ... // Center lon of zone, example: zone 10 = -123 int zoneNumber = ... // zone number, example: 10 double latitude, longitude = ... // lat, lon in degrees MathTransformFactory mtFactory = ReferencingFactoryFinder.getMathTransformFactory(null); ReferencingFactoryContainer factories = new ReferencingFactoryContainer(null); GeographicCRS geoCRS = org.geotools.referencing.crs.DefaultGeographicCRS.WGS84; CartesianCS cartCS = org.geotools.referencing.cs.DefaultCartesianCS.GENERIC_2D; ParameterValueGroup parameters = mtFactory.getDefaultParameters("Transverse_Mercator"); parameters.parameter("central_meridian").setValue(utmZoneCenterLongitude); parameters.parameter("latitude_of_origin").setValue(0.0); parameters.parameter("scale_factor").setValue(0.9996); parameters.parameter("false_easting").setValue(500000.0); parameters.parameter("false_northing").setValue(0.0); Map properties = Collections.singletonMap("name", "WGS 84 / UTM Zone " + zoneNumber); ProjectedCRS projCRS = factories.createProjectedCRS(properties, geoCRS, null, parameters, cartCS); MathTransform transform = CRS.findMathTransform(geoCRS, projCRS); double[] dest = new double[2]; transform.transform(new double[] {longitude, latitude}, 0, dest, 0, 1); int easting = (int)Math.round(dest[0]); int northing = (int)Math.round(dest[1]);
/** 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]; }
/** 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); } }
MathTransform crsToGrid = meta.getGrid().getGridToCRS().inverse(); GeneralEnvelope requestedGrid = CRS.transform(crsToGrid, requestedEnvelope); double[] spans = new double[requestedGrid.getDimension()];
&& !destinationToSourceTransform.isIdentity()) { throw new IllegalArgumentException( "The coverage coordinateReferenceSystem should be the same for all coverages");
/** Gets the derivative of this function at a value. */ public double derivative(final double value) throws TransformException { final double value1 = transform1.derivative(value); final double value2 = transform2.derivative(transform1.transform(value)); return value2 * value1; }
/** Transforms a list of coordinate point ordinal values. */ public void transform( final double[] srcPts, final int srcOff, final double[] dstPts, final int dstOff, final int numPts) throws TransformException { transform.transform(srcPts, srcOff, dstPts, dstOff, numPts); }
/** * Update the internal state after a change, before to apply transformation. The most important * change is to update the math transform, if needed. */ private void update() throws FactoryException { if (transform == null && sourceCRS != null && targetCRS != null) { transform = factory.createOperation(sourceCRS, targetCRS).getMathTransform(); } }
/** Transforms a list of coordinate point ordinal values. */ public void transform( final float[] srcPts, final int srcOff, final float[] dstPts, final int dstOff, final int numPts) throws TransformException { transform.transform(srcPts, srcOff, dstPts, dstOff, numPts); }
ReferencingFactoryFinder .getCoordinateOperationFactory(hints) .createOperation(crs, target) .getMathTransform(); } catch (Exception e) { String msg = "Could not transform for crs: " + crs;
/** * 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); }
/** * Test method for {@link CoordinateOperationFactoryUsingWKT#createCoordinateOperation}. * * @throws TransformException */ @Test public void testOverrideEPSGOperation() throws Exception { // Test CRSs CoordinateReferenceSystem source = CRS.decode("EPSG:4269"); CoordinateReferenceSystem target = CRS.decode("EPSG:4326"); 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); }
tr.transform(sourcePos, sourcePos);
/** * Computes the difference between the ellipsoid and geoid at a specified lat/lon using Geotools EarthGravitationalModel * * @param lat * @param lon * @return difference in meters * @throws FactoryException * @throws TransformException */ public static double computeEllipsoidToGeoidDifference(double lat, double lon) throws FactoryException, TransformException { // Set up a MathTransform based on the EarthGravitationalModel EarthGravitationalModel.Provider provider = new EarthGravitationalModel.Provider(); DefaultMathTransformFactory factory = new DefaultMathTransformFactory(); MathTransform mt = factory.createParameterizedTransform(provider.getParameters().createValue()); // Compute the offset DirectPosition3D dest = new DirectPosition3D(); mt.transform(new DirectPosition3D(lon, lat, 0), dest); return dest.z; } }
tr.transform(sourcePos, sourcePos);
DirectPosition targetPos = tr.transform(sourcePos, null); double lon = targetPos.getOrdinate(0); double lat = targetPos.getOrdinate(1);
DirectPosition2D orig = new DirectPosition2D(x, y); DirectPosition2D transformed = new DirectPosition2D(); mathTransform.transform(orig, transformed);