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

dan twining dantwining at googlemail.com
Wed Mar 3 06:15:55 EST 2010


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 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/geotoolkit/attachments/20100303/20784032/attachment.html


More information about the Geotoolkit mailing list