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.
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.
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.
21 #include <cstring> // for strcmp
22 #include <boost/foreach.hpp>
24 #include "dynamicloader.hxx"
26 #include <Navaids/NavDataCache.hxx>
27 #include <Airports/dynamics.hxx>
28 #include <Airports/airport.hxx>
32 /*****************************************************************************
33 * Helper function for parsing position string
34 ****************************************************************************/
35 static double processPosition(const string &pos)
44 prefix= subs.substr(0,1);
45 if (prefix == string("S") || (prefix == string("W")))
47 subs = subs.substr(1, subs.length());
48 degree = subs.substr(0, subs.find(" ",0));
49 decimal = subs.substr(subs.find(" ",0), subs.length());
51 value = sign * (atof(degree.c_str()) + atof(decimal.c_str())/60.0);
55 FGAirportDynamicsXMLLoader::FGAirportDynamicsXMLLoader(FGAirportDynamics* dyn):
56 XMLVisitor(), _dynamics(dyn)
59 void FGAirportDynamicsXMLLoader::startXML () {
60 //cout << "FGAirportDynamicsLoader::Start XML" << endl;
63 void FGAirportDynamicsXMLLoader::endXML ()
65 std::map<PositionedID, int>::iterator it;
66 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
68 for (it = _parkingPushbacks.begin(); it != _parkingPushbacks.end(); ++it) {
69 std::map<int, PositionedID>::iterator j = _idMap.find(it->second);
70 if (j == _idMap.end()) {
71 SG_LOG(SG_NAVAID, SG_WARN, "bad groundnet, no node for index:" << it->first);
75 cache->setParkingPushBackRoute(it->first, j->second);
78 BOOST_FOREACH(PositionedID id, _unreferencedNodes) {
79 SG_LOG(SG_NAVAID, SG_WARN, "unreferenced groundnet node:" << id);
84 void FGAirportDynamicsXMLLoader::startParking(const XMLAttributes &atts)
88 string gateName, gateNumber;
93 int pushBackRoute = 0;
95 for (int i = 0; i < atts.size(); i++)
97 string attname(atts.getName(i));
98 if (attname == "index") {
99 index = std::atoi(atts.getValue(i));
100 } else if (attname == "type")
101 type = atts.getValue(i);
102 else if (attname == "name")
103 gateName = atts.getValue(i);
104 else if (attname == "number")
105 gateNumber = atts.getValue(i);
106 else if (attname == "lat")
107 lat = atts.getValue(i);
108 else if (attname == "lon")
109 lon = atts.getValue(i);
110 else if (attname == "heading")
111 heading = std::atof(atts.getValue(i));
112 else if (attname == "radius") {
113 string radiusStr = atts.getValue(i);
114 if (radiusStr.find("M") != string::npos)
115 radiusStr = radiusStr.substr(0, radiusStr.find("M",0));
116 radius = std::atof(radiusStr.c_str());
118 else if (attname == "airlineCodes")
119 airlineCodes = atts.getValue(i);
120 else if (attname == "pushBackRoute") {
121 pushBackRoute = std::atoi(atts.getValue(i));
125 SGGeod pos(SGGeod::fromDeg(processPosition(lon), processPosition(lat)));
127 PositionedID guid = flightgear::NavDataCache::instance()->insertParking(gateName + gateNumber, pos,
128 _dynamics->parent()->guid(),
129 heading, radius, type, airlineCodes);
130 if (pushBackRoute > 0) {
131 _parkingPushbacks[guid] = pushBackRoute;
134 _idMap[index] = guid;
137 void FGAirportDynamicsXMLLoader::startNode(const XMLAttributes &atts)
141 bool onRunway = false;
142 int holdPointType = 0;
144 for (int i = 0; i < atts.size() ; i++)
146 string attname(atts.getName(i));
147 if (attname == "index")
148 index = std::atoi(atts.getValue(i));
149 else if (attname == "lat")
150 lat = atts.getValue(i);
151 else if (attname == "lon")
152 lon = atts.getValue(i);
153 else if (attname == "isOnRunway")
154 onRunway = std::atoi(atts.getValue(i)) != 0;
155 else if (attname == "holdPointType") {
156 string attval = atts.getValue(i);
157 if (attval=="none") {
159 } else if (attval=="normal") {
161 } else if (attval=="CAT II/III") {
163 } else if (attval=="PushBack") {
171 if (_idMap.find(index) != _idMap.end()) {
172 SG_LOG(SG_NAVAID, SG_WARN, "duplicate ground-net index:" << index);
175 SGGeod pos(SGGeod::fromDeg(processPosition(lon), processPosition(lat)));
176 PositionedID guid = flightgear::NavDataCache::instance()->insertTaxiNode(pos,
177 _dynamics->parent()->guid(), holdPointType, onRunway);
178 _idMap[index] = guid;
179 _unreferencedNodes.insert(guid);
182 void FGAirportDynamicsXMLLoader::startArc(const XMLAttributes &atts)
184 int begin = 0, end = 0;
185 bool isPushBackRoute = false;
187 for (int i = 0; i < atts.size() ; i++)
189 string attname = atts.getName(i);
190 if (attname == "begin")
191 begin = std::atoi(atts.getValue(i));
192 else if (attname == "end")
193 end = std::atoi(atts.getValue(i));
194 else if (attname == "isPushBackRoute")
195 isPushBackRoute = std::atoi(atts.getValue(i)) != 0;
198 IntPair e(begin, end);
199 if (_arcSet.find(e) != _arcSet.end()) {
200 SG_LOG(SG_NAVAID, SG_WARN, _dynamics->parent()->ident() << " ground-net: skipping duplicate edge:" << begin << "->" << end);
205 flightgear::NavDataCache::instance()->insertGroundnetEdge(_dynamics->parent()->guid(),
206 _idMap[begin], _idMap[end]);
208 _unreferencedNodes.erase(_idMap[begin]);
209 _unreferencedNodes.erase(_idMap[end]);
211 if (isPushBackRoute) {
212 flightgear::NavDataCache::instance()->markGroundnetAsPushback(_idMap[end]);
216 void FGAirportDynamicsXMLLoader::startElement (const char * name, const XMLAttributes &atts)
218 if (!strcmp("Parking", name)) {
220 } else if (!strcmp("node", name)) {
222 } else if (!strcmp("arc", name)) {
227 void FGAirportDynamicsXMLLoader::endElement (const char * name)
229 int valueAsInt = atoi(value.c_str());
230 if (!strcmp("version", name)) {
231 _dynamics->getGroundNetwork()->addVersion(valueAsInt);
232 } else if (!strcmp("AWOS", name)) {
233 _dynamics->addAwosFreq(valueAsInt);
234 } else if (!strcmp("UNICOM", name)) {
235 _dynamics->addUnicomFreq(valueAsInt);
236 } else if (!strcmp("CLEARANCE", name)) {
237 _dynamics->addClearanceFreq(valueAsInt);
238 } else if (!strcmp("GROUND", name)) {
239 _dynamics->addGroundFreq(valueAsInt);
240 } else if (!strcmp("TOWER", name)) {
241 _dynamics->addTowerFreq(valueAsInt);
242 } else if (!strcmp("APPROACH", name)) {
243 _dynamics->addApproachFreq(valueAsInt);
247 void FGAirportDynamicsXMLLoader::data (const char * s, int len) {
248 string token = string(s,len);
249 //cout << "Character data " << string(s,len) << endl;
250 if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
256 void FGAirportDynamicsXMLLoader::pi (const char * target, const char * data) {
257 //cout << "Processing instruction " << target << ' ' << data << endl;
260 void FGAirportDynamicsXMLLoader::warning (const char * message, int line, int column) {
261 SG_LOG(SG_IO, SG_WARN, "Warning: " << message << " (" << line << ',' << column << ')');
264 void FGAirportDynamicsXMLLoader::error (const char * message, int line, int column) {
265 SG_LOG(SG_IO, SG_ALERT, "Error: " << message << " (" << line << ',' << column << ')');