dms_parser.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2017, 2018.
  4. // Modifications copyright (c) 2017-2018, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_DMS_PARSER_HPP
  10. #define BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_DMS_PARSER_HPP
  11. // This file is totally revised from PROJ4 dmstor.c
  12. // PROJ4 is originally written by Gerald Evenden (then of the USGS)
  13. // PROJ4 is maintained by Frank Warmerdam
  14. // PROJ4 is converted to Geometry Library by Barend Gehrels (Geodan, Amsterdam)
  15. // Original copyright notice:
  16. // Permission is hereby granted, free of charge, to any person obtaining a
  17. // copy of this software and associated documentation files (the "Software"),
  18. // to deal in the Software without restriction, including without limitation
  19. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  20. // and/or sell copies of the Software, and to permit persons to whom the
  21. // Software is furnished to do so, subject to the following conditions:
  22. // The above copyright notice and this permission notice shall be included
  23. // in all copies or substantial portions of the Software.
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  25. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  30. // DEALINGS IN THE SOFTWARE.
  31. #include <string>
  32. #include <boost/algorithm/string.hpp>
  33. #include <boost/static_assert.hpp>
  34. #include <boost/geometry/core/config.hpp>
  35. #include <boost/geometry/core/cs.hpp>
  36. #include <boost/geometry/srs/projections/str_cast.hpp>
  37. #include <boost/geometry/util/math.hpp>
  38. namespace boost { namespace geometry { namespace projections
  39. {
  40. namespace detail
  41. {
  42. template <typename T>
  43. struct dms_result
  44. {
  45. enum axis_selector {axis_lat = 1, axis_lon = 0};
  46. private :
  47. T m_angle;
  48. axis_selector m_axis;
  49. public :
  50. explicit dms_result(T const& v, axis_selector ax)
  51. : m_angle(v)
  52. , m_axis(ax)
  53. {}
  54. inline axis_selector axis() const { return m_axis; }
  55. inline T angle() const { return m_angle; }
  56. template <typename CH, typename TR>
  57. inline friend std::basic_ostream<CH, TR>& operator<<(std::basic_ostream<CH, TR>& os,
  58. const dms_result& d)
  59. {
  60. os << d.m_angle;
  61. return os;
  62. }
  63. };
  64. template <typename T
  65. , bool as_radian = true
  66. , char N = 'N', char E = 'E', char S = 'S', char W = 'W' // translatable
  67. , char MIN = '\'', char SEC = '"' // other char's possible
  68. , char D = 'D', char R = 'R' // degree sign might be small o
  69. >
  70. struct dms_parser
  71. {
  72. // Question from Barend: can we compile-time select that it is case-sensitive/case-insensitive?
  73. // We have to change the switch then -> specializations
  74. // For now: make it (compile-time) case sensitive
  75. static const int diff = 'a' - 'A';
  76. #ifndef __GNUC__
  77. BOOST_STATIC_ASSERT((diff > 0)); // make sure we've the right assumption. GCC does not accept this here.
  78. #endif
  79. static const char n_alter = N <= 'Z' ? N + diff : N - diff;
  80. static const char e_alter = E <= 'Z' ? E + diff : E - diff;
  81. static const char s_alter = S <= 'Z' ? S + diff : S - diff;
  82. static const char w_alter = W <= 'Z' ? W + diff : W - diff;
  83. static const char r_alter = R <= 'Z' ? R + diff : R - diff;
  84. // degree is normally D (proj4) but might be superscript o
  85. // Note d_alter is not correct then, so map it to NULL now, guarded by the while
  86. static const char d_alter =
  87. ((D >= 'A' && D <= 'Z') || (D >= 'a' && D <= 'z')) ? (D <= 'Z' ? D + diff : D - diff) : '\0';
  88. struct dms_value
  89. {
  90. T dms[3];
  91. bool has_dms[3];
  92. dms_value()
  93. #ifdef BOOST_GEOMETRY_CXX11_ARRAY_UNIFIED_INITIALIZATION
  94. : dms{0, 0, 0}
  95. , has_dms{false, false, false}
  96. {}
  97. #else
  98. {
  99. std::fill(dms, dms + 3, T(0));
  100. std::fill(has_dms, has_dms + 3, false);
  101. }
  102. #endif
  103. };
  104. template <size_t I>
  105. static inline void assign_dms(dms_value& dms, std::string& value, bool& has_value)
  106. {
  107. dms.dms[I] = geometry::str_cast<T>(value);
  108. dms.has_dms[I] = true;
  109. has_value = false;
  110. value.clear();
  111. }
  112. static inline void process(dms_value& dms, std::string& value, bool& has_value)
  113. {
  114. if (has_value)
  115. {
  116. // Assign last one, sequentially
  117. if (! dms.has_dms[0]) assign_dms<0>(dms, value, has_value);
  118. else if (! dms.has_dms[1]) assign_dms<1>(dms, value, has_value);
  119. else if (! dms.has_dms[2]) assign_dms<2>(dms, value, has_value);
  120. }
  121. }
  122. static inline dms_result<T> apply(std::string const& is)
  123. {
  124. return apply(is.c_str());
  125. }
  126. static inline dms_result<T> apply(const char* is)
  127. {
  128. dms_value dms;
  129. bool has_value = false;
  130. std::string value;
  131. T factor = 1.0; // + denotes N/E values, -1 denotes S/W values
  132. typename dms_result<T>::axis_selector axis = dms_result<T>::axis_lon; // true denotes N/S values
  133. bool in_radian = false; // true denotes values as "0.1R"
  134. while(*is)
  135. {
  136. switch(*is)
  137. {
  138. case '-' :
  139. if (! has_value && ! dms.has_dms[0])
  140. {
  141. factor = -factor;
  142. }
  143. break;
  144. case N :
  145. case n_alter :
  146. axis = dms_result<T>::axis_lat;
  147. break;
  148. case S :
  149. case s_alter :
  150. axis = dms_result<T>::axis_lat;
  151. factor = -factor;
  152. break;
  153. case E :
  154. case e_alter :
  155. axis = dms_result<T>::axis_lon;
  156. break;
  157. case W :
  158. case w_alter :
  159. axis = dms_result<T>::axis_lon;
  160. factor = -factor;
  161. break;
  162. case D :
  163. case d_alter :
  164. if (! dms.has_dms[0] && has_value)
  165. {
  166. assign_dms<0>(dms, value, has_value);
  167. }
  168. break;
  169. case R :
  170. case r_alter :
  171. if (! dms.has_dms[0] && has_value)
  172. {
  173. // specified value is in radian!
  174. in_radian = true;
  175. assign_dms<0>(dms, value, has_value);
  176. }
  177. break;
  178. case MIN:
  179. if (! dms.has_dms[1] && has_value)
  180. {
  181. assign_dms<1>(dms, value, has_value);
  182. }
  183. break;
  184. case SEC :
  185. if (! dms.has_dms[2] && has_value)
  186. {
  187. assign_dms<2>(dms, value, has_value);
  188. }
  189. break;
  190. case ' ' :
  191. case '\t' :
  192. case '\n' :
  193. process(dms, value, has_value);
  194. break;
  195. default :
  196. value += *is;
  197. has_value = true;
  198. break;
  199. }
  200. is++;
  201. }
  202. // Assign last one, if any
  203. process(dms, value, has_value);
  204. T const d2r = math::d2r<T>();
  205. T const r2d = math::r2d<T>();
  206. return dms_result<T>(factor *
  207. (in_radian && as_radian
  208. ? dms.dms[0]
  209. : in_radian && ! as_radian
  210. ? dms.dms[0] * r2d
  211. : ! in_radian && as_radian
  212. ? dms.dms[0] * d2r + dms.dms[1] * d2r / 60.0 + dms.dms[2] * d2r / 3600.0
  213. : dms.dms[0] + dms.dms[1] / 60.0 + dms.dms[2] / 3600.0)
  214. , axis);
  215. }
  216. };
  217. } // namespace detail
  218. }}} // namespace boost::geometry::projections
  219. #endif // BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_DMS_PARSER_HPP