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
*/
// ===================================
/* DistanceSpherCoord ( _Lat1 ; _Lon1 ; _Lat2 ; _Lon2 )__LITBR__ __LITBR__by Andries Heylen, BH&A__LITBR____LITBR__v.ß1 - April 20, 2009__LITBR____LITBR__Calculates the distance between two spherical co-ordinates, with an almost exact accuracy using the Haversine formula__LITBR____LITBR__*/__LITBR____LITBR__Let ( __LITBR__[__LITBR__ _R = 6367000 //Radius of the Earth in meters__LITBR__; _Lat1 = ( _Lat1 * Pi ) / 180 //converted degrees to radians__LITBR__; _Lon1 = ( _Lon1 * Pi ) / 180 //converted degrees to radians__LITBR__; _Lat2 = ( _Lat2 * Pi ) / 180 //converted degrees to radians__LITBR__; _Lon2 = ( _Lon2 * Pi ) / 180 //converted degrees to radians__LITBR____LITBR__//Haversine Formula__LITBR__; _dlon = _Lon2 - _Lon1__LITBR____LITBR__; _dlat = _Lat2 - _Lat1__LITBR____LITBR__; _a = ( Sin ( _dlat / 2 ) ) ^ ( 2 ) + Cos ( _Lat1 ) * Cos ( _Lat2 ) * ( Sin ( _dlon / 2 ) ) ^ ( 2 )__LITBR____LITBR__; _Res = 2 * Asin ( Min ( 1 ; _a ^ ( 1 / 2 ) ) )__LITBR____LITBR__; _Final = _R * _Res__LITBR____LITBR__]__LITBR__;__LITBR____LITBR__//Divide by 1000 to convert to KM, and round to 2 digits__LITBR____LITBR__Round ( _Final / 1000 ; 2 )__LITBR____LITBR__)__LITBR____LITBR__// ===================================__LITBR__/*__LITBR____LITBR__ This function is published on FileMaker Custom Functions__LITBR__ to check for updates and provide feedback and bug reports__LITBR__ please visit http://www.fmfunctions.com/fid/218__LITBR____LITBR__ Prototype: DistanceSpherCoord( _Lat1; _Lon1; _Lat2; _Lon2 )__LITBR__ Function Author: Andries Heylen (http://www.fmfunctions.com/mid/57)__LITBR__ Last updated: 21 April 2009__LITBR__ Version: 1__LITBR____LITBR__*/__LITBR__// ===================================
Login or register to comment
Create a new account with fmcustomfunctions.com or login to post a comment.