2 // This file is manually converted from PROJ4
4 // This file was modified by Oracle on 2018, 2019.
5 // Modifications copyright (c) 2018-2019, Oracle and/or its affiliates.
6 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
8 // Use, modification and distribution is subject to the Boost Software License,
9 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
12 // This file is converted from PROJ4, http://trac.osgeo.org/proj
13 // PROJ4 is originally written by Gerald Evenden (then of the USGS)
14 // PROJ4 is maintained by Frank Warmerdam
15 // This file was converted to Geometry Library by Adam Wulkiewicz
17 // Original copyright notice:
18 // Author: Frank Warmerdam, warmerdam@pobox.com
20 // Copyright (c) 2000, Frank Warmerdam
22 // Permission is hereby granted, free of charge, to any person obtaining a
23 // copy of this software and associated documentation files (the "Software"),
24 // to deal in the Software without restriction, including without limitation
25 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
26 // and/or sell copies of the Software, and to permit persons to whom the
27 // Software is furnished to do so, subject to the following conditions:
29 // The above copyright notice and this permission notice shall be included
30 // in all copies or substantial portions of the Software.
32 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
33 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
35 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
37 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
38 // DEALINGS IN THE SOFTWARE.
40 #ifndef BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_PJ_GRIDLIST_HPP
41 #define BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_PJ_GRIDLIST_HPP
44 #include <boost/geometry/srs/projections/exception.hpp>
45 #include <boost/geometry/srs/projections/grids.hpp>
46 #include <boost/geometry/srs/projections/impl/pj_gridinfo.hpp>
47 #include <boost/geometry/srs/projections/impl/pj_strerrno.hpp>
48 #include <boost/geometry/srs/projections/par_data.hpp>
51 namespace boost { namespace geometry { namespace projections
57 /************************************************************************/
58 /* pj_gridlist_merge_grid() */
60 /* Find/load the named gridfile and merge it into the */
61 /* last_nadgrids_list. */
62 /************************************************************************/
64 // Originally one function, here divided into several functions
65 // with overloads for various types of grids and stream policies
67 inline bool pj_gridlist_find_all(std::string const& gridname,
68 pj_gridinfo const& grids,
69 std::vector<std::size_t> & gridindexes)
72 for (std::size_t i = 0 ; i < grids.size() ; ++i)
74 if (grids[i].gridname == gridname)
77 gridindexes.push_back(i);
83 // Fill container with sequentially increasing numbers
84 inline void pj_gridlist_add_seq_inc(std::vector<std::size_t> & gridindexes,
85 std::size_t first, std::size_t last)
87 gridindexes.reserve(gridindexes.size() + (last - first));
88 for ( ; first < last ; ++first)
90 gridindexes.push_back(first);
94 // Generic stream policy and standard grids
95 template <typename StreamPolicy, typename Grids>
96 inline bool pj_gridlist_merge_gridfile(std::string const& gridname,
97 StreamPolicy const& stream_policy,
99 std::vector<std::size_t> & gridindexes,
102 // Try to find in the existing list of loaded grids. Add all
103 // matching grids as with NTv2 we can get many grids from one
104 // file (one shared gridname).
105 if (pj_gridlist_find_all(gridname, grids.gridinfo, gridindexes))
108 std::size_t orig_size = grids.gridinfo.size();
110 // Try to load the named grid.
111 typename StreamPolicy::stream_type is;
112 stream_policy.open(is, gridname);
114 if (! pj_gridinfo_init(gridname, is, grids.gridinfo))
119 // Add the grid now that it is loaded.
120 pj_gridlist_add_seq_inc(gridindexes, orig_size, grids.gridinfo.size());
125 // Generic stream policy and shared grids
126 template <typename StreamPolicy, typename SharedGrids>
127 inline bool pj_gridlist_merge_gridfile(std::string const& gridname,
128 StreamPolicy const& stream_policy,
130 std::vector<std::size_t> & gridindexes,
133 // Try to find in the existing list of loaded grids. Add all
134 // matching grids as with NTv2 we can get many grids from one
135 // file (one shared gridname).
137 typename SharedGrids::read_locked lck_grids(grids);
139 if (pj_gridlist_find_all(gridname, lck_grids.gridinfo, gridindexes))
143 // Try to load the named grid.
144 typename StreamPolicy::stream_type is;
145 stream_policy.open(is, gridname);
147 pj_gridinfo new_grids;
149 if (! pj_gridinfo_init(gridname, is, new_grids))
154 // Add the grid now that it is loaded.
156 std::size_t orig_size = 0;
157 std::size_t new_size = 0;
160 typename SharedGrids::write_locked lck_grids(grids);
162 // Try to find in the existing list of loaded grids again
163 // in case other thread already added it.
164 if (pj_gridlist_find_all(gridname, lck_grids.gridinfo, gridindexes))
167 orig_size = lck_grids.gridinfo.size();
168 new_size = orig_size + new_grids.size();
170 lck_grids.gridinfo.resize(new_size);
171 for (std::size_t i = 0 ; i < new_grids.size() ; ++ i)
172 new_grids[i].swap(lck_grids.gridinfo[i + orig_size]);
175 pj_gridlist_add_seq_inc(gridindexes, orig_size, new_size);
181 /************************************************************************/
182 /* pj_gridlist_from_nadgrids() */
184 /* This functions loads the list of grids corresponding to a */
185 /* particular nadgrids string into a list, and returns it. The */
186 /* list is kept around till a request is made with a different */
187 /* string in order to cut down on the string parsing cost, and */
188 /* the cost of building the list of tables each time. */
189 /************************************************************************/
191 template <typename StreamPolicy, typename Grids>
192 inline void pj_gridlist_from_nadgrids(srs::detail::nadgrids const& nadgrids,
193 StreamPolicy const& stream_policy,
195 std::vector<std::size_t> & gridindexes)
198 // Loop processing names out of nadgrids one at a time.
199 for (srs::detail::nadgrids::const_iterator it = nadgrids.begin() ;
200 it != nadgrids.end() ; ++it)
202 bool required = (*it)[0] != '@';
204 std::string name(it->begin() + (required ? 0 : 1), it->end());
206 if ( ! pj_gridlist_merge_gridfile(name, stream_policy, grids, gridindexes,
207 typename Grids::tag())
210 BOOST_THROW_EXCEPTION( projection_exception(error_failed_to_load_grid) );
215 template <typename Par, typename ProjectionGrids>
216 inline void pj_gridlist_from_nadgrids(Par const& defn, ProjectionGrids & proj_grids)
218 pj_gridlist_from_nadgrids(defn.nadgrids,
219 proj_grids.grids_storage().stream_policy,
220 proj_grids.grids_storage().hgrids,
221 proj_grids.hindexes);
225 } // namespace detail
227 }}} // namespace boost::geometry::projections
229 #endif // BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_PJ_GRIDLIST_HPP