File indexing completed on 2025-01-18 09:35:37
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_DMS_PARSER_HPP
0015 #define BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_DMS_PARSER_HPP
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043 #include <algorithm>
0044 #include <string>
0045 #include <ostream>
0046
0047 #include <boost/static_assert.hpp>
0048
0049 #include <boost/geometry/core/config.hpp>
0050 #include <boost/geometry/core/cs.hpp>
0051 #include <boost/geometry/srs/projections/str_cast.hpp>
0052 #include <boost/geometry/util/math.hpp>
0053
0054 namespace boost { namespace geometry { namespace projections
0055 {
0056
0057 namespace detail
0058 {
0059
0060 template <typename T>
0061 struct dms_result
0062 {
0063 enum axis_selector {axis_lat = 1, axis_lon = 0};
0064
0065 private :
0066 T m_angle;
0067 axis_selector m_axis;
0068
0069 public :
0070
0071 explicit dms_result(T const& v, axis_selector ax)
0072 : m_angle(v)
0073 , m_axis(ax)
0074 {}
0075
0076 inline axis_selector axis() const { return m_axis; }
0077
0078 inline T angle() const { return m_angle; }
0079
0080 template <typename CH, typename TR>
0081 inline friend std::basic_ostream<CH, TR>& operator<<(std::basic_ostream<CH, TR>& os,
0082 const dms_result& d)
0083 {
0084 os << d.m_angle;
0085 return os;
0086 }
0087
0088 };
0089
0090
0091 template <typename T
0092 , bool as_radian = true
0093 , char N = 'N', char E = 'E', char S = 'S', char W = 'W'
0094 , char MIN = '\'', char SEC = '"'
0095 , char D = 'D', char R = 'R'
0096 >
0097 struct dms_parser
0098 {
0099
0100
0101
0102
0103
0104
0105 static const int diff = 'a' - 'A';
0106 #ifndef __GNUC__
0107 BOOST_STATIC_ASSERT((diff > 0));
0108 #endif
0109 static const char n_alter = N <= 'Z' ? N + diff : N - diff;
0110 static const char e_alter = E <= 'Z' ? E + diff : E - diff;
0111 static const char s_alter = S <= 'Z' ? S + diff : S - diff;
0112 static const char w_alter = W <= 'Z' ? W + diff : W - diff;
0113
0114 static const char r_alter = R <= 'Z' ? R + diff : R - diff;
0115
0116
0117
0118 static const char d_alter =
0119 ((D >= 'A' && D <= 'Z') || (D >= 'a' && D <= 'z')) ? (D <= 'Z' ? D + diff : D - diff) : '\0';
0120
0121
0122 struct dms_value
0123 {
0124 T dms[3];
0125 bool has_dms[3];
0126
0127 dms_value()
0128 {
0129 std::fill(dms, dms + 3, T(0));
0130 std::fill(has_dms, has_dms + 3, false);
0131 }
0132 };
0133
0134
0135 template <size_t I>
0136 static inline void assign_dms(dms_value& dms, std::string& value, bool& has_value)
0137 {
0138 dms.dms[I] = geometry::str_cast<T>(value);
0139 dms.has_dms[I] = true;
0140 has_value = false;
0141 value.clear();
0142 }
0143
0144 static inline void process(dms_value& dms, std::string& value, bool& has_value)
0145 {
0146 if (has_value)
0147 {
0148
0149 if (! dms.has_dms[0]) assign_dms<0>(dms, value, has_value);
0150 else if (! dms.has_dms[1]) assign_dms<1>(dms, value, has_value);
0151 else if (! dms.has_dms[2]) assign_dms<2>(dms, value, has_value);
0152 }
0153 }
0154
0155 static inline dms_result<T> apply(std::string const& is)
0156 {
0157 return apply(is.c_str());
0158 }
0159
0160 static inline dms_result<T> apply(const char* is)
0161 {
0162 dms_value dms;
0163 bool has_value = false;
0164 std::string value;
0165
0166 T factor = 1.0;
0167 typename dms_result<T>::axis_selector axis = dms_result<T>::axis_lon;
0168 bool in_radian = false;
0169
0170 while(*is)
0171 {
0172 switch(*is)
0173 {
0174 case '-' :
0175 if (! has_value && ! dms.has_dms[0])
0176 {
0177 factor = -factor;
0178 }
0179 break;
0180 case N :
0181 case n_alter :
0182 axis = dms_result<T>::axis_lat;
0183 break;
0184 case S :
0185 case s_alter :
0186 axis = dms_result<T>::axis_lat;
0187 factor = -factor;
0188 break;
0189 case E :
0190 case e_alter :
0191 axis = dms_result<T>::axis_lon;
0192 break;
0193 case W :
0194 case w_alter :
0195 axis = dms_result<T>::axis_lon;
0196 factor = -factor;
0197 break;
0198 case D :
0199 case d_alter :
0200 if (! dms.has_dms[0] && has_value)
0201 {
0202 assign_dms<0>(dms, value, has_value);
0203 }
0204 break;
0205 case R :
0206 case r_alter :
0207 if (! dms.has_dms[0] && has_value)
0208 {
0209
0210 in_radian = true;
0211 assign_dms<0>(dms, value, has_value);
0212 }
0213 break;
0214 case MIN:
0215 if (! dms.has_dms[1] && has_value)
0216 {
0217 assign_dms<1>(dms, value, has_value);
0218 }
0219 break;
0220 case SEC :
0221 if (! dms.has_dms[2] && has_value)
0222 {
0223 assign_dms<2>(dms, value, has_value);
0224 }
0225 break;
0226 case ' ' :
0227 case '\t' :
0228 case '\n' :
0229 process(dms, value, has_value);
0230 break;
0231 default :
0232 value += *is;
0233 has_value = true;
0234 break;
0235 }
0236 is++;
0237 }
0238
0239
0240 process(dms, value, has_value);
0241
0242 T const d2r = math::d2r<T>();
0243 T const r2d = math::r2d<T>();
0244
0245 return dms_result<T>(factor *
0246 (in_radian && as_radian
0247 ? dms.dms[0]
0248 : in_radian && ! as_radian
0249 ? dms.dms[0] * r2d
0250 : ! in_radian && as_radian
0251 ? dms.dms[0] * d2r + dms.dms[1] * d2r / 60.0 + dms.dms[2] * d2r / 3600.0
0252 : dms.dms[0] + dms.dms[1] / 60.0 + dms.dms[2] / 3600.0)
0253 , axis);
0254 }
0255 };
0256
0257
0258 }
0259
0260
0261 }}}
0262
0263
0264 #endif