1 // Boost.Geometry
2 // This file is manually converted from PROJ4
3 
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
7 
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)
11 
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
16 
17 // Original copyright notice:
18 // Author:   Frank Warmerdam, warmerdam@pobox.com
19 
20 // Copyright (c) 2000, Frank Warmerdam
21 
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:
28 
29 // The above copyright notice and this permission notice shall be included
30 // in all copies or substantial portions of the Software.
31 
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.
39 
40 #ifndef BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_PJ_GRIDLIST_HPP
41 #define BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_PJ_GRIDLIST_HPP
42 
43 
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>
49 
50 
51 namespace boost { namespace geometry { namespace projections
52 {
53 
54 namespace detail
55 {
56 
57 /************************************************************************/
58 /*                       pj_gridlist_merge_grid()                       */
59 /*                                                                      */
60 /*      Find/load the named gridfile and merge it into the              */
61 /*      last_nadgrids_list.                                             */
62 /************************************************************************/
63 
64 // Originally one function, here divided into several functions
65 // with overloads for various types of grids and stream policies
66 
pj_gridlist_find_all(std::string const & gridname,pj_gridinfo const & grids,std::vector<std::size_t> & gridindexes)67 inline bool pj_gridlist_find_all(std::string const& gridname,
68                                  pj_gridinfo const& grids,
69                                  std::vector<std::size_t> & gridindexes)
70 {
71     bool result = false;
72     for (std::size_t i = 0 ; i < grids.size() ; ++i)
73     {
74         if (grids[i].gridname == gridname)
75         {
76             result = true;
77             gridindexes.push_back(i);
78         }
79     }
80     return result;
81 }
82 
83 // Fill container with sequentially increasing numbers
pj_gridlist_add_seq_inc(std::vector<std::size_t> & gridindexes,std::size_t first,std::size_t last)84 inline void pj_gridlist_add_seq_inc(std::vector<std::size_t> & gridindexes,
85                                     std::size_t first, std::size_t last)
86 {
87     gridindexes.reserve(gridindexes.size() + (last - first));
88     for ( ; first < last ; ++first)
89     {
90         gridindexes.push_back(first);
91     }
92 }
93 
94 // Generic stream policy and standard grids
95 template <typename StreamPolicy, typename Grids>
pj_gridlist_merge_gridfile(std::string const & gridname,StreamPolicy const & stream_policy,Grids & grids,std::vector<std::size_t> & gridindexes,grids_tag)96 inline bool pj_gridlist_merge_gridfile(std::string const& gridname,
97                                        StreamPolicy const& stream_policy,
98                                        Grids & grids,
99                                        std::vector<std::size_t> & gridindexes,
100                                        grids_tag)
101 {
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))
106         return true;
107 
108     std::size_t orig_size = grids.gridinfo.size();
109 
110     // Try to load the named grid.
111     typename StreamPolicy::stream_type is;
112     stream_policy.open(is, gridname);
113 
114     if (! pj_gridinfo_init(gridname, is, grids.gridinfo))
115     {
116         return false;
117     }
118 
119     // Add the grid now that it is loaded.
120     pj_gridlist_add_seq_inc(gridindexes, orig_size, grids.gridinfo.size());
121 
122     return true;
123 }
124 
125 // Generic stream policy and shared grids
126 template <typename StreamPolicy, typename SharedGrids>
pj_gridlist_merge_gridfile(std::string const & gridname,StreamPolicy const & stream_policy,SharedGrids & grids,std::vector<std::size_t> & gridindexes,shared_grids_tag)127 inline bool pj_gridlist_merge_gridfile(std::string const& gridname,
128                                        StreamPolicy const& stream_policy,
129                                        SharedGrids & grids,
130                                        std::vector<std::size_t> & gridindexes,
131                                        shared_grids_tag)
132 {
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).
136     {
137         typename SharedGrids::read_locked lck_grids(grids);
138 
139         if (pj_gridlist_find_all(gridname, lck_grids.gridinfo, gridindexes))
140             return true;
141     }
142 
143     // Try to load the named grid.
144     typename StreamPolicy::stream_type is;
145     stream_policy.open(is, gridname);
146 
147     pj_gridinfo new_grids;
148 
149     if (! pj_gridinfo_init(gridname, is, new_grids))
150     {
151         return false;
152     }
153 
154     // Add the grid now that it is loaded.
155 
156     std::size_t orig_size = 0;
157     std::size_t new_size = 0;
158 
159     {
160         typename SharedGrids::write_locked lck_grids(grids);
161 
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))
165             return true;
166 
167         orig_size = lck_grids.gridinfo.size();
168         new_size = orig_size + new_grids.size();
169 
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]);
173     }
174 
175     pj_gridlist_add_seq_inc(gridindexes, orig_size, new_size);
176 
177     return true;
178 }
179 
180 
181 /************************************************************************/
182 /*                     pj_gridlist_from_nadgrids()                      */
183 /*                                                                      */
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 /************************************************************************/
190 
191 template <typename StreamPolicy, typename Grids>
pj_gridlist_from_nadgrids(srs::detail::nadgrids const & nadgrids,StreamPolicy const & stream_policy,Grids & grids,std::vector<std::size_t> & gridindexes)192 inline void pj_gridlist_from_nadgrids(srs::detail::nadgrids const& nadgrids,
193                                       StreamPolicy const& stream_policy,
194                                       Grids & grids,
195                                       std::vector<std::size_t> & gridindexes)
196 
197 {
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)
201     {
202         bool required = (*it)[0] != '@';
203 
204         std::string name(it->begin() + (required ? 0 : 1), it->end());
205 
206         if ( ! pj_gridlist_merge_gridfile(name, stream_policy, grids, gridindexes,
207                                           typename Grids::tag())
208           && required )
209         {
210             BOOST_THROW_EXCEPTION( projection_exception(error_failed_to_load_grid) );
211         }
212     }
213 }
214 
215 template <typename Par, typename ProjectionGrids>
pj_gridlist_from_nadgrids(Par const & defn,ProjectionGrids & proj_grids)216 inline void pj_gridlist_from_nadgrids(Par const& defn, ProjectionGrids & proj_grids)
217 {
218     pj_gridlist_from_nadgrids(defn.nadgrids,
219                               proj_grids.grids_storage().stream_policy,
220                               proj_grids.grids_storage().hgrids,
221                               proj_grids.hindexes);
222 }
223 
224 
225 } // namespace detail
226 
227 }}} // namespace boost::geometry::projections
228 
229 #endif // BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_PJ_GRIDLIST_HPP
230