QGIS API Documentation 4.1.0-Master (60fea48833c)
Loading...
Searching...
No Matches
qgsellipsoidutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsellipsoidutils.cpp
3 ----------------------
4 Date : April 2017
5 Copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsellipsoidutils.h"
17
18#include <mutex>
19#include <proj.h>
20#include <sqlite3.h>
21
22#include "qgsapplication.h"
23#include "qgscelestialbody.h"
25#include "qgslogger.h"
26#include "qgsmessagelog.h"
27#include "qgsprojutils.h"
28#include "qgsreadwritelocker.h"
29#include "qgsruntimeprofiler.h"
30
31#include <QCollator>
32#include <QString>
33
34using namespace Qt::StringLiterals;
35
36Q_GLOBAL_STATIC( QReadWriteLock, sEllipsoidCacheLock )
37typedef QHash< QString, QgsEllipsoidUtils::EllipsoidParameters > EllipsoidParamCache;
38Q_GLOBAL_STATIC( EllipsoidParamCache, sEllipsoidCache )
39
40Q_GLOBAL_STATIC( QReadWriteLock, sDefinitionCacheLock );
41typedef QList< QgsEllipsoidUtils::EllipsoidDefinition > EllipsoidDefinitionCache;
43
44static bool sDisableCache = false;
45
47{
48 // maps older QGIS ellipsoid acronyms to proj acronyms/names
49 static const QMap< QString, QString > sProj6EllipsoidAcronymMap {
50 { "clrk80", "clrk80ign" },
51 { "Adrastea2000", "ESRI:107909" },
52 { "Amalthea2000", "ESRI:107910" },
53 { "Ananke2000", "ESRI:107911" },
54 { "Ariel2000", "ESRI:107945" },
55 { "Atlas2000", "ESRI:107926" },
56 { "Belinda2000", "ESRI:107946" },
57 { "Bianca2000", "ESRI:107947" },
58 { "Callisto2000", "ESRI:107912" },
59 { "Calypso2000", "ESRI:107927" },
60 { "Carme2000", "ESRI:107913" },
61 { "Charon2000", "ESRI:107970" },
62 { "Cordelia2000", "ESRI:107948" },
63 { "Cressida2000", "ESRI:107949" },
64 { "Deimos2000", "ESRI:107906" },
65 { "Desdemona2000", "ESRI:107950" },
66 { "Despina2000", "ESRI:107961" },
67 { "Dione2000", "ESRI:107928" },
68 { "Elara2000", "ESRI:107914" },
69 { "Enceladus2000", "ESRI:107929" },
70 { "Epimetheus2000", "ESRI:107930" },
71 { "Europa2000", "ESRI:107915" },
72 { "Galatea2000", "ESRI:107962" },
73 { "Ganymede2000", "ESRI:107916" },
74 { "Helene2000", "ESRI:107931" },
75 { "Himalia2000", "ESRI:107917" },
76 { "Hyperion2000", "ESRI:107932" },
77 { "Iapetus2000", "ESRI:107933" },
78 { "Io2000", "ESRI:107918" },
79 { "Janus2000", "ESRI:107934" },
80 { "Juliet2000", "ESRI:107951" },
81 { "Jupiter2000", "ESRI:107908" },
82 { "Larissa2000", "ESRI:107963" },
83 { "Leda2000", "ESRI:107919" },
84 { "Lysithea2000", "ESRI:107920" },
85 { "Mars2000", "ESRI:107905" },
86 { "Mercury2000", "ESRI:107900" },
87 { "Metis2000", "ESRI:107921" },
88 { "Mimas2000", "ESRI:107935" },
89 { "Miranda2000", "ESRI:107952" },
90 { "Moon2000", "ESRI:107903" },
91 { "Naiad2000", "ESRI:107964" },
92 { "Neptune2000", "ESRI:107960" },
93 { "Nereid2000", "ESRI:107965" },
94 { "Oberon2000", "ESRI:107953" },
95 { "Ophelia2000", "ESRI:107954" },
96 { "Pan2000", "ESRI:107936" },
97 { "Pandora2000", "ESRI:107937" },
98 { "Pasiphae2000", "ESRI:107922" },
99 { "Phobos2000", "ESRI:107907" },
100 { "Phoebe2000", "ESRI:107938" },
101 { "Pluto2000", "ESRI:107969" },
102 { "Portia2000", "ESRI:107955" },
103 { "Prometheus2000", "ESRI:107939" },
104 { "Proteus2000", "ESRI:107966" },
105 { "Puck2000", "ESRI:107956" },
106 { "Rhea2000", "ESRI:107940" },
107 { "Rosalind2000", "ESRI:107957" },
108 { "Saturn2000", "ESRI:107925" },
109 { "Sinope2000", "ESRI:107923" },
110 { "Telesto2000", "ESRI:107941" },
111 { "Tethys2000", "ESRI:107942" },
112 { "Thalassa2000", "ESRI:107967" },
113 { "Thebe2000", "ESRI:107924" },
114 { "Titan2000", "ESRI:107943" },
115 { "Titania2000", "ESRI:107958" },
116 { "Triton2000", "ESRI:107968" },
117 { "Umbriel2000", "ESRI:107959" },
118 { "Uranus2000", "ESRI:107944" },
119 { "Venus2000", "ESRI:107902" },
120 { "IGNF:ELG053", "EPSG:7030" },
121 { "IGNF:ELG052", "EPSG:7043" },
122 { "IGNF:ELG102", "EPSG:7043" },
123 { "WGS66", "ESRI:107001" },
124 { "plessis", "EPSG:7027" },
125 { "IGNF:ELG017", "EPSG:7027" },
126 { "mod_airy", "EPSG:7002" },
127 { "IGNF:ELG037", "EPSG:7019" },
128 { "IGNF:ELG108", "EPSG:7036" },
129 { "cape", "EPSG:7034" },
130 { "IGNF:ELG010", "EPSG:7011" },
131 { "IGNF:ELG003", "EPSG:7012" },
132 { "IGNF:ELG004", "EPSG:7008" },
133 { "GSK2011", "EPSG:1025" },
134 { "airy", "EPSG:7001" },
135 { "aust_SA", "EPSG:7003" },
136 { "bessel", "EPSG:7004" },
137 { "clrk66", "EPSG:7008" },
138 { "clrk80ign", "EPSG:7011" },
139 { "evrst30", "EPSG:7015" },
140 { "evrstSS", "EPSG:7016" },
141 { "evrst48", "EPSG:7018" },
142 { "GRS80", "EPSG:7019" },
143 { "helmert", "EPSG:7020" },
144 { "intl", "EPSG:7022" },
145 { "krass", "EPSG:7024" },
146 { "NWL9D", "EPSG:7025" },
147 { "WGS84", "EPSG:7030" },
148 { "GRS67", "EPSG:7036" },
149 { "WGS72", "EPSG:7043" },
150 { "bess_nam", "EPSG:7046" },
151 { "IAU76", "EPSG:7049" },
152 { "sphere", "EPSG:7052" },
153 { "hough", "EPSG:7053" },
154 { "evrst69", "EPSG:7056" },
155 { "fschr60", "ESRI:107002" },
156 { "fschr68", "ESRI:107003" },
157 { "fschr60m", "ESRI:107004" },
158 { "walbeck", "ESRI:107007" },
159 { "IGNF:ELG001", "EPSG:7022" },
160 { "engelis", "EPSG:7054" },
161 { "evrst56", "EPSG:7044" },
162 { "SEasia", "ESRI:107004" },
163 { "SGS85", "EPSG:7054" },
164 { "andrae", "PROJ:ANDRAE" },
165 { "clrk80", "EPSG:7034" },
166 { "CPM", "PROJ:CPM" },
167 { "delmbr", "PROJ:DELMBR" },
168 { "Earth2000", "PROJ:EARTH2000" },
169 { "kaula", "PROJ:KAULA" },
170 { "lerch", "PROJ:LERCH" },
171 { "MERIT", "PROJ:MERIT" },
172 { "mprts", "PROJ:MPRTS" },
173 { "new_intl", "PROJ:NEW_INTL" },
174 { "WGS60", "PROJ:WGS60" }
175 };
176
177 QString ellipsoid = e;
178 // ensure ellipsoid database is populated when first called
179 static std::once_flag initialized;
180 std::call_once( initialized, [] {
181 const QgsScopedRuntimeProfile profile( QObject::tr( "Initialize ellipsoids" ) );
182 ( void ) definitions();
183 } );
184
185 ellipsoid = sProj6EllipsoidAcronymMap.value( ellipsoid, ellipsoid ); // silently upgrade older QGIS acronyms to proj acronyms
186
187 // check cache
188 {
189 const QgsReadWriteLocker locker( *sEllipsoidCacheLock(), QgsReadWriteLocker::Read );
190 if ( !sDisableCache )
191 {
192 const QHash< QString, EllipsoidParameters >::const_iterator cacheIt = sEllipsoidCache()->constFind( ellipsoid );
193 if ( cacheIt != sEllipsoidCache()->constEnd() )
194 {
195 // found a match in the cache
196 QgsEllipsoidUtils::EllipsoidParameters params = cacheIt.value();
197 return params;
198 }
199 }
200 }
201
202 EllipsoidParameters params;
203
204 // Check if we have a custom projection, and set from text string.
205 // Format is "PARAMETER:<semi-major axis>:<semi minor axis>
206 // Numbers must be with (optional) decimal point and no other separators (C locale)
207 // Distances in meters. Flattening is calculated.
208 if ( ellipsoid.startsWith( "PARAMETER"_L1 ) )
209 {
210 QStringList paramList = ellipsoid.split( ':' );
211 bool semiMajorOk, semiMinorOk;
212 const double semiMajor = paramList[1].toDouble( &semiMajorOk );
213 const double semiMinor = paramList[2].toDouble( &semiMinorOk );
214 if ( semiMajorOk && semiMinorOk )
215 {
216 params.semiMajor = semiMajor;
217 params.semiMinor = semiMinor;
218 params.inverseFlattening = semiMajor / ( semiMajor - semiMinor );
219 params.useCustomParameters = true;
220 params.crs.createFromProj( u"+proj=longlat +a=%1 +b=%2 +no_defs +type=crs"_s.arg( params.semiMajor, 0, 'g', 17 ).arg( params.semiMinor, 0, 'g', 17 ) );
221 }
222 else
223 {
224 params.valid = false;
225 }
226
227 const QgsReadWriteLocker locker( *sEllipsoidCacheLock(), QgsReadWriteLocker::Write );
228 if ( !sDisableCache )
229 {
230 sEllipsoidCache()->insert( ellipsoid, params );
231 }
232 return params;
233 }
234 params.valid = false;
235
236 const QgsReadWriteLocker l( *sEllipsoidCacheLock(), QgsReadWriteLocker::Write );
237 if ( !sDisableCache )
238 {
239 sEllipsoidCache()->insert( ellipsoid, params );
240 }
241
242 return params;
243}
244
245QList<QgsEllipsoidUtils::EllipsoidDefinition> QgsEllipsoidUtils::definitions()
246{
247 QgsReadWriteLocker defLocker( *sDefinitionCacheLock(), QgsReadWriteLocker::Read );
248 if ( !sDefinitionCache()->isEmpty() )
249 {
250 return *sDefinitionCache();
251 }
253
254 QList<QgsEllipsoidUtils::EllipsoidDefinition> defs;
255
256 QgsReadWriteLocker locker( *sEllipsoidCacheLock(), QgsReadWriteLocker::Write );
257
258 PJ_CONTEXT *context = QgsProjContext::get();
259 if ( PROJ_STRING_LIST authorities = proj_get_authorities_from_database( context ) )
260 {
261 PROJ_STRING_LIST authoritiesIt = authorities;
262 while ( char *authority = *authoritiesIt )
263 {
264 if ( PROJ_STRING_LIST codes = proj_get_codes_from_database( context, authority, PJ_TYPE_ELLIPSOID, 0 ) )
265 {
266 PROJ_STRING_LIST codesIt = codes;
267 while ( char *code = *codesIt )
268 {
269 const QgsProjUtils::proj_pj_unique_ptr ellipsoid( proj_create_from_database( context, authority, code, PJ_CATEGORY_ELLIPSOID, 0, nullptr ) );
270 if ( ellipsoid )
271 {
273 QString name = QString( proj_get_name( ellipsoid.get() ) );
274 def.acronym = u"%1:%2"_s.arg( authority, code );
275 name.replace( '_', ' ' );
276 def.description = u"%1 (%2:%3)"_s.arg( name, authority, code );
277
278 def.celestialBodyName = proj_get_celestial_body_name( context, ellipsoid.get() );
279
280 double semiMajor, semiMinor, invFlattening;
281 int semiMinorComputed = 0;
282 if ( proj_ellipsoid_get_parameters( context, ellipsoid.get(), &semiMajor, &semiMinor, &semiMinorComputed, &invFlattening ) )
283 {
284 def.parameters.semiMajor = semiMajor;
285 def.parameters.semiMinor = semiMinor;
286 def.parameters.inverseFlattening = invFlattening;
287 if ( !semiMinorComputed )
288 def.parameters.crs.createFromProj( u"+proj=longlat +a=%1 +b=%2 +no_defs +type=crs"_s.arg( def.parameters.semiMajor, 0, 'g', 17 ).arg( def.parameters.semiMinor, 0, 'g', 17 ), false );
289 else if ( !qgsDoubleNear( def.parameters.inverseFlattening, 0.0 ) )
290 def.parameters.crs.createFromProj( u"+proj=longlat +a=%1 +rf=%2 +no_defs +type=crs"_s.arg( def.parameters.semiMajor, 0, 'g', 17 ).arg( def.parameters.inverseFlattening, 0, 'g', 17 ), false );
291 else
292 def.parameters.crs.createFromProj( u"+proj=longlat +a=%1 +no_defs +type=crs"_s.arg( def.parameters.semiMajor, 0, 'g', 17 ), false );
293 }
294 else
295 {
296 def.parameters.valid = false;
297 }
298
299 defs << def;
300 if ( !sDisableCache )
301 {
302 sEllipsoidCache()->insert( def.acronym, def.parameters );
303 }
304 }
305
306 codesIt++;
307 }
308 proj_string_list_destroy( codes );
309 }
310
311 authoritiesIt++;
312 }
313 proj_string_list_destroy( authorities );
314 }
315 locker.unlock();
316
317 QCollator collator;
318 collator.setCaseSensitivity( Qt::CaseInsensitive );
319 std::sort( defs.begin(), defs.end(), [&collator]( const EllipsoidDefinition &a, const EllipsoidDefinition &b ) { return collator.compare( a.description, b.description ) < 0; } );
320 if ( !sDisableCache )
321 {
322 *sDefinitionCache() = defs;
323 }
324
325 return defs;
326}
327
329{
330 QStringList result;
331 const QList<QgsEllipsoidUtils::EllipsoidDefinition> defs = definitions();
332 result.reserve( defs.size() );
333 for ( const QgsEllipsoidUtils::EllipsoidDefinition &def : defs )
334 {
335 result << def.acronym;
336 }
337 return result;
338}
339
344
345void QgsEllipsoidUtils::invalidateCache( bool disableCache )
346{
347 const QgsReadWriteLocker locker1( *sEllipsoidCacheLock(), QgsReadWriteLocker::Write );
348 const QgsReadWriteLocker locker2( *sDefinitionCacheLock(), QgsReadWriteLocker::Write );
349
350 if ( !sDisableCache )
351 {
352 if ( disableCache )
353 sDisableCache = true;
354 sEllipsoidCache()->clear();
355 sDefinitionCache()->clear();
356 }
357}
static QgsCoordinateReferenceSystemRegistry * coordinateReferenceSystemRegistry()
Returns the application's coordinate reference system (CRS) registry, which handles known CRS definit...
QList< QgsCelestialBody > celestialBodies() const
Returns a list of all known celestial bodies.
bool createFromProj(const QString &projString, bool identify=true)
Sets this CRS by passing it a PROJ style formatted string.
Contains utility functions for working with ellipsoids and querying the ellipsoid database.
static QList< QgsEllipsoidUtils::EllipsoidDefinition > definitions()
Returns a list of the definitions for all known ellipsoids from the internal ellipsoid database.
static QList< QgsCelestialBody > celestialBodies()
Returns a list of all known celestial bodies.
static void invalidateCache(bool disableCache=false)
Clears the internal cache used.
static EllipsoidParameters ellipsoidParameters(const QString &ellipsoid)
Returns the parameters for the specified ellipsoid.
static QStringList acronyms()
Returns a list of all known ellipsoid acronyms from the internal ellipsoid database.
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.
A convenience class that simplifies locking and unlocking QReadWriteLocks.
@ Write
Lock for write.
void unlock()
Unlocks the lock.
void changeMode(Mode mode)
Change the mode of the lock to mode.
Scoped object for logging of the runtime for a single operation or group of operations.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6975
struct pj_ctx PJ_CONTEXT
QList< QgsEllipsoidUtils::EllipsoidDefinition > EllipsoidDefinitionCache
QHash< QString, QgsEllipsoidUtils::EllipsoidParameters > EllipsoidParamCache
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)
Contains definition of an ellipsoid.
QString acronym
authority:code for QGIS builds with proj version 6 or greater, or custom acronym for ellipsoid for ea...
QString celestialBodyName
Name of the associated celestial body (e.g.
QString description
Description of ellipsoid.
QgsEllipsoidUtils::EllipsoidParameters parameters
Ellipsoid parameters.
Contains parameters for an ellipsoid.
double semiMajor
Semi-major axis, in meters.
bool valid
Whether ellipsoid parameters are valid.
double semiMinor
Semi-minor axis, in meters.
QgsCoordinateReferenceSystem crs
Associated coordinate reference system.
double inverseFlattening
Inverse flattening.
bool useCustomParameters
Whether custom parameters alone should be used (semiMajor/semiMinor only).