[RTTY] GridSquare, Calculating Distance

Ekki Plicht ep at plicht.de
Mon Mar 8 21:45:09 EST 2004


Here is a small function which converts lat/lon into distance. It assumes a 
constant radius of the earth (a perfect sphere) which is of course only the 
simplest approximation. But usually that's sufficient for distances up to say 
2000km (1200mi). This formula is derived from the suggestions of the German 
DARC, and can be used to calculate "official" results in a contest.

Calculating the true distance on a non-perfect sphere, or more precisely on 
the squashed sphere of our actual earth, has become a subject of intense 
study and has led to a bewildering wealth of calculations. Just enter "geoid" 
and "distance calculation" into your favorite web searchengine.

The function returns a result in km, by changing the constant parameter you 
can turn it into US statute miles or whatever.

These functions are in Pascal, but should be clear enough to be used as a 
template in any language.

73,
Ekki, DF4OR


function QthDistance(qth1, qth2: pChar): real;
// qth1, qth2 are pointers to strings containing locators in old 5-digit 
// format or new maidenhead 6-digit format. Maidenhead 8-digit is not
// supported. Result is a real number, the distance in kilometers.
// DMS means degrees, minutes, seconds


var
   x1,y1,x2,y2,xt, yt: real;
begin
//Simple validity check of parms by len
     if (STrLen(qth1)<5) or (StrLen(qth2)<5) or
        (StrLen(qth1)>6) or (StrLen(qth2)>6)
        then begin QTHDistance:=0; exit; end;

//convert locators to lat/lon
//lat/lon are not in DMS but D.ddddd format
     if StrLen(qth1)=5 then
        QthOldToGeo(qth1, x1, y1)
     else
        QthNewToGeo(qth1, x1, y1);

     if StrLen(qth2)=5 then
        QthOldToGeo(qth2, x2, y2)
     else
        QthNewToGeo(qth2, x2, y2);

//the formula itself, broken into several lines for email...
//the function Dgr2Rad converts  geographical data (DMS) into radians,
//which are required by the trigonometric functions.

     xt:=x2-x1; yt:=y2-y1;
     QthDistance:= sqrt( 
		yt*yt + xt*xt 
		* cos(Dgr2Rad(y1)) 
		* cos(Dgr2Rad(y2))) *111.3;

end;




function QthNewToGeo(qth: pChar; VAR x,y: real): boolean;
var
   ok: boolean;
begin
 ok := TRUE;
 x:=0; y:=0;
 if StrLen(qth)=6 then
  begin
  if Isletter(@qth[0]) 
   then x:= (ord(qth[0]) - ord('A')) * 20 - 180 else x:=0;
  if isletter(@qth[1]) 
   then y:= (ord(qth[1]) - ord('A')) * 10 - 90 else y:=0;
  if isfigure(@qth[2]) 
   then x:= x + (ord(qth[2]) - ord('0')) * 2 else x:=0;
  if isfigure(@qth[3]) 
   then y:= y + (ord(qth[3]) - ord('0')) else y:=0;
  if isletter(@qth[4]) 
    then x:= ((x * 24) + (ord(qth[4]) - ord('A')) * 2 + 1) * 0.0416666666 
    else x:=0;
  if isletter(@qth[5]) 
    then y:= ((y * 48) + (ord(qth[5]) - ord('A')) * 2 + 1) * 0.0208333333 
    else y:=0;
  end
  else
  ok := FALSE;

  QthNewToGeo := ok;

end;




Am Mo März 8 2004 20:47 schrieb Dave W7DPW:
> I would like to find the formula for calculating the distance between
> two gridSquares.
>
> Can some one point me in the right direction ?
>
> Dave W7DPW
>
>
> _______________________________________________
> RTTY mailing list
> RTTY at contesting.com
> http://lists.contesting.com/mailman/listinfo/rtty



More information about the RTTY mailing list