Hi,<br><br>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.<br>
<br>This seems like the sort of thing that GeoToolkit should be able to do, but I'm having great difficulty in determining the best way to use the Referencing library.<br><br>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:<br>
<br><span style="font-family: courier new,monospace;">public class LonLatConverter {</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> private static final double WGS84_AXIS_MINOR = 6356752.314;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> private static final double WGS84_AXIS_MAJOR = 6378137.000;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> private GeoLocation origin;</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> private static MathTransformFactory factory;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> public LonLatConverter(GeoLocation origin) {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> this.origin = origin;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> public GeoLocation toLonLat(Vector2D location) {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> double lon = 0;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> double lat = 0;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> try {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> MathTransformFactory factory = getMathTransformFactory();</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> MathTransform tr = getMathTransform(factory);</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> DirectPosition sourcePt = new GeneralDirectPosition(</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> location.getX(), location.getY());</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> DirectPosition targetPt = tr.transform(sourcePt, null);</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> lon = targetPt.getOrdinate(0);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> lat = targetPt.getOrdinate(1);</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> } catch (Exception e) {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> throw new RuntimeException(e);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return new GeoLocation(lon, lat);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> private MathTransform getMathTransform(MathTransformFactory factory)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> throws NoninvertibleTransformException, FactoryException {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> ParameterValueGroup p = factory</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> .getDefaultParameters("Transverse_Mercator");</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> p.parameter("semi_major").setValue(WGS84_AXIS_MAJOR);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> p.parameter("semi_minor").setValue(WGS84_AXIS_MINOR);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> p.parameter("central_meridian").setValue(origin.getLongitude());</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> p.parameter("latitude_of_origin").setValue(origin.getLatitude());</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> MathTransform tr = factory.createParameterizedTransform(p).inverse();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return tr;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> private MathTransformFactory getMathTransformFactory() {</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if (factory == null) {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> FactoryRegistry registry = new FactoryRegistry(</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> MathTransformFactory.class);</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> factory = registry.getServiceProvider(MathTransformFactory.class,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> null, null, Hints.MATH_TRANSFORM_FACTORY);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return factory;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">}</span><br><br><br>Thanks,<br><br>dan<br>