[geotk] Concerting Coordinates between custom Transverse Mercator and WGS84 Lon-Lat

Martin Desruisseaux martin.desruisseaux at geomatys.fr
Wed Mar 3 07:35:11 EST 2010


Hello Dan

I just tried to executed the code snippet that you sent, replacing "GeoLocation" 
and "Vector2D" by Point2D.Double since I do not have those classes. I did not 
get any exception. However there is some tips that may make things easier (I 
will add them to the referencing FAQ later).


Le 03/03/10 12:15, dan twining a écrit :
> public GeoLocation toLonLat(Vector2D location) {
>   (...snip...)
>   MathTransform tr = getMathTransform(factory);
>   DirectPosition sourcePt = new GeneralDirectPosition(location.getX(), location.getY());
>   DirectPosition targetPt = tr.transform(sourcePt, null);
>   (...snip...)
> }

It is worth to point out that this method is called "toLonLat" but is actually 
projecting from "geographic" to UTM, since the map projection created in the 
"getMathTransform()" method. Not sure if it is relevant...


> private MathTransformFactory getMathTransformFactory() {
>   if (factory == null) {
>     FactoryRegistry registry = new FactoryRegistry(MathTransformFactory.class);
>     factory = registry.getServiceProvider(MathTransformFactory.class, ...);
>   }
>   return factory;
> }

Mentioning just in case, you can also just invoke the 
FactoryFinder.getMathTransformFactory(null) static method.

http://www.geotoolkit.org/apidocs/org/geotoolkit/factory/FactoryFinder.html#getMathTransformFactory%28org.geotoolkit.factory.Hints%29

A note about the getMathTransform(...) method. The code snippet that you send it 
correct (if you still have the ClassCastException, maybe posting the stack trace 
would help?) Just mentioning in case, an alternative is also to apply the usual 
2 step approach:

1) Define your source and target CRS where:
   - The source is DefaultGeographicCRS.WGS84
   - The target can be created with either of the following:
     * CRS.decode("EPSG:" + (32600 + zone)) where 'zone' is
       the UTM zone (this require the EPSG database - use
       32700 instead of 32600 if you want South hemisphere);
     * CRS.decode("AUTO:42002,long,lat") where 'long' and 'lat'
       are your central latitude and meridian.

2) Get your MathTransform with CRS.findMathTransform(sourceCRS,
    targetCRS);

This is more heavy than your approach (use it only if performance is not 
critical in this area of your code), but using CoordinateReferenceSystem objects 
that you can associate with your coordinates is often a recommanded approach, a 
little bit like associating Unit with measurement values.

	Regards,

		Martin


More information about the Geotoolkit mailing list