]> git.mxchange.org Git - flightgear.git/blobdiff - src/Airports/apt_loader.cxx
This should apply, and everything should build cleanly, in isolation from the
[flightgear.git] / src / Airports / apt_loader.cxx
index 7ae69b7a6e0e5a1ee93ecb91fdc7a6d72a634f7f..896be4ea1b4b737cad102182d3dc698e1f89c688 100644 (file)
@@ -17,7 +17,7 @@
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
@@ -29,7 +29,7 @@
 #include <simgear/compiler.h>
 
 #include <stdlib.h> // atof(), atoi()
-#include <strings.h> // memchr()
+#include <string.h> // memchr()
 #include <ctype.h> // isspace()
 
 #include <simgear/constants.h>
 #include <simgear/misc/sgstream.hxx>
 #include <simgear/misc/strutils.hxx>
 
-#include STL_STRING
+#include <string>
 
 #include "simple.hxx"
 #include "runways.hxx"
 
 #include "apt_loader.hxx"
 
+static void addAirport(FGAirportList *airports, const string& apt_id, const string& apt_name,
+    int rwy_count, double rwy_lat_accum, double rwy_lon_accum, double last_rwy_heading,
+    double apt_elev, SGGeod& tower, bool got_tower, int type)
+{
+    if (apt_id.empty())
+        return;
+
+    if (!rwy_count) {
+        SG_LOG(SG_GENERAL, SG_ALERT, "ERROR: No runways for " << apt_id
+                << ", skipping." );
+        return;
+    }
+
+    double lat = rwy_lat_accum / (double)rwy_count;
+    double lon = rwy_lon_accum / (double)rwy_count;
+
+    if (!got_tower) {
+        // tower height hard coded for now...
+        const float tower_height = 50.0f;
+        // make a little off the heading for 1 runway airports...
+        float fudge_lon = fabs(sin(last_rwy_heading * SGD_DEGREES_TO_RADIANS)) * .003f;
+        float fudge_lat = .003f - fudge_lon;
+        tower = SGGeod::fromDegFt(lon + fudge_lon, lat + fudge_lat, apt_elev + tower_height);
+    }
+
+    airports->add(apt_id, SGGeod::fromDegFt(lon, lat, apt_elev), tower, apt_name, false,
+            type == 1/*airport*/, type == 16/*seaport*/, type == 17/*heliport*/);
+}
 
 // Load the airport data base from the specified aptdb file.  The
 // metar file is used to mark the airports as having metar available
