views:

513

answers:

1

Hi,

I'm trying to put a bunch of (x,y) points on a map, that have a lon-lat origin. I'd like to set up a custom Transverse Mercator projection, centred on my origin, and use that to project my points into lon-lat.

This seems like the sort of thing that GeoToolkit (or GeoTools) should be able to do, but I'm having great difficulty in determining the best way to use the Referencing library.

I have managed to write some code that works sometimes, but other times I get a ClassCast exception, as GeoToolkit attempts to load the MathTransformFactory. Any help would be much appreciated. Here's my code:

 public class LonLatConverter {

    private static final double WGS84_AXIS_MINOR = 6356752.314;
    private static final double WGS84_AXIS_MAJOR = 6378137.000;
    private GeoLocation origin;

    private static MathTransformFactory factory;

    public LonLatConverter(GeoLocation origin) {
        this.origin = origin;
    }

    public GeoLocation toLonLat(Vector2D location) {
        double lon = 0;
        double lat = 0;
        try {
            MathTransformFactory factory = getMathTransformFactory();

            MathTransform tr = getMathTransform(factory);

            DirectPosition sourcePt = new GeneralDirectPosition(
                    location.getX(), location.getY());
            DirectPosition targetPt = tr.transform(sourcePt, null);

            lon = targetPt.getOrdinate(0);
            lat = targetPt.getOrdinate(1);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return new GeoLocation(lon, lat);
    }

    private MathTransform getMathTransform(MathTransformFactory factory)
            throws NoninvertibleTransformException, FactoryException {
        ParameterValueGroup p = factory
                .getDefaultParameters("Transverse_Mercator");
        p.parameter("semi_major").setValue(WGS84_AXIS_MAJOR);
        p.parameter("semi_minor").setValue(WGS84_AXIS_MINOR);
        p.parameter("central_meridian").setValue(origin.getLongitude());
        p.parameter("latitude_of_origin").setValue(origin.getLatitude());
        MathTransform tr = factory.createParameterizedTransform(p).inverse();
        return tr;
    }

    private MathTransformFactory getMathTransformFactory() {

        if (factory == null) {
            FactoryRegistry registry = new FactoryRegistry(
                    MathTransformFactory.class);

            factory = registry.getServiceProvider(MathTransformFactory.class,
                    null, null, Hints.MATH_TRANSFORM_FACTORY);
        }

        return factory;
    }
}

Thanks,

dan

A: 

It turns out that the exception is caused by a weird interaction between GeoToolkit and the GWT (Google Web Toolkit) "devmode" (the coordinate conversion is happening in the back-end of a GWT/Java web app). For the record, this is the exception:

Caused by: java.lang.RuntimeException: java.lang.ClassCastException: class org.geotoolkit.referencing.operation.DefaultMathTransformFactory
     at com.example.LonLatConverter.toLonLat(LonLatConverter.java:47)
...

Caused by: java.lang.ClassCastException: class org.geotoolkit.referencing.operation.DefaultMathTransformFactory
     at java.lang.Class.asSubclass(Class.java:3018)
     at org.geotoolkit.factory.FactoryRegistry.register(FactoryRegistry.java:921)
    at org.geotoolkit.factory.FactoryRegistry.scanForPlugins(FactoryRegistry.java:793)
    at org.geotoolkit.factory.FactoryRegistry.scanForPluginsIfNeeded(FactoryRegistry.java:843)
    at org.geotoolkit.factory.FactoryRegistry.getServiceProviders(FactoryRegistry.java:236)
    at com.example.LonLatConverter.getMathTransformFactory(LonLatConverter.java:71)
    at com.example.LonLatConverter.toLonLat(LonLatConverter.java:35)
...

My current work-around is to use the GWT Eclipse Plug-in. That doesn't throw the exception, nor does building the war and deploying the web-app to either Tomcat or Jetty. All very strange, but I'm happy to ignore it for now.

dan