]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/poidb.cxx
Checkpoint - ground-net skips the cache
[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     const int LINES_IN_POI_DAT = 769019;
56
57 static PositionedID readPOIFromStream(std::istream& aStream, NavDataCache* cache,
58                                         FGPositioned::Type type = FGPositioned::INVALID)
59 {
60     if (aStream.eof()) {
61         return 0;
62     }
63
64     aStream >> skipws;
65     if (aStream.peek() == '#') {
66         aStream >> skipeol;
67         return 0;
68     }
69     
70   int rawType;
71   aStream >> rawType;
72   double lat, lon;
73   std::string name;
74   aStream >> lat >> lon;
75   getline(aStream, name);
76
77   SGGeod pos(SGGeod::fromDeg(lon, lat));
78   name = simgear::strutils::strip(name);
79
80   // the type can be forced by our caller, but normally we use the value
81   // supplied in the .dat file
82   if (type == FGPositioned::INVALID) {
83     type = mapPOITypeToFGPType(rawType);
84   }
85   if (type == FGPositioned::INVALID) {
86     return 0;
87   }
88
89   return cache->createPOI(type, name, pos);
90 }
91
92 // load and initialize the POI database
93 bool poiDBInit(const SGPath& path)
94 {
95     sg_gzifstream in( path.str() );
96     if ( !in.is_open() ) {
97         SG_LOG( SG_NAVAID, SG_ALERT, "Cannot open file: " << path.str() );
98       return false;
99     }
100
101     unsigned int lineNumber = 0;
102     NavDataCache* cache = NavDataCache::instance();
103     while (!in.eof()) {
104       readPOIFromStream(in, cache);
105
106         ++lineNumber;
107         if ((lineNumber % 100) == 0) {
108             // every 100 lines
109             unsigned int percent = (lineNumber * 100) / LINES_IN_POI_DAT;
110             cache->setRebuildPhaseProgress(NavDataCache::REBUILD_POIS, percent);
111         }
112     } // of stream data loop
113
114     return true;
115 }
116
117 } // of namespace flightgear