QGIS API Documentation 4.3.0-Master (7c7bd4d7018)
Loading...
Searching...
No Matches
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
32#include <proj.h>
33
35#include "qgsprojutils.h"
36#include "qgsreadwritelocker.h"
37
38class QgsCoordinateReferenceSystemPrivate : public QSharedData
39{
40 public:
41 explicit QgsCoordinateReferenceSystemPrivate() {}
42
43 QgsCoordinateReferenceSystemPrivate( const QgsCoordinateReferenceSystemPrivate &other )
44 : QSharedData( other )
45 , mSrsId( other.mSrsId )
46 , mDescription( other.mDescription )
47 , mProjectionAcronym( other.mProjectionAcronym )
48 , mEllipsoidAcronym( other.mEllipsoidAcronym )
49 , mProjType( other.mProjType )
50 , mIsGeographic( other.mIsGeographic )
51 , mMapUnits( other.mMapUnits )
52 , mSRID( other.mSRID )
53 , mAuthId( other.mAuthId )
54 , mIsValid( other.mIsValid )
55 , mCoordinateEpoch( other.mCoordinateEpoch )
56 , mTopocentricBaseCrs( other.mTopocentricBaseCrs ? std::make_unique<QgsCoordinateReferenceSystem>( *other.mTopocentricBaseCrs ) : nullptr )
57 , mPj()
58 , mPjParentContext( nullptr )
59 , mProj4( other.mProj4 )
60 , mWktPreferred( other.mWktPreferred )
61 , mAxisInvertedDirty( other.mAxisInvertedDirty )
62 , mAxisInverted( other.mAxisInverted )
63 , mProjLock {}
64 , mProjObjects()
65 {}
66
67 ~QgsCoordinateReferenceSystemPrivate()
68 {
69 QgsReadWriteLocker locker( mProjLock, QgsReadWriteLocker::Read );
70 if ( !mProjObjects.empty() || mPj )
71 {
72 locker.changeMode( QgsReadWriteLocker::Write );
73 cleanPjObjects();
74 }
75 }
76
78 long mSrsId = 0;
79
81 QString mDescription;
82
84 QString mProjectionAcronym;
85
87 QString mEllipsoidAcronym;
88
89 PJ_TYPE mProjType = PJ_TYPE::PJ_TYPE_UNKNOWN;
90
92 bool mIsGeographic = false;
93
96
98 long mSRID = 0;
99
101 QString mAuthId;
102
104 bool mIsValid = false;
105
107 double mCoordinateEpoch = std::numeric_limits< double >::quiet_NaN();
108
110 std::unique_ptr<QgsCoordinateReferenceSystem> mTopocentricBaseCrs;
111
112 // this is the "master" proj object, to be used as a template for new proj objects created on different threads ONLY.
113 // Always use threadLocalProjObject() instead of this.
114
115 private:
117 PJ_CONTEXT *mPjParentContext = nullptr;
118
119 void cleanPjObjects()
120 {
121 // During destruction of PJ* objects, the errno is set in the underlying
122 // context. Consequently the context attached to the PJ* must still exist !
123 // Which is not necessarily the case currently unfortunately. So
124 // create a temporary dummy context, and attach it to the PJ* before destroying
125 // it
126 PJ_CONTEXT *tmpContext = proj_context_create();
127 for ( auto it = mProjObjects.begin(); it != mProjObjects.end(); ++it )
128 {
129 proj_assign_context( it.value(), tmpContext );
130 proj_destroy( it.value() );
131 }
132 mProjObjects.clear();
133 if ( mPj )
134 {
135 proj_assign_context( mPj.get(), tmpContext );
136 mPj.reset();
137 }
138 proj_context_destroy( tmpContext );
139 }
140
141 public:
142 void setPj( QgsProjUtils::proj_pj_unique_ptr obj )
143 {
144 const QgsReadWriteLocker locker( mProjLock, QgsReadWriteLocker::Write );
145 cleanPjObjects();
146
147 mPj = std::move( obj );
148 mPjParentContext = QgsProjContext::get();
149
150 if ( mPj )
151 {
152 mProjType = proj_get_type( mPj.get() );
153 }
154 else
155 {
156 mProjType = PJ_TYPE_UNKNOWN;
157 }
158 }
159
160 bool hasPj() const
161 {
162 const QgsReadWriteLocker locker( mProjLock, QgsReadWriteLocker::Read );
163 return static_cast< bool >( mPj );
164 }
165
166 mutable QString mProj4;
167
168 mutable QString mWktPreferred;
169
171 mutable bool mAxisInvertedDirty = false;
172
174 mutable bool mAxisInverted = false;
175
176 private:
177 mutable QReadWriteLock mProjLock {};
178 mutable QMap< PJ_CONTEXT *, PJ * > mProjObjects {};
179
180 public:
181 PJ *threadLocalProjObject() const
182 {
183 QgsReadWriteLocker locker( mProjLock, QgsReadWriteLocker::Read );
184 if ( !mPj )
185 return nullptr;
186
187 PJ_CONTEXT *context = QgsProjContext::get();
188 const QMap< PJ_CONTEXT *, PJ * >::const_iterator it = mProjObjects.constFind( context );
189
190 if ( it != mProjObjects.constEnd() )
191 {
192 return it.value();
193 }
194
195 // proj object doesn't exist yet, so we need to create
196 locker.changeMode( QgsReadWriteLocker::Write );
197
198 PJ *res = proj_clone( context, mPj.get() );
199 mProjObjects.insert( context, res );
200 return res;
201 }
202
203 // Only meant to be called by QgsCoordinateReferenceSystem::removeFromCacheObjectsBelongingToCurrentThread()
204 bool removeObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context )
205 {
206 const QgsReadWriteLocker locker( mProjLock, QgsReadWriteLocker::Write );
207
208 const QMap< PJ_CONTEXT *, PJ * >::iterator it = mProjObjects.find( pj_context );
209 if ( it != mProjObjects.end() )
210 {
211 proj_destroy( it.value() );
212 mProjObjects.erase( it );
213 }
214
215 if ( mPjParentContext == pj_context )
216 {
217 mPj.reset();
218 mPjParentContext = nullptr;
219 }
220
221 return mProjObjects.isEmpty();
222 }
223
224 private:
225 QgsCoordinateReferenceSystemPrivate &operator=( const QgsCoordinateReferenceSystemPrivate & ) = delete;
226};
227
229
230#endif //QGSCOORDINATEREFERENCESYSTEM_PRIVATE_H
DistanceUnit
Units of distance.
Definition qgis.h:5437
@ Unknown
Unknown distance unit.
Definition qgis.h:5487
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.
@ Write
Lock for write.
struct pj_ctx PJ_CONTEXT
struct PJconsts PJ