]> git.mxchange.org Git - flightgear.git/blob - src/Airports/apt_loader.cxx
Stefan C. Müller :
[flightgear.git] / src / Airports / apt_loader.cxx
1 // apt_loader.cxx -- a front end loader of the apt.dat file.  This loader
2 //                   populates the runway and basic classes.
3 //
4 // Written by Curtis Olson, started August 2000.
5 //
6 // Copyright (C) 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <simgear/compiler.h>
30
31 #include <stdlib.h> // atof(), atoi()
32 #include <string.h> // memchr()
33 #include <ctype.h> // isspace()
34
35 #include <simgear/constants.h>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/misc/sgstream.hxx>
38 #include <simgear/misc/strutils.hxx>
39
40 #include <string>
41
42 #include "simple.hxx"
43 #include "runways.hxx"
44
45 #include "apt_loader.hxx"
46
47 FGAirport* addAirport(FGAirportList *airports, const string& apt_id, const string& apt_name,
48     int rwy_count, double rwy_lat_accum, double rwy_lon_accum, double last_rwy_heading,
49     double apt_elev, SGGeod& tower, bool got_tower, int type)
50 {
51     if (apt_id.empty())
52         return NULL;
53
54     if (!rwy_count) {
55         SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << apt_id
56                 << ", skipping." );
57         return NULL;
58     }
59
60     double lat = rwy_lat_accum / (double)rwy_count;
61     double lon = rwy_lon_accum / (double)rwy_count;
62
63     if (!got_tower) {
64         // tower height hard coded for now...
65         const float tower_height = 50.0f;
66         // make a little off the heading for 1 runway airports...
67         float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
68         float fudge_lat = .003f - fudge_lon;
69         tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, apt_elev + tower_height);
70     }
71
72     return airports->add(apt_id, SGGeod::fromDegFt(lon, lat, apt_elev), tower, apt_name, false,
73             type == 1/*airport*/, type == 16/*seaport*/, type == 17/*heliport*/);
74 }
75
76 // Load the airport data base from the specified aptdb file.  The
77 // metar file is used to mark the airports as having metar available
78 // or not.
79 bool fgAirportDBLoad( FGAirportList *airports, 
80                       const string &aptdb_file, const string &metar_file )
81 {
82     //
83     // Load the apt.dat file
84     //
85
86     sg_gzifstream in( aptdb_file );
87     if ( !in.is_open() ) {
88         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
89         exit(-1);
90     }
91
92     vector<string> token;
93     string last_apt_id = "";
94     double last_apt_elev = 0.0;
95     string last_apt_name = "";
96     string last_apt_info = "";
97     int last_apt_type = 0;
98     SGGeod last_tower;
99     bool got_tower = false;
100     string line;
101     char tmp[2049];
102     tmp[2048] = 0;
103     vector<FGRunway> runways;
104     
105     unsigned int line_id = 0;
106     unsigned int line_num = 0;
107     double rwy_lon_accum = 0.0;
108     double rwy_lat_accum = 0.0;
109     int rwy_count = 0;
110     double last_rwy_heading = 0.0;
111
112     while ( ! in.eof() ) {
113         in.getline(tmp, 2048);
114         line = tmp;
115         line_num++;
116
117         SG_LOG( SG_GENERAL, SG_BULK, "#" << line_num << " '" << line << "'" );
118         if ( !line.size() || isspace(tmp[0]))
119             continue;
120
121         if (line.size() >= 3) {
122             char *p = (char *)memchr(tmp, ' ', 3);
123             if ( p )
124                 *p = 0;
125         }
126
127         line_id = atoi(tmp);
128         if ( tmp[0] == 'I' ) {
129             // First line, indicates IBM (i.e. DOS line endings I
130             // believe.)
131
132             // move past this line and read and discard the next line
133             // which is the version and copyright information
134             in.getline(tmp, 2048);
135             // vector<string> vers_token = simgear::strutils::split( tmp );
136             if ( strlen(tmp) > 4 ) {
137                char *p = (char *)memchr(tmp, ' ', 4);
138                if ( p )
139                   *p = 0;
140             }
141             SG_LOG( SG_GENERAL, SG_INFO, "Data file version = "
142                     << tmp );
143         } else if ( line_id == 1 /* Airport */ ||
144                     line_id == 16 /* Seaplane base */ ||
145                     line_id == 17 /* Heliport */ ) {
146
147             token.clear();
148             token = simgear::strutils::split(line);
149             string id = token[4];
150             double elev = atof( token[1].c_str() );
151             SG_LOG( SG_GENERAL, SG_BULK, "Next airport = " << id << " "
152                     << elev );
153
154             FGAirport* apt = addAirport(airports, last_apt_id, last_apt_name, rwy_count, rwy_lat_accum, rwy_lon_accum,
155                 last_rwy_heading, last_apt_elev, last_tower, got_tower, last_apt_type);
156
157             for (unsigned int r=0; r< runways.size(); ++r) {
158               apt->addRunway(runways[r]);
159             }
160
161             runways.clear();
162             
163             last_apt_id = id;
164             last_apt_elev = elev;
165             last_apt_name = "";
166             got_tower = false;
167
168             // build the name
169             for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
170                 last_apt_name += token[i];
171                 last_apt_name += " ";
172             }
173             last_apt_name += token[token.size() - 1];
174
175             last_apt_info = line;
176             last_apt_type = atoi( token[0].c_str() );
177
178             // clear runway list for start of next airport
179             rwy_lon_accum = 0.0;
180             rwy_lat_accum = 0.0;
181             rwy_count = 0;
182         } else if ( line_id == 10 ) {
183             token.clear();
184             token = simgear::strutils::split(line);
185
186             // runway entry
187             double lat = atof( token[1].c_str() );
188             double lon = atof( token[2].c_str() );
189             rwy_lat_accum += lat;
190             rwy_lon_accum += lon;
191             rwy_count++;
192
193             string rwy_no = token[3];
194
195             double heading = atof( token[4].c_str() );
196             double length = atoi( token[5].c_str() );
197             double width = atoi( token[8].c_str() );
198             
199             last_rwy_heading = heading;
200
201             string rwy_displ_threshold = token[6];
202             vector<string> displ
203                 = simgear::strutils::split( rwy_displ_threshold, "." );
204             double displ_thresh1 = atof( displ[0].c_str() );
205             double displ_thresh2 = atof( displ[1].c_str() );
206
207             string rwy_stopway = token[7];
208             vector<string> stop
209                 = simgear::strutils::split( rwy_stopway, "." );
210             double stopway1 = atof( stop[0].c_str() );
211             double stopway2 = atof( stop[1].c_str() );
212
213             int surface_code = atoi( token[10].c_str() );
214
215             FGRunway rwy(rwy_no, lon, lat, heading, length,
216                           width, displ_thresh1, displ_thresh2,
217                           stopway1, stopway2, surface_code);
218             runways.push_back(rwy);
219         } else if ( line_id == 18 ) {
220             // beacon entry (ignore)
221         } else if ( line_id == 14 ) {
222             // control tower entry
223             token.clear();
224             token = simgear::strutils::split(line);
225
226             double lat = atof( token[1].c_str() );
227             double lon = atof( token[2].c_str() );
228             double elev = atof( token[3].c_str() );
229             last_tower = SGGeod::fromDegFt(lon, lat, elev);
230             got_tower = true;
231         } else if ( line_id == 19 ) {
232             // windsock entry (ignore)
233         } else if ( line_id == 15 ) {
234             // custom startup locations (ignore)
235         } else if ( line_id == 0 ) {
236             // ??
237         } else if ( line_id >= 50 && line_id <= 56 ) {
238             // frequency entries (ignore)
239         } else if ( line_id == 99 ) {
240             SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
241         } else {
242             SG_LOG( SG_GENERAL, SG_ALERT, 
243                     "Unknown line(#" << line_num << ") in file: " << line );
244             exit(-1);
245         }
246     }
247
248     // add the last airport being processed if any
249     addAirport(airports, last_apt_id, last_apt_name, rwy_count, rwy_lat_accum, rwy_lon_accum,
250         last_rwy_heading, last_apt_elev, last_tower, got_tower, 0);
251
252
253     //
254     // Load the metar.dat file and update apt db with stations that
255     // have metar data.
256     //
257
258     sg_gzifstream metar_in( metar_file );
259     if ( !metar_in.is_open() ) {
260         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
261     }
262
263     string ident;
264     while ( metar_in ) {
265         metar_in >> ident;
266         if ( ident == "#" || ident == "//" ) {
267             metar_in >> skipeol;
268         } else {
269             const FGAirport* a = airports->search( ident );
270             if ( a ) airports->has_metar( ident );
271         }
272     }
273
274     SG_LOG(SG_GENERAL, SG_INFO, "[FINISHED LOADING]");
275
276     return true;
277 }