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  typedef std::unique_ptr<PJ, OSRPJDeleter> UniquePtrPJ;
55 
56  struct EPSGCacheKey
57  {
58  int nCode_;
59  bool bUseNonDeprecated_;
60  bool bAddTOWGS84_;
61 
62  EPSGCacheKey(int nCode, bool bUseNonDeprecated, bool bAddTOWGS84)
63  : nCode_(nCode), bUseNonDeprecated_(bUseNonDeprecated),
64  bAddTOWGS84_(bAddTOWGS84)
65  {
66  }
67 
68  bool operator==(const EPSGCacheKey &other) const
69  {
70  return nCode_ == other.nCode_ &&
71  bUseNonDeprecated_ == other.bUseNonDeprecated_ &&
72  bAddTOWGS84_ == other.bAddTOWGS84_;
73  }
74  };
75  struct EPSGCacheKeyHasher
76  {
77  std::size_t operator()(const EPSGCacheKey &k) const
78  {
79  return k.nCode_ | ((k.bUseNonDeprecated_ ? 1 : 0) << 16) |
80  ((k.bAddTOWGS84_ ? 1 : 0) << 17);
81  }
82  };
83 
84  PJ_CONTEXT *m_tlsContext =
85  nullptr; // never use it directly. use GetPJContext()
86  lru11::Cache<EPSGCacheKey, UniquePtrPJ, lru11::NullLock,
87  std::unordered_map<EPSGCacheKey,
88  typename std::list<lru11::KeyValuePair<
89  EPSGCacheKey, UniquePtrPJ>>::iterator,
90  EPSGCacheKeyHasher>>
91  m_oCacheEPSG{};
92  lru11::Cache<std::string, UniquePtrPJ> m_oCacheWKT{};
93 
94  PJ_CONTEXT *GetPJContext();
95 
96  OSRProjTLSCache(const OSRProjTLSCache &) = delete;
97  OSRProjTLSCache &operator=(const OSRProjTLSCache &) = delete;
98 
99  public:
100  explicit OSRProjTLSCache(PJ_CONTEXT *tlsContext) : m_tlsContext(tlsContext)
101  {
102  }
103 
104  void clear();
105 
106  PJ *GetPJForEPSGCode(int nCode, bool bUseNonDeprecated, bool bAddTOWGS84);
107  void CachePJForEPSGCode(int nCode, bool bUseNonDeprecated, bool bAddTOWGS84,
108  PJ *pj);
109 
110  PJ *GetPJForWKT(const std::string &wkt);
111  void CachePJForWKT(const std::string &wkt, PJ *pj);
112 };
113 
114 OSRProjTLSCache *OSRGetProjTLSCache();
115 
116 void OGRCTDumpStatistics();
117 
118 void OSRCTCleanCache();
119 
122 #endif