]> git.mxchange.org Git - flightgear.git/blob - src/Airports/apt_loader.cxx
Fix for refueling and radar calculations.
[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 STL_STRING
41
42 #include "simple.hxx"
43 #include "runways.hxx"
44
45 #include "apt_loader.hxx"
46
47
48 // Load the airport data base from the specified aptdb file.  The
49 // metar file is used to mark the airports as having metar available
50 // or not.
51 bool fgAirportDBLoad( FGAirportList *airports, FGRunwayList *runways,
52                       const string &aptdb_file, const string &metar_file )
53 {
54     //
55     // Load the apt.dat file
56     //
57
58     sg_gzifstream in( aptdb_file );
59     if ( !in.is_open() ) {
60         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
61         exit(-1);
62     }
63
64     vector<string> token;
65     string last_apt_id = "";
66     double last_apt_elev = 0.0;
67     string last_apt_name = "";
68     string last_apt_info = "";
69     string last_apt_type = "";
70     string line;
71     char tmp[2049];
72     tmp[2048] = 0;
73
74     unsigned int line_id = 0;
75     unsigned int line_num = 0;
76     double rwy_lon_accum = 0.0;
77     double rwy_lat_accum = 0.0;
78     int rwy_count = 0;
79
80     while ( ! in.eof() ) {
81         in.getline(tmp, 2048);
82         line = tmp;
83         line_num++;
84
85         SG_LOG( SG_GENERAL, SG_BULK, "#" << line_num << " '" << line << "'" );
86         if ( !line.size() || isspace(tmp[0]))
87             continue;
88
89         if (line.size() >= 3) {
90             char *p = (char *)memchr(tmp, ' ', 3);
91             if ( p )
92                 *p = 0;
93         }
94
95         line_id = atoi(tmp);
96         if ( tmp[0] == 'I' ) {
97             // First line, indicates IBM (i.e. DOS line endings I
98             // believe.)
99
100             // move past this line and read and discard the next line
101             // which is the version and copyright information
102             in.getline(tmp, 2048);
103             // vector<string> vers_token = simgear::strutils::split( tmp );
104             if ( strlen(tmp) > 4 ) {
105                char *p = (char *)memchr(tmp, ' ', 4);
106                if ( p )
107                   *p = 0;
108             }
109             SG_LOG( SG_GENERAL, SG_INFO, "Data file version = "
110                     << tmp );
111         } else if ( line_id == 1 /* Airport */ ||
112                     line_id == 16 /* Seaplane base */ ||
113                     line_id == 17 /* Heliport */ ) {
114
115             token.clear();
116             token = simgear::strutils::split(line);
117             string id = token[4];
118             double elev = atof( token[1].c_str() );
119             SG_LOG( SG_GENERAL, SG_BULK, "Next airport = " << id << " "
120                     << elev );
121
122             if ( !last_apt_id.empty()) {
123                 if ( rwy_count > 0 ) {
124                     double lat = rwy_lat_accum / (double)rwy_count;
125                     double lon = rwy_lon_accum / (double)rwy_count;
126                     airports->add( last_apt_id, lon, lat, last_apt_elev,
127                                    last_apt_name, false );
128                 } else {
129                     if ( !last_apt_id.length() ) {
130                         SG_LOG(SG_GENERAL, SG_ALERT,
131                                "ERROR: No runways for " << last_apt_id
132                                << " skipping." );
133                     }
134                 }
135             }
136
137             last_apt_id = id;
138             last_apt_elev = atof( token[1].c_str() );
139             last_apt_name = "";
140
141             // build the name
142             for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
143                 last_apt_name += token[i];
144                 last_apt_name += " ";
145             }
146             last_apt_name += token[token.size() - 1];
147
148             last_apt_info = line;
149             last_apt_type = token[0];
150
151             // clear runway list for start of next airport
152             rwy_lon_accum = 0.0;
153             rwy_lat_accum = 0.0;
154             rwy_count = 0;
155         } else if ( line_id == 10 ) {
156             token.clear();
157             token = simgear::strutils::split(line);
158
159             // runway entry
160             double lat = atof( token[1].c_str() );
161             double lon = atof( token[2].c_str() );
162             rwy_lat_accum += lat;
163             rwy_lon_accum += lon;
164             rwy_count++;
165
166             string rwy_no = token[3];
167
168             double heading = atof( token[4].c_str() );
169             double length = atoi( token[5].c_str() );
170             double width = atoi( token[8].c_str() );
171
172             string rwy_displ_threshold = token[6];
173             vector<string> displ
174                 = simgear::strutils::split( rwy_displ_threshold, "." );
175             double displ_thresh1 = atof( displ[0].c_str() );
176             double displ_thresh2 = atof( displ[1].c_str() );
177
178             string rwy_stopway = token[7];
179             vector<string> stop
180                 = simgear::strutils::split( rwy_stopway, "." );
181             double stopway1 = atof( stop[0].c_str() );
182             double stopway2 = atof( stop[1].c_str() );
183
184             string lighting_flags = token[9];
185             int surface_code = atoi( token[10].c_str() );
186             string shoulder_code = token[11];
187             int marking_code = atoi( token[12].c_str() );
188             double smoothness = atof( token[13].c_str() );
189             bool dist_remaining = (atoi( token[14].c_str() ) == 1 );
190
191             runways->add( last_apt_id, rwy_no, lon, lat, heading, length,
192                           width, displ_thresh1, displ_thresh2,
193                           stopway1, stopway2, lighting_flags, surface_code,
194                           shoulder_code, marking_code, smoothness,
195                           dist_remaining );
196         } else if ( line_id == 18 ) {
197             // beacon entry (ignore)
198         } else if ( line_id == 14 ) {
199             // control tower entry (ignore)
200         } else if ( line_id == 19 ) {
201             // windsock entry (ignore)
202         } else if ( line_id == 15 ) {
203             // custom startup locations (ignore)
204         } else if ( line_id >= 50 && line_id <= 56 ) {
205             // frequency entries (ignore)
206         } else if ( line_id == 99 ) {
207             SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
208         } else {
209             SG_LOG( SG_GENERAL, SG_ALERT, 
210                     "Unknown line(#" << line_num << ") in file: " << line );
211             exit(-1);
212         }
213     }
214
215     if ( !last_apt_id.empty()) {
216         if ( rwy_count > 0 ) {
217             double lat = rwy_lat_accum / (double)rwy_count;
218             double lon = rwy_lon_accum / (double)rwy_count;
219             airports->add( last_apt_id, lon, lat, last_apt_elev,
220                            last_apt_name, false );
221         } else {
222             if ( !last_apt_id.length() ) {
223                 SG_LOG(SG_GENERAL, SG_ALERT,
224                        "ERROR: No runways for " << last_apt_id
225                        << " skipping." );
226             }
227         }
228     }
229
230     //
231     // Load the metar.dat file and update apt db with stations that
232     // have metar data.
233     //
234
235     sg_gzifstream metar_in( metar_file );
236     if ( !metar_in.is_open() ) {
237         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
238     }
239
240     string ident;
241     while ( metar_in ) {
242         metar_in >> ident;
243         if ( ident == "#" || ident == "//" ) {
244             metar_in >> skipeol;
245         } else {
246             const FGAirport* a = airports->search( ident );
247             if ( a ) airports->has_metar( ident );
248         }
249     }
250
251     SG_LOG(SG_GENERAL, SG_INFO, "[FINISHED LOADING]");
252
253     return true;
254 }