DistanceSpherCoord ( )

Function stats

Average user rating
57
218
9999
Support
FileMaker 7.0 +
Date posted
21 April 2009
Version
1
Recursive function
No

Author Info
 Andries Heylen

6 functions

Average Rating 5.0

author_avatar



 

Function overview

Prototype

DistanceSpherCoord  ( _Lat1;   _Lon1;   _Lat2;   _Lon2 )


Parameters

_Lat1  Latitude location 1


_Lon1  Longitude location 1


_Lat2  Latitude location 2


_Lon2  Longitude location 2


Description

Tags:  Spherical   Global   Distance   Coordinates  

Haversine Formula (from R.W. Sinnott, "Virtues of the Haversine", Sky and Telescope, vol. 68, no. 2, 1984, p. 159):

dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin^2(dlat/2) + cos(lat1) * cos(lat2) * sin^2(dlon/2)
c = 2 * arcsin(min(1,sqrt(a)))
d = R * c

will give mathematically and computationally exact results. The intermediate result c is the great circle distance in radians. The great circle distance d will be in the same units as R.

The min() function protects against possible roundoff errors that could sabotage computation of the arcsine if the two points are very nearly antipodal (that is, on opposide sides of the Earth). Under these conditions, the Haversine Formula is ill-conditioned (see the discussion below), but the error, perhaps as large as 2 km (1 mi), is in the context of a distance near 20,000 km (12,000 mi).

Source : http://www.movable-type.co.uk/scripts/gis-faq-5.1.html

Examples

Sample input

DistanceCoord ( 52,3826 ; 4,637 ; 52,23 ; 4,55 )


Sample output

17,96

 

Function code

/* DistanceSpherCoord ( _Lat1 ; _Lon1 ; _Lat2 ; _Lon2 )

by Andries Heylen, BH&A

v.ß1 - April 20, 2009

Calculates the distance between two spherical co-ordinates, with an almost exact accuracy using the Haversine formula

*/

Let (
[
_R = 6367000 //Radius of the Earth in meters
; _Lat1 = ( _Lat1 * Pi ) / 180 //converted degrees to radians
; _Lon1 = ( _Lon1 * Pi ) / 180 //converted degrees to radians
; _Lat2 = ( _Lat2 * Pi ) / 180 //converted degrees to radians
; _Lon2 = ( _Lon2 * Pi ) / 180 //converted degrees to radians

//Haversine Formula
; _dlon = _Lon2 - _Lon1

; _dlat = _Lat2 - _Lat1

; _a = ( Sin ( _dlat / 2 ) ) ^ ( 2 ) + Cos ( _Lat1 ) * Cos ( _Lat2 ) * ( Sin ( _dlon / 2 ) ) ^ ( 2 )

; _Res = 2 * Asin ( Min ( 1 ; _a ^ ( 1 / 2 ) ) )

; _Final = _R * _Res

]
;

//Divide by 1000 to convert to KM, and round to 2 digits

Round ( _Final / 1000 ; 2 )

)

// ===================================
/*

    This function is published on FileMaker Custom Functions
    to check for updates and provide feedback and bug reports
    please visit http://www.fmfunctions.com/fid/218

    Prototype: DistanceSpherCoord( _Lat1; _Lon1; _Lat2; _Lon2 )
    Function Author: Andries Heylen (http://www.fmfunctions.com/mid/57)
    Last updated: 21 April 2009
    Version: 1

*/
// ===================================

 

 

 

 


Login or register to comment

Create a new account with fmcustomfunctions.com or login to post a comment.

 

 

 

Top Tags

Text Parsing  (31)
List  (20)
XML  (20)
Date  (19)
Format  (18)
Debug  (12)
Dev  (11)
Variables  (11)
Layout  (11)
Interface  (10)
Text  (10)
Filter  (10)
ValueIterator  (6)
Layout Objects  (6)
Uuid  (6)
HTML  (6)