Imported Upstream version 1.71.0
[platform/upstream/boost.git] / boost / geometry / srs / projections / impl / pj_gridlist_shared.hpp
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_SHARED_HPP
41 #define BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_PJ_GRIDLIST_SHARED_HPP
42
43
44 #include <boost/geometry/srs/projections/shared_grids.hpp>
45 #include <boost/geometry/srs/projections/impl/pj_gridinfo.hpp>
46 #include <boost/geometry/srs/projections/impl/pj_gridlist.hpp>
47
48
49 namespace boost { namespace geometry { namespace projections
50 {
51
52 namespace detail
53 {
54
55
56 /************************************************************************/
57 /*                       pj_gridlist_merge_grid()                       */
58 /*                                                                      */
59 /*      Find/load the named gridfile and merge it into the              */
60 /*      last_nadgrids_list.                                             */
61 /************************************************************************/
62
63 // Generic stream policy and shared grids
64 template <typename StreamPolicy>
65 inline bool pj_gridlist_merge_gridfile(std::string const& gridname,
66                                        StreamPolicy const& stream_policy,
67                                        shared_grids & grids,
68                                        std::vector<std::size_t> & gridindexes)
69 {
70     // Try to find in the existing list of loaded grids.  Add all
71     // matching grids as with NTv2 we can get many grids from one
72     // file (one shared gridname).    
73     {
74         boost::shared_lock<boost::shared_mutex> lock(grids.mutex);
75
76         if (pj_gridlist_find_all(gridname, grids.gridinfo, gridindexes))
77             return true;
78     }
79
80     // Try to load the named grid.
81     typename StreamPolicy::stream_type is;
82     stream_policy.open(is, gridname);
83
84     pj_gridinfo new_grids;
85     
86     if (! pj_gridinfo_init(gridname, is, new_grids))
87     {
88         return false;
89     }
90
91     // Add the grid now that it is loaded.
92
93     std::size_t orig_size = 0;
94     std::size_t new_size = 0;
95
96     {
97         boost::unique_lock<boost::shared_mutex> lock(grids.mutex);
98
99         // Try to find in the existing list of loaded grids again
100         // in case other thread already added it.
101         if (pj_gridlist_find_all(gridname, grids.gridinfo, gridindexes))
102             return true;
103
104         orig_size = grids.gridinfo.size();
105         new_size = orig_size + new_grids.size();
106
107         grids.gridinfo.resize(new_size);
108         for (std::size_t i = 0 ; i < new_grids.size() ; ++ i)
109             new_grids[i].swap(grids.gridinfo[i + orig_size]);
110     }
111     
112     pj_gridlist_add_seq_inc(gridindexes, orig_size, new_size);
113     
114     return true;
115 }
116
117
118 } // namespace detail
119
120 }}} // namespace boost::geometry::projections
121
122 #endif // BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_PJ_GRIDLIST_SHARED_HPP