gpxjson.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #include <iostream>
  2. #include <cstring>
  3. #include <fstream>
  4. #include <vector>
  5. #include <cmath>
  6. #include <limits>
  7. #include <iomanip>
  8. #include "XMLParser.h"
  9. #include "Arguments.h"
  10. #include "Algorithm.h"
  11. // ----------------------------------------------------------------------------
  12. namespace gpxtools
  13. {
  14. class GPXJson : public XMLParserHandler
  15. {
  16. public:
  17. // -- Constructor -----------------------------------------------------------
  18. GPXJson() :
  19. _arguments("gpxjson [OPTION].. [FILE]\nDisplay the GeoJson data from a GPX-file on standard output.\n", "gpxjson v0.1", "Display the waypoints, routes and/or tracks in GeoJson format from a GPX-file on standard output."),
  20. _waypoints(_arguments, true, 'w', "waypoints", "convert the waypoints"),
  21. _tracks (_arguments, true, 't', "tracks", "convert the tracks"),
  22. _routes (_arguments, true, 'r', "routes", "convert the routes"),
  23. _mode (_arguments, true, 'm', "mode", "compact|normal", "set the output mode (def:normal)", "normal"),
  24. _number (_arguments, true, 'p', "points", "NUMBER", "set the number of points per line in normal mode (def:4)", "4"),
  25. _xmlParser(this)
  26. {
  27. }
  28. // -- Deconstructor ---------------------------------------------------------
  29. virtual ~GPXJson()
  30. {
  31. }
  32. // -- Properties ------------------------------------------------------------
  33. // -- Parse arguments -------------------------------------------------------
  34. bool parseArguments(int argc, char *argv[])
  35. {
  36. std::vector<std::string> filenames;
  37. if (!_arguments.parse(argc,argv, filenames))
  38. {
  39. return false;
  40. }
  41. else if (!checkArguments(filenames))
  42. {
  43. return false;
  44. }
  45. else if (filenames.empty())
  46. {
  47. return _xmlParser.parse(std::cin);
  48. }
  49. else
  50. {
  51. return parseFile(filenames.front());
  52. }
  53. }
  54. // -- Check arguments -------------------------------------------------------
  55. bool checkArguments(const std::vector<std::string> &filenames)
  56. {
  57. if (filenames.size() > 1)
  58. {
  59. std::cerr << "Too many input files." << std::endl;
  60. return false;
  61. }
  62. if ((_mode.value() != "compact") && (_mode.value() != "normal"))
  63. {
  64. std::cerr << "Invalid value for --" << _mode.longOption() << ": " << _mode.value() << std::endl;
  65. return false;
  66. }
  67. try
  68. {
  69. int number = std::stoi(_number.value());
  70. if (number <= 0)
  71. {
  72. std::cerr << "Invalid value for --" << _number.longOption() << ": " << _number.value() << std::endl;
  73. return false;
  74. }
  75. }
  76. catch (...)
  77. {
  78. std::cerr << "Invalid number for --" << _number.longOption() << ": " << _number.value() << std::endl;
  79. return false;
  80. }
  81. return true;
  82. }
  83. // -- Parse a file ----------------------------------------------------------
  84. bool parseFile(const std::string &filename)
  85. {
  86. bool ok =false;
  87. std::ifstream file(filename);
  88. if (file.is_open())
  89. {
  90. ok = _xmlParser.parse(file);
  91. file.close();
  92. }
  93. else
  94. {
  95. std::cerr << "Unable to open: " << filename << std::endl;
  96. }
  97. return ok;
  98. }
  99. void outputJson(std::ostream &output)
  100. {
  101. std::size_t points = _points.size();
  102. std::size_t lines = _lines.size();
  103. if (points == 1) outputOnePoint(output);
  104. if (points > 1) outputMultiplePoints(output);
  105. if (lines == 1) outputOneLine(output);
  106. if (lines > 1) outputMultipleLines(output);
  107. }
  108. private:
  109. void outputOnePoint(std::ostream &output)
  110. {
  111. output << std::fixed << std::setprecision(6);
  112. output << '{'; doEndl(output);
  113. doIndent();
  114. output << _indent << "\"type\":\"Point\","; doEndl(output);
  115. output << _indent << "\"coordinates\":[" << _points.front()._lon << ',' << _points.front()._lat << "]"; doEndl(output);
  116. doOutdent();
  117. output << '}'; doEndl(output);
  118. }
  119. void outputMultiplePoints(std::ostream &output)
  120. {
  121. output << std::fixed << std::setprecision(6);
  122. output << '{'; doEndl(output);
  123. doIndent();
  124. output << _indent << "\"type\":\"MultiPoint\","; doEndl(output);
  125. output << _indent << "\"coordinates\":["; doEndl(output);
  126. doIndent();
  127. int number = std::stoi(_number.value());
  128. auto iter = _points.begin();
  129. while (iter != _points.end())
  130. {
  131. output << _indent;
  132. int count = 0;
  133. while (iter != _points.end() && count < number)
  134. {
  135. auto next = iter + 1;
  136. output << '[' << iter->_lon << ',' << iter->_lat << ']';
  137. if (next != _points.end()) output << ',';
  138. ++iter; count++;
  139. }
  140. doEndl(output);
  141. }
  142. doOutdent();
  143. output << _indent << "]"; doEndl(output);
  144. doOutdent();
  145. output << '}'; doEndl(output);
  146. }
  147. void outputOneLine(std::ostream &output)
  148. {
  149. output << std::fixed << std::setprecision(6);
  150. output << '{'; doEndl(output);
  151. doIndent();
  152. output << _indent << "\"type\":\"LineString\","; doEndl(output);
  153. output << _indent << "\"coordinates\":["; doEndl(output);
  154. doIndent();
  155. int number = std::stoi(_number.value());
  156. auto iter = _lines.front().begin();
  157. auto end = _lines.front().end();
  158. while (iter != end)
  159. {
  160. output << _indent;
  161. int count = 0;
  162. while (iter != end && count < number)
  163. {
  164. auto next = iter + 1;
  165. output << '[' << iter->_lon << ',' << iter->_lat << ']';
  166. if (next != end) output << ',';
  167. ++iter; count++;
  168. }
  169. doEndl(output);
  170. }
  171. doOutdent();
  172. output << _indent << "]"; doEndl(output);
  173. doOutdent();
  174. output << '}'; doEndl(output);
  175. }
  176. void outputMultipleLines(std::ostream &output)
  177. {
  178. output << std::fixed << std::setprecision(6);
  179. output << '{'; doEndl(output);
  180. doIndent();
  181. output << _indent << "\"type\":\"MultiLineString\","; doEndl(output);
  182. output << _indent << "\"coordinates\":["; doEndl(output);
  183. doIndent();
  184. int number = std::stoi(_number.value());
  185. output << _indent;
  186. for (auto iter2 = _lines.begin(); iter2 != _lines.end(); ++iter2)
  187. {
  188. auto next2 = iter2 + 1;
  189. output << "["; doEndl(output);
  190. doIndent();
  191. auto iter = iter2->begin();
  192. while (iter != iter2->end())
  193. {
  194. output << _indent;
  195. int count = 0;
  196. while (iter != iter2->end() && count < number)
  197. {
  198. auto next = iter + 1;
  199. output << '[' << iter->_lon << ',' << iter->_lat << ']';
  200. if (next != iter2->end()) output << ',';
  201. ++iter; count++;
  202. }
  203. doEndl(output);
  204. }
  205. doOutdent();
  206. output << _indent << "]";
  207. if (next2 != _lines.end()) output << ",";
  208. }
  209. doEndl(output);
  210. doOutdent();
  211. output << _indent << "]"; doEndl(output);
  212. doOutdent();
  213. output << "}"; doEndl(output);
  214. }
  215. void doEndl(std::ostream &output)
  216. {
  217. if (_mode.value() == "normal") output << std::endl;
  218. }
  219. void doIndent()
  220. {
  221. if (_mode.value() == "normal") _indent += " ";
  222. }
  223. void doOutdent()
  224. {
  225. if (_mode.value() == "normal") _indent.erase(_indent.size()-2);
  226. }
  227. static double getDoubleAttribute(const Attributes &atts, const std::string &key)
  228. {
  229. auto iter = atts.find(key);
  230. try
  231. {
  232. return iter != atts.end() ? std::stod(iter->second) : std::numeric_limits<double>::min();
  233. }
  234. catch(...)
  235. {
  236. return std::numeric_limits<double>::min();
  237. }
  238. }
  239. public:
  240. // -- Callbacks -------------------------------------------------------------
  241. virtual void startElement(const std::string &path, const std::string &, const Attributes &attributes)
  242. {
  243. if ((_tracks.active() && (path == "/gpx/trk/trkseg")) ||
  244. (_routes.active() && (path == "/gpx/rte")))
  245. {
  246. _line.clear();
  247. }
  248. else if ((_tracks.active() && (path == "/gpx/trk/trkseg/trkpt")) ||
  249. (_routes.active() && (path == "/gpx/rte/rtept")))
  250. {
  251. double lat = getDoubleAttribute(attributes, "lat");
  252. double lon = getDoubleAttribute(attributes, "lon");
  253. _line.push_back(Point(lat, lon));
  254. }
  255. else if (_waypoints.active() && (path == "/gpx/wpt"))
  256. {
  257. double lat = getDoubleAttribute(attributes, "lat");
  258. double lon = getDoubleAttribute(attributes, "lon");
  259. _points.push_back(Point(lat, lon));
  260. }
  261. }
  262. virtual void text(const std::string &, const std::string &)
  263. {
  264. }
  265. virtual void endElement(const std::string &path, const std::string &)
  266. {
  267. if ((_tracks.active() && (path == "/gpx/trk/trkseg")) ||
  268. (_routes.active() && (path == "/gpx/rte")))
  269. {
  270. _lines.push_back(_line);
  271. }
  272. }
  273. private:
  274. arg::Arguments _arguments;
  275. arg::Argument _waypoints;
  276. arg::Argument _tracks;
  277. arg::Argument _routes;
  278. arg::Argument _mode;
  279. arg::Argument _number;
  280. XMLParser _xmlParser;
  281. // Types
  282. struct Point
  283. {
  284. Point(double lat, double lon)
  285. {
  286. _lat = lat;
  287. _lon = lon;
  288. }
  289. double _lat;
  290. double _lon;
  291. };
  292. typedef std::vector<Point> Line;
  293. // Members
  294. Line _line;
  295. std::vector<Line> _lines;
  296. std::vector<Point> _points;
  297. std::string _indent;
  298. };
  299. }
  300. // -- Main program ------------------------------------------------------------
  301. int main(int argc, char *argv[])
  302. {
  303. gpxtools::GPXJson gpxJson;
  304. if (gpxJson.parseArguments(argc, argv))
  305. {
  306. gpxJson.outputJson(std::cout);
  307. return 0;
  308. }
  309. return 1;
  310. }