Question

Je suis en train de mettre un tas de (x, y) des points sur une carte, qui ont une origine lat-lon. Je voudrais mettre en place une projection personnalisée Transverse Mercator, centrée sur mon origine, et l'utiliser pour projeter mes points en-lat lon.

Cela semble être le genre de chose qui GeoToolkit (ou GeoTools) devraient être en mesure de le faire, mais je vais avoir beaucoup de mal à déterminer la meilleure façon d'utiliser la bibliothèque Référencer.

Je suis parvenu à écrire un code qui fonctionne parfois, mais d'autres fois, je reçois une exception ClassCast, comme GeoToolkit tente de charger le MathTransformFactory. Toute aide serait très appréciée. Voici mon 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;
    }
}

Merci,

dan

Était-ce utile?

La solution

Il se trouve que l'exception est causée par une interaction bizarre entre GeoToolkit et le GWT (Google Web Toolkit) « devmode » (la conversion de coordonnées se passe dans l'arrière-plan d'une application Web GWT / Java). Pour mémoire, c'est l'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)
...

Mon travail autour actuelle est d'utiliser l'Eclipse GWT Plug-in. Cela ne jette pas l'exception, ni la construction de la guerre et le déploiement de la web-app soit Tomcat ou Jetty. Tout cela est très étrange, mais je suis content de l'ignorer pour l'instant.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top