我正在尝试在地图上放置一堆(x,y)点,这些点具有lon-lat原点。我想设置一个自定义的横轴墨卡托投影,以我的原点为中心,然后用它将我的点投射到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;
}
}
谢谢,
丹
答案 0 :(得分: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插件。这不会引发异常,也不会构建战争并将Web应用程序部署到Tomcat或Jetty。一切都很奇怪,但我现在很高兴忽略它。