QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgscoordinatereferencesystem_p.h
Go to the documentation of this file.
1/***************************************************************************
2 qgscoordinatereferencesystem_p.h
3
4 --------------------------------
5 begin : 2016
6 copyright : (C) 2016 by Nyall Dawson
7 email : nyall dot dawson at gmail dot com
8***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18#ifndef QGSCOORDINATEREFERENCESYSTEM_PRIVATE_H
19#define QGSCOORDINATEREFERENCESYSTEM_PRIVATE_H
20
22
23//
24// W A R N I N G
25// -------------
26//
27// This file is not part of the QGIS API. It exists purely as an
28// implementation detail. This header file may change from version to
29// version without notice, or even be removed.
30//
31
33
34#include <proj.h>
35#include "qgsprojutils.h"
36#include "qgsreadwritelocker.h"
37
38#ifdef DEBUG
39typedef struct OGRSpatialReferenceHS *OGRSpatialReferenceH;
40#else
41typedef void *OGRSpatialReferenceH;
42#endif
43
44class QgsCoordinateReferenceSystemPrivate : public QSharedData
45{
46 public:
47
48 explicit QgsCoordinateReferenceSystemPrivate()
49 {
50 }
51
52 QgsCoordinateReferenceSystemPrivate( const QgsCoordinateReferenceSystemPrivate &other )
53 : QSharedData( other )
54 , mSrsId( other.mSrsId )
55 , mDescription( other.mDescription )
56 , mProjectionAcronym( other.mProjectionAcronym )
57 , mEllipsoidAcronym( other.mEllipsoidAcronym )
58 , mIsGeographic( other.mIsGeographic )
59 , mMapUnits( other.mMapUnits )
60 , mSRID( other.mSRID )
61 , mAuthId( other.mAuthId )
62 , mIsValid( other.mIsValid )
63 , mCoordinateEpoch( other.mCoordinateEpoch )
64 , mPj()
65 , mPjParentContext( nullptr )
66 , mProj4( other.mProj4 )
67 , mWktPreferred( other.mWktPreferred )
68 , mAxisInvertedDirty( other.mAxisInvertedDirty )
69 , mAxisInverted( other.mAxisInverted )
70 , mProjLock{}
71 , mProjObjects()
72 {
73 }
74
75 ~QgsCoordinateReferenceSystemPrivate()
76 {
78 if ( !mProjObjects.empty() || mPj )
79 {
80 locker.changeMode( QgsReadWriteLocker::Write );
81 cleanPjObjects();
82 }
83 }
84
86 long mSrsId = 0;
87
89 QString mDescription;
90
92 QString mProjectionAcronym;
93
95 QString mEllipsoidAcronym;
96
98 bool mIsGeographic = false;
99
101 Qgis::DistanceUnit mMapUnits = Qgis::DistanceUnit::Unknown;
102
104 long mSRID = 0;
105
107 QString mAuthId;
108
110 bool mIsValid = false;
111
113 double mCoordinateEpoch = std::numeric_limits< double >::quiet_NaN();
114
115 // this is the "master" proj object, to be used as a template for new proj objects created on different threads ONLY.
116 // Always use threadLocalProjObject() instead of this.
117
118 private:
120 PJ_CONTEXT *mPjParentContext = nullptr;
121
122 void cleanPjObjects()
123 {
124
125 // During destruction of PJ* objects, the errno is set in the underlying
126 // context. Consequently the context attached to the PJ* must still exist !
127 // Which is not necessarily the case currently unfortunately. So
128 // create a temporary dummy context, and attach it to the PJ* before destroying
129 // it
130 PJ_CONTEXT *tmpContext = proj_context_create();
131 for ( auto it = mProjObjects.begin(); it != mProjObjects.end(); ++it )
132 {
133 proj_assign_context( it.value(), tmpContext );
134 proj_destroy( it.value() );
135 }
136 mProjObjects.clear();
137 if ( mPj )
138 {
139 proj_assign_context( mPj.get(), tmpContext );
140 mPj.reset();
141 }
142 proj_context_destroy( tmpContext );
143 }
144
145 public:
146
147 void setPj( QgsProjUtils::proj_pj_unique_ptr obj )
148 {
149 const QgsReadWriteLocker locker( mProjLock, QgsReadWriteLocker::Write );
150 cleanPjObjects();
151
152 mPj = std::move( obj );
153 mPjParentContext = QgsProjContext::get();
154 }
155
156 bool hasPj() const
157 {
158 const QgsReadWriteLocker locker( mProjLock, QgsReadWriteLocker::Read );
159 return static_cast< bool >( mPj );
160 }
161
162 mutable QString mProj4;
163
164 mutable QString mWktPreferred;
165
167 mutable bool mAxisInvertedDirty = false;
168
170 mutable bool mAxisInverted = false;
171
172 private:
173 mutable QReadWriteLock mProjLock{};
174 mutable QMap < PJ_CONTEXT *, PJ * > mProjObjects{};
175
176 public:
177
178 PJ *threadLocalProjObject() const
179 {
180 QgsReadWriteLocker locker( mProjLock, QgsReadWriteLocker::Read );
181 if ( !mPj )
182 return nullptr;
183
184 PJ_CONTEXT *context = QgsProjContext::get();
185 const QMap < PJ_CONTEXT *, PJ * >::const_iterator it = mProjObjects.constFind( context );
186
187 if ( it != mProjObjects.constEnd() )
188 {
189 return it.value();
190 }
191
192 // proj object doesn't exist yet, so we need to create
193 locker.changeMode( QgsReadWriteLocker::Write );
194
195 PJ *res = proj_clone( context, mPj.get() );
196 mProjObjects.insert( context, res );
197 return res;
198 }
199
200 // Only meant to be called by QgsCoordinateReferenceSystem::removeFromCacheObjectsBelongingToCurrentThread()
201 bool removeObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context )
202 {
203 const QgsReadWriteLocker locker( mProjLock, QgsReadWriteLocker::Write );
204
205 const QMap < PJ_CONTEXT *, PJ * >::iterator it = mProjObjects.find( pj_context );
206 if ( it != mProjObjects.end() )
207 {
208 proj_destroy( it.value() );
209 mProjObjects.erase( it );
210 }
211
212 if ( mPjParentContext == pj_context )
213 {
214 mPj.reset();
215 mPjParentContext = nullptr;
216 }
217
218 return mProjObjects.isEmpty();
219 }
220
221 private:
222 QgsCoordinateReferenceSystemPrivate &operator= ( const QgsCoordinateReferenceSystemPrivate & ) = delete;
223
224};
225
227
228#endif //QGSCOORDINATEREFERENCESYSTEM_PRIVATE_H
DistanceUnit
Units of distance.
Definition: qgis.h:3310
static PJ_CONTEXT * get()
Returns a thread local instance of a proj context, safe for use in the current thread.
std::unique_ptr< PJ, ProjPJDeleter > proj_pj_unique_ptr
Scoped Proj PJ object.
Definition: qgsprojutils.h:141
The QgsReadWriteLocker class is a convenience class that simplifies locking and unlocking QReadWriteL...
@ Write
Lock for write.
@ Read
Lock for read.
void * OGRSpatialReferenceH
struct PJconsts PJ
struct projCtx_t PJ_CONTEXT