gpxls.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #include <iostream>
  2. #include <cstring>
  3. #include <fstream>
  4. #include <vector>
  5. #include <limits>
  6. #include <iomanip>
  7. #include "Arguments.h"
  8. #include "XMLParser.h"
  9. #include "Algorithm.h"
  10. namespace gpxtools
  11. {
  12. class GPXLs : public XMLParserHandler
  13. {
  14. public:
  15. // -- Constructor -----------------------------------------------------------
  16. GPXLs() :
  17. _arguments("gpxls [OPTION].. [FILE]..\nList summary or full information from GPX-files.\n", "gpxls v0.1", "List the contents of GPX-files"),
  18. _summary(_arguments, true, 's', "summary", "display summary information"),
  19. _full(_arguments, true, 'f', "full", "display full information"),
  20. _xmlParser(this),
  21. _waypoints(),
  22. _routes(),
  23. _tracks()
  24. {
  25. }
  26. // -- Deconstructor----------------------------------------------------------
  27. virtual ~GPXLs()
  28. {
  29. }
  30. // -- Properties ------------------------------------------------------------
  31. // -- Parse arguments -------------------------------------------------------
  32. bool parseArguments(int argc, char *argv[])
  33. {
  34. std::vector<std::string> filenames;
  35. if (!_arguments.parse(argc,argv, filenames))
  36. {
  37. return false;
  38. }
  39. else if (!checkArguments())
  40. {
  41. return false;
  42. }
  43. else if (filenames.empty())
  44. {
  45. return _xmlParser.parse(std::cin);
  46. }
  47. else
  48. {
  49. for (auto filename = filenames.begin(); filename != filenames.end(); ++filename)
  50. {
  51. if (!parseFile(*filename)) return false;
  52. }
  53. }
  54. return true;
  55. }
  56. // -- Check arguments ---------------------------------------------------------
  57. bool checkArguments()
  58. {
  59. if (_summary.active() && _full.active())
  60. {
  61. _summary.active(false);
  62. }
  63. else if (!_summary.active() && !_full.active())
  64. {
  65. _summary.active(true);
  66. }
  67. return true;
  68. }
  69. // -- Parse a file ----------------------------------------------------------
  70. bool parseFile(const std::string &filename)
  71. {
  72. bool ok =false;
  73. std::ifstream file(filename);
  74. if (file.is_open())
  75. {
  76. ok = _xmlParser.parse(file);
  77. file.close();
  78. }
  79. else
  80. {
  81. std::cerr << "Unable to open: " << filename << std::endl;
  82. }
  83. return ok;
  84. }
  85. // -- Output the result -----------------------------------------------------
  86. void report()
  87. {
  88. std::cout << " Waypoints: " << _waypoints.size() << std::endl;
  89. if (_full.active() && !_waypoints.empty())
  90. {
  91. std::cout << " Waypoint Latitude Longitude Time Elevation" << std::fixed << std::endl;
  92. for (auto iter = _waypoints.begin(); iter != _waypoints.end(); ++iter)
  93. {
  94. std::cout << " ";
  95. report(iter->_name, 10);
  96. report(iter->_lat, 10, 5);
  97. report(iter->_lon, 10, 5);
  98. report(iter->_time, 24);
  99. report(getDouble(iter->_ele), 8, 3);
  100. std::cout << std::endl;
  101. }
  102. }
  103. std::cout << " Routes: " << _routes.size() << std::endl;
  104. for(auto iter = _routes.begin(); iter != _routes.end(); ++iter)
  105. {
  106. std::cout << " Route: '" << iter->_name
  107. << "' Points: " << iter->_points.size()
  108. << " Distance: " << std::fixed << std::setw(8) << std::setprecision(1) << getDistance(iter->_points)
  109. << std::endl;
  110. if (_full.active() && !iter->_points.empty())
  111. {
  112. std::cout << " Latitude Longitude" << std::fixed << std::endl;
  113. for (auto iter2 = iter->_points.begin(); iter2 != iter->_points.end(); ++iter2)
  114. {
  115. std::cout << " ";
  116. report(iter2->_lat, 10, 5);
  117. report(iter2->_lon, 10, 5);
  118. std::cout << std::endl;
  119. }
  120. }
  121. }
  122. std::cout << " Tracks: " << _tracks.size() << std::endl;
  123. for (auto iter = _tracks.begin(); iter != _tracks.end(); ++iter)
  124. {
  125. std::cout << " Track: '" << iter->_name << "' Segments: " << iter->_segments.size() << std::endl;
  126. int i = 1;
  127. for (auto iter2 = iter->_segments.begin(); iter2 != iter->_segments.end(); ++iter2, i++)
  128. {
  129. std::cout << " Segment: " << std::setw(2) << i
  130. << " Points: " << std::setw(4) << iter2->_points.size()
  131. << " Distance: " << std::fixed << std::setw(8) << std::setprecision(1) << getDistance(iter2->_points);
  132. if (!iter2->_minTime.empty() && !iter2->_maxTime.empty())
  133. {
  134. std::cout << " Bounds: " << iter2->_minTime << "..." << iter2->_maxTime;
  135. }
  136. std::cout << std::endl;
  137. if (_full.active() && !iter2->_points.empty())
  138. {
  139. std::cout << " Latitude Longitude Time Elevation Distance" << std::fixed << std::endl;
  140. double prevLat = std::numeric_limits<double>::min();
  141. double prevLon = std::numeric_limits<double>::min();
  142. for (auto iter3 = iter2->_points.begin(); iter3 != iter2->_points.end(); ++iter3)
  143. {
  144. std::cout << " ";
  145. report(iter3->_lat, 10, 5);
  146. report(iter3->_lon, 10, 5);
  147. report(iter3->_time, 24);
  148. report(getDouble(iter3->_ele), 8, 1);
  149. report(getDistance(prevLat, prevLon, iter3->_lat, iter3->_lon), 10, 1);
  150. std::cout << std::endl;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. // -- Callbacks -------------------------------------------------------------
  157. virtual void startElement(const std::string &path, const std::string &, const Attributes &attributes)
  158. {
  159. if (path == "/gpx/wpt")
  160. {
  161. _waypoint.reset();
  162. _waypoint._lat = getDoubleAttribute(attributes, "lat");
  163. _waypoint._lon = getDoubleAttribute(attributes, "lon");
  164. }
  165. else if (path == "/gpx/rte")
  166. {
  167. _route.reset();
  168. }
  169. else if (path == "/gpx/rte/rtept")
  170. {
  171. _routepoint.reset();
  172. _routepoint._lat = getDoubleAttribute(attributes, "lat");
  173. _routepoint._lon = getDoubleAttribute(attributes, "lon");
  174. }
  175. else if (path == "/gpx/trk")
  176. {
  177. _track.reset();
  178. }
  179. else if (path == "/gpx/trk/trkseg")
  180. {
  181. _trackSegment.reset();
  182. }
  183. else if (path == "/gpx/trk/trkseg/trkpt")
  184. {
  185. _trackpoint.reset();
  186. _trackpoint._lat = getDoubleAttribute(attributes, "lat");
  187. _trackpoint._lon = getDoubleAttribute(attributes, "lon");
  188. }
  189. }
  190. virtual void text(const std::string &path, const std::string &text)
  191. {
  192. if (path == "/gpx/wpt/name")
  193. {
  194. _waypoint._name += text;
  195. }
  196. else if (path == "/gpx/wpt/ele")
  197. {
  198. _waypoint._ele += text;
  199. }
  200. else if (path == "/gpx/wpt/time")
  201. {
  202. _waypoint._time += text;
  203. }
  204. else if (path == "/gpx/rte/name")
  205. {
  206. _route._name += text;
  207. }
  208. else if (path == "/gpx/trk/name")
  209. {
  210. _track._name += text;
  211. }
  212. else if (path == "/gpx/trk/trkseg/trkpt/ele")
  213. {
  214. _trackpoint._ele += text;
  215. }
  216. else if (path == "/gpx/trk/trkseg/trkpt/time")
  217. {
  218. _trackpoint._time += text;
  219. }
  220. }
  221. virtual void endElement(const std::string &path, const std::string &)
  222. {
  223. if (path == "/gpx/wpt")
  224. {
  225. XMLParser::trim(_waypoint._name);
  226. XMLParser::trim(_waypoint._ele);
  227. XMLParser::trim(_waypoint._time);
  228. _waypoints.push_back(_waypoint);
  229. }
  230. else if (path == "/gpx/rte/rtept")
  231. {
  232. _route._points.push_back(_routepoint);
  233. }
  234. else if (path == "/gpx/rte")
  235. {
  236. XMLParser::trim(_route._name);
  237. _routes.push_back(_route);
  238. }
  239. else if (path == "/gpx/trk")
  240. {
  241. XMLParser::trim(_track._name);
  242. _tracks.push_back(_track);
  243. }
  244. else if (path == "/gpx/trk/trkseg")
  245. {
  246. _track._segments.push_back(_trackSegment);
  247. }
  248. else if (path == "/gpx/trk/trkseg/trkpt")
  249. {
  250. XMLParser::trim(_trackpoint._ele);
  251. XMLParser::trim(_trackpoint._time);
  252. if (_trackpoint._time.size() >= 18)
  253. {
  254. if (_trackSegment._minTime.empty() || _trackSegment._minTime > _trackpoint._time) _trackSegment._minTime = _trackpoint._time;
  255. if (_trackSegment._maxTime.empty() || _trackSegment._maxTime < _trackpoint._time) _trackSegment._maxTime = _trackpoint._time;
  256. }
  257. _trackSegment._points.push_back(_trackpoint);
  258. }
  259. }
  260. // -- Privates ----------------------------------------------------------------
  261. private:
  262. double getDouble(const std::string &value)
  263. {
  264. try
  265. {
  266. if (value.empty())
  267. {
  268. return std::numeric_limits<double>::min();
  269. }
  270. else
  271. {
  272. return std::stod(value);
  273. }
  274. }
  275. catch (...)
  276. {
  277. return std::numeric_limits<double>::min();
  278. }
  279. }
  280. double getDoubleAttribute(const Attributes &atts, const std::string &key)
  281. {
  282. auto iter = atts.find(key);
  283. return iter != atts.end() ? getDouble(iter->second) : std::numeric_limits<double>::min();
  284. }
  285. double getDistance(double &prevLat, double &prevLon, double lat, double lon)
  286. {
  287. double distance = std::numeric_limits<double>::min();
  288. if ((prevLat != std::numeric_limits<double>::min()) && (prevLon != std::numeric_limits<double>::min()) &&
  289. (lat != std::numeric_limits<double>::min()) && (lon != std::numeric_limits<double>::min()))
  290. {
  291. distance = gpx::calcDistance(prevLat, prevLon, lat, lon);
  292. }
  293. if ((lat != std::numeric_limits<double>::min()) && (lon != std::numeric_limits<double>::min()))
  294. {
  295. prevLat = lat;
  296. prevLon = lon;
  297. }
  298. return distance;
  299. }
  300. struct Trackpoint;
  301. double getDistance(const std::vector<Trackpoint> &points)
  302. {
  303. double distance = 0.0;
  304. double prevLat = std::numeric_limits<double>::min();
  305. double prevLon = std::numeric_limits<double>::min();
  306. for (auto point = points.begin(); point != points.end(); ++point)
  307. {
  308. distance += getDistance(prevLat, prevLon, point->_lat, point->_lon);
  309. }
  310. return distance;
  311. }
  312. struct Routepoint;
  313. double getDistance(const std::vector<Routepoint> &points)
  314. {
  315. double distance = 0.0;
  316. double prevLat = std::numeric_limits<double>::min();
  317. double prevLon = std::numeric_limits<double>::min();
  318. for (auto point = points.begin(); point != points.end(); ++point)
  319. {
  320. distance += getDistance(prevLat, prevLon, point->_lat, point->_lon);
  321. }
  322. return distance;
  323. }
  324. void report(double value, int width, int precision)
  325. {
  326. if (value == std::numeric_limits<double>::min())
  327. {
  328. std::cout << std::setw(width) << ' ';
  329. }
  330. else
  331. {
  332. std::cout << std::setw(width) << std::setprecision(precision) << value;
  333. }
  334. std::cout << ' ';
  335. }
  336. void report(const std::string &value, size_t width)
  337. {
  338. std::cout << std::left;
  339. if (value.size() > width)
  340. {
  341. std::cout << value.substr(0, width-3) << "...";
  342. }
  343. else
  344. {
  345. std::cout << std::setw(width) << value;
  346. }
  347. std::cout << ' ';
  348. std::cout << std::right;
  349. }
  350. // -- Members ---------------------------------------------------------------
  351. arg::Arguments _arguments;
  352. arg::Argument _summary;
  353. arg::Argument _full;
  354. XMLParser _xmlParser;
  355. std::string _path;
  356. struct Waypoint
  357. {
  358. void reset()
  359. {
  360. _name.clear();
  361. _lat = std::numeric_limits<double>::min();
  362. _lon = std::numeric_limits<double>::min();
  363. _ele.clear();
  364. _time.clear();
  365. }
  366. std::string _name;
  367. double _lat;
  368. double _lon;
  369. std::string _ele;
  370. std::string _time;
  371. };
  372. Waypoint _waypoint;
  373. std::vector<Waypoint> _waypoints;
  374. struct Routepoint
  375. {
  376. void reset()
  377. {
  378. _lat = std::numeric_limits<double>::min();
  379. _lon = std::numeric_limits<double>::min();
  380. }
  381. double _lat;
  382. double _lon;
  383. };
  384. Routepoint _routepoint;
  385. struct Route
  386. {
  387. void reset()
  388. {
  389. _name.clear();
  390. _points.clear();
  391. }
  392. std::string _name;
  393. std::vector<Routepoint> _points;
  394. };
  395. Route _route;
  396. std::vector<Route> _routes;
  397. struct Trackpoint
  398. {
  399. void reset()
  400. {
  401. _lat = std::numeric_limits<double>::min();
  402. _lon = std::numeric_limits<double>::min();
  403. _ele.clear();
  404. _time.clear();
  405. }
  406. double _lat;
  407. double _lon;
  408. std::string _ele;
  409. std::string _time;
  410. };
  411. Trackpoint _trackpoint;
  412. struct TrackSegment
  413. {
  414. void reset()
  415. {
  416. _minTime.clear();
  417. _maxTime.clear();
  418. _points.clear();
  419. }
  420. std::string _minTime;
  421. std::string _maxTime;
  422. std::vector<Trackpoint> _points;
  423. };
  424. TrackSegment _trackSegment;
  425. struct Track
  426. {
  427. void reset()
  428. {
  429. _name.clear();
  430. _segments.clear();
  431. }
  432. std::string _name;
  433. std::vector<TrackSegment> _segments;
  434. };
  435. Track _track;
  436. std::vector<Track> _tracks;
  437. };
  438. }
  439. // -- Main program ------------------------------------------------------------
  440. int main(int argc, char *argv[])
  441. {
  442. gpxtools::GPXLs gpxLs;
  443. if (gpxLs.parseArguments(argc, argv))
  444. {
  445. gpxLs.report();
  446. return 0;
  447. }
  448. return 1;
  449. }