]> git.mxchange.org Git - flightgear.git/blob - src/Airports/apt_loader.cxx
92495181a7ea5ec08fcbd277c8f9d5176f0a8899
[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 #include <simgear/structure/exception.hxx>
40
41 #include <string>
42
43 #include "simple.hxx"
44 #include "runways.hxx"
45
46 #include "apt_loader.hxx"
47
48 static FGPositioned::Type fptypeFromRobinType(int aType)
49 {
50   switch (aType) {
51   case 1: return FGPositioned::AIRPORT;
52   case 16: return FGPositioned::SEAPORT;
53   case 17: return FGPositioned::HELIPORT;
54   default:
55     SG_LOG(SG_GENERAL, SG_ALERT, "unsupported type:" << aType);
56     throw sg_range_exception("Unsupported airport type", "fptypeFromRobinType");
57   }
58 }
59
60 FGAirport* addAirport(FGAirportList *airports, const string& apt_id, const string& apt_name,
61     int rwy_count, double rwy_lat_accum, double rwy_lon_accum, double last_rwy_heading,
62     double apt_elev, SGGeod& tower, bool got_tower, int type)
63 {
64     if (apt_id.empty())
65         return NULL;
66
67     if (!rwy_count) {
68         SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << apt_id
69                 << ", skipping." );
70         return NULL;
71     }
72
73     double lat = rwy_lat_accum / (double)rwy_count;
74     double lon = rwy_lon_accum / (double)rwy_count;
75
76     if (!got_tower) {
77         // tower height hard coded for now...
78         const float tower_height = 50.0f;
79         // make a little off the heading for 1 runway airports...
80         float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
81         float fudge_lat = .003f - fudge_lon;
82         tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, apt_elev + tower_height);
83     }
84
85   
86
87     return airports->add(apt_id, SGGeod::fromDegFt(lon, lat, apt_elev), tower, apt_name, false,
88         fptypeFromRobinType(type));
89 }
90
91 // Load the airport data base from the specified aptdb file.  The
92 // metar file is used to mark the airports as having metar available
93 // or not.
94 bool fgAirportDBLoad( FGAirportList *airports, 
95                       const string &aptdb_file, const string &metar_file )
96 {
97     //
98     // Load the apt.dat file
99     //
100
101     sg_gzifstream in( aptdb_file );
102     if ( !in.is_open() ) {
103         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << aptdb_file );
104         exit(-1);
105     }
106
107     vector<string> token;
108     string last_apt_id = "";
109     double last_apt_elev = 0.0;
110     string last_apt_name = "";
111     string last_apt_info = "";
112     int last_apt_type = 0;
113     SGGeod last_tower;
114     bool got_tower = false;
115     string line;
116     char tmp[2049];
117     tmp[2048] = 0;
118     vector<FGRunwayPtr> runways;
119     
120     unsigned int line_id = 0;
121     unsigned int line_num = 0;
122     double rwy_lon_accum = 0.0;
123     double rwy_lat_accum = 0.0;
124     int rwy_count = 0;
125     double last_rwy_heading = 0.0;
126
127     while ( ! in.eof() ) {
128         in.getline(tmp, 2048);
129         line = tmp;
130         line_num++;
131
132         SG_LOG( SG_GENERAL, SG_BULK, "#" << line_num << " '" << line << "'" );
133         if ( !line.size() || isspace(tmp[0]))
134             continue;
135
136         if (line.size() >= 3) {
137             char *p = (char *)memchr(tmp, ' ', 3);
138             if ( p )
139                 *p = 0;
140         }
141
142         line_id = atoi(tmp);
143         if ( tmp[0] == 'I' ) {
144             // First line, indicates IBM (i.e. DOS line endings I
145             // believe.)
146
147             // move past this line and read and discard the next line
148             // which is the version and copyright information
149             in.getline(tmp, 2048);
150             // vector<string> vers_token = simgear::strutils::split( tmp );
151             if ( strlen(tmp) > 4 ) {
152                char *p = (char *)memchr(tmp, ' ', 4);
153                if ( p )
154                   *p = 0;
155             }
156             SG_LOG( SG_GENERAL, SG_INFO, "Data file version = "
157                     << tmp );
158         } else if ( line_id == 1 /* Airport */ ||
159                     line_id == 16 /* Seaplane base */ ||
160                     line_id == 17 /* Heliport */ ) {
161
162             token.clear();
163             token = simgear::strutils::split(line);
164             string id = token[4];
165             double elev = atof( token[1].c_str() );
166             SG_LOG( SG_GENERAL, SG_BULK, "Next airport = " << id << " "
167                     << elev );
168
169             FGAirport* apt = addAirport(airports, last_apt_id, last_apt_name, rwy_count, rwy_lat_accum, rwy_lon_accum,
170                 last_rwy_heading, last_apt_elev, last_tower, got_tower, last_apt_type);
171
172             for (unsigned int r=0; r< runways.size(); ++r) {
173               apt->addRunway(runways[r]);
174             }
175
176             runways.clear();
177             
178             last_apt_id = id;
179             last_apt_elev = elev;
180             last_apt_name = "";
181             got_tower = false;
182
183             // build the name
184             for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
185                 last_apt_name += token[i];
186                 last_apt_name += " ";
187             }
188             last_apt_name += token[token.size() - 1];
189
190             last_apt_info = line;
191             last_apt_type = atoi( token[0].c_str() );
192
193             // clear runway list for start of next airport
194             rwy_lon_accum = 0.0;
195             rwy_lat_accum = 0.0;
196             rwy_count = 0;
197         } else if ( line_id == 10 ) {
198             token.clear();
199             token = simgear::strutils::split(line);
200
201             // runway entry
202             double lat = atof( token[1].c_str() );
203             double lon = atof( token[2].c_str() );
204             rwy_lat_accum += lat;
205             rwy_lon_accum += lon;
206             rwy_count++;
207
208             string rwy_no = token[3];
209
210             double heading = atof( token[4].c_str() );
211             double length = atoi( token[5].c_str() );
212             double width = atoi( token[8].c_str() );
213             
214             last_rwy_heading = heading;
215
216             string rwy_displ_threshold = token[6];
217             vector<string> displ
218                 = simgear::strutils::split( rwy_displ_threshold, "." );
219             double displ_thresh1 = atof( displ[0].c_str() );
220             double displ_thresh2 = atof( displ[1].c_str() );
221
222             string rwy_stopway = token[7];
223             vector<string> stop
224                 = simgear::strutils::split( rwy_stopway, "." );
225             double stopway1 = atof( stop[0].c_str() );
226             double stopway2 = atof( stop[1].c_str() );
227
228             int surface_code = atoi( token[10].c_str() );
229             SGGeod pos(SGGeod::fromDegFt(lon, lat, 0.0));
230             FGRunway* rwy = new FGRunway(NULL, rwy_no, pos, heading, length,
231                           width, displ_thresh1, stopway1, surface_code, false);
232             runways.push_back(rwy);
233             
234             if (rwy_no[0] != 'x') {
235               // runways need a reciprocal, taxiways do not
236               FGRunway* reciprocal = new FGRunway(NULL, FGRunway::reverseIdent(rwy_no), 
237               pos, heading + 180.0, length, width, 
238               displ_thresh2, stopway2, surface_code, true);
239             
240               runways.push_back(reciprocal);
241             }
242         } else if ( line_id == 18 ) {
243             // beacon entry (ignore)
244         } else if ( line_id == 14 ) {
245             // control tower entry
246             token.clear();
247             token = simgear::strutils::split(line);
248
249             double lat = atof( token[1].c_str() );
250             double lon = atof( token[2].c_str() );
251             double elev = atof( token[3].c_str() );
252             last_tower = SGGeod::fromDegFt(lon, lat, elev + last_apt_elev);
253             got_tower = true;
254         } else if ( line_id == 19 ) {
255             // windsock entry (ignore)
256         } else if ( line_id == 15 ) {
257             // custom startup locations (ignore)
258         } else if ( line_id == 0 ) {
259             // ??
260         } else if ( line_id >= 50 && line_id <= 56 ) {
261             // frequency entries (ignore)
262         } else if ( line_id == 99 ) {
263             SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
264         } else {
265             SG_LOG( SG_GENERAL, SG_ALERT, 
266                     "Unknown line(#" << line_num << ") in file: " << line );
267             exit(-1);
268         }
269     }
270
271     // add the last airport being processed if any
272     addAirport(airports, last_apt_id, last_apt_name, rwy_count, rwy_lat_accum, rwy_lon_accum,
273         last_rwy_heading, last_apt_elev, last_tower, got_tower, last_apt_type);
274
275
276     //
277     // Load the metar.dat file and update apt db with stations that
278     // have metar data.
279     //
280
281     sg_gzifstream metar_in( metar_file );
282     if ( !metar_in.is_open() ) {
283         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
284     }
285
286     string ident;
287     while ( metar_in ) {
288         metar_in >> ident;
289         if ( ident == "#" || ident == "//" ) {
290             metar_in >> skipeol;
291         } else {
292             const FGAirport* a = airports->search( ident );
293             if ( a ) const_cast<FGAirport*>(a)->setMetar(true);
294         }
295     }
296
297     SG_LOG(SG_GENERAL, SG_INFO, "[FINISHED LOADING]");
298
299     return true;
300 }