Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:35:37

0001 // Boost.Geometry (aka GGL, Generic Geometry Library)
0002 
0003 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
0004 // Copyright (c) 2023 Adam Wulkiewicz, Lodz, Poland.
0005 
0006 // This file was modified by Oracle on 2017, 2018.
0007 // Modifications copyright (c) 2017-2018, Oracle and/or its affiliates.
0008 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
0009 
0010 // Use, modification and distribution is subject to the Boost Software License,
0011 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0012 // http://www.boost.org/LICENSE_1_0.txt)
0013 
0014 #ifndef BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_DMS_PARSER_HPP
0015 #define BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_DMS_PARSER_HPP
0016 
0017 // This file is totally revised from PROJ4 dmstor.c
0018 
0019 // PROJ4 is originally written by Gerald Evenden (then of the USGS)
0020 // PROJ4 is maintained by Frank Warmerdam
0021 // PROJ4 is converted to Geometry Library by Barend Gehrels (Geodan, Amsterdam)
0022 
0023 // Original copyright notice:
0024 
0025 // Permission is hereby granted, free of charge, to any person obtaining a
0026 // copy of this software and associated documentation files (the "Software"),
0027 // to deal in the Software without restriction, including without limitation
0028 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
0029 // and/or sell copies of the Software, and to permit persons to whom the
0030 // Software is furnished to do so, subject to the following conditions:
0031 
0032 // The above copyright notice and this permission notice shall be included
0033 // in all copies or substantial portions of the Software.
0034 
0035 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0036 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0037 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
0038 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0039 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0040 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
0041 // DEALINGS IN THE SOFTWARE.
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' // translatable
0094         , char MIN = '\'', char SEC = '"' // other char's possible
0095         , char D = 'D', char R = 'R' // degree sign might be small o
0096         >
0097 struct dms_parser
0098 {
0099 
0100 
0101     // Question from Barend: can we compile-time select that it is case-sensitive/case-insensitive?
0102     // We have to change the switch then -> specializations
0103 
0104     // For now: make it (compile-time) case sensitive
0105     static const int diff = 'a' - 'A';
0106 #ifndef __GNUC__
0107     BOOST_STATIC_ASSERT((diff > 0)); // make sure we've the right assumption. GCC does not accept this here.
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     // degree is normally D (proj4) but might be superscript o
0117     // Note d_alter is not correct then, so map it to NULL now, guarded by the while
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             // Assign last one, sequentially
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; // + denotes N/E values, -1 denotes S/W values
0167         typename dms_result<T>::axis_selector axis = dms_result<T>::axis_lon; // true denotes N/S values
0168         bool in_radian = false; // true denotes values as "0.1R"
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                         // specified value is in radian!
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         // Assign last one, if any
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 } // namespace detail
0259 
0260 
0261 }}} // namespace boost::geometry::projections
0262 
0263 
0264 #endif // BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_DMS_PARSER_HPP