views:

5783

answers:

6

Does anyone know of a way, in Java, to convert an earth surface position from lat, lon to UTM (say in WGS84)? I'm currently looking at Geotools but unfortunately the solution is not obvious.

A: 

This problem is analogous to the one in this post. If you look at the solutions to it, you can easily adapt them to UTM.

Paul Tomblin
They're similar, yes, but not duplicates. The post you linked to is about a different map projection than UTM.
Scottie T
Alberta TM is a form of TM, as is UTM. If it works for Alberta TM, it will work for UTM with slightly different parameters.
Paul Tomblin
+2  A: 

Hi Steve - the Alberta 10 TM answer is probably overkill for what you need - this link from developer works probably has all the information you need.

Michael Sharek
A: 

Take a look at OpenMap specifically the com.bbn.openmap.proj.coords package in the API.

Matt
+1  A: 

Steve Dutch at University of Wisconson has a pretty good write-up on the algorithm. Also includes an Excel document to help verify your numbers.

James Schek
+1  A: 

I was able to use Geotools 2.4 to get something that works, based on some example code.

double utmZoneCenterLongitude = ...  // Center lon of zone, example: zone 10 = -123
int zoneNumber = ...                 // zone number, example: 10
double latitude, longitude = ...     // lat, lon in degrees

MathTransformFactory mtFactory = ReferencingFactoryFinder.getMathTransformFactory(null);
ReferencingFactoryContainer factories = new ReferencingFactoryContainer(null);

GeographicCRS geoCRS = org.geotools.referencing.crs.DefaultGeographicCRS.WGS84;
CartesianCS cartCS = org.geotools.referencing.cs.DefaultCartesianCS.GENERIC_2D;

ParameterValueGroup parameters = mtFactory.getDefaultParameters("Transverse_Mercator");
parameters.parameter("central_meridian").setValue(utmZoneCenterLongitude);
parameters.parameter("latitude_of_origin").setValue(0.0);
parameters.parameter("scale_factor").setValue(0.9996);
parameters.parameter("false_easting").setValue(500000.0);
parameters.parameter("false_northing").setValue(0.0);

Map properties = Collections.singletonMap("name", "WGS 84 / UTM Zone " + zoneNumber);
ProjectedCRS projCRS = factories.createProjectedCRS(properties, geoCRS, null, parameters, cartCS);

MathTransform transform = CRS.findMathTransform(geoCRS, projCRS);

double[] dest = new double[2];
transform.transform(new double[] {longitude, latitude}, 0, dest, 0, 1);

int easting = (int)Math.round(dest[0]);
int northing = (int)Math.round(dest[1]);
Steve Kuo
+1  A: 

I suggest JCoord. It allows you to convert between various cartographic coordinate schemes using a very simple API.

Iy you're feeling saucy, have a look at the source code; it's pages and pages of dense trigonometry. Splendid stuff.

There's also a javascript version called JSCoord.

skaffman