GPS coordinates to metres

Francis Nugent Dixon effendi at
Tue Mar 7 13:46:01 EST 2006

Hi Monte,

in your mail you don't show the complete Lat/Long information
(you need N/S for Lat, and E/W for Long).  I suppose that you
didn't include them, but you need them for what follows.

Try the Haversine formula I use to get distances between 2
points. You first have to convert your Lat/Long into Radians.

I got my co-ordinates (corresponding to a city) from a data
base, and converted them to radians first.

Let's assume you have your Lat and Long co-ordinates in
fields MyLatitude and MyLongitude, and the direction in fields
MyNS and MyEW, and you want the radian answer in fields
MyRadLatitude and MyRadLongitude, you convert to radians
by :

   set numberFormat to 0.000000000000  -- We need precision here !
   --     Convert Latitude and Logitude from degrees to Radians
   --     (Latitude and Longitude must be valid co-ordinates).
   --     This is in preparation for the calculation of distances between
   --     two point on the earths surface.
   --     These co-ordinates must be converted into Radians to use them  
   --     my Haversine formula.
   put field MyLatitude into LVWork1
   put LVWork1 * 0.017453293 into LVIntermed1
   put LVIntermed1 into field MyRadLatitude
   put field MyLongitude into LVWork2
   put LVWork2 * 0.017453293 into LVIntermed2
   put LVIntermed2 into field MyRadLongitude
   if field MyNS = "S" then put - field MyRadLatitude into field  
   if field MyEW = "W" then put - field MyRadLongitude into field  

Then, using function :

Function RFHaversine PVLat1,PVLong1,PVLat2,PVLong2
   --     V1 for Hypercard  - 2003/06/28 - 18:00   no atan2 function  
available in Hypercard.
   --     V2 for Revolution - 2006/02/24 - 11:23   use atan2 function  
available in Revolution.
   --     Use Haversine two-argument Inverse Tangent Function to  
   --     the distance between two geographic locations (cities).
   --     The two geographic locations represent cities or towns in my
   --     City Stack. The pre-calling sequence has already identified
   --     the two cities, and extracted their locations in Radians.
   --     Input Parameters :
   --     Latitude and Longitude of location 1 in Radians  
   --     Latitude and Longitude of location 2 in Radians  
   --     The Haversine formula is :
   --     dlon = lon2 - lon1  where lon1 and lon2 are longitudes of two  
   --     dlat = lat2 - lat1  where lat1 and lat2 are latitudes of two  
   --     a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
   --     c = 2 * atan2( sqrt(a), sqrt(1-a) )
   --     d = R * c           where R is the radius of the earth in  
   set numberFormat to 0.000000000000  -- Heavy precision required here !
   put 6367 into LVRadiusOfTheEarth       -- Generally accepted Radius  
of the Earth
   put PVLat1 into LVLat1
   put PVLong1 into LVLong1
   put PVLat2 into LVLat2
   put PVLong2 into LVLong2
   put LVLong2 - LVLong1 into LVInt1
   put LVLat2 - LVLat1 into LVInt2
   put (sin(LVInt2/2))^2 + cos(LVLat1) * cos(LVLat2) * (sin(LVInt1/2))^2  
into LVWork1
   put 2 * atan2(sqrt(LVWork1),sqrt(1-LVWork1)) into LVWork2
   put LVRadiusOfTheEarth * LVWork2 into LVWork3
   --     We now have the distance in kilometres in LVWork3.
   put round(LVWork3,3) into LVWork3     -- Kilometres to 3 decimal  
   return LVWork3
end RFHaversine

You can get the distance between your two points with :

put RFHaversine (LVRadLat1,LVRadLong1,LVRadLat2,LVRadLong2) into  

Either set the radius of the Earth to metres, or remove the
point in the kilometre answer field and "Bobs your Uncle !"

If anybody else is interested, I have a stack containing data
for 4000 cities and the possibility of calculating the distance
between any two cities, with up to 6 city stopovers if necessary.
(its got 4001 cards and is 1.6 Mb).

I hope this fills your requirements.

Best from Paris


More information about the Use-livecode mailing list