gpxcoord.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //==============================================================================
  2. //
  3. // gpxcoord - the gpx coordinate formatter
  4. //
  5. // Copyright (C) 2018 Dick van Oudheusden
  6. //
  7. // This library is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU Lesser General Public
  9. // License as published by the Free Software Foundation; either
  10. // version 3 of the License, or (at your option) any later version.
  11. //
  12. // This library is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. // Lesser General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU Lesser General Public
  18. // License along with this library; if not, write to the Free
  19. // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. //
  21. //==============================================================================
  22. #include <iostream>
  23. #include <sstream>
  24. #include <cstring>
  25. #include <cctype>
  26. #include <iomanip>
  27. #include <cmath>
  28. #include "Arguments.h"
  29. // -- Is the next char present in matchers ------------------------------------
  30. bool isChars(char ch, const std::string &matchers)
  31. {
  32. for (auto iter = matchers.begin(); iter != matchers.end(); ++iter)
  33. {
  34. if (ch == *iter) return true;
  35. }
  36. return false;
  37. }
  38. // -- Skip spaces -------------------------------------------------------------
  39. void skipSpaces(const std::string &input, std::size_t &i)
  40. {
  41. const std::size_t length = input.length();
  42. while (i < length && isspace(input[i]))
  43. {
  44. i++;
  45. }
  46. }
  47. // -- Scan a value ------------------------------------------------------------
  48. bool scanValue(const std::string &input, std::size_t &i, double &value, bool &withPoint)
  49. {
  50. const std::size_t length = input.length();
  51. if (i < length && !isdigit(input[i])) return false;
  52. std::string text;
  53. while (i < length && isdigit(input[i]))
  54. {
  55. text += input[i++];
  56. }
  57. withPoint = (i < length && input[i] == '.');
  58. if (withPoint)
  59. {
  60. text += input[i++];
  61. while (i < length && isdigit(input[i]))
  62. {
  63. text += input[i++];
  64. }
  65. }
  66. try
  67. {
  68. value = std::stod(text);
  69. }
  70. catch (...)
  71. {
  72. std::cerr << "Fail to convert: " << text << std::endl;
  73. }
  74. return true;
  75. }
  76. // -- Scan the coordinate -----------------------------------------------------
  77. bool scanCoordinate(const std::string &input, const std::string &positives, const std::string &negatives, double &degrees)
  78. {
  79. bool negative = false;
  80. bool withSign = false;
  81. std::size_t i = 0;
  82. const
  83. std::size_t length = input.length();
  84. degrees = 0.0;
  85. skipSpaces(input, i);
  86. if (i < length && isChars(input[i], positives))
  87. {
  88. i++;
  89. withSign = true;
  90. skipSpaces(input, i);
  91. }
  92. else if (i < length && isChars(input[i], negatives))
  93. {
  94. i++;
  95. negative = true;
  96. withSign = true;
  97. skipSpaces(input, i);
  98. }
  99. else if (i < length && input[i] == '-')
  100. {
  101. i++;
  102. negative = true;
  103. withSign = true;
  104. }
  105. double value = 0.0;
  106. bool withPoint = false;
  107. if (!scanValue(input, i, value, withPoint)) return false;
  108. degrees = value;
  109. skipSpaces(input, i);
  110. if (!withPoint && scanValue(input, i, value, withPoint))
  111. {
  112. degrees += (value / 60.0);
  113. }
  114. skipSpaces(input, i);
  115. if (!withPoint && scanValue(input, i, value, withPoint))
  116. {
  117. degrees += (value / 3600.0);
  118. }
  119. skipSpaces(input, i);
  120. if (!withSign && i < length)
  121. {
  122. if (isChars(input[i], positives))
  123. {
  124. i++;
  125. }
  126. else if (isChars(input[i], negatives))
  127. {
  128. i++;
  129. negative = true;
  130. }
  131. }
  132. skipSpaces(input, i);
  133. if (i < length)
  134. {
  135. if (input[i] != ',' && input[i] != ';') return false;
  136. i++;
  137. }
  138. if (negative) degrees = -degrees;
  139. return true;
  140. }
  141. // -- Output ------------------------------------------------------------------
  142. void output(double latitude, double longitude, const std::string &browser)
  143. {
  144. std::cout << "Results:" << std::endl;
  145. std::cout << std::setprecision(8) << std::fixed;
  146. std::cout << " " << latitude << ", " << longitude << std::endl;
  147. std::string ns = (latitude >= 0 ? "N" : "S");
  148. std::string ew = (longitude >= 0 ? "E" : "W");
  149. double degLat, degLon;
  150. double minLat, minLon;
  151. minLat = std::modf(std::abs(latitude), &degLat) * 60.0;
  152. minLon = std::modf(std::abs(longitude), &degLon) * 60.0;
  153. std::cout << std::setprecision(6) << std::fixed;
  154. std::cout << " " << ns << " " << int(degLat) << " " << minLat << ", " << ew << " " << int(degLon) << " " << minLon << std::endl;
  155. double secLat, secLon;
  156. secLat = std::modf(minLat, &minLat) * 60.0;
  157. secLon = std::modf(minLon, &minLon) * 60.0;
  158. std::cout << std::setprecision(4) << std::fixed;
  159. std::cout << " " << ns << " " << int(degLat) << " " << int(minLat) << " " << secLat << ", " << ew << " " << int(degLon) << " " << int(minLon) << " " << secLon << std::endl;
  160. if (!browser.empty())
  161. {
  162. std::stringstream cmd;
  163. cmd << std::fixed;
  164. cmd << browser
  165. << " \"http://www.openstreetmap.org/?mlat=" << std::setprecision(6) << latitude
  166. << "&mlon=" << longitude
  167. << "#map=16/" << std::setprecision(4) << latitude << "/" << longitude << "\"";
  168. system(cmd.str().c_str());
  169. }
  170. }
  171. // -- Process the input -------------------------------------------------------
  172. bool process(const std::string &lat, const std::string &lon, const std::string &browser)
  173. {
  174. double latitude = 0.0;
  175. double longitude = 0.0;
  176. if (!scanCoordinate(lat, "Nn", "Ss", latitude)) return false;
  177. if (!scanCoordinate(lon, "Ee", "Ww", longitude)) return false;
  178. if (latitude < -90.0 || latitude > 90.0) return false;
  179. if (longitude < -180.0 || longitude > 180.0) return false;
  180. output(latitude, longitude, browser);
  181. return true;
  182. }
  183. // -- Help text ---------------------------------------------------------------
  184. const std::string HELP =
  185. "Show all formats for gps coordinates and optional the location in the browser.\n\n"
  186. "Examples for gps coordinate formats:\n"
  187. " 51.90540, 4.46660\n"
  188. " -51.90540, -4.46660\n"
  189. " N 51 54.324, E 4 27.996\n"
  190. " 51 54.324 S, 4 27.996 W\n"
  191. " N 51 54 19.4, E 4 27 59.8\n"
  192. " 51 54 19.4 S, 4 27 59.8 W";
  193. // -- Main program ------------------------------------------------------------
  194. int main(int argc, char *argv[])
  195. {
  196. arg::Arguments arguments("gpxcoord [OPTION].. [\"LAT\" \"LON\"]..\nDisplay the LAT LOT gps coordinate in different formats.\n", "gpxcoord v0.1", HELP);
  197. arg::Argument browser (arguments, true, 'b', "browser", "BROWSER", "set the browser", "");
  198. std::vector<std::string> coordinates;
  199. if (arguments.parse(argc, argv, coordinates))
  200. {
  201. if (coordinates.empty())
  202. {
  203. std::cerr << "Nothing to do ..." << std::endl;
  204. }
  205. else if (coordinates.size() % 2 == 1)
  206. {
  207. std::cerr << "Missing latitude or longitude coordinate." << std::endl;
  208. }
  209. else
  210. {
  211. for (std::size_t i = 0; (i + 1) < coordinates.size(); i += 2)
  212. {
  213. process(coordinates[i], coordinates[i+1], browser.value());
  214. }
  215. }
  216. }
  217. return 0;
  218. }