2 * fgcom - VoIP-Client for the FlightGear-Radio-Infrastructure
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.
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.
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,
21 #include <simgear/debug/logstream.hxx>
24 #define EARTHRADIUS 6370.0 //radius of earth
25 #define UF 0.01745329251994329509 //conversion factor pi/180 degree->rad
28 distance (double lat1, double lon1, double lat2, double lon2)
32 d = sin (lat1 * UF) * sin (lat2 * UF);
33 d += cos (lat1 * UF) * cos (lat2 * UF) * cos ((lon2 - lon1) * UF);
35 return (acos (d) * EARTHRADIUS);
39 icao2number (char *icao, float frequency, char *buf)
43 if (strlen (icao) == 0)
44 strcpy (icao, "ZZZZ");
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));
54 icao2atisnumber (char *icao, float frequency, char *buf)
58 if (strlen (icao) == 0)
59 strcpy (icao, "ZZZZ");
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));
70 icaobypos (struct airport *airports, double frequency,
71 double plane_lat, double plane_lon, double range)
74 int frq = (int) (frequency * 1000 + 0.5);
76 if( (frq%10) !=0 && (frq%5) == 0 ){
78 frequency = ceilf(frequency*1000.0)/1000.0;
81 if (frequency >= DEFAULT_LOWER_FRQ_LIMIT
82 && frequency <= DEFAULT_UPPER_FRQ_LIMIT)
84 while (airports->next != NULL)
86 if ( ceilf(airports->frequency*1000.0)/1000.0 == frequency || airports->frequency == frequency)
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" );
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);
101 airports = airports->next;
110 posbyicao (struct airport *airports, char *icao)
117 while (airports->next != NULL)
119 if (!strcmp (airports->icao, icao))
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;
127 airports = airports->next;