rexx-things/projects/oorexx/dist.rex

57 lines
2.8 KiB
Rexx

/* ---------------------------------------------------------------- */
/* Calculates the distance between (possibly a series) of */
/* 2 latitude/longitude pairs. May be used in a pipe. */
/* ---------------------------------------------------------------- */
Call RxFuncAdd "MathLoadFuncs","rxmath","MathLoadFuncs"
Call MathLoadFuncs
/* ---------------------------------------------------------------- */
/* The following formula's can be used to calculate the distance */
/* between two points on earth: */
/* */
/* d=R*acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2)) */
/* */
/* or */
/* */
/* d=R*2*asin(sqrt((sin((lat1-lat2)/2))^2 + cos(lat1)*cos(lat2)* */
/* (sin((lon1-lon2)/2)^2)) */
/* */
/* where */
/* */
/* R -- the radius of the earth, commonly taken to be: 6378,137 km */
/* lat1/lat2 -- latitude in degrees (NOT degrees minutes seconds) */
/* lon1/lon2 -- longitude in degrees (NOT degrees minutes seconds */
/* */
/* The first formula is more subject to possible rounding errors */
/* for short distances and since the route description produced by */
/* Google Earth/Maps often has points within a couple of meters */
/* this code uses the second formula. */
/* */
/* ---------------------------------------------------------------- */
/* -oldradius = 6366710 */
googleradius = 6378137 /* -- Equatorial earth radius used by Google */
Say "Enter 4 values: FROM(latitude,longitude) TO(latitude,longitude)"
Parse Upper Value Linein() With "FROM(" flat "," flon ") TO(" tlat "," tlon ")"
If (tlat<>'' & tlon<>'') Then Say "Distance: "||Distance(flat,flon,tlat,tlon)
Do While Lines()>0
Parse Value Linein() With tlat tlon .
Say Distance(flat,flon,tlat,tlon)
flat = tlat
flon = tlon
End
Exit
Distance: Procedure Expose googleradius
Parse Arg flat, flon, tlat, tlon
sinlat = RxCalcSin((flat-tlat)/2)
cosplat = RxCalcCos(flat)
coslati = RxCalcCos(tlat)
sinlon = RxCalcSin((flon-tlon)/2)
sinlatp = (sinlat)**2
sinlonp = (sinlon)**2
radians = 2*RxCalcArcSin(RxCalcSqrt(sinlatp + cosplat*coslati*sinlonp),,'R')
distance = (radians*googleradius)~format(,0)/1000 'km'
Return distance /* -- returns distance in thousands of a Km */