2
votes

enter image description hereI'm looking at the formula listed here: http://www.movable-type.co.uk/scripts/latlong.html

I seem to be having trouble as the resulting coordinates are not what I would expect them to be.

Given the following information:

Start lat: 28.455556

Start lon: -80.527778

Bearing: 317.662819(Degrees)

Distance: 130.224835(Nautical miles)

def getEndpoint(lat1,lon1,bearing,d):
        R = 6378.1                   #Radius of the Earth
        brng = math.radians(bearing) #convert degrees to radians
        d = d*1.852                  #convert nautical miles to km
        lat2 = math.asin( math.sin(lat1)*math.cos(d/R) + math.cos(lat1)*math.sin(d/R)*math.cos(brng))
        lon2 = lon1 + math.atan2(math.sin(brng)*math.sin(d/R)*math.cos(lat1),math.cos(d/R)-math.sin(lat1)*math.sin(lat2))
        return lat2,lon2

The function returns:

end lat: -0.209110644042

end lon: -80.5017472335

But this is a coordinate east of my beginning location, it doesn't make any sense because 317 bearing is pointing north-west of my starting location.

Above picture is what It should look like with the final ending coordinate on the upper left.

Where is it going wrong?

4
I think lat1 and lon1 need to be in radians. And the result will be in radians.jcfollower
stackoverflow.com/questions/7222382/… This should be more exact position.bryan wang

4 Answers

2
votes

Doh I had forgot to convert to radians then convert back to degrees when calculation was done. Here is the final code:

def getEndpoint(lat1,lon1,bearing,d):
    R = 6371                     #Radius of the Earth
    brng = math.radians(bearing) #convert degrees to radians
    d = d*1.852                  #convert nautical miles to km
    lat1 = math.radians(lat1)    #Current lat point converted to radians
    lon1 = math.radians(lon1)    #Current long point converted to radians
    lat2 = math.asin( math.sin(lat1)*math.cos(d/R) + math.cos(lat1)*math.sin(d/R)*math.cos(brng))
    lon2 = lon1 + math.atan2(math.sin(brng)*math.sin(d/R)*math.cos(lat1),math.cos(d/R)-math.sin(lat1)*math.sin(lat2))
    lat2 = math.degrees(lat2)
    lon2 = math.degrees(lon2)
    return lat2,lon2
2
votes

If you want a high precision result, consider using geodesics. Here is an example with GeographicLib, which uses angular units in degrees and distance units in metres.

from geographiclib.constants import Constants
from geographiclib.geodesic import Geodesic

def getEndpoint(lat1, lon1, bearing, d):
    geod = Geodesic(Constants.WGS84_a, Constants.WGS84_f)
    d = geod.Direct(lat1, lon1, bearing, d * 1852.0)
    return d['lat2'], d['lon2']

print(getEndpoint(28.455556, -80.527778, 317.662819, 130.224835))
# (30.05352669918092, -82.21197985232848)

This should be no more than a few nanometres from the exact position.

1
votes

According to this page

math.sin(x) ... Return the sine of x radians.

So, you need to convert lat1 and lon1 to radians before your equation and then later you can convert lat2 and lon2 back into degrees.

0
votes

Also note that your latitude is just south of the equator. I suspect that your problem is coordinate system: trig functions generally work in Cartesian: the reference angle (bearing 0) is the +x axis, also known as "due east", and progress toward +y (counter-clockwise). Compass headings start at north and go clockwise.

Replacement 1:

brng = math.radians(90-bearing) #convert degrees to radians

You've also missed using your starting latitutde. Try:

lat2 = lat1 + math.asin(...

This gives us the final location of

(28.246445355975514, -80.50284677329569)