Distance Calculation – Converting WGS-84 Distance to Meters in Java

distancejavawgs84

my (java) project uses com.vividsolutions.jts.geom, which provides a distance function. After some googling I finally found that the return value from this function is in central angle degrees, when the CRS is WGS-84 (as it is in my project).

What is an easy to implement and accurate on short distances (no need for long distances) conversion from central angle degrees to meters?

I tried converting by dividing by 360 (from degrees), multiplying by 2 * PI (to radians) and multiplying by 6371000 meters, but my answer is off by quite a lot (176m (QGis ellipsoidal ruler tool) vs 204m (mine)).

I found that GeoTools has some conversion functions included, but I would like to not have to depend on another library, since this code is meant to run on the server and an Android client and is meant to be easy to use by other devs.

EDIT: I also emailed the JTS mailing list, and established that wrapping the distance functions for every possible Geometry.distance(geometry) would be a little tedious.

I decided to use the 111 111 m per degree hack described here: Algorithm for offsetting a latitude/longitude by some amount of meters

Thanks for the help, though!

Best Answer

I have had reason to pursue a similar goal in the past where accuracy is not of huge importance. We can start by getting the Earth's radius according to the WGS84 Well-Known Text definition.

GEOGCS["WGS 84",
DATUM["WGS_1984",
    SPHEROID["WGS 84",6378137,298.257223563,
        AUTHORITY["EPSG","7030"]],
    AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,
    AUTHORITY["EPSG","8901"]],
UNIT["degree",0.01745329251994328,
    AUTHORITY["EPSG","9122"]],
AUTHORITY["EPSG","4326"]]

To make sense of the angular distance units, you can use the following formula to convert angular units to meters:

angular_units * (PI/180) * 6378137

That should give you a reasonably accurate short-distance measurements in meters. The calculation is based on a perfect sphere (non-ellipsoidal).