]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/geometry/srs/projections/impl/dms_parser.hpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / boost / geometry / srs / projections / impl / dms_parser.hpp
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/static_assert.hpp>
45
46 #if !defined(BOOST_GEOMETRY_NO_LEXICAL_CAST)
47 #include <boost/lexical_cast.hpp>
48 #endif // !defined(BOOST_GEOMETRY_NO_LEXICAL_CAST)
49
50 #include <boost/algorithm/string.hpp>
51
52 #include <boost/config.hpp>
53
54 #include <boost/geometry/core/cs.hpp>
55
56 #include <boost/geometry/util/math.hpp>
57
58 namespace boost { namespace geometry { namespace projections
59 {
60
61 namespace detail
62 {
63
64 template <typename T>
65 struct dms_result
66 {
67 enum axis_selector {axis_lat = 1, axis_lon = 0};
68
69 private :
70 T m_angle;
71 axis_selector m_axis;
72
73 public :
74
75 explicit dms_result(T const& v, axis_selector ax)
76 : m_angle(v)
77 , m_axis(ax)
78 {}
79
80 inline axis_selector axis() const { return m_axis; }
81
82 inline T angle() const { return m_angle; }
83
84 template <typename CH, typename TR>
85 inline friend std::basic_ostream<CH, TR>& operator<<(std::basic_ostream<CH, TR>& os,
86 const dms_result& d)
87 {
88 os << d.m_angle;
89 return os;
90 }
91
92 };
93
94
95 template <typename T
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
100 >
101 struct dms_parser
102 {
103
104
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
107
108 // For now: make it (compile-time) case sensitive
109 static const int diff = 'a' - 'A';
110 #ifndef __GNUC__
111 BOOST_STATIC_ASSERT((diff > 0)); // make sure we've the right assumption. GCC does not accept this here.
112 #endif
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;
117
118 static const char r_alter = R <= 'Z' ? R + diff : R - diff;
119
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';
124
125
126 struct dms_value
127 {
128 T dms[3];
129 bool has_dms[3];
130
131 dms_value()
132 #ifndef BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
133 : dms{0, 0, 0}
134 , has_dms{false, false, false}
135 #endif
136 {
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);
140 #endif
141 }
142 };
143
144
145 template <size_t I>
146 static inline void assign_dms(dms_value& dms, std::string& value, bool& has_value)
147 {
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;
154 has_value = false;
155 value.clear();
156 }
157
158 static inline void process(dms_value& dms, std::string& value, bool& has_value)
159 {
160 if (has_value)
161 {
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);
166 }
167 }
168
169 dms_result<T> apply(std::string const& is) const
170 {
171 return apply(is.c_str());
172 }
173
174 dms_result<T> apply(const char* is) const
175 {
176 dms_value dms;
177 bool has_value = false;
178 std::string value;
179
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"
183
184 while(*is)
185 {
186 switch(*is)
187 {
188 case '-' :
189 if (! has_value && ! dms.has_dms[0])
190 {
191 factor = -factor;
192 }
193 break;
194 case N :
195 case n_alter :
196 axis = dms_result<T>::axis_lat;
197 break;
198 case S :
199 case s_alter :
200 axis = dms_result<T>::axis_lat;
201 factor = -factor;
202 break;
203 case E :
204 case e_alter :
205 axis = dms_result<T>::axis_lon;
206 break;
207 case W :
208 case w_alter :
209 axis = dms_result<T>::axis_lon;
210 factor = -factor;
211 break;
212 case D :
213 case d_alter :
214 if (! dms.has_dms[0] && has_value)
215 {
216 assign_dms<0>(dms, value, has_value);
217 }
218 break;
219 case R :
220 case r_alter :
221 if (! dms.has_dms[0] && has_value)
222 {
223 // specified value is in radian!
224 in_radian = true;
225 assign_dms<0>(dms, value, has_value);
226 }
227 break;
228 case MIN:
229 if (! dms.has_dms[1] && has_value)
230 {
231 assign_dms<1>(dms, value, has_value);
232 }
233 break;
234 case SEC :
235 if (! dms.has_dms[2] && has_value)
236 {
237 assign_dms<2>(dms, value, has_value);
238 }
239 break;
240 case ' ' :
241 case '\t' :
242 case '\n' :
243 process(dms, value, has_value);
244 break;
245 default :
246 value += *is;
247 has_value = true;
248 break;
249 }
250 is++;
251 }
252
253 // Assign last one, if any
254 process(dms, value, has_value);
255
256 T const d2r = math::d2r<T>();
257 T const r2d = math::r2d<T>();
258
259 return dms_result<T>(factor *
260 (in_radian && as_radian
261 ? dms.dms[0]
262 : in_radian && ! as_radian
263 ? dms.dms[0] * r2d
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)
267 , axis);
268 }
269 };
270
271
272 } // namespace detail
273
274
275 }}} // namespace boost::geometry::projections
276
277
278 #endif // BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_DMS_PARSER_HPP