22 QReadWriteLock QgsEllipsoidUtils::sEllipsoidCacheLock;
23 QHash< QString, QgsEllipsoidUtils::EllipsoidParameters > QgsEllipsoidUtils::sEllipsoidCache;
24 QReadWriteLock QgsEllipsoidUtils::sDefinitionCacheLock;
25 QList< QgsEllipsoidUtils::EllipsoidDefinition > QgsEllipsoidUtils::sDefinitionCache;
26 static bool sDisableCache =
false;
32 sEllipsoidCacheLock.lockForRead();
35 QHash< QString, EllipsoidParameters >::const_iterator cacheIt = sEllipsoidCache.constFind( ellipsoid );
36 if ( cacheIt != sEllipsoidCache.constEnd() )
40 sEllipsoidCacheLock.unlock();
44 sEllipsoidCacheLock.unlock();
53 if ( ellipsoid.startsWith( QLatin1String(
"PARAMETER" ) ) )
55 QStringList paramList = ellipsoid.split(
':' );
56 bool semiMajorOk, semiMinorOk;
57 double semiMajor = paramList[1].toDouble( & semiMajorOk );
58 double semiMinor = paramList[2].toDouble( & semiMinorOk );
59 if ( semiMajorOk && semiMinorOk )
71 sEllipsoidCacheLock.lockForWrite();
74 sEllipsoidCache.insert( ellipsoid, params );
76 sEllipsoidCacheLock.unlock();
82 QString radius, parameter2;
100 QString sql =
"select radius, parameter2 from tbl_ellipsoid where acronym='" + ellipsoid +
'\'';
101 statement = database.
prepare( sql, result );
103 if ( result == SQLITE_OK )
105 if ( statement.
step() == SQLITE_ROW )
112 if ( radius.isEmpty() || parameter2.isEmpty() )
114 QgsDebugMsg( QStringLiteral(
"setEllipsoid: no row in tbl_ellipsoid for acronym '%1'" ).arg( ellipsoid ) );
115 params.
valid =
false;
116 sEllipsoidCacheLock.lockForWrite();
117 if ( !sDisableCache )
119 sEllipsoidCache.insert( ellipsoid, params );
121 sEllipsoidCacheLock.unlock();
126 if ( radius.left( 2 ) == QLatin1String(
"a=" ) )
127 params.
semiMajor = radius.midRef( 2 ).toDouble();
130 QgsDebugMsg( QStringLiteral(
"setEllipsoid: wrong format of radius field: '%1'" ).arg( radius ) );
131 params.
valid =
false;
132 sEllipsoidCacheLock.lockForWrite();
133 if ( !sDisableCache )
135 sEllipsoidCache.insert( ellipsoid, params );
137 sEllipsoidCacheLock.unlock();
144 if ( parameter2.left( 2 ) == QLatin1String(
"b=" ) )
146 params.
semiMinor = parameter2.midRef( 2 ).toDouble();
149 else if ( parameter2.left( 3 ) == QLatin1String(
"rf=" ) )
156 QgsDebugMsg( QStringLiteral(
"setEllipsoid: wrong format of parameter2 field: '%1'" ).arg( parameter2 ) );
157 params.
valid =
false;
158 sEllipsoidCacheLock.lockForWrite();
159 if ( !sDisableCache )
161 sEllipsoidCache.insert( ellipsoid, params );
163 sEllipsoidCacheLock.unlock();
171 QString proj4 =
"+proj=longlat +ellps=" + ellipsoid +
" +no_defs";
177 if ( destCRS.
srsid() == 0 )
179 QString name = QStringLiteral(
" * %1 (%2)" )
180 .arg( QObject::tr(
"Generated CRS",
"A CRS automatically generated from layer info get this prefix for description" ),
187 params.
crs = destCRS;
189 sEllipsoidCacheLock.lockForWrite();
190 if ( !sDisableCache )
192 sEllipsoidCache.insert( ellipsoid, params );
194 sEllipsoidCacheLock.unlock();
201 sDefinitionCacheLock.lockForRead();
202 if ( !sDefinitionCache.isEmpty() )
204 QList<QgsEllipsoidUtils::EllipsoidDefinition> defs = sDefinitionCache;
205 sDefinitionCacheLock.unlock();
208 sDefinitionCacheLock.unlock();
210 sDefinitionCacheLock.lockForWrite();
216 QList<QgsEllipsoidUtils::EllipsoidDefinition> defs;
225 Q_ASSERT( result == 0 );
229 QString sql = QStringLiteral(
"select acronym, name from tbl_ellipsoid order by name" );
230 statement = database.
prepare( sql, result );
232 if ( result == SQLITE_OK )
234 while ( statement.
step() == SQLITE_ROW )
247 if ( !sDisableCache )
249 sDefinitionCache = defs;
251 sDefinitionCacheLock.unlock();
268 sEllipsoidCacheLock.lockForWrite();
269 sDefinitionCacheLock.lockForWrite();
271 if ( !sDisableCache )
274 sDisableCache =
true;
275 sEllipsoidCache.clear();
276 sDefinitionCache.clear();
279 sDefinitionCacheLock.unlock();
280 sEllipsoidCacheLock.unlock();
static QgsCoordinateReferenceSystem fromProj4(const QString &proj4)
Creates a CRS from a proj4 style formatted string.
static void invalidateCache(bool disableCache=false)
Clears the internal cache used.
bool useCustomParameters
Whether custom parameters alone should be used (semiMajor/semiMinor only)
Contains definition of an ellipsoid.
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
long srsid() const
Returns the internal CRS ID, if available.
sqlite3_statement_unique_ptr prepare(const QString &sql, int &resultCode) const
Prepares a sql statement, returning the result.
Contains parameters for an ellipsoid.
static EllipsoidParameters ellipsoidParameters(const QString &ellipsoid)
Returns the parameters for the specified ellipsoid.
long saveAsUserCrs(const QString &name)
Save the proj4-string as a custom CRS.
QgsCoordinateReferenceSystem crs
Associated coordinate reference system.
bool valid
Whether ellipsoid parameters are valid.
#define QgsDebugMsgLevel(str, level)
int step()
Steps to the next record in the statement, returning the sqlite3 result code.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
QString acronym
Acronym for ellipsoid.
double semiMinor
Semi-minor axis.
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
int open_v2(const QString &path, int flags, const char *zVfs)
Opens the database at the specified file path.
This class represents a coordinate reference system (CRS).
double inverseFlattening
Inverse flattening.
static QString srsDatabaseFilePath()
Returns the path to the srs.db file.
QgsEllipsoidUtils::EllipsoidParameters parameters
Ellipsoid parameters.
static QList< QgsEllipsoidUtils::EllipsoidDefinition > definitions()
Returns a list of the definitions for all known ellipsoids from the internal ellipsoid database...
QString columnAsText(int column) const
Returns the column value from the current statement row as a string.
static QStringList acronyms()
Returns a list of all known ellipsoid acronyms from the internal ellipsoid database.
QString errorMessage() const
Returns the most recent error message encountered by the database.
QString description
Description of ellipsoid.
double semiMajor
Semi-major axis.
QString toProj4() const
Returns a Proj4 string representation of this CRS.