2

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.

Ellery Familia
  • 530
  • 5
  • 20
  • Just as an FYI, most formulas tend to work with radians. You might be expecting a result in degrees but that formula might be giving result in radians, so you might need to convert your result. – npinti Dec 01 '11 at 15:01
  • thanks npinti. I have been converting from degrees to radians and back... but I'll verify to ensure I'm doing those conversions correctly. – Ellery Familia Dec 01 '11 at 15:11

3 Answers3

1

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)
Ellery Familia
  • 530
  • 5
  • 20
0

My answer in Javascript http://jsfiddle.net/efwjames/NVhg6/

//stop location - the radii end point
var x1 = 44.9631;
var y1 = -93.2492;

//bus location from the southeast - the circle center
var x2 = 44.95517;
var y2 = -93.2427;

var radians = getAtan2((y1 - y2), (x1 - x2));

function getAtan2(y, x) {
    return Math.atan2(y, x);
};

$("#output").text(radians);
var newdeg = radians * (180 / Math.PI);

$("#deg").append(newdeg);

var coordNames = ["N", "NE", "E", "SE", "S", "SW", "W", "NW", "N"];
var directionid = Math.round(newdeg / 45);
if (directionid < 0) {
    directionid = directionid + 8
};

$("#dir").append("The vehicle is moving " + coordNames[directionid]);
ericjam
  • 1,501
  • 2
  • 20
  • 30
0

Please find here my answer related to an open source coordinates converter. It might be helpfull for you given your need.

Community
  • 1
  • 1
menjaraz
  • 7,551
  • 4
  • 41
  • 81
  • Thanks for sharing. The documentation is in french, but I'll look through the code when I get a chance. It looks like pascal. – Ellery Familia Dec 04 '11 at 22:00
  • @Ellery Familia: You are welcome. Yes indeed, it's definitely Delphi. Any programming language preference ? – menjaraz Dec 05 '11 at 05:12
  • I hope [Marcel](http://stackoverflow.com/users/79485/marcel)'s answer [here](http://stackoverflow.com/questions/569980/how-to-calculate-distance-from-a-gpx-file/8376709#8376709) is good for you, given your C# background. – menjaraz Dec 05 '11 at 05:24