1

I am already using proj4j in my project to convert between different coordinate reference systems. (Internally I use WGS84 but some sources supply data in a different CRS.)

Now I need to calculate the distance between two points in meters and am wondering if (and how) that is possible with PROJ4J. (Just in case: my use case is limited to a maximum distance of around 100 km.)

I have come across org.osgeo.proj4j.util.ProjectionMath#greatCircleDistance(double, double, double, double), but that class is not documented at all. What CRS and unit does that method expect for the latitude/longitude values? And what is the unit for the result?

2

This function implements the Haversine formula (see the source code here - it is indeed not documented).

The input parameters are geographical coordinates (double lon1, double lat1, double lon2, double lat2) in radian. It returns a coefficient which should be multiplied by the earth radius to become a distance.

So, use for example:

double distanceInMeters = 6378137 * ProjectionMath.greatCircleDistance(lon1 * Math.PI/180, lat1 * Math.PI/180, lon2 * Math.PI/180, lat2 * Math.PI/180);

to get a distance in meters.

| improve this answer | |
  • 1
    Thanks! Looking at the source code, I see that lat/lon args are used as input to Math.sin() and Math.cos(). So I assume they need to be in radians, using any spherical coordinate system (including WGS84), correct? – user149408 May 26 at 22:47
  • Oh, yes, good point I forgot: These angles have to be in radian ! – julien May 27 at 5:21

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.