Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Bearings

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-08-08 : 07:39:28
Kevin writes "I am trying to build a function that will return the bearing (0 to 360 degrees east of north) from decimal degree coordinates (lat1,lon1,lat2,lon2). I see formulas on the web but they don't seem to be SQL friendly (or I'm not up to the job of translating them into SQL language).

bearing =mod(atan2(sin(lon2-lon1)*cos(lat2),
cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1)),
2*pi)

Here is one from Ask Dr.Math who says the answer is the modulo of this formula.
So I tried:

(atan2(sin(lon2-lon1)*cos(lat2),
cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1))%
2*pi)

The error says there is a datatype of float and modulo only works for integers.

Is anyone aware of a usable formula or able to figure out how to simulate a modulo that works for 'real' numbers?

Again the goal is to return the heading from 0 to 306 degrees."

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-08-08 : 09:19:21
All the trigonometric funcirons in your formula are available in SQL Server.

Since modulua is just the computation of the reaminder, you should be able to do that by dividing, rounding down the result to the nearest integer, multiplying the divisor by that integer, and subtracting from the starting number to get the remainder.





CODO ERGO SUM
Go to Top of Page
   

- Advertisement -