pj_param.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // This file is manually converted from PROJ4
  3. // Copyright (c) 2008-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // This file was modified by Oracle on 2017, 2018.
  5. // Modifications copyright (c) 2017-2018, Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. // This file is converted from PROJ4, http://trac.osgeo.org/proj
  11. // PROJ4 is originally written by Gerald Evenden (then of the USGS)
  12. // PROJ4 is maintained by Frank Warmerdam
  13. // PROJ4 is converted to Geometry Library by Barend Gehrels (Geodan, Amsterdam)
  14. // Original copyright notice:
  15. // Permission is hereby granted, free of charge, to any person obtaining a
  16. // copy of this software and associated documentation files (the "Software"),
  17. // to deal in the Software without restriction, including without limitation
  18. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. // and/or sell copies of the Software, and to permit persons to whom the
  20. // Software is furnished to do so, subject to the following conditions:
  21. // The above copyright notice and this permission notice shall be included
  22. // in all copies or substantial portions of the Software.
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  24. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  26. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. // DEALINGS IN THE SOFTWARE.
  30. #ifndef BOOST_GEOMETRY_PROJECTIONS_PJ_PARAM_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_PJ_PARAM_HPP
  32. #include <string>
  33. #include <vector>
  34. #include <boost/geometry/srs/projections/exception.hpp>
  35. #include <boost/geometry/srs/projections/impl/dms_parser.hpp>
  36. #include <boost/geometry/srs/projections/impl/projects.hpp>
  37. #include <boost/mpl/assert.hpp>
  38. #include <boost/type_traits/is_integral.hpp>
  39. namespace boost { namespace geometry { namespace projections {
  40. namespace detail {
  41. /* create pvalue list entry */
  42. template <typename T>
  43. inline pvalue<T> pj_mkparam(std::string const& name, std::string const& value)
  44. {
  45. pvalue<T> newitem;
  46. newitem.param = name;
  47. newitem.s = value;
  48. //newitem.used = false;
  49. return newitem;
  50. }
  51. /* create pvalue list entry */
  52. template <typename T>
  53. inline pvalue<T> pj_mkparam(std::string const& str)
  54. {
  55. std::string name = str;
  56. std::string value;
  57. boost::trim_left_if(name, boost::is_any_of("+"));
  58. std::string::size_type loc = name.find("=");
  59. if (loc != std::string::npos)
  60. {
  61. value = name.substr(loc + 1);
  62. name.erase(loc);
  63. }
  64. return pj_mkparam<T>(name, value);
  65. }
  66. /* input exists */
  67. template <typename T>
  68. inline typename std::vector<pvalue<T> >::const_iterator
  69. pj_param_find(std::vector<pvalue<T> > const& pl, std::string const& name)
  70. {
  71. typedef typename std::vector<pvalue<T> >::const_iterator iterator;
  72. for (iterator it = pl.begin(); it != pl.end(); it++)
  73. {
  74. if (it->param == name)
  75. {
  76. //it->used = true;
  77. return it;
  78. }
  79. // TODO: needed for pipeline
  80. /*else if (it->param == "step")
  81. {
  82. return pl.end();
  83. }*/
  84. }
  85. return pl.end();
  86. }
  87. /* input exists */
  88. template <typename T>
  89. inline bool pj_param_exists(std::vector<pvalue<T> > const& pl, std::string const& name)
  90. {
  91. return pj_param_find(pl, name) != pl.end();
  92. }
  93. /* integer input */
  94. template <typename T>
  95. inline bool pj_param_i(std::vector<pvalue<T> > const& pl, std::string const& name, int & par)
  96. {
  97. typename std::vector<pvalue<T> >::const_iterator it = pj_param_find(pl, name);
  98. if (it != pl.end())
  99. {
  100. par = geometry::str_cast<int>(it->s);
  101. return true;
  102. }
  103. return false;
  104. }
  105. /* floating point input */
  106. template <typename T>
  107. inline bool pj_param_f(std::vector<pvalue<T> > const& pl, std::string const& name, T & par)
  108. {
  109. typename std::vector<pvalue<T> >::const_iterator it = pj_param_find(pl, name);
  110. if (it != pl.end())
  111. {
  112. par = geometry::str_cast<T>(it->s);
  113. return true;
  114. }
  115. return false;
  116. }
  117. /* radians input */
  118. template <typename T>
  119. inline bool pj_param_r(std::vector<pvalue<T> > const& pl, std::string const& name, T & par)
  120. {
  121. typename std::vector<pvalue<T> >::const_iterator it = pj_param_find(pl, name);
  122. if (it != pl.end())
  123. {
  124. dms_parser<T, true> parser;
  125. par = parser.apply(it->s.c_str()).angle();
  126. return true;
  127. }
  128. return false;
  129. }
  130. /* string input */
  131. template <typename T>
  132. inline bool pj_param_s(std::vector<pvalue<T> > const& pl, std::string const& name, std::string & par)
  133. {
  134. typename std::vector<pvalue<T> >::const_iterator it = pj_param_find(pl, name);
  135. if (it != pl.end())
  136. {
  137. par = it->s;
  138. return true;
  139. }
  140. return false;
  141. }
  142. /* bool input */
  143. template <typename T>
  144. inline bool pj_get_param_b(std::vector<pvalue<T> > const& pl, std::string const& name)
  145. {
  146. typename std::vector<pvalue<T> >::const_iterator it = pj_param_find(pl, name);
  147. if (it != pl.end())
  148. {
  149. switch (it->s[0])
  150. {
  151. case '\0': case 'T': case 't':
  152. return true;
  153. case 'F': case 'f':
  154. return false;
  155. default:
  156. BOOST_THROW_EXCEPTION( projection_exception(error_invalid_boolean_param) );
  157. return false;
  158. }
  159. }
  160. return false;
  161. }
  162. // NOTE: In the original code, in pl_ell_set.c there is a function pj_get_param
  163. // which behavior is similar to pj_param but it doesn't set `user` member to TRUE
  164. // while pj_param does in the original code. In Boost.Geometry this member is not used.
  165. template <typename T>
  166. inline int pj_get_param_i(std::vector<pvalue<T> > const& pl, std::string const& name)
  167. {
  168. int res = 0;
  169. pj_param_i(pl, name, res);
  170. return res;
  171. }
  172. template <typename T>
  173. inline T pj_get_param_f(std::vector<pvalue<T> > const& pl, std::string const& name)
  174. {
  175. T res = 0;
  176. pj_param_f(pl, name, res);
  177. return res;
  178. }
  179. template <typename T>
  180. inline T pj_get_param_r(std::vector<pvalue<T> > const& pl, std::string const& name)
  181. {
  182. T res = 0;
  183. pj_param_r(pl, name, res);
  184. return res;
  185. }
  186. template <typename T>
  187. inline std::string pj_get_param_s(std::vector<pvalue<T> > const& pl, std::string const& name)
  188. {
  189. std::string res;
  190. pj_param_s(pl, name, res);
  191. return res;
  192. }
  193. } // namespace detail
  194. }}} // namespace boost::geometry::projections
  195. #endif