gpxjson.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. _simplify (_arguments, true, 's', "simplify", "METRES", "simplify the route by track distance", ""),
  26. _xmlParser(this)
  27. {
  28. }
  29. // -- Deconstructor ---------------------------------------------------------
  30. virtual ~GPXJson()
  31. {
  32. }
  33. // -- Properties ------------------------------------------------------------
  34. // -- Parse arguments -------------------------------------------------------
  35. bool parseArguments(int argc, char *argv[])
  36. {
  37. std::vector<std::string> filenames;
  38. if (!_arguments.parse(argc,argv, filenames))
  39. {
  40. return false;
  41. }
  42. else if (!checkArguments(filenames))
  43. {
  44. return false;
  45. }
  46. else if (filenames.empty())
  47. {
  48. return _xmlParser.parse(std::cin);
  49. }
  50. else
  51. {
  52. return parseFile(filenames.front());
  53. }
  54. }
  55. // -- Check arguments -------------------------------------------------------
  56. bool checkArguments(const std::vector<std::string> &filenames)
  57. {
  58. if (filenames.size() > 1)
  59. {
  60. std::cerr << "Too many input files." << std::endl;
  61. return false;
  62. }
  63. if ((_mode.value() != "compact") && (_mode.value() != "normal"))
  64. {
  65. std::cerr << "Invalid value for --" << _mode.longOption() << ": " << _mode.value() << std::endl;
  66. return false;
  67. }
  68. try
  69. {
  70. int number = std::stoi(_number.value());
  71. if (number <= 0)
  72. {
  73. std::cerr << "Invalid value for --" << _number.longOption() << ": " << _number.value() << std::endl;
  74. return false;
  75. }
  76. }
  77. catch (...)
  78. {
  79. std::cerr << "Invalid number for --" << _number.longOption() << ": " << _number.value() << std::endl;
  80. return false;
  81. }
  82. if (_simplify.active())
  83. {
  84. try
  85. {
  86. double distance = std::stod(_simplify.value());
  87. if (distance <= 0.0)
  88. {
  89. std::cerr << "Invalid value for --" << _number.longOption() << ": " << _simplify.value() << std::endl;
  90. return false;
  91. }
  92. }
  93. catch(...)
  94. {
  95. std::cerr << "Invalid value for --" << _number.longOption() << ": " << _simplify.value() << std::endl;
  96. return false;
  97. }
  98. }
  99. return true;
  100. }
  101. // -- Parse a file ----------------------------------------------------------
  102. bool parseFile(const std::string &filename)
  103. {
  104. bool ok =false;
  105. std::ifstream file(filename);
  106. if (file.is_open())
  107. {
  108. ok = _xmlParser.parse(file);
  109. file.close();
  110. }
  111. else
  112. {
  113. std::cerr << "Unable to open: " << filename << std::endl;
  114. }
  115. return ok;
  116. }
  117. void outputJson(std::ostream &output)
  118. {
  119. std::size_t points = _points.size();
  120. std::size_t lines = _lines.size();
  121. if (points == 1) outputOnePoint(output);
  122. if (points > 1) outputMultiplePoints(output);
  123. if (lines == 1) outputOneLine(output);
  124. if (lines > 1) outputMultipleLines(output);
  125. }
  126. void simplify()
  127. {
  128. if (!_simplify.active()) return;
  129. double setting = std::stod(_simplify.value());
  130. for (auto line = _lines.begin(); line != _lines.end(); ++line)
  131. {
  132. while (true)
  133. {
  134. auto p1 = line->end();
  135. auto p2 = line->end();
  136. auto p3 = line->begin();
  137. auto ps = line->end();
  138. double low = 9999.99;
  139. while (p3 != line->end())
  140. {
  141. if ((p1 != line->end()) && (p2 != line->end()))
  142. {
  143. double d = (gpx::calcDistance(p1->_lat, p1->_lon, p2->_lat, p2->_lon) +
  144. gpx::calcDistance(p2->_lat, p2->_lon, p3->_lat, p3->_lon)) -
  145. gpx::calcDistance(p1->_lat, p1->_lon, p3->_lat, p3->_lon);
  146. if (d < low)
  147. {
  148. low = d;
  149. ps = p2;
  150. }
  151. }
  152. p1 = p2;
  153. p2 = p3;
  154. ++p3;
  155. }
  156. if ((low > setting) || (ps == line->end())) break;
  157. line->erase(ps);
  158. }
  159. }
  160. }
  161. private:
  162. void outputOnePoint(std::ostream &output)
  163. {
  164. output << std::fixed << std::setprecision(6);
  165. output << '{'; doEndl(output);
  166. doIndent();
  167. output << _indent << "\"type\":\"Point\","; doEndl(output);
  168. output << _indent << "\"coordinates\":[" << _points.front()._lon << ',' << _points.front()._lat << "]"; doEndl(output);
  169. doOutdent();
  170. output << '}'; doEndl(output);
  171. }
  172. void outputMultiplePoints(std::ostream &output)
  173. {
  174. output << std::fixed << std::setprecision(6);
  175. output << '{'; doEndl(output);
  176. doIndent();
  177. output << _indent << "\"type\":\"MultiPoint\","; doEndl(output);
  178. output << _indent << "\"coordinates\":["; doEndl(output);
  179. doIndent();
  180. int number = std::stoi(_number.value());
  181. auto iter = _points.begin();
  182. while (iter != _points.end())
  183. {
  184. output << _indent;
  185. int count = 0;
  186. while (iter != _points.end() && count < number)
  187. {
  188. auto next = iter + 1;
  189. output << '[' << iter->_lon << ',' << iter->_lat << ']';
  190. if (next != _points.end()) output << ',';
  191. ++iter; count++;
  192. }
  193. doEndl(output);
  194. }
  195. doOutdent();
  196. output << _indent << "]"; doEndl(output);
  197. doOutdent();
  198. output << '}'; doEndl(output);
  199. }
  200. void outputOneLine(std::ostream &output)
  201. {
  202. output << std::fixed << std::setprecision(6);
  203. output << '{'; doEndl(output);
  204. doIndent();
  205. output << _indent << "\"type\":\"LineString\","; doEndl(output);
  206. output << _indent << "\"coordinates\":["; doEndl(output);
  207. doIndent();
  208. int number = std::stoi(_number.value());
  209. auto iter = _lines.front().begin();
  210. auto end = _lines.front().end();
  211. while (iter != end)
  212. {
  213. output << _indent;
  214. int count = 0;
  215. while (iter != end && count < number)
  216. {
  217. auto next = iter + 1;
  218. output << '[' << iter->_lon << ',' << iter->_lat << ']';
  219. if (next != end) output << ',';
  220. ++iter; count++;
  221. }
  222. doEndl(output);
  223. }
  224. doOutdent();
  225. output << _indent << "]"; doEndl(output);
  226. doOutdent();
  227. output << '}'; doEndl(output);
  228. }
  229. void outputMultipleLines(std::ostream &output)
  230. {
  231. output << std::fixed << std::setprecision(6);
  232. output << '{'; doEndl(output);
  233. doIndent();
  234. output << _indent << "\"type\":\"MultiLineString\","; doEndl(output);
  235. output << _indent << "\"coordinates\":["; doEndl(output);
  236. doIndent();
  237. int number = std::stoi(_number.value());
  238. output << _indent;
  239. for (auto iter2 = _lines.begin(); iter2 != _lines.end(); ++iter2)
  240. {
  241. auto next2 = iter2 + 1;
  242. output << "["; doEndl(output);
  243. doIndent();
  244. auto iter = iter2->begin();
  245. while (iter != iter2->end())
  246. {
  247. output << _indent;
  248. int count = 0;
  249. while (iter != iter2->end() && count < number)
  250. {
  251. auto next = iter + 1;
  252. output << '[' << iter->_lon << ',' << iter->_lat << ']';
  253. if (next != iter2->end()) output << ',';
  254. ++iter; count++;
  255. }
  256. doEndl(output);
  257. }
  258. doOutdent();
  259. output << _indent << "]";
  260. if (next2 != _lines.end()) output << ",";
  261. }
  262. doEndl(output);
  263. doOutdent();
  264. output << _indent << "]"; doEndl(output);
  265. doOutdent();
  266. output << "}"; doEndl(output);
  267. }
  268. void doEndl(std::ostream &output)
  269. {
  270. if (_mode.value() == "normal") output << std::endl;
  271. }
  272. void doIndent()
  273. {
  274. if (_mode.value() == "normal") _indent += " ";
  275. }
  276. void doOutdent()
  277. {
  278. if (_mode.value() == "normal") _indent.erase(_indent.size()-2);
  279. }
  280. static double getDoubleAttribute(const Attributes &atts, const std::string &key)
  281. {
  282. auto iter = atts.find(key);
  283. try
  284. {
  285. return iter != atts.end() ? std::stod(iter->second) : std::numeric_limits<double>::min();
  286. }
  287. catch(...)
  288. {
  289. return std::numeric_limits<double>::min();
  290. }
  291. }
  292. public:
  293. // -- Callbacks -------------------------------------------------------------
  294. virtual void startElement(const std::string &path, const std::string &, const Attributes &attributes)
  295. {
  296. if ((_tracks.active() && (path == "/gpx/trk/trkseg")) ||
  297. (_routes.active() && (path == "/gpx/rte")))
  298. {
  299. _line.clear();
  300. }
  301. else if ((_tracks.active() && (path == "/gpx/trk/trkseg/trkpt")) ||
  302. (_routes.active() && (path == "/gpx/rte/rtept")))
  303. {
  304. double lat = getDoubleAttribute(attributes, "lat");
  305. double lon = getDoubleAttribute(attributes, "lon");
  306. _line.push_back(Point(lat, lon));
  307. }
  308. else if (_waypoints.active() && (path == "/gpx/wpt"))
  309. {
  310. double lat = getDoubleAttribute(attributes, "lat");
  311. double lon = getDoubleAttribute(attributes, "lon");
  312. _points.push_back(Point(lat, lon));
  313. }
  314. }
  315. virtual void text(const std::string &, const std::string &)
  316. {
  317. }
  318. virtual void endElement(const std::string &path, const std::string &)
  319. {
  320. if ((_tracks.active() && (path == "/gpx/trk/trkseg")) ||
  321. (_routes.active() && (path == "/gpx/rte")))
  322. {
  323. _lines.push_back(_line);
  324. }
  325. }
  326. private:
  327. arg::Arguments _arguments;
  328. arg::Argument _waypoints;
  329. arg::Argument _tracks;
  330. arg::Argument _routes;
  331. arg::Argument _mode;
  332. arg::Argument _number;
  333. arg::Argument _simplify;
  334. XMLParser _xmlParser;
  335. // Types
  336. struct Point
  337. {
  338. Point(double lat, double lon)
  339. {
  340. _lat = lat;
  341. _lon = lon;
  342. }
  343. double _lat;
  344. double _lon;
  345. };
  346. typedef std::vector<Point> Line;
  347. // Members
  348. Line _line;
  349. std::vector<Line> _lines;
  350. std::vector<Point> _points;
  351. std::string _indent;
  352. };
  353. }
  354. // -- Main program ------------------------------------------------------------
  355. int main(int argc, char *argv[])
  356. {
  357. gpxtools::GPXJson gpxJson;
  358. if (gpxJson.parseArguments(argc, argv))
  359. {
  360. gpxJson.simplify();
  361. gpxJson.outputJson(std::cout);
  362. return 0;
  363. }
  364. return 1;
  365. }