]> git.mxchange.org Git - flightgear.git/blob - src/Airports/dynamicloader.cxx
apt.dat parser: various little improvements
[flightgear.git] / src / Airports / dynamicloader.cxx
1 // This program is free software; you can redistribute it and/or
2 // modify it under the terms of the GNU General Public License as
3 // published by the Free Software Foundation; either version 2 of the
4 // License, or (at your option) any later version.
5 //
6 // This program is distributed in the hope that it will be useful, but
7 // WITHOUT ANY WARRANTY; without even the implied warranty of
8 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9 // General Public License for more details.
10 //
11 // You should have received a copy of the GNU General Public License
12 // along with this program; if not, write to the Free Software
13 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
14 //
15
16 #ifdef HAVE_CONFIG_H
17 #  include <config.h>
18 #endif
19
20 #include <cstdlib>
21 #include <cstring> // for strcmp
22 #include <boost/foreach.hpp>
23
24 #include "dynamicloader.hxx"
25
26 #include <Navaids/NavDataCache.hxx>
27 #include <Airports/airport.hxx>
28 #include <Airports/dynamics.hxx>
29 #include <Airports/groundnetwork.hxx>
30
31 using std::string;
32
33 /*****************************************************************************
34  * Helper function for parsing position string
35  ****************************************************************************/
36 static double processPosition(const string &pos)
37 {
38   string prefix;
39   string subs;
40   string degree;
41   string decimal;
42   int sign = 1;
43   double value;
44   subs = pos;
45   prefix= subs.substr(0,1);
46   if (prefix == string("S") || (prefix == string("W")))
47     sign = -1;
48   subs    = subs.substr(1, subs.length());
49   degree  = subs.substr(0, subs.find(" ",0));
50   decimal = subs.substr(subs.find(" ",0), subs.length());
51   
52   value = sign * (atof(degree.c_str()) + atof(decimal.c_str())/60.0);
53   return value;
54 }
55
56 FGGroundNetXMLLoader::FGGroundNetXMLLoader(FGGroundNetwork* net):
57     XMLVisitor(),
58     _groundNetwork(net)
59 {}
60
61 void  FGGroundNetXMLLoader::startXML () {
62   //cout << "FGAirportDynamicsLoader::Start XML" << endl;
63 }
64
65 void  FGGroundNetXMLLoader::endXML ()
66 {
67   ParkingPushbackIndex::const_iterator it;
68   
69   for (it = _parkingPushbacks.begin(); it != _parkingPushbacks.end(); ++it) {
70     NodeIndexMap::const_iterator j = _indexMap.find(it->second);
71     if (j == _indexMap.end()) {
72       SG_LOG(SG_NAVAID, SG_WARN, "bad groundnet, no node for index:" << it->first);
73       continue;
74     }
75
76     it->first->setPushBackPoint(j->second);
77     
78   }
79   
80   BOOST_FOREACH(FGTaxiNodeRef node, _unreferencedNodes) {
81     SG_LOG(SG_NAVAID, SG_WARN, "unreferenced groundnet node:" << node->ident());
82   }
83   
84 }
85
86 void FGGroundNetXMLLoader::startParking(const XMLAttributes &atts)
87 {
88   string type;
89   int index = 0;
90   string gateName, gateNumber;
91   string lat, lon;
92   double heading = 0.0;
93   double radius = 1.0;
94   string airlineCodes;
95   int pushBackRoute = 0;
96   
97   for (int i = 0; i < atts.size(); i++)
98         {
99     string attname(atts.getName(i));
100           if (attname == "index") {
101       index = std::atoi(atts.getValue(i));
102     } else if (attname == "type")
103             type = atts.getValue(i);
104     else if (attname == "name")
105       gateName = atts.getValue(i);
106           else if (attname == "number")
107             gateNumber = atts.getValue(i);
108           else if (attname == "lat")
109       lat = atts.getValue(i);
110           else if (attname == "lon")
111             lon = atts.getValue(i);
112           else if (attname == "heading")
113             heading = std::atof(atts.getValue(i));
114           else if (attname == "radius") {
115             string radiusStr = atts.getValue(i);
116             if (radiusStr.find("M") != string::npos)
117               radiusStr = radiusStr.substr(0, radiusStr.find("M",0));
118             radius = std::atof(radiusStr.c_str());
119           }
120           else if (attname == "airlineCodes")
121       airlineCodes = atts.getValue(i);
122     else if (attname == "pushBackRoute") {
123       pushBackRoute = std::atoi(atts.getValue(i));      
124     }
125         }
126  
127   SGGeod pos(SGGeod::fromDeg(processPosition(lon), processPosition(lat)));
128   
129   FGParkingRef parking(new FGParking(index,
130                                      pos, heading, radius,
131                                      gateName + gateNumber,
132                                      type, airlineCodes));
133   if (pushBackRoute > 0) {
134     _parkingPushbacks[parking] = pushBackRoute;
135   }
136   
137   _indexMap[index] = parking;
138   _groundNetwork->addParking(parking);
139 }
140
141 void FGGroundNetXMLLoader::startNode(const XMLAttributes &atts)
142 {
143   int index = 0;
144   string lat, lon;
145   bool onRunway = false;
146   int holdPointType = 0;
147   
148   for (int i = 0; i < atts.size() ; i++)
149         {
150           string attname(atts.getName(i));
151           if (attname == "index")
152             index = std::atoi(atts.getValue(i));
153           else if (attname == "lat")
154       lat = atts.getValue(i);
155           else if (attname == "lon")
156             lon = atts.getValue(i);
157     else if (attname == "isOnRunway")
158       onRunway = std::atoi(atts.getValue(i)) != 0;
159           else if (attname == "holdPointType") {
160       string attval = atts.getValue(i);
161       if (attval=="none") {
162         holdPointType=0;
163       } else if (attval=="normal") {
164         holdPointType=1;
165       } else if (attval=="CAT II/III") {
166         holdPointType=3;
167       } else if (attval=="PushBack") {
168         holdPointType=3;
169       } else {
170         holdPointType=0;
171       }
172     }
173         }
174   
175   if (_indexMap.find(index) != _indexMap.end()) {
176     SG_LOG(SG_NAVAID, SG_WARN, "duplicate ground-net index:" << index);
177   }
178   
179   SGGeod pos(SGGeod::fromDeg(processPosition(lon), processPosition(lat)));
180   FGTaxiNodeRef node(new FGTaxiNode(index, pos, onRunway, holdPointType));
181   _indexMap[index] = node;
182   _unreferencedNodes.insert(node);
183 }
184
185 void FGGroundNetXMLLoader::startArc(const XMLAttributes &atts)
186 {  
187   int begin = 0, end = 0;
188   bool isPushBackRoute = false;
189   
190   for (int i = 0; i < atts.size() ; i++)
191         {
192           string attname = atts.getName(i);
193           if (attname == "begin")
194             begin = std::atoi(atts.getValue(i));
195           else if (attname == "end")
196             end = std::atoi(atts.getValue(i));
197     else if (attname == "isPushBackRoute")
198             isPushBackRoute = std::atoi(atts.getValue(i)) != 0;
199         }
200   
201   IntPair e(begin, end);
202   if (_arcSet.find(e) != _arcSet.end()) {
203     SG_LOG(SG_NAVAID, SG_WARN, _groundNetwork->airport()->ident() << " ground-net: skipping duplicate edge:" << begin << "->" << end);
204     return;
205   }
206   
207   NodeIndexMap::const_iterator it;
208   FGTaxiNodeRef fromNode, toNode;
209   it = _indexMap.find(begin);
210   if (it == _indexMap.end()) {
211       SG_LOG(SG_NAVAID, SG_WARN, "ground-net: bad edge:" << begin << "->" << end << ", begin index unknown");
212       return;
213   } else {
214       _unreferencedNodes.erase(it->second);
215       fromNode = it->second;
216   }
217
218   it = _indexMap.find(end);
219   if (it == _indexMap.end()) {
220       SG_LOG(SG_NAVAID, SG_WARN, "ground-net: bad edge:" << begin << "->" << end << ", end index unknown");
221       return;
222   } else {
223       _unreferencedNodes.erase(it->second);
224       toNode = it->second;
225   }
226
227   _arcSet.insert(e);  
228   _groundNetwork->addSegment(fromNode, toNode);
229   if (isPushBackRoute) {
230 //    toNode->setIsPushback();
231   }
232 }
233
234 void FGGroundNetXMLLoader::startElement (const char * name, const XMLAttributes &atts)
235 {
236   if (!strcmp("Parking", name)) {
237     startParking(atts);
238   } else if (!strcmp("node", name)) {
239     startNode(atts);
240   } else if (!strcmp("arc", name)) {
241     startArc(atts);
242   }
243 }
244
245 void  FGGroundNetXMLLoader::endElement (const char * name)
246 {
247   int valueAsInt = atoi(value.c_str());
248   if (!strcmp("version", name)) {
249     _groundNetwork->addVersion(valueAsInt);
250   } else if (!strcmp("AWOS", name)) {
251     _groundNetwork->addAwosFreq(valueAsInt);
252   } else if (!strcmp("UNICOM", name)) {
253     _groundNetwork->addUnicomFreq(valueAsInt);
254   } else if (!strcmp("CLEARANCE", name)) {
255     _groundNetwork->addClearanceFreq(valueAsInt);
256   } else if (!strcmp("GROUND", name)) {
257     _groundNetwork->addGroundFreq(valueAsInt);
258   } else if (!strcmp("TOWER", name)) {
259     _groundNetwork->addTowerFreq(valueAsInt);
260   } else if (!strcmp("APPROACH", name)) {
261     _groundNetwork->addApproachFreq(valueAsInt);
262   }
263 }
264
265 void  FGGroundNetXMLLoader::data (const char * s, int len) {
266   string token = string(s,len);
267   //cout << "Character data " << string(s,len) << endl;
268   if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
269     value += token;
270   else
271     value = string("");
272 }
273
274 void  FGGroundNetXMLLoader::pi (const char * target, const char * data) {
275   //cout << "Processing instruction " << target << ' ' << data << endl;
276 }
277
278 void  FGGroundNetXMLLoader::warning (const char * message, int line, int column) {
279   SG_LOG(SG_IO, SG_WARN, "Warning: " << message << " (" << line << ',' << column << ')');
280 }
281
282 void  FGGroundNetXMLLoader::error (const char * message, int line, int column) {
283   SG_LOG(SG_IO, SG_ALERT, "Error: " << message << " (" << line << ',' << column << ')');
284 }