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 * SPDX-License-Identifier: MIT
11 ****************************************************************************/
12
13#ifndef OGR_PROJ_P_H_INCLUDED
14#define OGR_PROJ_P_H_INCLUDED
15
16#include "proj.h"
17
18#include "cpl_mem_cache.h"
19
20#include <unordered_map>
21#include <memory>
22#include <utility>
23
26PJ_CONTEXT CPL_DLL *OSRGetProjTLSContext();
27void OSRCleanupTLSContext();
28
29class OSRProjTLSCache
30{
31 struct OSRPJDeleter
32 {
33 void operator()(PJ *pj) const
34 {
35 proj_destroy(pj);
36 }
37 };
38
39 typedef std::unique_ptr<PJ, OSRPJDeleter> UniquePtrPJ;
40
41 struct EPSGCacheKey
42 {
43 int nCode_;
44 bool bUseNonDeprecated_;
45 bool bAddTOWGS84_;
46
47 EPSGCacheKey(int nCode, bool bUseNonDeprecated, bool bAddTOWGS84)
48 : nCode_(nCode), bUseNonDeprecated_(bUseNonDeprecated),
49 bAddTOWGS84_(bAddTOWGS84)
50 {
51 }
52
53 bool operator==(const EPSGCacheKey &other) const
54 {
55 return nCode_ == other.nCode_ &&
56 bUseNonDeprecated_ == other.bUseNonDeprecated_ &&
57 bAddTOWGS84_ == other.bAddTOWGS84_;
58 }
59 };
60
61 struct EPSGCacheKeyHasher
62 {
63 std::size_t operator()(const EPSGCacheKey &k) const
64 {
65 return k.nCode_ | ((k.bUseNonDeprecated_ ? 1 : 0) << 16) |
66 ((k.bAddTOWGS84_ ? 1 : 0) << 17);
67 }
68 };
69
70 PJ_CONTEXT *m_tlsContext =
71 nullptr; // never use it directly. use GetPJContext()
72 lru11::Cache<EPSGCacheKey, UniquePtrPJ, lru11::NullLock,
73 std::unordered_map<EPSGCacheKey,
74 typename std::list<lru11::KeyValuePair<
75 EPSGCacheKey, UniquePtrPJ>>::iterator,
76 EPSGCacheKeyHasher>>
77 m_oCacheEPSG{};
78 lru11::Cache<std::string, UniquePtrPJ> m_oCacheWKT{};
79
80 PJ_CONTEXT *GetPJContext();
81
82 OSRProjTLSCache(const OSRProjTLSCache &) = delete;
83 OSRProjTLSCache &operator=(const OSRProjTLSCache &) = delete;
84
85 public:
86 explicit OSRProjTLSCache(PJ_CONTEXT *tlsContext) : m_tlsContext(tlsContext)
87 {
88 }
89
90 void clear();
91
92 PJ *GetPJForEPSGCode(int nCode, bool bUseNonDeprecated, bool bAddTOWGS84);
93 void CachePJForEPSGCode(int nCode, bool bUseNonDeprecated, bool bAddTOWGS84,
94 PJ *pj);
95
96 PJ *GetPJForWKT(const std::string &wkt);
97 void CachePJForWKT(const std::string &wkt, PJ *pj);
98};
99
100OSRProjTLSCache *OSRGetProjTLSCache();
101
102void OGRCTDumpStatistics();
103
104void OSRCTCleanCache();
105
108#endif