Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:35:38

0001 // Boost.Geometry
0002 // This file is manually converted from PROJ4
0003 
0004 // This file was modified by Oracle on 2018, 2019.
0005 // Modifications copyright (c) 2018-2019, Oracle and/or its affiliates.
0006 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
0007 
0008 // Use, modification and distribution is subject to the Boost Software License,
0009 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0010 // http://www.boost.org/LICENSE_1_0.txt)
0011 
0012 // This file is converted from PROJ4, http://trac.osgeo.org/proj
0013 // PROJ4 is originally written by Gerald Evenden (then of the USGS)
0014 // PROJ4 is maintained by Frank Warmerdam
0015 // This file was converted to Geometry Library by Adam Wulkiewicz
0016 
0017 // Original copyright notice:
0018 // Author:   Frank Warmerdam, warmerdam@pobox.com
0019 
0020 // Copyright (c) 2000, Frank Warmerdam
0021 
0022 // Permission is hereby granted, free of charge, to any person obtaining a
0023 // copy of this software and associated documentation files (the "Software"),
0024 // to deal in the Software without restriction, including without limitation
0025 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
0026 // and/or sell copies of the Software, and to permit persons to whom the
0027 // Software is furnished to do so, subject to the following conditions:
0028 
0029 // The above copyright notice and this permission notice shall be included
0030 // in all copies or substantial portions of the Software.
0031 
0032 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0033 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0034 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
0035 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0036 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0037 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
0038 // DEALINGS IN THE SOFTWARE.
0039 
0040 #ifndef BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_PJ_GRIDLIST_HPP
0041 #define BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_PJ_GRIDLIST_HPP
0042 
0043 
0044 #include <boost/geometry/srs/projections/exception.hpp>
0045 #include <boost/geometry/srs/projections/grids.hpp>
0046 #include <boost/geometry/srs/projections/impl/pj_gridinfo.hpp>
0047 #include <boost/geometry/srs/projections/impl/pj_strerrno.hpp>
0048 #include <boost/geometry/srs/projections/par_data.hpp>
0049 
0050 
0051 namespace boost { namespace geometry { namespace projections
0052 {
0053 
0054 namespace detail
0055 {
0056 
0057 /************************************************************************/
0058 /*                       pj_gridlist_merge_grid()                       */
0059 /*                                                                      */
0060 /*      Find/load the named gridfile and merge it into the              */
0061 /*      last_nadgrids_list.                                             */
0062 /************************************************************************/
0063 
0064 // Originally one function, here divided into several functions
0065 // with overloads for various types of grids and stream policies
0066 
0067 inline bool pj_gridlist_find_all(std::string const& gridname,
0068                                  pj_gridinfo const& grids,
0069                                  std::vector<std::size_t> & gridindexes)
0070 {
0071     bool result = false;
0072     for (std::size_t i = 0 ; i < grids.size() ; ++i)
0073     {
0074         if (grids[i].gridname == gridname)
0075         {
0076             result = true;
0077             gridindexes.push_back(i);
0078         }
0079     }
0080     return result;
0081 }
0082 
0083 // Fill container with sequentially increasing numbers
0084 inline void pj_gridlist_add_seq_inc(std::vector<std::size_t> & gridindexes,
0085                                     std::size_t first, std::size_t last)
0086 {
0087     gridindexes.reserve(gridindexes.size() + (last - first));
0088     for ( ; first < last ; ++first)
0089     {
0090         gridindexes.push_back(first);
0091     }
0092 }
0093 
0094 // Generic stream policy and standard grids
0095 template <typename StreamPolicy, typename Grids>
0096 inline bool pj_gridlist_merge_gridfile(std::string const& gridname,
0097                                        StreamPolicy const& stream_policy,
0098                                        Grids & grids,
0099                                        std::vector<std::size_t> & gridindexes,
0100                                        grids_tag)
0101 {
0102     // Try to find in the existing list of loaded grids.  Add all
0103     // matching grids as with NTv2 we can get many grids from one
0104     // file (one shared gridname).
0105     if (pj_gridlist_find_all(gridname, grids.gridinfo, gridindexes))
0106         return true;
0107 
0108     std::size_t orig_size = grids.gridinfo.size();
0109 
0110     // Try to load the named grid.
0111     typename StreamPolicy::stream_type is;
0112     stream_policy.open(is, gridname);
0113 
0114     if (! pj_gridinfo_init(gridname, is, grids.gridinfo))
0115     {
0116         return false;
0117     }
0118 
0119     // Add the grid now that it is loaded.
0120     pj_gridlist_add_seq_inc(gridindexes, orig_size, grids.gridinfo.size());
0121 
0122     return true;
0123 }
0124 
0125 // Generic stream policy and shared grids
0126 template <typename StreamPolicy, typename SharedGrids>
0127 inline bool pj_gridlist_merge_gridfile(std::string const& gridname,
0128                                        StreamPolicy const& stream_policy,
0129                                        SharedGrids & grids,
0130                                        std::vector<std::size_t> & gridindexes,
0131                                        shared_grids_tag)
0132 {
0133     // Try to find in the existing list of loaded grids.  Add all
0134     // matching grids as with NTv2 we can get many grids from one
0135     // file (one shared gridname).
0136     {
0137         typename SharedGrids::read_locked lck_grids(grids);
0138 
0139         if (pj_gridlist_find_all(gridname, lck_grids.gridinfo, gridindexes))
0140             return true;
0141     }
0142 
0143     // Try to load the named grid.
0144     typename StreamPolicy::stream_type is;
0145     stream_policy.open(is, gridname);
0146 
0147     pj_gridinfo new_grids;
0148 
0149     if (! pj_gridinfo_init(gridname, is, new_grids))
0150     {
0151         return false;
0152     }
0153 
0154     // Add the grid now that it is loaded.
0155 
0156     std::size_t orig_size = 0;
0157     std::size_t new_size = 0;
0158 
0159     {
0160         typename SharedGrids::write_locked lck_grids(grids);
0161 
0162         // Try to find in the existing list of loaded grids again
0163         // in case other thread already added it.
0164         if (pj_gridlist_find_all(gridname, lck_grids.gridinfo, gridindexes))
0165             return true;
0166 
0167         orig_size = lck_grids.gridinfo.size();
0168         new_size = orig_size + new_grids.size();
0169 
0170         lck_grids.gridinfo.resize(new_size);
0171         for (std::size_t i = 0 ; i < new_grids.size() ; ++ i)
0172             new_grids[i].swap(lck_grids.gridinfo[i + orig_size]);
0173     }
0174 
0175     pj_gridlist_add_seq_inc(gridindexes, orig_size, new_size);
0176 
0177     return true;
0178 }
0179 
0180 
0181 /************************************************************************/
0182 /*                     pj_gridlist_from_nadgrids()                      */
0183 /*                                                                      */
0184 /*      This functions loads the list of grids corresponding to a       */
0185 /*      particular nadgrids string into a list, and returns it. The     */
0186 /*      list is kept around till a request is made with a different     */
0187 /*      string in order to cut down on the string parsing cost, and     */
0188 /*      the cost of building the list of tables each time.              */
0189 /************************************************************************/
0190 
0191 template <typename StreamPolicy, typename Grids>
0192 inline void pj_gridlist_from_nadgrids(srs::detail::nadgrids const& nadgrids,
0193                                       StreamPolicy const& stream_policy,
0194                                       Grids & grids,
0195                                       std::vector<std::size_t> & gridindexes)
0196 
0197 {
0198     // Loop processing names out of nadgrids one at a time.
0199     for (srs::detail::nadgrids::const_iterator it = nadgrids.begin() ;
0200             it != nadgrids.end() ; ++it)
0201     {
0202         bool required = (*it)[0] != '@';
0203 
0204         std::string name(it->begin() + (required ? 0 : 1), it->end());
0205 
0206         if ( ! pj_gridlist_merge_gridfile(name, stream_policy, grids, gridindexes,
0207                                           typename Grids::tag())
0208           && required )
0209         {
0210             BOOST_THROW_EXCEPTION( projection_exception(error_failed_to_load_grid) );
0211         }
0212     }
0213 }
0214 
0215 template <typename Par, typename ProjectionGrids>
0216 inline void pj_gridlist_from_nadgrids(Par const& defn, ProjectionGrids & proj_grids)
0217 {
0218     pj_gridlist_from_nadgrids(defn.nadgrids,
0219                               proj_grids.grids_storage().stream_policy,
0220                               proj_grids.grids_storage().hgrids,
0221                               proj_grids.hindexes);
0222 }
0223 
0224 
0225 } // namespace detail
0226 
0227 }}} // namespace boost::geometry::projections
0228 
0229 #endif // BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_PJ_GRIDLIST_HPP