在自定义横轴墨卡托坐标和 WGS84 经纬度之间转换坐标

发布于 2024-08-23 10:26:30 字数 2293 浏览 3 评论 0原文

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

这似乎是 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;
    }
}

谢谢,

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场信仰旅途 2024-08-30 10:26:30

事实证明,该异常是由 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 插件。这不会引发异常,构建战争并将 Web 应用程序部署到 Tomcat 或 Jetty 也不会引发异常。一切都很奇怪,但我很高兴暂时忽略它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文