]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/poidb.cxx
bfc7c81ef4fcff54130b977d5ea87de75b52b008
[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 <simgear/compiler.h>
26 #include <simgear/debug/logstream.hxx>
27 #include <simgear/math/sg_geodesy.hxx>
28 #include <simgear/misc/sg_path.hxx>
29 #include <simgear/structure/exception.hxx>
30 #include <simgear/misc/sgstream.hxx>
31
32 #include <Navaids/NavDataCache.hxx>
33 #include "poidb.hxx"
34
35 using std::string;
36
37 static FGPositioned::Type
38 mapPOITypeToFGPType(int aTy)
39 {
40   switch (aTy) {
41   case 10: return FGPositioned::COUNTRY;
42   case 12: return FGPositioned::CITY;
43   case 13: return FGPositioned::TOWN;
44   case 14: return FGPositioned::VILLAGE;
45   default:
46     throw sg_range_exception("Unknown POI type", "FGNavDataCache::readPOIFromStream");
47   }
48 }
49
50
51
52 namespace flightgear
53 {
54
55 static PositionedID readPOIFromStream(std::istream& aStream,
56                                         FGPositioned::Type type = FGPositioned::INVALID)
57 {
58   NavDataCache* cache = NavDataCache::instance();
59
60   int rawType;
61   aStream >> rawType;
62   if (aStream.eof() || (rawType == '#')) {
63     return 0;
64   }
65
66   double lat, lon;
67   std::string name;
68   aStream >> lat >> lon;
69   getline(aStream, name);
70
71   SGGeod pos(SGGeod::fromDeg(lon, lat));
72   name = simgear::strutils::strip(name);
73
74   // the type can be forced by our caller, but normally we use the value
75   // supplied in the .dat file
76   if (type == FGPositioned::INVALID) {
77     type = mapPOITypeToFGPType(rawType);
78   }
79   if (type == FGPositioned::INVALID) {
80     return 0;
81   }
82
83   PositionedID r = cache->createPOI(type, name, pos);
84   return r;
85 }
86
87 // load and initialize the POI database
88 bool poiDBInit(const SGPath& path)
89 {
90     sg_gzifstream in( path.str() );
91     if ( !in.is_open() ) {
92         SG_LOG( SG_NAVAID, SG_ALERT, "Cannot open file: " << path.str() );
93       return false;
94     }
95
96     in >> skipcomment;
97
98     while (!in.eof()) {
99       readPOIFromStream(in);
100       in >> skipcomment;
101     } // of stream data loop
102
103   return true;
104 }
105
106 } // of namespace flightgear