1 // runways.hxx -- a simple class to manage airport runway info
3 // Written by Curtis Olson, started August 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 // #include <sys/types.h> // for gdbm open flags
29 // #include <sys/stat.h> // for gdbm open flags
31 #include <simgear/compiler.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/misc/sgstream.hxx>
37 #include STL_FUNCTIONAL
38 #include STL_ALGORITHM
40 #include "runways.hxx"
42 SG_USING_NAMESPACE(std);
45 FGRunway::FGRunway() {
49 FGRunway::~FGRunway() {
53 FGRunways::FGRunways( const string& file ) {
54 // open the specified database readonly
55 storage = new c4_Storage( file.c_str(), false );
57 if ( !storage->Strategy().IsValid() ) {
58 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
62 vRunway = new c4_View;
64 storage->GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:F,End2Flags:F]");
70 // search for the specified apt id
71 bool FGRunways::search( const string& aptid, FGRunway* r ) {
72 c4_StringProp pID ("ID");
73 c4_StringProp pRwy ("Rwy");
74 c4_FloatProp pLon ("Longitude");
75 c4_FloatProp pLat ("Latitude");
76 c4_FloatProp pHdg ("Heading");
77 c4_FloatProp pLen ("Length");
78 c4_FloatProp pWid ("Width");
79 c4_StringProp pSurf ("SurfaceFlags");
80 c4_StringProp pEnd1 ("End1Flags");
81 c4_StringProp pEnd2 ("End2Flags");
83 int index = vRunway->Find(pID[aptid.c_str()]);
84 cout << "index = " << index << endl;
90 next_index = index + 1;
92 c4_RowRef row = vRunway->GetAt(index);
94 r->id = (const char *) pID(row);
95 r->rwy_no = (const char *) pRwy(row);
96 r->lon = (double) pLon(row);
97 r->lat = (double) pLat(row);
98 r->heading = (double) pHdg(row);
99 r->length = (double) pLen(row);
100 r->width = (double) pWid(row);
101 r->surface_flags = (const char *) pSurf(row);
102 r->end1_flags = (const char *) pEnd1(row);
103 r->end2_flags = (const char *) pEnd2(row);
109 // search for the specified apt id and runway no
110 bool FGRunways::search( const string& aptid, const string& rwyno, FGRunway* r )
112 c4_StringProp pID ("ID");
113 c4_StringProp pRwy ("Rwy");
114 c4_FloatProp pLon ("Longitude");
115 c4_FloatProp pLat ("Latitude");
116 c4_FloatProp pHdg ("Heading");
117 c4_FloatProp pLen ("Length");
118 c4_FloatProp pWid ("Width");
119 c4_StringProp pSurf ("SurfaceFlags");
120 c4_StringProp pEnd1 ("End1Flags");
121 c4_StringProp pEnd2 ("End2Flags");
123 int index = vRunway->Find(pID[aptid.c_str()]);
124 cout << "index = " << index << endl;
130 c4_RowRef row = vRunway->GetAt(index);
131 string rowid = (const char *) pID(row);
132 string rowrwyno = (const char *) pRwy(row);
133 while ( rowid == aptid ) {
134 next_index = index + 1;
136 if ( rowrwyno == rwyno ) {
137 r->id = (const char *) pID(row);
138 r->rwy_no = (const char *) pRwy(row);
139 r->lon = (double) pLon(row);
140 r->lat = (double) pLat(row);
141 r->heading = (double) pHdg(row);
142 r->length = (double) pLen(row);
143 r->width = (double) pWid(row);
144 r->surface_flags = (const char *) pSurf(row);
145 r->end1_flags = (const char *) pEnd1(row);
146 r->end2_flags = (const char *) pEnd2(row);
152 c4_RowRef row = vRunway->GetAt(index);
153 string rowid = (const char *) pID(row);
154 string rowrwyno = (const char *) pRwy(row);
161 FGRunway FGRunways::search( const string& aptid ) {
168 // search for the specified id
169 bool FGRunways::next( FGRunway* r ) {
170 c4_StringProp pID ("ID");
171 c4_StringProp pRwy ("Rwy");
172 c4_FloatProp pLon ("Longitude");
173 c4_FloatProp pLat ("Latitude");
174 c4_FloatProp pHdg ("Heading");
175 c4_FloatProp pLen ("Length");
176 c4_FloatProp pWid ("Width");
177 c4_StringProp pSurf ("SurfaceFlags");
178 c4_StringProp pEnd1 ("End1Flags");
179 c4_StringProp pEnd2 ("End2Flags");
181 int size = vRunway->GetSize();
182 // cout << "total records = " << size << endl;
184 int index = next_index;
185 // cout << "index = " << index << endl;
187 if ( index == -1 || index >= size ) {
191 next_index = index + 1;
193 c4_RowRef row = vRunway->GetAt(index);
195 r->id = (const char *) pID(row);
196 r->rwy_no = (const char *) pRwy(row);
197 r->lon = (double) pLon(row);
198 r->lat = (double) pLat(row);
199 r->heading = (double) pHdg(row);
200 r->length = (double) pLen(row);
201 r->width = (double) pWid(row);
202 r->surface_flags = (const char *) pSurf(row);
203 r->end1_flags = (const char *) pEnd1(row);
204 r->end2_flags = (const char *) pEnd2(row);
211 FGRunways::~FGRunways( void ) {
217 FGRunwaysUtil::FGRunwaysUtil() {
222 int FGRunwaysUtil::load( const string& file ) {
226 runways.erase( runways.begin(), runways.end() );
228 sg_gzifstream in( file );
229 if ( !in.is_open() ) {
230 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
234 // skip first line of file
236 in.getline( tmp, 256 );
238 // read in each line of the file
244 while ( in.get(c) && c != '\0' ) {
248 } else if ( c == 'R' ) {
251 runways.push_back(r);
261 while ( ! in.eof() ) {
267 } else if ( c == 'R' ) {
270 // cout << apt_id << " " << r.rwy_no << endl;
271 runways.push_back(r);
284 // save the data in gdbm format
285 bool FGRunwaysUtil::dump_mk4( const string& file ) {
287 // open database for writing
288 c4_Storage storage( file.c_str(), true );
290 // need to do something about error handling here!
292 // define the properties
293 c4_StringProp pID ("ID");
294 c4_StringProp pRwy ("Rwy");
295 c4_FloatProp pLon ("Longitude");
296 c4_FloatProp pLat ("Latitude");
297 c4_FloatProp pHdg ("Heading");
298 c4_FloatProp pLen ("Length");
299 c4_FloatProp pWid ("Width");
300 c4_StringProp pSurf ("SurfaceFlags");
301 c4_StringProp pEnd1 ("End1Flags");
302 c4_StringProp pEnd2 ("End2Flags");
304 // Start with an empty view of the proper structure.
306 storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:F,End2Flags:F]");
310 iterator current = runways.begin();
311 const_iterator end = runways.end();
312 while ( current != end ) {
313 // add each runway record
314 pID (row) = current->id.c_str();
315 pRwy (row) = current->rwy_no.c_str();
316 pLon (row) = current->lon;
317 pLat (row) = current->lat;
318 pHdg (row) = current->heading;
319 pLen (row) = current->length;
320 pWid (row) = current->width;
321 pSurf (row) = current->surface_flags.c_str();
322 pEnd1 (row) = current->end1_flags.c_str();
323 pEnd2 (row) = current->end2_flags.c_str();
329 // commit our changes
337 // search for the specified id
339 FGRunwaysUtil::search( const string& id, FGRunway* a ) const
341 const_iterator it = runways.find( FGRunway(id) );
342 if ( it != runways.end() )
355 FGRunwaysUtil::search( const string& id ) const
358 this->search( id, &a );
364 FGRunwaysUtil::~FGRunwaysUtil( void ) {