gpxcat.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include <iostream>
  2. #include <cstring>
  3. #include <fstream>
  4. #include <list>
  5. #include <cmath>
  6. #include <limits>
  7. #include <iomanip>
  8. #include "XMLRawParser.h"
  9. #include "Arguments.h"
  10. #include "Algorithm.h"
  11. // ----------------------------------------------------------------------------
  12. namespace gpxtools
  13. {
  14. class GPXCat : public XMLParserHandler
  15. {
  16. public:
  17. // -- Constructor -----------------------------------------------------------
  18. GPXCat() :
  19. _arguments ("gpxcat [OPTION].. [FILE]\nConcat the track segments in a GPX-file.\n", "gpxcat v0.1",
  20. "Concatenate the track segments in a GPX-file and display the resulting GPX-file on standard output."),
  21. _distance (_arguments, true, 'd', "distance", "METRES", "concat only if both segments are less than METRES apart", ""),
  22. _xmlParser(this),
  23. _segmentDistance(-1.0),
  24. _doConcat(false)
  25. {
  26. }
  27. // -- Deconstructor ---------------------------------------------------------
  28. virtual ~GPXCat()
  29. {
  30. }
  31. bool processArguments(int argc, char *argv[])
  32. {
  33. std::vector<std::string> filenames;
  34. if (!_arguments.parse(argc,argv, filenames))
  35. {
  36. return false;
  37. }
  38. else if (!checkArguments(filenames))
  39. {
  40. return false;
  41. }
  42. else if (filenames.empty())
  43. {
  44. return _xmlParser.parse(std::cin);
  45. }
  46. else
  47. {
  48. return parseFile(filenames.front());
  49. }
  50. }
  51. // -- Check arguments ---------------------------------------------------------
  52. bool checkArguments(const std::vector<std::string> &filenames)
  53. {
  54. if (filenames.size() > 1)
  55. {
  56. std::cerr << "Too many input files." << std::endl;
  57. return false;
  58. }
  59. if (!_distance.value().empty())
  60. {
  61. try
  62. {
  63. _segmentDistance = std::stoi(_distance.value());
  64. if (_segmentDistance < 0)
  65. {
  66. std::cerr << "Invalid value for --" << _distance.longOption() << " : " << _distance.value() << std::endl;
  67. return false;
  68. }
  69. }
  70. catch(...)
  71. {
  72. std::cerr << "Invalid value for --" << _distance.longOption() << " : " << _distance.value() << std::endl;
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. // -- Parse a file ----------------------------------------------------------
  79. bool parseFile(const std::string &filename)
  80. {
  81. bool ok =false;
  82. std::ifstream file(filename);
  83. if (file.is_open())
  84. {
  85. ok = _xmlParser.parse(file);
  86. file.close();
  87. }
  88. else
  89. {
  90. std::cerr << "Unable to open: " << filename << std::endl;
  91. }
  92. return ok;
  93. }
  94. private:
  95. void store(const std::string &text)
  96. {
  97. if (_doConcat)
  98. {
  99. _current.append(text);
  100. }
  101. else
  102. {
  103. std::cout << text;
  104. }
  105. }
  106. static bool getDoubleAttribute(const Attributes &atts, const std::string &key, double &value)
  107. {
  108. auto iter = atts.find(key);
  109. if (iter == atts.end()) return false;
  110. if (iter->second.empty()) return false;
  111. try
  112. {
  113. value = std::stod(iter->second);
  114. return true;
  115. }
  116. catch(...)
  117. {
  118. return false;
  119. }
  120. }
  121. public:
  122. // -- Callbacks -------------------------------------------------------------
  123. virtual void startElement(const std::string &path, const std::string &text, const std::string &, const Attributes &attributes)
  124. {
  125. if (path == "/gpx/trk/trkseg/trkpt")
  126. {
  127. double lat = 0.0, lon = 0.0;
  128. if (getDoubleAttribute(attributes, "lat", lat) && getDoubleAttribute(attributes, "lon", lon))
  129. {
  130. if (_doConcat)
  131. {
  132. if (_segmentDistance >= 0.0 && gpx::calcDistance(_lastLat, _lastLon, lat, lon) > _segmentDistance)
  133. {
  134. std::cout << _current;
  135. }
  136. _doConcat = false;
  137. }
  138. _lastLat = lat;
  139. _lastLon = lon;
  140. }
  141. }
  142. store(text);
  143. }
  144. virtual void text(const std::string &, const std::string &text)
  145. {
  146. store(text);
  147. }
  148. virtual void unhandled(const std::string &, const std::string &text)
  149. {
  150. store(text);
  151. }
  152. virtual void endElement(const std::string &path, const std::string &text, const std::string &name)
  153. {
  154. if (path == "/gpx/trk")
  155. {
  156. if (_doConcat)
  157. {
  158. std::cout << _current;
  159. _doConcat = false;
  160. }
  161. }
  162. else if (path == "/gpx/trk/trkseg")
  163. {
  164. _doConcat = true;
  165. _current.clear();
  166. }
  167. store(text);
  168. }
  169. private:
  170. // Members
  171. arg::Arguments _arguments;
  172. arg::Argument _distance;
  173. XMLRawParser _xmlParser;
  174. double _segmentDistance;
  175. std::string _path;
  176. std::string _current;
  177. double _lastLat;
  178. double _lastLon;
  179. bool _doConcat;
  180. };
  181. }
  182. // -- Main program ------------------------------------------------------------
  183. int main(int argc, char *argv[])
  184. {
  185. gpxtools::GPXCat gpxCat;
  186. return gpxCat.processArguments(argc, argv) ? 0 : 1;
  187. }