gpxcoord.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. value = std::stod(text);
  67. return true;
  68. }
  69. // -- Scan the coordinate -----------------------------------------------------
  70. bool scanCoordinate(const std::string &input, const std::string &positives, const std::string &negatives, double &degrees)
  71. {
  72. bool negative = false;
  73. bool withSign = false;
  74. std::size_t i = 0;
  75. const
  76. std::size_t length = input.length();
  77. degrees = 0.0;
  78. skipSpaces(input, i);
  79. if (i < length && isChars(input[i], positives))
  80. {
  81. i++;
  82. withSign = true;
  83. skipSpaces(input, i);
  84. }
  85. else if (i < length && isChars(input[i], negatives))
  86. {
  87. i++;
  88. negative = true;
  89. withSign = true;
  90. skipSpaces(input, i);
  91. }
  92. else if (i < length && input[i] == '-')
  93. {
  94. i++;
  95. negative = true;
  96. withSign = true;
  97. }
  98. double value = 0.0;
  99. bool withPoint = false;
  100. if (!scanValue(input, i, value, withPoint)) return false;
  101. degrees = value;
  102. skipSpaces(input, i);
  103. if (!withPoint && scanValue(input, i, value, withPoint))
  104. {
  105. degrees += (value / 60.0);
  106. }
  107. skipSpaces(input, i);
  108. if (!withPoint && scanValue(input, i, value, withPoint))
  109. {
  110. degrees += (value / 3600.0);
  111. }
  112. skipSpaces(input, i);
  113. if (!withSign && i < length)
  114. {
  115. if (isChars(input[i], positives))
  116. {
  117. i++;
  118. }
  119. else if (isChars(input[i], negatives))
  120. {
  121. i++;
  122. negative = true;
  123. }
  124. }
  125. skipSpaces(input, i);
  126. if (i < length)
  127. {
  128. if (input[i] != ',' && input[i] != ';') return false;
  129. i++;
  130. }
  131. if (negative) degrees = -degrees;
  132. return true;
  133. }
  134. // -- Output ------------------------------------------------------------------
  135. void output(double latitude, double longitude, const std::string &browser)
  136. {
  137. std::cout << "Results:" << std::endl;
  138. std::cout << std::setprecision(8) << std::fixed;
  139. std::cout << " " << latitude << ", " << longitude << std::endl;
  140. std::string ns = (latitude >= 0 ? "N" : "S");
  141. std::string ew = (longitude >= 0 ? "E" : "W");
  142. double degLat, degLon;
  143. double minLat, minLon;
  144. minLat = std::modf(std::abs(latitude), &degLat) * 60.0;
  145. minLon = std::modf(std::abs(longitude), &degLon) * 60.0;
  146. std::cout << std::setprecision(6) << std::fixed;
  147. std::cout << " " << ns << " " << int(degLat) << " " << minLat << ", " << ew << " " << int(degLon) << " " << minLon << std::endl;
  148. double secLat, secLon;
  149. secLat = std::modf(minLat, &minLat) * 60.0;
  150. secLon = std::modf(minLon, &minLon) * 60.0;
  151. std::cout << std::setprecision(4) << std::fixed;
  152. std::cout << " " << ns << " " << int(degLat) << " " << int(minLat) << " " << secLat << ", " << ew << " " << int(degLon) << " " << int(minLon) << " " << secLon << std::endl;
  153. if (!browser.empty())
  154. {
  155. std::stringstream cmd;
  156. cmd << std::fixed;
  157. cmd << browser
  158. << " \"http://www.openstreetmap.org/?mlat=" << std::setprecision(6) << latitude
  159. << "&mlon=" << longitude
  160. << "#map=16/" << std::setprecision(4) << latitude << "/" << longitude << "\"";
  161. system(cmd.str().c_str());
  162. }
  163. }
  164. // -- Process the input -------------------------------------------------------
  165. bool process(const std::string &lat, const std::string &lon, const std::string &browser)
  166. {
  167. double latitude = 0.0;
  168. double longitude = 0.0;
  169. if (!scanCoordinate(lat, "Nn", "Ss", latitude)) return false;
  170. if (!scanCoordinate(lon, "Ee", "Ww", longitude)) return false;
  171. if (latitude < -90.0 || latitude > 90.0) return false;
  172. if (longitude < -180.0 || longitude > 180.0) return false;
  173. output(latitude, longitude, browser);
  174. return true;
  175. }
  176. // -- Help text ---------------------------------------------------------------
  177. const std::string HELP =
  178. "Show all formats for gps coordinates and optional the location in the browser.\n\n"
  179. "Examples for gps coordinate formats:\n"
  180. " 51.90540, 4.46660\n"
  181. " -51.90540, -4.46660\n"
  182. " N 51 54.324, E 4 27.996\n"
  183. " 51 54.324 S, 4 27.996 W\n"
  184. " N 51 54 19.4, E 4 27 59.8\n"
  185. " 51 54 19.4 S, 4 27 59.8 W";
  186. // -- Main program ------------------------------------------------------------
  187. int main(int argc, char *argv[])
  188. {
  189. arg::Arguments arguments("gpxcoord [OPTION].. [LAT LON]..\nDisplay the LAT LOT gps coordinate in different formats.\n", "gpxcoord v0.1", HELP);
  190. arg::Argument browser (arguments, true, 'b', "browser", "BROWSER", "set the browser", "");
  191. std::vector<std::string> coordinates;
  192. if (arguments.parse(argc, argv, coordinates))
  193. {
  194. if (coordinates.empty())
  195. {
  196. std::cerr << "Nothing to do ..." << std::endl;
  197. }
  198. else if (coordinates.size() % 2 == 1)
  199. {
  200. std::cerr << "Missing latitude or longitude coordinate." << std::endl;
  201. }
  202. else
  203. {
  204. for (std::size_t i = 0; (i + 1) < coordinates.size(); i += 2)
  205. {
  206. process(coordinates[i], coordinates[i+1], browser.value());
  207. }
  208. }
  209. }
  210. return 0;
  211. }