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]);
/** * Creates an instance from the specified hints. This method recognizes the {@link * Hints#CRS_FACTORY CRS}, {@link Hints#CS_FACTORY CS}, {@link Hints#DATUM_FACTORY DATUM} and * {@link Hints#MATH_TRANSFORM_FACTORY MATH_TRANSFORM} {@code FACTORY} hints. * * @param hints The hints, or {@code null} if none. * @return A factory group created from the specified set of hints. */ public static ReferencingFactoryContainer instance(final Hints hints) { final Hints completed = GeoTools.getDefaultHints(); if (hints != null) { completed.add(hints); } /* * Use the same synchronization lock than ReferencingFactoryFinder (instead of this class) * in order to reduce the risk of dead lock. This is because ReferencingFactoryContainer * creation may queries ReferencingFactoryFinder, and some implementations managed by * ReferencingFactoryFinder may ask for a ReferencingFactoryContainer in turn. */ synchronized (ReferencingFactoryFinder.class) { if (cache == null) { cache = new FactoryCreator( Arrays.asList(new Class<?>[] {ReferencingFactoryContainer.class})); cache.registerFactory( new ReferencingFactoryContainer(null), ReferencingFactoryContainer.class); } return cache.getFactory(ReferencingFactoryContainer.class, null, completed, null); } }
hints.put(Hints.MATH_TRANSFORM_FACTORY, mtFactory); final ReferencingFactoryContainer container = new ReferencingFactoryContainer(hints); assertSame(datumFactory, container.getDatumFactory()); assertSame(csFactory, container.getCSFactory());
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]);
ReferencingFactoryContainer factories = new ReferencingFactoryContainer(null);