I am having problems implementing the function described here here.
This is my Java implementation:
private static double[] pointRadialDistance(double lat1, double lon1,
double radianBearing, double radialDistance) {
double lat = Math.asin(Math.sin(lat1)*Math.cos(radialDistance)+Math.cos(lat1)
*Math.sin(radialDistance)*Math.cos(radianBearing));
double lon;
if(Math.cos(lat) == 0) { // Endpoint a pole
lon=lon1;
}
else {
lon = ((lon1-Math.asin(Math.sin(radianBearing)*Math.sin(radialDistance)/Math.cos(lat))
+Math.PI) % (2*Math.PI)) - Math.PI;
}
return (new double[]{lat, lon});
}
I convert the degree bearing to radians and convert the distance (km) into a radians distance before calling the function - so that's not the problem.
However, when I input coordinates such as: lat = 49.25705; lon = -123.140259; with a bearing of 225 (south-west) and a distance of 1km
I get this returned: lat: -1.0085434360125864 lon: -3.7595299668539504
Its obviously not correct, can anyone see what I am doing wrong?
Thanks
Everything is working as intended, but the problem is that your maths assumes the Earth to be a sphere when in reality it approximates an ellipsoid.
A quick trawl of your favoured search engine for 'Vincenty Formula' will hopefully prove useful.
When I implemented this, my resulting latitudes were correct but the longitudes were wrong. For example starting point: 36.9460678N 9.434807E, Bearing 45.03334, Distance 15.0083313km The result was 37.0412865N 9.315302E That's further west than my starting point, rather than further east. In fact it's as if the bearing was 315.03334 degrees.
More web searching led me to: http://www.movable-type.co.uk/scripts/latlong.html The longitude code is show below (in C# with everything in radians)
This seems to work fine for me. Hope it's helpful.
Thanks for your python code I tried setting it up in my use case where I'm trying to find the lat lon of a point in between two others at a set distance from the first point so it's quite similare to your code appart that my bearing is dynamically calculated
startpoint(lat1) lon1/lat1 = 55.625541,-21.142463
end point (lat2) lon2/lat2 = 55.625792,-22.142248
my result should be a point in between these two at lon3/lat3 unfortunetly I get lon3/lat3 = 0.0267695450609,0.0223553243666
I thought this might be a difference in lat lon but no when I add or sub it it's not good
any advice would be really great Thanks
here's my implementation
distance = 0.001 epsilon = 0.000001
calculating bearing dynamically
calculating lat3 lon3 dynamically
I think there is a problem in the Algorithm provided in message 5.
It works but for only for the latitude, for the longitude there is a problem because of the sign.
The data speaks for themselves :
If you start from -123.14° and go WEST you should have something FAR in the WEST. Here we go back on the EAST (-123.13) !
The formula should includes somewhere :
before radian convertion.
Fundamentally, it appears that your problem is that you are passing latitude, longitude and bearing as degrees rather than radians. Try ensuring that you are always passing radians to your function and see what you get back.
PS: see similar issues discussed here and here.
It seems like these are the issues in your code:
lat1
andlon1
to radians before calling your function.radialDistance
incorrectly.abs(x-y) < threshold
is safer thanx == y
for testing two floating-point numbersx
andy
for equality.lat
andlon
from radians to degrees.Here is my implementation of your code in Python:
Here is the output: