2

我正在尝试在地图上放置一堆 (x,y) 点,它们的原点为经纬度。我想设置一个自定义的横向墨卡托投影,以我的原点为中心,并使用它将我的点投影到 lon-lat。

这似乎是 GeoToolkit(或 GeoTools)应该能够做的事情,但我很难确定使用引用库的最佳方式。

我设法编写了一些有时可以工作的代码,但有时我得到一个 ClassCast 异常,因为 GeoToolkit 试图加载 MathTransformFactory。任何帮助将非常感激。这是我的代码:

 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;
    }
}

谢谢,

4

1 回答 1

0

事实证明,该异常是由 GeoToolkit 和 GWT(Google Web Toolkit)“devmode”之间的奇怪交互引起的(坐标转换发生在 GWT/Java Web 应用程序的后端)。为了记录,这是一个例外:

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)
...

我目前的解决方法是使用 GWT Eclipse 插件。这不会引发异常,也不会构建战争并将网络应用程序部署到 Tomcat 或 Jetty。一切都很奇怪,但我很高兴暂时忽略它。

于 2010-03-04T09:07:31.523 回答