]> git.mxchange.org Git - flightgear.git/blob - utils/fgcom/position.cxx
Fix standalone terrasync build
[flightgear.git] / utils / fgcom / position.cxx
1 /*
2  * fgcom - VoIP-Client for the FlightGear-Radio-Infrastructure
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17  * MA  02110-1301, USA.
18  *
19  */
20
21 #include <simgear/debug/logstream.hxx>
22 #include "fgcom.hxx"
23
24 #define EARTHRADIUS 6370.0         //radius of earth
25 #define UF 0.01745329251994329509  //conversion factor pi/180 degree->rad
26
27 double
28 distance (double lat1, double lon1, double lat2, double lon2)
29 {
30   double d;
31
32   d = sin (lat1 * UF) * sin (lat2 * UF);
33   d += cos (lat1 * UF) * cos (lat2 * UF) * cos ((lon2 - lon1) * UF);
34
35   return (acos (d) * EARTHRADIUS);
36 }
37
38 void
39 icao2number (char *icao, float frequency, char *buf)
40 {
41   char icao_work[5];
42
43   if (strlen (icao) == 0)
44     strcpy (icao, "ZZZZ");
45
46   sprintf (icao_work, "%4s", icao);
47   sprintf (buf, "%02d%02d%02d%02d%02d%06d", DEFAULT_CODE, icao_work[0],
48            icao_work[1], icao_work[2], icao_work[3],
49            (int) (frequency * 1000 + 0.5));
50   buf[16] = '\0';
51 }
52
53 void
54 icao2atisnumber (char *icao, float frequency, char *buf)
55 {
56   char icao_work[5];
57
58   if (strlen (icao) == 0)
59     strcpy (icao, "ZZZZ");
60
61   sprintf (icao_work, "%4s", icao);
62   sprintf (buf, "%02d%02d%02d%02d%02d%06d", ATIS_CODE, icao_work[0],
63           icao_work[1], icao_work[2], icao_work[3],
64           (int) (frequency * 1000 + 0.5));
65   buf[16] = '\0';
66 }
67
68
69 const char *
70 icaobypos (struct airport *airports, double frequency,
71            double plane_lat, double plane_lon, double range)
72 {
73   double r;
74   int frq = (int) (frequency * 1000 + 0.5);
75
76   if( (frq%10) !=0 && (frq%5) == 0 ){
77     frequency -= 0.005;
78     frequency  = ceilf(frequency*1000.0)/1000.0;
79   }
80
81   if (frequency >= DEFAULT_LOWER_FRQ_LIMIT
82       && frequency <= DEFAULT_UPPER_FRQ_LIMIT)
83     {
84       while (airports->next != NULL)
85         {
86           if ( ceilf(airports->frequency*1000.0)/1000.0 == frequency || airports->frequency == frequency)
87             {
88               r = distance(plane_lat, plane_lon, airports->lat, airports->lon);
89               SG_LOG( SG_GENERAL, SG_DEBUG, "icaobypos() - APT: " << airports->text << " (" << airports->icao << " " << airports->type << ")" );
90               SG_LOG( SG_GENERAL, SG_DEBUG, "icaobypos() - APT lat: " << airports->lat << " APT lon: " << airports->lon );
91               SG_LOG( SG_GENERAL, SG_DEBUG, "icaobypos() - Plane lat: " << plane_lat << " Plane lon: " << plane_lon );
92               SG_LOG( SG_GENERAL, SG_DEBUG, "icaobypos() - Distance to " << airports->icao << ": " << r << " Km" );
93               if (r <= range)
94                 {
95                   SG_LOG( SG_GENERAL, SG_ALERT, "Airport " << airports->text << " (" << airports->icao
96                                                 << " " << airports->type << " at " << frequency << " MHz)"
97                                                 << " is in range (" << r << " km)" );
98                   return (airports->icao);
99                 }
100             }
101           airports = airports->next;
102         }
103       return ("");
104     }
105
106   return ("");
107 }
108
109 struct pos
110 posbyicao (struct airport *airports, char *icao)
111 {
112   struct pos p;
113
114   p.lon = 0.0;
115   p.lat = 0.0;
116
117   while (airports->next != NULL)
118     {
119       if (!strcmp (airports->icao, icao))
120         {
121           SG_LOG( SG_GENERAL, SG_DEBUG, "posbyicao() - APT: " << airports->text << " (" << airports->icao << " " << airports->type << ")" );
122           SG_LOG( SG_GENERAL, SG_DEBUG, "posbyicao() - APT lat: " << airports->lat << " APT lon:" << airports->lon );
123           p.lon = airports->lon;
124           p.lat = airports->lat;
125           return (p);
126         }
127       airports = airports->next;
128     }
129   return p;
130 }