]> git.mxchange.org Git - flightgear.git/blob - src/Airports/apt_loader.cxx
Change tower location to an SGGeod. Include taxiways too.
[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 static void 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)
50 {
51     if (!apt_id.empty()) {
52         if (rwy_count > 0) {
53             double lat = rwy_lat_accum / (double)rwy_count;
54             double lon = rwy_lon_accum / (double)rwy_count;
55
56             if (!got_tower) {
57                 // tower height hard coded for now...
58                 const float tower_height = 50.0f;
59                 // make a little off the heading for 1 runway airports...
60                 float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
61                 float fudge_lat = .003f - fudge_lon;
62         
63                 tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, apt_elev + tower_height);
64             }
65                     
66             airports->add(apt_id, SGGeod::fromDegFt(lon, lat, apt_elev), tower, apt_name, false);
67         } else {
68             if ( apt_id.length() ) {
69                 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << apt_id
70                         << ", skipping." );
71             }
72         }
73     }
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, FGRunwayList *runways,
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     string last_apt_type = "";
98     SGGeod last_tower;
99     bool got_tower = false;
100     string line;
101     char tmp[2049];
102     tmp[2048] = 0;
103
104     unsigned int line_id = 0;
105     unsigned int line_num = 0;
106     double rwy_lon_accum = 0.0;
107     double rwy_lat_accum = 0.0;
108     int rwy_count = 0;
109     double last_rwy_heading = 0.0;
110
111     while ( ! in.eof() ) {
112         in.getline(tmp, 2048);
113         line = tmp;
114         line_num++;
115
116         SG_LOG( SG_GENERAL, SG_BULK, "#" << line_num << " '" << line << "'" );
117         if ( !line.size() || isspace(tmp[0]))
118             continue;
119
120         if (line.size() >= 3) {
121             char *p = (char *)memchr(tmp, ' ', 3);
122             if ( p )
123                 *p = 0;
124         }
125
126         line_id = atoi(tmp);
127         if ( tmp[0] == 'I' ) {
128             // First line, indicates IBM (i.e. DOS line endings I
129             // believe.)
130
131             // move past this line and read and discard the next line
132             // which is the version and copyright information
133             in.getline(tmp, 2048);
134             // vector<string> vers_token = simgear::strutils::split( tmp );
135             if ( strlen(tmp) > 4 ) {
136                char *p = (char *)memchr(tmp, ' ', 4);
137                if ( p )
138                   *p = 0;
139             }
140             SG_LOG( SG_GENERAL, SG_INFO, "Data file version = "
141                     << tmp );
142         } else if ( line_id == 1 /* Airport */ ||
143                     line_id == 16 /* Seaplane base */ ||
144                     line_id == 17 /* Heliport */ ) {
145
146             token.clear();
147             token = simgear::strutils::split(line);
148             string id = token[4];
149             double elev = atof( token[1].c_str() );
150             SG_LOG( SG_GENERAL, SG_BULK, "Next airport = " << id << " "
151                     << elev );
152
153             addAirport(airports, last_apt_id, last_apt_name, rwy_count, rwy_lat_accum, rwy_lon_accum,
154                 last_rwy_heading, last_apt_elev, last_tower, got_tower);
155
156             last_apt_id = id;
157             last_apt_elev = elev;
158             last_apt_name = "";
159             got_tower = false;
160
161             // build the name
162             for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
163                 last_apt_name += token[i];
164                 last_apt_name += " ";
165             }
166             last_apt_name += token[token.size() - 1];
167
168             last_apt_info = line;
169             last_apt_type = token[0];
170
171             // clear runway list for start of next airport
172             rwy_lon_accum = 0.0;
173             rwy_lat_accum = 0.0;
174             rwy_count = 0;
175         } else if ( line_id == 10 ) {
176             token.clear();
177             token = simgear::strutils::split(line);
178
179             // runway entry
180             double lat = atof( token[1].c_str() );
181             double lon = atof( token[2].c_str() );
182             rwy_lat_accum += lat;
183             rwy_lon_accum += lon;
184             rwy_count++;
185
186             string rwy_no = token[3];
187
188             double heading = atof( token[4].c_str() );
189             double length = atoi( token[5].c_str() );
190             double width = atoi( token[8].c_str() );
191             
192             last_rwy_heading = heading;
193
194             string rwy_displ_threshold = token[6];
195             vector<string> displ
196                 = simgear::strutils::split( rwy_displ_threshold, "." );
197             double displ_thresh1 = atof( displ[0].c_str() );
198             double displ_thresh2 = atof( displ[1].c_str() );
199
200             string rwy_stopway = token[7];
201             vector<string> stop
202                 = simgear::strutils::split( rwy_stopway, "." );
203             double stopway1 = atof( stop[0].c_str() );
204             double stopway2 = atof( stop[1].c_str() );
205
206             string lighting_flags = token[9];
207             int surface_code = atoi( token[10].c_str() );
208             string shoulder_code = token[11];
209             int marking_code = atoi( token[12].c_str() );
210             double smoothness = atof( token[13].c_str() );
211             bool dist_remaining = (atoi( token[14].c_str() ) == 1 );
212
213             runways->add( last_apt_id, rwy_no, lon, lat, heading, length,
214                           width, displ_thresh1, displ_thresh2,
215                           stopway1, stopway2, lighting_flags, surface_code,
216                           shoulder_code, marking_code, smoothness,
217                           dist_remaining );
218         } else if ( line_id == 18 ) {
219             // beacon entry (ignore)
220         } else if ( line_id == 14 ) {
221             // control tower entry
222             token.clear();
223             token = simgear::strutils::split(line);
224
225             double lat = atof( token[1].c_str() );
226             double lon = atof( token[2].c_str() );
227             double elev = atof( token[3].c_str() );
228             last_tower = SGGeod::fromDegFt(lon, lat, elev);
229             got_tower = true;
230         } else if ( line_id == 19 ) {
231             // windsock entry (ignore)
232         } else if ( line_id == 15 ) {
233             // custom startup locations (ignore)
234         } else if ( line_id >= 50 && line_id <= 56 ) {
235             // frequency entries (ignore)
236         } else if ( line_id == 99 ) {
237             SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
238         } else {
239             SG_LOG( SG_GENERAL, SG_ALERT, 
240                     "Unknown line(#" << line_num << ") in file: " << line );
241             exit(-1);
242         }
243     }
244
245     // add the last airport being processed if any    
246     addAirport(airports, last_apt_id, last_apt_name, rwy_count, rwy_lat_accum, rwy_lon_accum,
247         last_rwy_heading, last_apt_elev, last_tower, got_tower);
248
249
250     //
251     // Load the metar.dat file and update apt db with stations that
252     // have metar data.
253     //
254
255     sg_gzifstream metar_in( metar_file );
256     if ( !metar_in.is_open() ) {
257         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
258     }
259
260     string ident;
261     while ( metar_in ) {
262         metar_in >> ident;
263         if ( ident == "#" || ident == "//" ) {
264             metar_in >> skipeol;
265         } else {
266             const FGAirport* a = airports->search( ident );
267             if ( a ) airports->has_metar( ident );
268         }
269     }
270
271     SG_LOG(SG_GENERAL, SG_INFO, "[FINISHED LOADING]");
272
273     return true;
274 }