Search code examples
sql-serverazure-sql-databaseazure-sql-server

How calculate two geo distance (Latitude,Logitude,Altidute) points in Azure Sql server?


How do I get an accurate distance (in meters) given two geo-points (two latitude/longitude/Altitude pair) in Azure Sql server?

Latitude Longitude Altitude 22.30634 113.92934 125

Latitude Longitude Altitude 58.31962 -157.54788 35000

Is there any possible way to calculate the distance including Altitude in SQL server?


Solution

  • Assuming you are looking for the hypotenuse which is SQRT(Power(L,2)+Power(H,2)

    To be clear, this hypotenuse does NOT account for the curvature of the earth.

    Here is a little example which shows both.

     declare @lat1 float = 22.30634 
     declare @lng1 float = 113.92934
     declare @alt1 float = 125
    
     declare @lat2 float = 58.31962
     declare @lng2 float = -157.54788
     declare @alt2 float = 35000
    
     
    SELECT JustLatLng = geography::Point(@lat1, @lng1, 4326).STDistance(geography::Point(@lat2, @lng2, 4326))
          ,WithAlt    = SQRT(Power(geography::Point(@lat1, @lng1, 4326).STDistance(geography::Point(@lat2, @lng2, 4326)),2)
                            +Power((@alt1-@alt2)*0.3048,2))
    

    Returns

    JustLatLng          WithAlt
    7838823.94282909    7838831.1502063   --<< delta of 7.20737721