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/airport.hxx>
28 #include <Airports/dynamics.hxx>
29 #include <Airports/groundnetwork.hxx>
33 /*****************************************************************************
34 * Helper function for parsing position string
35 ****************************************************************************/
36 static double processPosition(const string &pos)
45 prefix= subs.substr(0,1);
46 if (prefix == string("S") || (prefix == string("W")))
48 subs = subs.substr(1, subs.length());
49 degree = subs.substr(0, subs.find(" ",0));
50 decimal = subs.substr(subs.find(" ",0), subs.length());
52 value = sign * (atof(degree.c_str()) + atof(decimal.c_str())/60.0);
56 FGGroundNetXMLLoader::FGGroundNetXMLLoader(FGGroundNetwork* net):
61 void FGGroundNetXMLLoader::startXML () {
62 //cout << "FGAirportDynamicsLoader::Start XML" << endl;
65 void FGGroundNetXMLLoader::endXML ()
67 ParkingPushbackIndex::const_iterator it;
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);
76 it->first->setPushBackPoint(j->second);
80 BOOST_FOREACH(FGTaxiNodeRef node, _unreferencedNodes) {
81 SG_LOG(SG_NAVAID, SG_WARN, "unreferenced groundnet node:" << node->ident());
86 void FGGroundNetXMLLoader::startParking(const XMLAttributes &atts)
90 string gateName, gateNumber;
95 int pushBackRoute = 0;
97 for (int i = 0; i < atts.size(); i++)
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());
120 else if (attname == "airlineCodes")
121 airlineCodes = atts.getValue(i);
122 else if (attname == "pushBackRoute") {
123 pushBackRoute = std::atoi(atts.getValue(i));
127 SGGeod pos(SGGeod::fromDeg(processPosition(lon), processPosition(lat)));
129 FGParkingRef parking(new FGParking(index,
130 pos, heading, radius,
131 gateName + gateNumber,
132 type, airlineCodes));
133 if (pushBackRoute > 0) {
134 _parkingPushbacks[parking] = pushBackRoute;
137 _indexMap[index] = parking;
138 _groundNetwork->addParking(parking);
141 void FGGroundNetXMLLoader::startNode(const XMLAttributes &atts)
145 bool onRunway = false;
146 int holdPointType = 0;
148 for (int i = 0; i < atts.size() ; i++)
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") {
163 } else if (attval=="normal") {
165 } else if (attval=="CAT II/III") {
167 } else if (attval=="PushBack") {
175 if (_indexMap.find(index) != _indexMap.end()) {
176 SG_LOG(SG_NAVAID, SG_WARN, "duplicate ground-net index:" << index);
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);
185 void FGGroundNetXMLLoader::startArc(const XMLAttributes &atts)
187 int begin = 0, end = 0;
188 bool isPushBackRoute = false;
190 for (int i = 0; i < atts.size() ; i++)
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;
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);
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");
214 _unreferencedNodes.erase(it->second);
215 fromNode = it->second;
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");
223 _unreferencedNodes.erase(it->second);
228 _groundNetwork->addSegment(fromNode, toNode);
229 if (isPushBackRoute) {
230 // toNode->setIsPushback();
234 void FGGroundNetXMLLoader::startElement (const char * name, const XMLAttributes &atts)
236 if (!strcmp("Parking", name)) {
238 } else if (!strcmp("node", name)) {
240 } else if (!strcmp("arc", name)) {
245 void FGGroundNetXMLLoader::endElement (const char * name)
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);
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))
274 void FGGroundNetXMLLoader::pi (const char * target, const char * data) {
275 //cout << "Processing instruction " << target << ' ' << data << endl;
278 void FGGroundNetXMLLoader::warning (const char * message, int line, int column) {
279 SG_LOG(SG_IO, SG_WARN, "Warning: " << message << " (" << line << ',' << column << ')');
282 void FGGroundNetXMLLoader::error (const char * message, int line, int column) {
283 SG_LOG(SG_IO, SG_ALERT, "Error: " << message << " (" << line << ',' << column << ')');