在自定义横轴墨卡托坐标和 WGS84 经纬度之间转换坐标
我试图在地图上放置一堆具有经纬度原点的 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,该异常是由 GeoToolkit 和 GWT(Google Web Toolkit)“devmode”之间奇怪的交互引起的(坐标转换发生在 GWT/Java Web 应用程序的后端)。根据记录,这是一个例外:
我当前的解决方法是使用 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:
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.