File: grid.c

package info (click to toggle)
cwirc 1.8.8-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 640 kB
  • ctags: 585
  • sloc: ansic: 5,399; makefile: 292
file content (95 lines) | stat: -rw-r--r-- 2,410 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* CWirc - X-Chat plugin for sending and receiving raw morse code over IRC
   (c) Pierre-Philippe Coupard - 18/06/2003

   Grid squares distance routines

   This program is distributed under the terms of the GNU General Public License
   See the COPYING file for details
*/
#include <string.h>
#include <ctype.h>
#include <math.h>

#include "grid.h"



/* Definitions */
#define EARTH_RADIUS	6367		/* km, average */



/* Prototypes */
static void gridsquare_to_latlon(char *gs,double *lat,double *lon);



/* Check if a string is a valid 4- or 6-character grid square */
int cwirc_is_grid_square(char *gs)
{
  int i;
  
  /* Test the length of the string */
  i=strlen(gs);
  if(i!=4 && i!=6)
    return(0);

  /* Test its format */
  if(toupper(gs[0])<'A' || toupper(gs[0])>'R' ||
	toupper(gs[1])<'A' || toupper(gs[1])>'R' ||
	!isdigit(gs[2]) ||
	!isdigit(gs[3]) ||
	(i==6 && (toupper(gs[4])<'A' || toupper(gs[4])>'X' ||
		toupper(gs[5])<'A' || toupper(gs[5])>'X')))
    return(0);

  return(1);
}



/* Calculate the great circle path between 2 grid squares' centers in Km */
int cwirc_great_circle_path(char *gs1,char *gs2)
{
  double lat1,lon1;
  double lat2,lon2;
  double a,d;

  /* Convert the grid squares to latitude/longitude */
  gridsquare_to_latlon(gs1,&lat1,&lon1);
  gridsquare_to_latlon(gs2,&lat2,&lon2);

  /* Calculate the great circle path */
  a=pow(sin((lat2-lat1)/2),2) + cos(lat1)*cos(lat2)*pow(sin((lon2-lon1)/2),2);
  d=EARTH_RADIUS * 2*atan2(sqrt(a),sqrt(1-a));

  return(d);
}



/* Calculate the latitude/longitude (in rads) of the center of a grid square */
static void gridsquare_to_latlon(char *gs,double *lat,double *lon)
{
  /* Calculate the base coordinates of the corner of the grid square */
  *lon=-M_PI   +(toupper(gs[0])-'A')*((M_PI/180)*20)+(gs[2]-'0')*((M_PI/180)*2);
  *lat=-M_PI/2 +(toupper(gs[1])-'A')*((M_PI/180)*10)+(gs[3]-'0')*((M_PI/180)*1);

  /* Is it a short (4 character) or long (6 character) grid square ? */
  if(strlen(gs)==4)
  {
    /* Calculate the center of the grid square */
    *lon+=(M_PI/180)*1;
    *lat+=(M_PI/180)*.5;
  }
  else
  {
    /* Increase precision of the coordinates of the corner of the grid square */
    *lon+=(toupper(gs[4])-'A')*(((M_PI/180)/60)*5);
    *lat+=(toupper(gs[5])-'A')*(((M_PI/180)/60)*2.5);

    /* Calculate the center of the grid square */
    *lon+=((M_PI/180)/60)*2.5;
    *lat+=((M_PI/180)/60)*1.25;
  }
}