]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/poidb.cxx
When the turn angle is large, don’t fly-by.
[flightgear.git] / src / Navaids / poidb.cxx
1 // poidb.cxx -- points of interest management routines
2 //
3 // Written by Christian Schmitt, March 2013
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 //
19 // $Id$
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include "poidb.hxx"
26
27 #include <simgear/compiler.h>
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/math/sg_geodesy.hxx>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/structure/exception.hxx>
32 #include <simgear/misc/sgstream.hxx>
33
34 #include <Navaids/NavDataCache.hxx>
35
36
37 using std::string;
38
39 static FGPositioned::Type
40 mapPOITypeToFGPType(int aTy)
41 {
42   switch (aTy) {
43   case 10: return FGPositioned::COUNTRY;
44   case 12: return FGPositioned::CITY;
45   case 13: return FGPositioned::TOWN;
46   case 14: return FGPositioned::VILLAGE;
47   default:
48     throw sg_range_exception("Unknown POI type", "FGNavDataCache::readPOIFromStream");
49   }
50 }
51
52 namespace flightgear
53 {
54
55 static PositionedID readPOIFromStream(std::istream& aStream, NavDataCache* cache,
56                                         FGPositioned::Type type = FGPositioned::INVALID)
57 {
58     if (aStream.eof()) {
59         return 0;
60     }
61
62     aStream >> skipws;
63     if (aStream.peek() == '#') {
64         aStream >> skipeol;
65         return 0;
66     }
67     
68   int rawType;
69   aStream >> rawType;
70   double lat, lon;
71   std::string name;
72   aStream >> lat >> lon;
73   getline(aStream, name);
74
75   SGGeod pos(SGGeod::fromDeg(lon, lat));
76   name = simgear::strutils::strip(name);
77
78   // the type can be forced by our caller, but normally we use the value
79   // supplied in the .dat file
80   if (type == FGPositioned::INVALID) {
81     type = mapPOITypeToFGPType(rawType);
82   }
83   if (type == FGPositioned::INVALID) {
84     return 0;
85   }
86
87   return cache->createPOI(type, name, pos);
88 }
89
90 // load and initialize the POI database
91 bool poiDBInit(const SGPath& path)
92 {
93     sg_gzifstream in( path.str() );
94     if ( !in.is_open() ) {
95         SG_LOG( SG_NAVAID, SG_ALERT, "Cannot open file: " << path.str() );
96       return false;
97     }
98
99     NavDataCache* cache = NavDataCache::instance();
100     while (!in.eof()) {
101       readPOIFromStream(in, cache);
102     } // of stream data loop
103
104     return true;
105 }
106
107 } // of namespace flightgear