]> git.mxchange.org Git - flightgear.git/blob - src/Airports/dynamicloader.cxx
Make FGTaxiNode and FGParking inherit FGPositioned.
[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
22 #include "dynamicloader.hxx"
23
24 /*****************************************************************************
25  * Helper function for parsing position string
26  ****************************************************************************/
27 static double processPosition(const string &pos)
28 {
29   string prefix;
30   string subs;
31   string degree;
32   string decimal;
33   int sign = 1;
34   double value;
35   subs = pos;
36   prefix= subs.substr(0,1);
37   if (prefix == string("S") || (prefix == string("W")))
38     sign = -1;
39   subs    = subs.substr(1, subs.length());
40   degree  = subs.substr(0, subs.find(" ",0));
41   decimal = subs.substr(subs.find(" ",0), subs.length());
42   
43   value = sign * (atof(degree.c_str()) + atof(decimal.c_str())/60.0);
44   return value;
45 }
46
47 FGAirportDynamicsXMLLoader::FGAirportDynamicsXMLLoader(FGAirportDynamics* dyn):
48     XMLVisitor(), _dynamics(dyn)
49 {}
50
51 void  FGAirportDynamicsXMLLoader::startXML () {
52   //cout << "FGAirportDynamicsLoader::Start XML" << endl;
53 }
54
55 void  FGAirportDynamicsXMLLoader::endXML () {
56   //cout << "End XML" << endl;
57 }
58
59 void FGAirportDynamicsXMLLoader::startParking(const XMLAttributes &atts)
60 {
61   string type;
62   int index;
63   string gateName, gateNumber;
64   string lat, lon;
65   double heading = 0.0;
66   double radius = 1.0;
67   string airlineCodes;
68   int pushBackRoute = 0;
69   
70   for (int i = 0; i < atts.size(); i++)
71         {
72     string attname(atts.getName(i));
73           if (attname == "index") {
74       index = std::atoi(atts.getValue(i));
75     } else if (attname == "type")
76             type = atts.getValue(i);
77     else if (attname == "name")
78       gateName = atts.getValue(i);
79           else if (attname == "number")
80             gateNumber = atts.getValue(i);
81           else if (attname == "lat")
82       lat = atts.getValue(i);
83           else if (attname == "lon")
84             lon = atts.getValue(i);
85           else if (attname == "heading")
86             heading = std::atof(atts.getValue(i));
87           else if (attname == "radius") {
88             string radiusStr = atts.getValue(i);
89             if (radiusStr.find("M") != string::npos)
90               radiusStr = radiusStr.substr(0, radiusStr.find("M",0));
91             radius = std::atof(radiusStr.c_str());
92           }
93           else if (attname == "airlineCodes")
94       airlineCodes = atts.getValue(i);
95     else if (attname == "pushBackRoute") {
96       pushBackRoute = std::atoi(atts.getValue(i));      
97     }
98         }
99  
100   SGGeod pos(SGGeod::fromDeg(processPosition(lon), processPosition(lat)));
101   
102   FGParking* pk = new FGParking(0, index, pos, heading, radius,
103                                 gateName + gateNumber, type, airlineCodes);
104   pk->setPushBackPoint(pushBackRoute);
105   _dynamics->addParking(pk);
106 }
107
108 void FGAirportDynamicsXMLLoader::startNode(const XMLAttributes &atts)
109 {
110   int index;
111   string lat, lon;
112   bool onRunway;
113   int holdPointType;
114   
115   for (int i = 0; i < atts.size() ; i++)
116         {
117           string attname(atts.getName(i));
118           if (attname == "index")
119             index = std::atoi(atts.getValue(i));
120           else if (attname == "lat")
121       lat = atts.getValue(i);
122           else if (attname == "lon")
123             lon = atts.getValue(i);
124     else if (attname == "isOnRunway")
125       onRunway = (bool) std::atoi(atts.getValue(i));
126           else if (attname == "holdPointType") {
127       string attval = atts.getValue(i);
128       if (attval=="none") {
129         holdPointType=0;
130       } else if (attval=="normal") {
131         holdPointType=1;
132       } else if (attval=="CAT II/III") {
133         holdPointType=3;
134       } else if (attval=="PushBack") {
135         holdPointType=3;
136       } else {
137         holdPointType=0;
138       }
139     }
140         }
141   
142   SGGeod pos(SGGeod::fromDeg(processPosition(lon), processPosition(lat)));
143   FGTaxiNode* taxiNode = new FGTaxiNode(0, index, pos, onRunway, holdPointType);
144   _dynamics->getGroundNetwork()->addNode(taxiNode);
145 }
146
147 void FGAirportDynamicsXMLLoader::startArc(const XMLAttributes &atts)
148 {  
149   int begin, end;
150   bool isPushBackRoute = false;
151   
152   for (int i = 0; i < atts.size() ; i++)
153         {
154           string attname = atts.getName(i);
155           if (attname == "begin")
156             begin = std::atoi(atts.getValue(i));
157           else if (attname == "end")
158             end = std::atoi(atts.getValue(i));
159     else if (attname == "isPushBackRoute")
160             isPushBackRoute = (bool) std::atoi(atts.getValue(i));
161         }
162   
163   _dynamics->getGroundNetwork()->addSegment(new FGTaxiSegment(begin, end, isPushBackRoute));
164 }
165
166 void FGAirportDynamicsXMLLoader::startElement (const char * name, const XMLAttributes &atts)
167 {
168   if (!strcmp("Parking", name)) {
169     startParking(atts);
170   } else if (!strcmp("node", name)) {
171     startNode(atts);
172   } else if (!strcmp("arc", name)) {
173     startArc(atts);
174   }
175 }
176
177 void  FGAirportDynamicsXMLLoader::endElement (const char * name)
178 {
179   int valueAsInt = atoi(value.c_str());
180   if (!strcmp("version", name)) {
181     _dynamics->getGroundNetwork()->addVersion(valueAsInt);
182   } else if (!strcmp("AWOS", name)) {
183     _dynamics->addAwosFreq(valueAsInt);
184   } else if (!strcmp("UNICOM", name)) {
185     _dynamics->addUnicomFreq(valueAsInt);
186   } else if (!strcmp("CLEARANCE", name)) {
187     _dynamics->addClearanceFreq(valueAsInt);
188   } else if (!strcmp("GROUND", name)) {
189     _dynamics->addGroundFreq(valueAsInt);
190   } else if (!strcmp("TOWER", name)) {
191     _dynamics->addTowerFreq(valueAsInt);
192   } else if (!strcmp("APPROACH", name)) {
193     _dynamics->addApproachFreq(valueAsInt);
194   }
195 }
196
197 void  FGAirportDynamicsXMLLoader::data (const char * s, int len) {
198   string token = string(s,len);
199   //cout << "Character data " << string(s,len) << endl;
200   if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
201     value += token;
202   else
203     value = string("");
204 }
205
206 void  FGAirportDynamicsXMLLoader::pi (const char * target, const char * data) {
207   //cout << "Processing instruction " << target << ' ' << data << endl;
208 }
209
210 void  FGAirportDynamicsXMLLoader::warning (const char * message, int line, int column) {
211   SG_LOG(SG_IO, SG_WARN, "Warning: " << message << " (" << line << ',' << column << ')');
212 }
213
214 void  FGAirportDynamicsXMLLoader::error (const char * message, int line, int column) {
215   SG_LOG(SG_IO, SG_ALERT, "Error: " << message << " (" << line << ',' << column << ')');
216 }