@@ -66,34 +94,37 @@ bool fgAirportDBLoad( FGAirportList *airports, FGRunwayList *runways,
     double last_apt_elev = 0.0;
     string last_apt_name = "";
     string last_apt_info = "";
-    string last_apt_type = "";
+    int last_apt_type = 0;
+    SGGeod last_tower;
+    bool got_tower = false;
     string line;
-    char tmp[2048];
-
-    char sp_pos = 0, lid = 0;
+    char tmp[2049];
+    tmp[2048] = 0;
 
+    unsigned int line_id = 0;
+    unsigned int line_num = 0;
     double rwy_lon_accum = 0.0;
     double rwy_lat_accum = 0.0;
     int rwy_count = 0;
+    double last_rwy_heading = 0.0;
 
     while ( ! in.eof() ) {
        in.getline(tmp, 2048);
        line = tmp;
+        line_num++;
 
-        SG_LOG( SG_GENERAL, SG_BULK, "-> '" << line << "'" );
+        SG_LOG( SG_GENERAL, SG_BULK, "#" << line_num << " '" << line << "'" );
         if ( !line.size() || isspace(tmp[0]))
             continue;
 
-        if (line.size() > 3) {
+        if (line.size() >= 3) {
             char *p = (char *)memchr(tmp, ' ', 3);
             if ( p )
                 *p = 0;
         }
 
-        lid = atoi(tmp);
-        if ( tmp[0] == '#' || (tmp[0] == '/' && tmp[1] == '/') ) {
-           // comment, skip
-        } else if ( tmp[0] == 'I' ) {
+        line_id = atoi(tmp);
+        if ( tmp[0] == 'I' ) {
             // First line, indicates IBM (i.e. DOS line endings I
             // believe.)
 
@@ -108,9 +139,9 @@ bool fgAirportDBLoad( FGAirportList *airports, FGRunwayList *runways,
             }
             SG_LOG( SG_GENERAL, SG_INFO, "Data file version = "
                     << tmp );
-       } else if ( lid == 1 /* Airport */ ||
-                    lid == 16 /* Seaplane base */ ||
-                    lid == 17 /* Heliport */ ) {
+       } else if ( line_id == 1 /* Airport */ ||
+                    line_id == 16 /* Seaplane base */ ||
+                    line_id == 17 /* Heliport */ ) {
 
             token.clear();
             token = simgear::strutils::split(line);
@@ -119,24 +150,13 @@ bool fgAirportDBLoad( FGAirportList *airports, FGRunwayList *runways,
             SG_LOG( SG_GENERAL, SG_BULK, "Next airport = " << id << " "
                     << elev );
 
-            if ( !last_apt_id.empty()) {
-                if ( rwy_count > 0 ) {
-                    double lat = rwy_lat_accum / (double)rwy_count;
-                    double lon = rwy_lon_accum / (double)rwy_count;
-                    airports->add( last_apt_id, lon, lat, last_apt_elev,
-                                   last_apt_name, false );
-               } else {
-                   if ( !last_apt_id.length() ) {
-                       SG_LOG(SG_GENERAL, SG_ALERT,
-                               "ERROR: No runways for " << last_apt_id
-                               << " skipping." );
-                   }
-               }
-            }
+            addAirport(airports, last_apt_id, last_apt_name, rwy_count, rwy_lat_accum, rwy_lon_accum,
+                last_rwy_heading, last_apt_elev, last_tower, got_tower, last_apt_type);
 
             last_apt_id = id;
-            last_apt_elev = atof( token[1].c_str() );
+            last_apt_elev = elev;
             last_apt_name = "";
+            got_tower = false;
 
             // build the name
             for ( unsigned int i = 5; i < token.size() - 1; ++i ) {
@@ -146,13 +166,13 @@ bool fgAirportDBLoad( FGAirportList *airports, FGRunwayList *runways,
             last_apt_name += token[token.size() - 1];
 
             last_apt_info = line;
-            last_apt_type = token[0];
+            last_apt_type = atoi( token[0].c_str() );
 
             // clear runway list for start of next airport
             rwy_lon_accum = 0.0;
             rwy_lat_accum = 0.0;
             rwy_count = 0;
-        } else if ( lid == 10 ) {
+        } else if ( line_id == 10 ) {
             token.clear();
             token = simgear::strutils::split(line);
 
@@ -168,6 +188,8 @@ bool fgAirportDBLoad( FGAirportList *airports, FGRunwayList *runways,
             double heading = atof( token[4].c_str() );
             double length = atoi( token[5].c_str() );
             double width = atoi( token[8].c_str() );
+            
+            last_rwy_heading = heading;
 
             string rwy_displ_threshold = token[6];
             vector<string> displ
@@ -193,39 +215,39 @@ bool fgAirportDBLoad( FGAirportList *airports, FGRunwayList *runways,
                           stopway1, stopway2, lighting_flags, surface_code,
                           shoulder_code, marking_code, smoothness,
                           dist_remaining );
-        } else if ( lid == 18 ) {
+        } else if ( line_id == 18 ) {
             // beacon entry (ignore)
-        } else if ( lid == 14 ) {
-            // control tower entry (ignore)
-        } else if ( lid == 19 ) {
+        } else if ( line_id == 14 ) {
+            // control tower entry
+            token.clear();
+            token = simgear::strutils::split(line);
+
+            double lat = atof( token[1].c_str() );
+            double lon = atof( token[2].c_str() );
+            double elev = atof( token[3].c_str() );
+            last_tower = SGGeod::fromDegFt(lon, lat, elev);
+            got_tower = true;
+        } else if ( line_id == 19 ) {
             // windsock entry (ignore)
-        } else if ( lid == 15 ) {
+        } else if ( line_id == 15 ) {
             // custom startup locations (ignore)
-        } else if ( lid >= 50 && lid <= 56 ) {
+        } else if ( line_id == 0 ) {
+            // ??
+        } else if ( line_id >= 50 && line_id <= 56 ) {
             // frequency entries (ignore)
-        } else if ( lid == 99 ) {
+        } else if ( line_id == 99 ) {
             SG_LOG( SG_GENERAL, SG_DEBUG, "End of file reached" );
         } else {
             SG_LOG( SG_GENERAL, SG_ALERT, 
-                    "Unknown line in file: " << line );
+                    "Unknown line(#" << line_num << ") in file: " << line );
             exit(-1);
         }
     }
 
-    if ( !last_apt_id.empty()) {
-        if ( rwy_count > 0 ) {
-            double lat = rwy_lat_accum / (double)rwy_count;
-            double lon = rwy_lon_accum / (double)rwy_count;
-            airports->add( last_apt_id, lon, lat, last_apt_elev,
-                           last_apt_name, false );
-        } else {
-            if ( !last_apt_id.length() ) {
-                SG_LOG(SG_GENERAL, SG_ALERT,
-                       "ERROR: No runways for " << last_apt_id
-                       << " skipping." );
-            }
-        }
-    }
+    // add the last airport being processed if any
+    addAirport(airports, last_apt_id, last_apt_name, rwy_count, rwy_lat_accum, rwy_lon_accum,
+        last_rwy_heading, last_apt_elev, last_tower, got_tower, 0);
+
 
     //
     // Load the metar.dat file and update apt db with stations that
@@ -243,10 +265,8 @@ bool fgAirportDBLoad( FGAirportList *airports, FGRunwayList *runways,
         if ( ident == "#" || ident == "//" ) {
             metar_in >> skipeol;
         } else {
-            const FGAirport &a = airports->search( ident );
-            if ( a.getId() == ident ) {
-                airports->has_metar( ident );
-            }
+            const FGAirport* a = airports->search( ident );
+            if ( a ) airports->has_metar( ident );
         }
     }