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