我正在尝试在地图上放置一堆 (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;
}
}
谢谢,
担