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