1 // poidb.cxx -- points of interest management routines
3 // Written by Christian Schmitt, March 2013
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.
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.
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.
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>
34 #include <Navaids/NavDataCache.hxx>
39 static FGPositioned::Type
40 mapPOITypeToFGPType(int 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;
48 throw sg_range_exception("Unknown POI type", "FGNavDataCache::readPOIFromStream");
55 const int LINES_IN_POI_DAT = 769019;
57 static PositionedID readPOIFromStream(std::istream& aStream, NavDataCache* cache,
58 FGPositioned::Type type = FGPositioned::INVALID)
65 if (aStream.peek() == '#') {
74 aStream >> lat >> lon;
75 getline(aStream, name);
77 SGGeod pos(SGGeod::fromDeg(lon, lat));
78 name = simgear::strutils::strip(name);
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);
85 if (type == FGPositioned::INVALID) {
89 return cache->createPOI(type, name, pos);
92 // load and initialize the POI database
93 bool poiDBInit(const SGPath& path)
95 sg_gzifstream in( path.str() );
96 if ( !in.is_open() ) {
97 SG_LOG( SG_NAVAID, SG_ALERT, "Cannot open file: " << path.str() );
101 unsigned int lineNumber = 0;
102 NavDataCache* cache = NavDataCache::instance();
104 readPOIFromStream(in, cache);
107 if ((lineNumber % 100) == 0) {
109 unsigned int percent = (lineNumber * 100) / LINES_IN_POI_DAT;
110 cache->setRebuildPhaseProgress(NavDataCache::REBUILD_POIS, percent);
112 } // of stream data loop
117 } // of namespace flightgear