Skip to content

Commit 72cb49c

Browse files
committed
Improve performance of ellipsoid lookups on Proj >= 6
1 parent 6e57bfa commit 72cb49c

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/core/qgsellipsoidutils.cpp

+36-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#if PROJ_VERSION_MAJOR>=6
2424
#include <proj.h>
25+
#include <mutex>
2526
#endif
2627

2728
QReadWriteLock QgsEllipsoidUtils::sEllipsoidCacheLock;
@@ -31,6 +32,15 @@ QList< QgsEllipsoidUtils::EllipsoidDefinition > QgsEllipsoidUtils::sDefinitionCa
3132

3233
QgsEllipsoidUtils::EllipsoidParameters QgsEllipsoidUtils::ellipsoidParameters( const QString &ellipsoid )
3334
{
35+
#if PROJ_VERSION_MAJOR >= 6
36+
// ensure ellipsoid database is populated when first called
37+
static std::once_flag initialized;
38+
std::call_once( initialized, [ = ]
39+
{
40+
( void )definitions();
41+
} );
42+
#endif
43+
3444
// check cache
3545
sEllipsoidCacheLock.lockForRead();
3646
QHash< QString, EllipsoidParameters >::const_iterator cacheIt = sEllipsoidCache.constFind( ellipsoid );
@@ -73,7 +83,9 @@ QgsEllipsoidUtils::EllipsoidParameters QgsEllipsoidUtils::ellipsoidParameters( c
7383
return params;
7484
}
7585

86+
#if PROJ_VERSION_MAJOR< 6
7687
// cache miss - get from database
88+
// NOT REQUIRED FOR PROJ >= 6 -- we populate known types once by calling definitions() above
7789

7890
QString radius, parameter2;
7991
//
@@ -177,6 +189,13 @@ QgsEllipsoidUtils::EllipsoidParameters QgsEllipsoidUtils::ellipsoidParameters( c
177189
sEllipsoidCache.insert( ellipsoid, params );
178190
sEllipsoidCacheLock.unlock();
179191
return params;
192+
#else
193+
params.valid = false;
194+
sEllipsoidCacheLock.lockForWrite();
195+
sEllipsoidCache.insert( ellipsoid, params );
196+
sEllipsoidCacheLock.unlock();
197+
return params;
198+
#endif
180199
}
181200

182201
QList<QgsEllipsoidUtils::EllipsoidDefinition> QgsEllipsoidUtils::definitions()
@@ -194,6 +213,8 @@ QList<QgsEllipsoidUtils::EllipsoidDefinition> QgsEllipsoidUtils::definitions()
194213
QList<QgsEllipsoidUtils::EllipsoidDefinition> defs;
195214

196215
#if PROJ_VERSION_MAJOR>=6
216+
sEllipsoidCacheLock.lockForWrite();
217+
197218
// use proj to get ellipsoids
198219
const PJ_ELLPS *ellipsoid = proj_list_ellps();
199220
while ( ellipsoid->name )
@@ -218,13 +239,26 @@ QList<QgsEllipsoidUtils::EllipsoidDefinition> QgsEllipsoidUtils::definitions()
218239
}
219240

220241
QgsCoordinateReferenceSystem crs = QgsCoordinateReferenceSystem::fromProj4( QStringLiteral( "+proj=longlat +ellps=%1 +no_defs" ).arg( def.acronym ) );
221-
if ( crs.isValid() )
222-
def.parameters.crs = crs;
242+
if ( crs.srsid() == 0 )
243+
{
244+
//TODO: createFromProj4 used to save to the user database any new CRS
245+
// this behavior was changed in order to separate creation and saving.
246+
// Not sure if it necessary to save it here, should be checked by someone
247+
// familiar with the code (should also give a more descriptive name to the generated CRS)
248+
QString name = QStringLiteral( " * %1 (%2)" )
249+
.arg( QObject::tr( "Generated CRS", "A CRS automatically generated from layer info get this prefix for description" ),
250+
crs.toProj4() );
251+
crs.saveAsUserCrs( name );
252+
}
253+
def.parameters.crs = crs;
223254

224255
defs << def;
225256

257+
sEllipsoidCache.insert( QString( ellipsoid->id ), def.parameters );
258+
226259
ellipsoid++;
227260
}
261+
sEllipsoidCacheLock.unlock();
228262

229263

230264
#else

0 commit comments

Comments
 (0)