Calculating the distance/bearing between two lat lon co ordinates

3.8k views Asked by At

I've been trying to work out the bearing between two lat lon co ordinates but am having trouble grasping the concept.

I have been to http://www.movable-type.co.uk/scripts/latlong.html and have been able to change to distance code to work with lua, however the bearing code in the paragraphs under has me a little confused.

local y = Math.sin(dLon) * Math.cos(lat2)
local x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon)
local brng = Math.atan2(y, x).toDeg()

The naming of the variables (lat1, lat2, dLon) has me confused.

If my initial lat lon is:

Latitude = -33.8830555556 Longitude = 151.216666667

and my destination lat and lon is:

Latitude = 22.25 Longitude = 114.1667

What variables need to match up to which lat and lons?

Is the dLon variable refering to the distance between the two points longitudinally?

Thanks a bunch!

3

There are 3 answers

0
lhf On BEST ANSWER

Based on the JavaScript code, lat1 is the latitude of the initial point and lat2 is the latitude of the destination point.

Note that all latitudes and longitudes must be in radians; use math.rad() for the conversion.

Also the math library in Lua is called math, not Math.

0
Dave Dopson On

Try this

local function geo_distance(lat1, lon1, lat2, lon2)
  if lat1 == nil or lon1 == nil or lat2 == nil or lon2 == nil then
    return nil
  end
  local dlat = math.rad(lat2-lat1)
  local dlon = math.rad(lon2-lon1)
  local sin_dlat = math.sin(dlat/2)
  local sin_dlon = math.sin(dlon/2)
  local a = sin_dlat * sin_dlat + math.cos(math.rad(lat1)) * math.cos(math.rad(lat2)) * sin_dlon * sin_dlon
  local c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
  -- 6378 km is the earth's radius at the equator.
  -- 6357 km would be the radius at the poles (earth isn't a perfect circle).
  -- Thus, high latitude distances will be slightly overestimated
  -- To get miles, use 3963 as the constant (equator again)
  local d = 6378 * c
  return d
end

Input coordinates are in degrees, so geo_distance(30.19, 71.51, 31.33, 74.21) = ~287km

0
Ilias Tselios On

Here is a function I've made and works perfect. Hard tested in X-Plane Flight Simulator. At the end there is a division of earth's radius by 1852, so the function returns the distance to nautical miles.

function GC_distance_calc(lat1, lon1, lat2, lon2)

--This function returns great circle distance between 2 points.
--Found here: http://bluemm.blogspot.gr/2007/01/excel-formula-to-calculate-distance.html
--lat1, lon1 = the coords from start position (or aircraft's) / lat2, lon2 coords of the target waypoint.
--6371km is the mean radius of earth in meters. Since X-Plane uses 6378 km as radius, which does not makes a big difference,
--(about 5 NM at 6000 NM), we are going to use the same.
--Other formulas I've tested, seem to break when latitudes are in different hemisphere (west-east).

local distance = math.acos(math.cos(math.rad(90-lat1))*math.cos(math.rad(90-lat2))+
    math.sin(math.rad(90-lat1))*math.sin(math.rad(90-lat2))*math.cos(math.rad(lon1-lon2))) * (6378000/1852)

return distance

end