QGIS API Documentation 4.1.0-Master (60fea48833c)
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 , mPj()
57 , mPjParentContext( nullptr )
58 , mProj4( other.mProj4 )
59 , mWktPreferred( other.mWktPreferred )
60 , mAxisInvertedDirty( other.mAxisInvertedDirty )
61 , mAxisInverted( other.mAxisInverted )
62 , mProjLock {}
63 , mProjObjects()
64 {}
65
66 ~QgsCoordinateReferenceSystemPrivate()
67 {
68 QgsReadWriteLocker locker( mProjLock, QgsReadWriteLocker::Read );
69 if ( !mProjObjects.empty() || mPj )
70 {
71 locker.changeMode( QgsReadWriteLocker::Write );
72 cleanPjObjects();
73 }
74 }
75
77 long mSrsId = 0;
78
80 QString mDescription;
81
83 QString mProjectionAcronym;
84
86 QString mEllipsoidAcronym;
87
88 PJ_TYPE mProjType = PJ_TYPE::PJ_TYPE_UNKNOWN;
89
91 bool mIsGeographic = false;
92
95
97 long mSRID = 0;
98
100 QString mAuthId;
101
103 bool mIsValid = false;
104
106 double mCoordinateEpoch = std::numeric_limits< double >::quiet_NaN();
107
108 // this is the "master" proj object, to be used as a template for new proj objects created on different threads ONLY.
109 // Always use threadLocalProjObject() instead of this.
110
111 private:
113 PJ_CONTEXT *mPjParentContext = nullptr;
114
115 void cleanPjObjects()
116 {
117 // During destruction of PJ* objects, the errno is set in the underlying
118 // context. Consequently the context attached to the PJ* must still exist !
119 // Which is not necessarily the case currently unfortunately. So
120 // create a temporary dummy context, and attach it to the PJ* before destroying
121 // it
122 PJ_CONTEXT *tmpContext = proj_context_create();
123 for ( auto it = mProjObjects.begin(); it != mProjObjects.end(); ++it )
124 {
125 proj_assign_context( it.value(), tmpContext );
126 proj_destroy( it.value() );
127 }
128 mProjObjects.clear();
129 if ( mPj )
130 {
131 proj_assign_context( mPj.get(), tmpContext );
132 mPj.reset();
133 }
134 proj_context_destroy( tmpContext );
135 }
136
137 public:
138 void setPj( QgsProjUtils::proj_pj_unique_ptr obj )
139 {
140 const QgsReadWriteLocker locker( mProjLock, QgsReadWriteLocker::Write );
141 cleanPjObjects();
142
143 mPj = std::move( obj );
144 mPjParentContext = QgsProjContext::get();
145
146 if ( mPj )
147 {
148 mProjType = proj_get_type( mPj.get() );
149 }
150 else
151 {
152 mProjType = PJ_TYPE_UNKNOWN;
153 }
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 PJ *threadLocalProjObject() const
178 {
179 QgsReadWriteLocker locker( mProjLock, QgsReadWriteLocker::Read );
180 if ( !mPj )
181 return nullptr;
182
183 PJ_CONTEXT *context = QgsProjContext::get();
184 const QMap< PJ_CONTEXT *, PJ * >::const_iterator it = mProjObjects.constFind( context );
185
186 if ( it != mProjObjects.constEnd() )
187 {
188 return it.value();
189 }
190
191 // proj object doesn't exist yet, so we need to create
192 locker.changeMode( QgsReadWriteLocker::Write );
193
194 PJ *res = proj_clone( context, mPj.get() );
195 mProjObjects.insert( context, res );
196 return res;
197 }
198
199 // Only meant to be called by QgsCoordinateReferenceSystem::removeFromCacheObjectsBelongingToCurrentThread()
200 bool removeObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context )
201 {
202 const QgsReadWriteLocker locker( mProjLock, QgsReadWriteLocker::Write );
203
204 const QMap< PJ_CONTEXT *, PJ * >::iterator it = mProjObjects.find( pj_context );
205 if ( it != mProjObjects.end() )
206 {
207 proj_destroy( it.value() );
208 mProjObjects.erase( it );
209 }
210
211 if ( mPjParentContext == pj_context )
212 {
213 mPj.reset();
214 mPjParentContext = nullptr;
215 }
216
217 return mProjObjects.isEmpty();
218 }
219
220 private:
221 QgsCoordinateReferenceSystemPrivate &operator=( const QgsCoordinateReferenceSystemPrivate & ) = delete;
222};
223
225
226#endif //QGSCOORDINATEREFERENCESYSTEM_PRIVATE_H
DistanceUnit
Units of distance.
Definition qgis.h:5170
@ Unknown
Unknown distance unit.
Definition qgis.h:5220
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