]> git.mxchange.org Git - flightgear.git/blob - src/Airports/apt_loader.cxx
Use OSG polytope intersector to fill ground cache
[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
230             FGRunway* rwy = new FGRunway(NULL, rwy_no, lon, lat, heading, length,
231                           width, displ_thresh1, stopway1, surface_code, false);
232             
233             FGRunway* reciprocal = new FGRunway(NULL, FGRunway::reverseIdent(rwy_no), 
234                           lon, lat, heading + 180.0, length, width, 
235                           displ_thresh2, stopway2, surface_code, true);
236             runways.push_back(rwy);
237             runways.push_back(reciprocal);
238             
239         } else if ( line_id == 18 ) {
240             // beacon entry (ignore)
241         } else if ( line_id == 14 ) {
242             // control tower entry
243             token.clear();
244             token = simgear::strutils::split(line);
245
246             double lat = atof( token[1].c_str() );
247             double lon = atof( token[2].c_str() );
248             double elev = atof( token[3].c_str() );
249             last_tower = SGGeod::fromDegFt(lon, lat, elev + last_apt_elev);
250             got_tower = true;
251         } else if ( line_id == 19 ) {
252             // windsock entry (ignore)
253         } else if ( line_id == 15 ) {
254             // custom startup locations (ignore)
255         } else if ( line_id == 0 ) {
256             // ??
257         } else if ( line_id >= 50 && line_id <= 56 ) {
258             // frequency entries (ignore)
259         } else if ( line_id == 99 ) {
260             SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
261         } else {
262             SG_LOG( SG_GENERAL, SG_ALERT, 
263                     "Unknown line(#" << line_num << ") in file: " << line );
264             exit(-1);
265         }
266     }
267
268     // add the last airport being processed if any
269     addAirport(airports, last_apt_id, last_apt_name, rwy_count, rwy_lat_accum, rwy_lon_accum,
270         last_rwy_heading, last_apt_elev, last_tower, got_tower, last_apt_type);
271
272
273     //
274     // Load the metar.dat file and update apt db with stations that
275     // have metar data.
276     //
277
278     sg_gzifstream metar_in( metar_file );
279     if ( !metar_in.is_open() ) {
280         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << metar_file );
281     }
282
283     string ident;
284     while ( metar_in ) {
285         metar_in >> ident;
286         if ( ident == "#" || ident == "//" ) {
287             metar_in >> skipeol;
288         } else {
289             const FGAirport* a = airports->search( ident );
290             if ( a ) airports->has_metar( ident );
291         }
292     }
293
294     SG_LOG(SG_GENERAL, SG_INFO, "[FINISHED LOADING]");
295
296     return true;
297 }