Search code examples
geolocationgpscoordinatescoordinate-transformationutm

Calculating Bearing from UTM (Latitude, Longitude)


I want to calculate the relative bearing between two geo-coordinate points. I've gone ahead and converted the coordinates to UTM, but need assistance on figuring out the actual bearing. I'm aware that UTM is only good enough for same-zone calculations, which is fine as the computation will be done across very small distances.

Say point1 is my location 44.4N,-97.7W and point2 is the location I'd like to get the relative bearing to: 44.4N, -103.3W

Since point 2 is directly to the left of point 1, I'd interpret that as 270 degrees (North being 0 or 360 degrees).

I found this formula: arctan((y1-y2)/(x1-x2)) but it's result don't make sense to me when I plot the points and measure the angles.


Solution

  • FYI- For now, I'm using SQL Server 2008 Spatial Data Types, Spatial Functions, and some T-SQL function I found on the web.

    ALTER FUNCTION dbo.GeographyBearing (
      @Point1 geography,
      @Point2 geography  )
    RETURNS FLOAT
    AS
    BEGIN
      DECLARE @Bearing DECIMAL(18,15)
      DECLARE @Lat1 FLOAT = RADIANS(@Point1.Lat)
      DECLARE @Lat2 FLOAT = RADIANS(@Point2.Lat)
      DECLARE @dLon FLOAT = RADIANS(@Point2.Long - @Point1.Long)
      IF (@Point1.STEquals(@Point2) = 1)
        SET @Bearing = NULL
      ELSE
        SET @Bearing = ATN2(
          SIN(@dLon)*COS(@Lat2),
         (COS(@Lat1)*SIN(@Lat2)) - (SIN(@Lat1)*COS(@Lat2)*COS(@dLon))
        )
        SET @Bearing = (DEGREES(@Bearing) + 360) % 360
      RETURN ISNULL(@Bearing,0);
    END
    GO
    
    
    DECLARE @Vienna geography = geography::Point(16.37, 48.21, 4326)
    DECLARE @Moscow geography = geography::Point(37.60, 55.75, 4326)
    SELECT dbo.GeographyBearing(@Vienna,@Moscow)