1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
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
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)
13 #ifndef BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_DMS_PARSER_HPP
14 #define BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_DMS_PARSER_HPP
16 // This file is totally revised from PROJ4 dmstor.c
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)
22 // Original copyright notice:
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:
31 // The above copyright notice and this permission notice shall be included
32 // in all copies or substantial portions of the Software.
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.
44 #include <boost/static_assert.hpp>
46 #if !defined(BOOST_GEOMETRY_NO_LEXICAL_CAST)
47 #include <boost/lexical_cast.hpp>
48 #endif // !defined(BOOST_GEOMETRY_NO_LEXICAL_CAST)
50 #include <boost/algorithm/string.hpp>
52 #include <boost/config.hpp>
54 #include <boost/geometry/core/cs.hpp>
56 #include <boost/geometry/util/math.hpp>
58 namespace boost { namespace geometry { namespace projections
67 enum axis_selector {axis_lat = 1, axis_lon = 0};
75 explicit dms_result(T const& v, axis_selector ax)
80 inline axis_selector axis() const { return m_axis; }
82 inline T angle() const { return m_angle; }
84 template <typename CH, typename TR>
85 inline friend std::basic_ostream<CH, TR>& operator<<(std::basic_ostream<CH, TR>& os,
96 , bool as_radian = true
97 , char N = 'N', char E = 'E', char S = 'S', char W = 'W' // translatable
98 , char MIN = '\'', char SEC = '"' // other char's possible
99 , char D = 'D', char R = 'R' // degree sign might be small o
105 // Question from Barend: can we compile-time select that it is case-sensitive/case-insensitive?
106 // We have to change the switch then -> specializations
108 // For now: make it (compile-time) case sensitive
109 static const int diff = 'a' - 'A';
111 BOOST_STATIC_ASSERT((diff > 0)); // make sure we've the right assumption. GCC does not accept this here.
113 static const char n_alter = N <= 'Z' ? N + diff : N - diff;
114 static const char e_alter = E <= 'Z' ? E + diff : E - diff;
115 static const char s_alter = S <= 'Z' ? S + diff : S - diff;
116 static const char w_alter = W <= 'Z' ? W + diff : W - diff;
118 static const char r_alter = R <= 'Z' ? R + diff : R - diff;
120 // degree is normally D (proj4) but might be superscript o
121 // Note d_alter is not correct then, so map it to NULL now, guarded by the while
122 static const char d_alter =
123 ((D >= 'A' && D <= 'Z') || (D >= 'a' && D <= 'z')) ? (D <= 'Z' ? D + diff : D - diff) : '\0';
132 #ifndef BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
134 , has_dms{false, false, false}
137 #ifdef BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
138 std::fill(dms, dms + 3, T(0));
139 std::fill(has_dms, has_dms + 3, false);
146 static inline void assign_dms(dms_value& dms, std::string& value, bool& has_value)
148 #if !defined(BOOST_GEOMETRY_NO_LEXICAL_CAST)
149 dms.dms[I] = boost::lexical_cast<T>(value.c_str());
150 #else // !defined(BOOST_GEOMETRY_NO_LEXICAL_CAST)
151 dms.dms[I] = std::atof(value.c_str());
152 #endif // !defined(BOOST_GEOMETRY_NO_LEXICAL_CAST)
153 dms.has_dms[I] = true;
158 static inline void process(dms_value& dms, std::string& value, bool& has_value)
162 // Assign last one, sequentially
163 if (! dms.has_dms[0]) assign_dms<0>(dms, value, has_value);
164 else if (! dms.has_dms[1]) assign_dms<1>(dms, value, has_value);
165 else if (! dms.has_dms[2]) assign_dms<2>(dms, value, has_value);
169 dms_result<T> apply(std::string const& is) const
171 return apply(is.c_str());
174 dms_result<T> apply(const char* is) const
177 bool has_value = false;
180 T factor = 1.0; // + denotes N/E values, -1 denotes S/W values
181 typename dms_result<T>::axis_selector axis = dms_result<T>::axis_lon; // true denotes N/S values
182 bool in_radian = false; // true denotes values as "0.1R"
189 if (! has_value && ! dms.has_dms[0])
196 axis = dms_result<T>::axis_lat;
200 axis = dms_result<T>::axis_lat;
205 axis = dms_result<T>::axis_lon;
209 axis = dms_result<T>::axis_lon;
214 if (! dms.has_dms[0] && has_value)
216 assign_dms<0>(dms, value, has_value);
221 if (! dms.has_dms[0] && has_value)
223 // specified value is in radian!
225 assign_dms<0>(dms, value, has_value);
229 if (! dms.has_dms[1] && has_value)
231 assign_dms<1>(dms, value, has_value);
235 if (! dms.has_dms[2] && has_value)
237 assign_dms<2>(dms, value, has_value);
243 process(dms, value, has_value);
253 // Assign last one, if any
254 process(dms, value, has_value);
256 T const d2r = math::d2r<T>();
257 T const r2d = math::r2d<T>();
259 return dms_result<T>(factor *
260 (in_radian && as_radian
262 : in_radian && ! as_radian
264 : ! in_radian && as_radian
265 ? dms.dms[0] * d2r + dms.dms[1] * d2r / 60.0 + dms.dms[2] * d2r / 3600.0
266 : dms.dms[0] + dms.dms[1] / 60.0 + dms.dms[2] / 3600.0)
272 } // namespace detail
275 }}} // namespace boost::geometry::projections
278 #endif // BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_DMS_PARSER_HPP