Jump to content
Server Maintenance This Week. ×

haversine formula


This topic is 3340 days old. Please don't post here. Open a new topic instead.

Recommended Posts

for shortest distance between two points along great circle
someone may already have done this...
or you can write a custom function to do the same thing..

Let ( [
R = 6372.8 ;
dLat = Radians ( laty - latx ) ;
dLon = Radians ( lony - lonx ) ;
lati = Radians ( latx ) ;
latj = Radians ( laty ) ;
a = Sin ( dLat / 2 ) * Sin ( dLat / 2 ) + Sin ( dLon / 2 ) * Sin ( dLon / 2 ) * Cos ( lati ) * Cos ( latj ) ;
c = 2 * Asin ( Sqrt ( a ) ) ; 
M = R * c * .621371192
] ;
Case ( IsEmpty ( units ) ; Int ( M ) & " mi " & Round ( ( M - Int ( M ) ) * 1760 ; 2 ) & " yds" ; Round ( R * c ; 3 ) & " km" )
)
// HaversineDistance ( latx ; lonx ; laty ; lony ; units )
// 15_02_03 JR
// v1.0
// returns shortest diatance by Haversine method
// if units then it is in KM else miles and yds

R = 6372.8d

lati = latx.toDouble()
loni = lonx.toDouble()
latj = laty.toDouble()
lonj = lony.toDouble()

dLat = Math.toRadians(latj - lati)
dLon = Math.toRadians(lonj - loni)
lati = Math.toRadians(lati)
latj = Math.toRadians(latj)
a = Math.sin(dLat / 2 ) * Math.sin (dLat / 2 ) + Math.sin(dLon / 2) * Math.sin(dLon / 2 ) * Math.cos(lati) * Math.cos(latj)
c = 2 * Math.asin(Math.sqrt( a ))
if ( units ){
	return (R * c).round(3) + ' km'
} else {
	M = ((R * c) * 0.621371192)
	return M.toInteger() + ' mi ' + ((M - M.toInteger()) * 1760).round(2) + ' yds'
} //end if
Link to comment
Share on other sites

What's the benefit of doing this with ScriptMaster instead of as a custom function?

 

Presuming that the goal is to calculate distances between two points on the surface of the Earth (based on the radius being used), there's also a custom function using a different formula based on an ellipsoidal rather than spherical approximation, and so is more accurate (and more complicated).

Link to comment
Share on other sites

Nice CF JB...

I had written a ScriptMaster function to use a SOAP call to a site returning geo-location information, and adding this to the parsing of the JSON meant I could return the information they are really after in one step.

Link to comment
Share on other sites

This topic is 3340 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.