proj4.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Boost.Geometry
  2. // Copyright (c) 2008-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2017-2018, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_GEOMETRY_SRS_PROJECTIONS_PROJ4_HPP
  9. #define BOOST_GEOMETRY_SRS_PROJECTIONS_PROJ4_HPP
  10. #include <string>
  11. #include <vector>
  12. #include <boost/algorithm/string/trim.hpp>
  13. namespace boost { namespace geometry
  14. {
  15. namespace srs
  16. {
  17. struct dynamic {};
  18. struct proj4
  19. {
  20. explicit proj4(const char* s)
  21. : m_str(s)
  22. {}
  23. explicit proj4(std::string const& s)
  24. : m_str(s)
  25. {}
  26. std::string const& str() const
  27. {
  28. return m_str;
  29. }
  30. private:
  31. std::string m_str;
  32. };
  33. namespace detail
  34. {
  35. struct proj4_parameter
  36. {
  37. proj4_parameter() {}
  38. proj4_parameter(std::string const& n, std::string const& v) : name(n), value(v) {}
  39. std::string name;
  40. std::string value;
  41. };
  42. struct proj4_parameters
  43. : std::vector<proj4_parameter>
  44. {
  45. // Initially implemented as part of pj_init_plus() and pj_init()
  46. proj4_parameters(std::string const& proj4_str)
  47. {
  48. const char* sep = " +";
  49. /* split into arguments based on '+' and trim white space */
  50. // boost::split splits on one character, here it should be on " +", so implementation below
  51. // todo: put in different routine or sort out
  52. std::string def = boost::trim_copy(proj4_str);
  53. boost::trim_left_if(def, boost::is_any_of(sep));
  54. std::string::size_type loc = def.find(sep);
  55. while (loc != std::string::npos)
  56. {
  57. std::string par = def.substr(0, loc);
  58. boost::trim(par);
  59. if (! par.empty())
  60. {
  61. this->add(par);
  62. }
  63. def.erase(0, loc);
  64. boost::trim_left_if(def, boost::is_any_of(sep));
  65. loc = def.find(sep);
  66. }
  67. if (! def.empty())
  68. {
  69. this->add(def);
  70. }
  71. }
  72. void add(std::string const& str)
  73. {
  74. std::string name = str;
  75. std::string value;
  76. boost::trim_left_if(name, boost::is_any_of("+"));
  77. std::string::size_type loc = name.find("=");
  78. if (loc != std::string::npos)
  79. {
  80. value = name.substr(loc + 1);
  81. name.erase(loc);
  82. }
  83. this->add(name, value);
  84. }
  85. void add(std::string const& name, std::string const& value)
  86. {
  87. this->push_back(proj4_parameter(name, value));
  88. }
  89. };
  90. }
  91. } // namespace srs
  92. }} // namespace boost::geometry
  93. #endif // BOOST_GEOMETRY_SRS_PROJECTIONS_PROJ4_HPP