I am using Java GeoTools to compute the azimuth between two UTM32N points, e.g. between POINT(496383,5419109) and POINT(528196,6070949). Is it possible to do so without converting the points to WGS84 first?

The following code does not run because Latitude 5419109°00.0'N is out of range (±90°):

CoordinateReferenceSystem crs = CRS.decode("EPSG:32632");
GeodeticCalculator gc = new GeodeticCalculator();
gc.setStartingPosition(new DirectPosition2D(crs, p1.getX(), p1.getY()));
gc.setDestinationPosition(new DirectPosition2D(crs, p2.getX(), p2.getY()));
return gc.getAzimuth();

After a ton of trial-and-error I was able to get the code to run by converting the points to WGS84 first:

CoordinateReferenceSystem crs = CRS.decode("EPSG:32632");
MathTransform transformToWgs84 = CRS.findMathTransform(crs, DefaultGeographicCRS.WGS84);
Coordinate p1Wgs84 = JTS.transform(p1.getCoordinate(), null, transformToWgs84);
Coordinate p2Wgs84 = JTS.transform(p2.getCoordinate(), null, transformToWgs84);
GeodeticCalculator gc = new GeodeticCalculator();
gc.setStartingPosition(new DirectPosition2D(DefaultGeographicCRS.WGS84, p1Wgs84.getX(), p1Wgs84.getY()));
gc.setDestinationPosition(new DirectPosition2D(DefaultGeographicCRS.WGS84, p2Wgs84.getX(), p2Wgs84.getY()));
return gc.getAzimuth();

However, this "solution" is somewhat unsatisfactory. Is there a way to directly compute the azimuth between two UTM32N points?

  • 3
    What kind of accuracy are you after? UTM is cartesian, you could use simple plane geometry.
    – inc42
    Apr 2 '20 at 17:57
  • 2
    @inc42 This is true. I haven't considered it and will look into it. How big of an error am I looking at if I stay within the same zone?
    – dotwin
    Apr 2 '20 at 18:01

Following inc42's comment, I implemented some simple Cartesian acrobatics to obtain the azimuth between two UTM32N points:

double[] v12 = {p2.getX() - p1.getX(), p2.getY() - p1.getY()};
double deg = Math.toDegrees(Math.acos(v12[1] / Math.sqrt(v12[0]*v12[0] + v12[1]*v12[1])));
return v12[0]<0.0 ? -deg : deg;

Checking the resulting azimuths for 100 randomly picked points within Germany, the difference between both methods appears to be within 2°. This is an error I can live with...

  • Using Math.atan(v12[1],v12[0]) should give more accurate results.
    – julien
    Apr 20 '20 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.