GDAL
ogr_proj_p.h
1 /******************************************************************************
2  *
3  * Project: GDAL
4  * Purpose: Private header
5  * Author: Even Rouault <even dot rouault at spatialys dot com>
6  *
7  ******************************************************************************
8  * Copyright (c) 2018, Even Rouault <even dot rouault at spatialys dot com>
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  ****************************************************************************/
28 
29 #ifndef OGR_PROJ_P_H_INCLUDED
30 #define OGR_PROJ_P_H_INCLUDED
31 
32 #include "proj.h"
33 
34 #include "cpl_mem_cache.h"
35 
36 #include <unordered_map>
37 #include <memory>
38 #include <utility>
39 
42 PJ_CONTEXT *OSRGetProjTLSContext();
43 void OSRCleanupTLSContext();
44 
45 class OSRProjTLSCache
46 {
47  struct OSRPJDeleter
48  {
49  void operator()(PJ *pj) const
50  {
51  proj_destroy(pj);
52  }
53  };
54 
55  typedef std::unique_ptr<PJ, OSRPJDeleter> UniquePtrPJ;
56 
57  struct EPSGCacheKey
58  {
59  int nCode_;
60  bool bUseNonDeprecated_;
61  bool bAddTOWGS84_;
62 
63  EPSGCacheKey(int nCode, bool bUseNonDeprecated, bool bAddTOWGS84)
64  : nCode_(nCode), bUseNonDeprecated_(bUseNonDeprecated),
65  bAddTOWGS84_(bAddTOWGS84)
66  {
67  }
68 
69  bool operator==(const EPSGCacheKey &other) const
70  {
71  return nCode_ == other.nCode_ &&
72  bUseNonDeprecated_ == other.bUseNonDeprecated_ &&
73  bAddTOWGS84_ == other.bAddTOWGS84_;
74  }
75  };
76 
77  struct EPSGCacheKeyHasher
78  {
79  std::size_t operator()(const EPSGCacheKey &k) const
80  {
81  return k.nCode_ | ((k.bUseNonDeprecated_ ? 1 : 0) << 16) |
82  ((k.bAddTOWGS84_ ? 1 : 0) << 17);
83  }
84  };
85 
86  PJ_CONTEXT *m_tlsContext =
87  nullptr; // never use it directly. use GetPJContext()
88  lru11::Cache<EPSGCacheKey, UniquePtrPJ, lru11::NullLock,
89  std::unordered_map<EPSGCacheKey,
90  typename std::list<lru11::KeyValuePair<
91  EPSGCacheKey, UniquePtrPJ>>::iterator,
92  EPSGCacheKeyHasher>>
93  m_oCacheEPSG{};
94  lru11::Cache<std::string, UniquePtrPJ> m_oCacheWKT{};
95 
96  PJ_CONTEXT *GetPJContext();
97 
98  OSRProjTLSCache(const OSRProjTLSCache &) = delete;
99  OSRProjTLSCache &operator=(const OSRProjTLSCache &) = delete;
100 
101  public:
102  explicit OSRProjTLSCache(PJ_CONTEXT *tlsContext) : m_tlsContext(tlsContext)
103  {
104  }
105 
106  void clear();
107 
108  PJ *GetPJForEPSGCode(int nCode, bool bUseNonDeprecated, bool bAddTOWGS84);
109  void CachePJForEPSGCode(int nCode, bool bUseNonDeprecated, bool bAddTOWGS84,
110  PJ *pj);
111 
112  PJ *GetPJForWKT(const std::string &wkt);
113  void CachePJForWKT(const std::string &wkt, PJ *pj);
114 };
115 
116 OSRProjTLSCache *OSRGetProjTLSCache();
117 
118 void OGRCTDumpStatistics();
119 
120 void OSRCTCleanCache();
121 
124 #endif