pj_gridlist.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Boost.Geometry
  2. // This file is manually converted from PROJ4
  3. // This file was modified by Oracle on 2018, 2019.
  4. // Modifications copyright (c) 2018-2019, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. // This file is converted from PROJ4, http://trac.osgeo.org/proj
  10. // PROJ4 is originally written by Gerald Evenden (then of the USGS)
  11. // PROJ4 is maintained by Frank Warmerdam
  12. // This file was converted to Geometry Library by Adam Wulkiewicz
  13. // Original copyright notice:
  14. // Author: Frank Warmerdam, warmerdam@pobox.com
  15. // Copyright (c) 2000, Frank Warmerdam
  16. // Permission is hereby granted, free of charge, to any person obtaining a
  17. // copy of this software and associated documentation files (the "Software"),
  18. // to deal in the Software without restriction, including without limitation
  19. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  20. // and/or sell copies of the Software, and to permit persons to whom the
  21. // Software is furnished to do so, subject to the following conditions:
  22. // The above copyright notice and this permission notice shall be included
  23. // in all copies or substantial portions of the Software.
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  25. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  30. // DEALINGS IN THE SOFTWARE.
  31. #ifndef BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_PJ_GRIDLIST_HPP
  32. #define BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_PJ_GRIDLIST_HPP
  33. #include <boost/geometry/srs/projections/exception.hpp>
  34. #include <boost/geometry/srs/projections/grids.hpp>
  35. #include <boost/geometry/srs/projections/impl/pj_gridinfo.hpp>
  36. #include <boost/geometry/srs/projections/impl/pj_strerrno.hpp>
  37. #include <boost/geometry/srs/projections/par_data.hpp>
  38. namespace boost { namespace geometry { namespace projections
  39. {
  40. namespace detail
  41. {
  42. /************************************************************************/
  43. /* pj_gridlist_merge_grid() */
  44. /* */
  45. /* Find/load the named gridfile and merge it into the */
  46. /* last_nadgrids_list. */
  47. /************************************************************************/
  48. // Originally one function, here divided into several functions
  49. // with overloads for various types of grids and stream policies
  50. inline bool pj_gridlist_find_all(std::string const& gridname,
  51. pj_gridinfo const& grids,
  52. std::vector<std::size_t> & gridindexes)
  53. {
  54. bool result = false;
  55. for (std::size_t i = 0 ; i < grids.size() ; ++i)
  56. {
  57. if (grids[i].gridname == gridname)
  58. {
  59. result = true;
  60. gridindexes.push_back(i);
  61. }
  62. }
  63. return result;
  64. }
  65. // Fill container with sequentially increasing numbers
  66. inline void pj_gridlist_add_seq_inc(std::vector<std::size_t> & gridindexes,
  67. std::size_t first, std::size_t last)
  68. {
  69. gridindexes.reserve(gridindexes.size() + (last - first));
  70. for ( ; first < last ; ++first)
  71. {
  72. gridindexes.push_back(first);
  73. }
  74. }
  75. // Generic stream policy and standard grids
  76. template <typename StreamPolicy, typename Grids>
  77. inline bool pj_gridlist_merge_gridfile(std::string const& gridname,
  78. StreamPolicy const& stream_policy,
  79. Grids & grids,
  80. std::vector<std::size_t> & gridindexes,
  81. grids_tag)
  82. {
  83. // Try to find in the existing list of loaded grids. Add all
  84. // matching grids as with NTv2 we can get many grids from one
  85. // file (one shared gridname).
  86. if (pj_gridlist_find_all(gridname, grids.gridinfo, gridindexes))
  87. return true;
  88. std::size_t orig_size = grids.gridinfo.size();
  89. // Try to load the named grid.
  90. typename StreamPolicy::stream_type is;
  91. stream_policy.open(is, gridname);
  92. if (! pj_gridinfo_init(gridname, is, grids.gridinfo))
  93. {
  94. return false;
  95. }
  96. // Add the grid now that it is loaded.
  97. pj_gridlist_add_seq_inc(gridindexes, orig_size, grids.gridinfo.size());
  98. return true;
  99. }
  100. // Generic stream policy and shared grids
  101. template <typename StreamPolicy, typename SharedGrids>
  102. inline bool pj_gridlist_merge_gridfile(std::string const& gridname,
  103. StreamPolicy const& stream_policy,
  104. SharedGrids & grids,
  105. std::vector<std::size_t> & gridindexes,
  106. shared_grids_tag)
  107. {
  108. // Try to find in the existing list of loaded grids. Add all
  109. // matching grids as with NTv2 we can get many grids from one
  110. // file (one shared gridname).
  111. {
  112. typename SharedGrids::read_locked lck_grids(grids);
  113. if (pj_gridlist_find_all(gridname, lck_grids.gridinfo, gridindexes))
  114. return true;
  115. }
  116. // Try to load the named grid.
  117. typename StreamPolicy::stream_type is;
  118. stream_policy.open(is, gridname);
  119. pj_gridinfo new_grids;
  120. if (! pj_gridinfo_init(gridname, is, new_grids))
  121. {
  122. return false;
  123. }
  124. // Add the grid now that it is loaded.
  125. std::size_t orig_size = 0;
  126. std::size_t new_size = 0;
  127. {
  128. typename SharedGrids::write_locked lck_grids(grids);
  129. // Try to find in the existing list of loaded grids again
  130. // in case other thread already added it.
  131. if (pj_gridlist_find_all(gridname, lck_grids.gridinfo, gridindexes))
  132. return true;
  133. orig_size = lck_grids.gridinfo.size();
  134. new_size = orig_size + new_grids.size();
  135. lck_grids.gridinfo.resize(new_size);
  136. for (std::size_t i = 0 ; i < new_grids.size() ; ++ i)
  137. new_grids[i].swap(lck_grids.gridinfo[i + orig_size]);
  138. }
  139. pj_gridlist_add_seq_inc(gridindexes, orig_size, new_size);
  140. return true;
  141. }
  142. /************************************************************************/
  143. /* pj_gridlist_from_nadgrids() */
  144. /* */
  145. /* This functions loads the list of grids corresponding to a */
  146. /* particular nadgrids string into a list, and returns it. The */
  147. /* list is kept around till a request is made with a different */
  148. /* string in order to cut down on the string parsing cost, and */
  149. /* the cost of building the list of tables each time. */
  150. /************************************************************************/
  151. template <typename StreamPolicy, typename Grids>
  152. inline void pj_gridlist_from_nadgrids(srs::detail::nadgrids const& nadgrids,
  153. StreamPolicy const& stream_policy,
  154. Grids & grids,
  155. std::vector<std::size_t> & gridindexes)
  156. {
  157. // Loop processing names out of nadgrids one at a time.
  158. for (srs::detail::nadgrids::const_iterator it = nadgrids.begin() ;
  159. it != nadgrids.end() ; ++it)
  160. {
  161. bool required = (*it)[0] != '@';
  162. std::string name(it->begin() + (required ? 0 : 1), it->end());
  163. if ( ! pj_gridlist_merge_gridfile(name, stream_policy, grids, gridindexes,
  164. typename Grids::tag())
  165. && required )
  166. {
  167. BOOST_THROW_EXCEPTION( projection_exception(error_failed_to_load_grid) );
  168. }
  169. }
  170. }
  171. template <typename Par, typename ProjectionGrids>
  172. inline void pj_gridlist_from_nadgrids(Par const& defn, ProjectionGrids & proj_grids)
  173. {
  174. pj_gridlist_from_nadgrids(defn.nadgrids,
  175. proj_grids.grids_storage().stream_policy,
  176. proj_grids.grids_storage().hgrids,
  177. proj_grids.hindexes);
  178. }
  179. } // namespace detail
  180. }}} // namespace boost::geometry::projections
  181. #endif // BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_PJ_GRIDLIST_HPP