19#include "moc_qgscoordinatereferencesystem.cpp"
33#include <QRegularExpression>
53#include <proj_experimental.h>
56#include <ogr_srs_api.h>
68bool QgsCoordinateReferenceSystem::sDisableSrIdCache =
false;
72bool QgsCoordinateReferenceSystem::sDisableOgcCache =
false;
76bool QgsCoordinateReferenceSystem::sDisableProjCache =
false;
80bool QgsCoordinateReferenceSystem::sDisableWktCache =
false;
84bool QgsCoordinateReferenceSystem::sDisableSrsIdCache =
false;
88bool QgsCoordinateReferenceSystem::sDisableStringCache =
false;
97 if (
const char *proj4src = proj_as_proj_string(
QgsProjContext::get(), boundCrs.get(), PJ_PROJ_4,
nullptr ) )
99 return QString( proj4src );
116 d =
new QgsCoordinateReferenceSystemPrivate();
122 d =
new QgsCoordinateReferenceSystemPrivate();
130 , mValidationHint( srs.mValidationHint )
131 , mNativeFormat( srs.mNativeFormat )
138 mValidationHint = srs.mValidationHint;
139 mNativeFormat = srs.mNativeFormat;
149 const auto constDbs = dbs;
150 for (
const QString &db : constDbs )
152 QFileInfo myInfo( db );
153 if ( !myInfo.exists() )
163 int result = openDatabase( db, database );
164 if ( result != SQLITE_OK )
170 QString sql = QStringLiteral(
"select srs_id from tbl_srs" );
172 statement = database.
prepare( sql, rc );
176 int ret = statement.
step();
178 if ( ret == SQLITE_DONE )
184 if ( ret == SQLITE_ROW )
190 QgsMessageLog::logMessage( QObject::tr(
"SQLite error: %2\nSQL: %1" ).arg( sql, sqlite3_errmsg( database.get() ) ), QObject::tr(
"SpatiaLite" ) );
195 std::sort( results.begin(), results.end() );
251 if ( horizontalObj && verticalObj )
258 QStringList formattedErrorList;
259 for (
const QString &rawError : std::as_const( errors ) )
261 QString formattedError = rawError;
262 formattedError.replace( QLatin1String(
"proj_create_compound_crs: " ), QString() );
263 formattedErrorList.append( formattedError );
265 error = formattedErrorList.join(
'\n' );
273 if ( !ellipsoidParams.
valid )
315 QgsDebugError( QStringLiteral(
"Unexpected case reached!" ) );
322 if ( definition.isEmpty() )
326 if ( !sDisableStringCache )
328 QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = sStringCache()->constFind( definition );
329 if ( crsIt != sStringCache()->constEnd() )
332 *
this = crsIt.value();
339 const thread_local QRegularExpression reCrsId( QStringLiteral(
"^(epsg|esri|osgeo|ignf|ogc|nkg|zangi|iau_2015|iau2000|postgis|internal|user)\\:(\\w+)$" ), QRegularExpression::CaseInsensitiveOption );
340 QRegularExpressionMatch match = reCrsId.match( definition );
341 if ( match.capturedStart() == 0 )
343 QString authName = match.captured( 1 ).toLower();
344 if ( authName == QLatin1String(
"epsg" ) )
348 else if ( authName == QLatin1String(
"postgis" ) )
350 const long id = match.captured( 2 ).toLong();
355 else if ( authName == QLatin1String(
"esri" )
356 || authName == QLatin1String(
"osgeo" )
357 || authName == QLatin1String(
"ignf" )
358 || authName == QLatin1String(
"zangi" )
359 || authName == QLatin1String(
"iau2000" )
360 || authName == QLatin1String(
"ogc" )
361 || authName == QLatin1String(
"nkg" )
362 || authName == QLatin1String(
"iau_2015" )
369 const long id = match.captured( 2 ).toLong();
377 const thread_local QRegularExpression reCrsStr( QStringLiteral(
"^(?:(wkt|proj4|proj)\\:)?(.+)$" ), QRegularExpression::CaseInsensitiveOption );
378 match = reCrsStr.match( definition );
379 if ( match.capturedStart() == 0 )
381 if ( match.captured( 1 ).startsWith( QLatin1String(
"proj" ), Qt::CaseInsensitive ) )
393 if ( !sDisableStringCache )
394 sStringCache()->insert( definition, *
this );
400 if ( definition.isEmpty() )
404 OGRSpatialReferenceH
crs = OSRNewSpatialReference(
nullptr );
406 if ( OSRSetFromUserInput(
crs, definition.toLocal8Bit().constData() ) == OGRERR_NONE )
409 OSRDestroySpatialReference(
crs );
419 const char *configOld = CPLGetConfigOption(
"GDAL_FIX_ESRI_WKT",
"" );
420 const char *configNew =
"GEOGCS";
422 if ( strcmp( configOld,
"" ) == 0 )
424 CPLSetConfigOption(
"GDAL_FIX_ESRI_WKT", configNew );
425 if ( strcmp( configNew, CPLGetConfigOption(
"GDAL_FIX_ESRI_WKT",
"" ) ) != 0 )
427 .arg( configNew, CPLGetConfigOption(
"GDAL_FIX_ESRI_WKT",
"" ) ) );
428 QgsDebugMsgLevel( QStringLiteral(
"set GDAL_FIX_ESRI_WKT : %1" ).arg( configNew ), 4 );
432 QgsDebugMsgLevel( QStringLiteral(
"GDAL_FIX_ESRI_WKT was already set : %1" ).arg( configNew ), 4 );
442 if ( !sDisableOgcCache )
444 QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = sOgcCache()->constFind(
crs );
445 if ( crsIt != sOgcCache()->constEnd() )
448 *
this = crsIt.value();
454 QString wmsCrs =
crs;
459 const QString authorityLower = authority.toLower();
461 ( authorityLower == QLatin1String(
"user" ) ||
462 authorityLower == QLatin1String(
"custom" ) ||
463 authorityLower == QLatin1String(
"qgis" ) ) )
468 if ( !sDisableOgcCache )
469 sOgcCache()->insert(
crs, *
this );
475 wmsCrs = authority +
':' + code;
479 const QString legacyKey = wmsCrs.toLower();
482 if ( it.key().compare( legacyKey, Qt::CaseInsensitive ) == 0 )
484 const QStringList parts = it.key().split(
':' );
485 const QString auth = parts.at( 0 );
486 const QString code = parts.at( 1 );
487 if ( loadFromAuthCode( auth, code ) )
490 if ( !sDisableOgcCache )
491 sOgcCache()->insert(
crs, *
this );
500 if ( !sDisableOgcCache )
501 sOgcCache()->insert(
crs, *
this );
506 if ( wmsCrs.compare( QLatin1String(
"CRS:27" ), Qt::CaseInsensitive ) == 0 ||
507 wmsCrs.compare( QLatin1String(
"OGC:CRS27" ), Qt::CaseInsensitive ) == 0 )
514 if ( wmsCrs.compare( QLatin1String(
"CRS:83" ), Qt::CaseInsensitive ) == 0 ||
515 wmsCrs.compare( QLatin1String(
"OGC:CRS83" ), Qt::CaseInsensitive ) == 0 )
522 if ( wmsCrs.compare( QLatin1String(
"CRS:84" ), Qt::CaseInsensitive ) == 0 ||
523 wmsCrs.compare( QLatin1String(
"OGC:CRS84" ), Qt::CaseInsensitive ) == 0 )
527 d->mAxisInverted =
false;
528 d->mAxisInvertedDirty =
false;
532 if ( !sDisableOgcCache )
533 sOgcCache()->insert(
crs, *
this );
540 if ( !authority.isEmpty() && !code.isEmpty() && loadFromAuthCode( authority, code ) )
543 if ( !sDisableOgcCache )
544 sOgcCache()->insert(
crs, *
this );
549 if ( !sDisableOgcCache )
559 if ( d->mIsValid || !sCustomSrsValidation )
563 if ( sCustomSrsValidation )
564 sCustomSrsValidation( *
this );
570 if ( !sDisableSrIdCache )
572 QHash< long, QgsCoordinateReferenceSystem >::const_iterator crsIt = sSrIdCache()->constFind(
id );
573 if ( crsIt != sSrIdCache()->constEnd() )
576 *
this = crsIt.value();
585 if ( it.value().endsWith( QStringLiteral(
",%1" ).arg(
id ) ) )
587 const QStringList parts = it.key().split(
':' );
588 const QString auth = parts.at( 0 );
589 const QString code = parts.at( 1 );
590 if ( loadFromAuthCode( auth, code ) )
593 if ( !sDisableSrIdCache )
594 sSrIdCache()->insert(
id, *
this );
604 if ( !sDisableSrIdCache )
605 sSrIdCache()->insert(
id, *
this );
613 if ( !sDisableSrsIdCache )
615 QHash< long, QgsCoordinateReferenceSystem >::const_iterator crsIt = sSrsIdCache()->constFind(
id );
616 if ( crsIt != sSrsIdCache()->constEnd() )
619 *
this = crsIt.value();
628 if ( it.value().startsWith( QString::number(
id ) +
',' ) )
630 const QStringList parts = it.key().split(
':' );
631 const QString auth = parts.at( 0 );
632 const QString code = parts.at( 1 );
633 if ( loadFromAuthCode( auth, code ) )
636 if ( !sDisableSrsIdCache )
637 sSrsIdCache()->insert(
id, *
this );
645 QStringLiteral(
"srs_id" ), QString::number(
id ) );
648 if ( !sDisableSrsIdCache )
649 sSrsIdCache()->insert(
id, *
this );
653bool QgsCoordinateReferenceSystem::loadFromDatabase(
const QString &db,
const QString &expression,
const QString &value )
657 QgsDebugMsgLevel(
"load CRS from " + db +
" where " + expression +
" is " + value, 3 );
659 d->mWktPreferred.clear();
661 QFileInfo myInfo( db );
662 if ( !myInfo.exists() )
672 myResult = openDatabase( db, database );
673 if ( myResult != SQLITE_OK )
690 QString mySql =
"select srs_id,description,projection_acronym,"
691 "ellipsoid_acronym,parameters,srid,auth_name||':'||auth_id,is_geo,wkt "
693 statement = database.
prepare( mySql, myResult );
696 if ( myResult == SQLITE_OK && statement.
step() == SQLITE_ROW )
701 d->mEllipsoidAcronym.clear();
703 d->mWktPreferred.clear();
706 d->mIsGeographic = statement.
columnAsText( 7 ).toInt() != 0;
708 d->mAxisInvertedDirty =
true;
712 d->mAuthId = QStringLiteral(
"USER:%1" ).arg( d->mSrsId );
714 else if ( !d->mAuthId.startsWith( QLatin1String(
"USER:" ), Qt::CaseInsensitive ) )
716 QStringList parts = d->mAuthId.split(
':' );
717 QString auth = parts.at( 0 );
718 QString code = parts.at( 1 );
725 d->mIsValid = d->hasPj();
731 if ( !wkt.isEmpty() )
739 setProjString( d->mProj4 );
749void QgsCoordinateReferenceSystem::removeFromCacheObjectsBelongingToCurrentThread(
PJ_CONTEXT *pj_context )
756 if ( !sDisableSrIdCache )
759 if ( !sDisableSrIdCache )
761 for (
auto it = sSrIdCache()->begin(); it != sSrIdCache()->end(); )
763 auto &v = it.value();
764 if ( v.d->removeObjectsBelongingToCurrentThread( pj_context ) )
765 it = sSrIdCache()->erase( it );
771 if ( !sDisableOgcCache )
774 if ( !sDisableOgcCache )
776 for (
auto it = sOgcCache()->begin(); it != sOgcCache()->end(); )
778 auto &v = it.value();
779 if ( v.d->removeObjectsBelongingToCurrentThread( pj_context ) )
780 it = sOgcCache()->erase( it );
786 if ( !sDisableProjCache )
789 if ( !sDisableProjCache )
791 for (
auto it = sProj4Cache()->begin(); it != sProj4Cache()->end(); )
793 auto &v = it.value();
794 if ( v.d->removeObjectsBelongingToCurrentThread( pj_context ) )
795 it = sProj4Cache()->erase( it );
801 if ( !sDisableWktCache )
804 if ( !sDisableWktCache )
806 for (
auto it = sWktCache()->begin(); it != sWktCache()->end(); )
808 auto &v = it.value();
809 if ( v.d->removeObjectsBelongingToCurrentThread( pj_context ) )
810 it = sWktCache()->erase( it );
816 if ( !sDisableSrsIdCache )
819 if ( !sDisableSrsIdCache )
821 for (
auto it = sSrsIdCache()->begin(); it != sSrsIdCache()->end(); )
823 auto &v = it.value();
824 if ( v.d->removeObjectsBelongingToCurrentThread( pj_context ) )
825 it = sSrsIdCache()->erase( it );
831 if ( !sDisableStringCache )
834 if ( !sDisableStringCache )
836 for (
auto it = sStringCache()->begin(); it != sStringCache()->end(); )
838 auto &v = it.value();
839 if ( v.d->removeObjectsBelongingToCurrentThread( pj_context ) )
840 it = sStringCache()->erase( it );
850 if ( d->mAxisInvertedDirty )
853 d->mAxisInvertedDirty =
false;
856 return d->mAxisInverted;
873 const thread_local QMap< Qgis::CrsAxisDirection, QString > mapping =
916 QList< Qgis::CrsAxisDirection > res;
917 const int axisCount = proj_cs_get_axis_count( context, pjCs.get() );
920 res.reserve( axisCount );
922 for (
int i = 0; i < axisCount; ++i )
924 const char *outDirection =
nullptr;
925 proj_cs_get_axis_info( context, pjCs.get(), i,
935 const thread_local QRegularExpression rx( QStringLiteral(
"([^\\s]+).*" ) );
936 const QRegularExpressionMatch match = rx.match( QString( outDirection ) );
937 if ( !match.hasMatch() )
940 const QString direction = match.captured( 1 );
942 for (
auto it = mapping.constBegin(); it != mapping.constEnd(); ++it )
944 if ( it.value().compare( direction, Qt::CaseInsensitive ) == 0 )
959 return createFromWktInternal( wkt, QString() );
962bool QgsCoordinateReferenceSystem::createFromWktInternal(
const QString &wkt,
const QString &description )
970 if ( !sDisableWktCache )
972 QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = sWktCache()->constFind( wkt );
973 if ( crsIt != sWktCache()->constEnd() )
976 *
this = crsIt.value();
978 if ( !
description.isEmpty() && d->mDescription.isEmpty() )
983 sWktCache()->insert( wkt, *
this );
992 d->mWktPreferred.clear();
995 QgsDebugMsgLevel( QStringLiteral(
"theWkt is uninitialized, operation failed" ), 4 );
1000 QgsCoordinateReferenceSystem::RecordMap record = getRecord(
"select * from tbl_srs where wkt=" +
QgsSqliteUtils::quotedString( wkt ) +
" order by deprecated" );
1001 if ( !record.empty() )
1003 long srsId = record[QStringLiteral(
"srs_id" )].toLong();
1011 setWktString( wkt );
1016 if ( d->mSrsId == 0 )
1019 long id = matchToUserCrs();
1028 if ( !sDisableWktCache )
1029 sWktCache()->insert( wkt, *
this );
1047 if ( projString.isEmpty() )
1052 if ( projString.trimmed().isEmpty() )
1054 d->mIsValid =
false;
1056 d->mWktPreferred.clear();
1061 if ( !sDisableProjCache )
1063 QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = sProj4Cache()->constFind( projString );
1064 if ( crsIt != sProj4Cache()->constEnd() )
1067 *
this = crsIt.value();
1081 QString myProj4String = projString.trimmed();
1082 myProj4String.remove( QStringLiteral(
"+type=crs" ) );
1083 myProj4String = myProj4String.trimmed();
1085 d->mIsValid =
false;
1086 d->mWktPreferred.clear();
1091 const QString projCrsString = myProj4String + ( myProj4String.contains( QStringLiteral(
"+type=crs" ) ) ? QString() : QStringLiteral(
" +type=crs" ) );
1099 const QString
authid = QStringLiteral(
"%1:%2" ).arg( authName, authCode );
1103 if ( !sDisableProjCache )
1104 sProj4Cache()->insert( projString, *
this );
1111 QgsCoordinateReferenceSystem::RecordMap myRecord = getRecord(
"select * from tbl_srs where parameters=" +
QgsSqliteUtils::quotedString( myProj4String ) +
" order by deprecated" );
1113 if ( !myRecord.empty() )
1115 id = myRecord[QStringLiteral(
"srs_id" )].toLong();
1124 setProjString( myProj4String );
1127 id = matchToUserCrs();
1136 setProjString( myProj4String );
1140 if ( !sDisableProjCache )
1141 sProj4Cache()->insert( projString, *
this );
1147QgsCoordinateReferenceSystem::RecordMap QgsCoordinateReferenceSystem::getRecord(
const QString &sql )
1149 QString myDatabaseFileName;
1150 QgsCoordinateReferenceSystem::RecordMap myMap;
1151 QString myFieldName;
1152 QString myFieldValue;
1159 QFileInfo myInfo( myDatabaseFileName );
1160 if ( !myInfo.exists() )
1162 QgsDebugError(
"failed : " + myDatabaseFileName +
" does not exist!" );
1167 myResult = openDatabase( myDatabaseFileName, database );
1168 if ( myResult != SQLITE_OK )
1173 statement = database.
prepare( sql, myResult );
1175 if ( myResult == SQLITE_OK && statement.
step() == SQLITE_ROW )
1179 for (
int myColNo = 0; myColNo < myColumnCount; myColNo++ )
1181 myFieldName = statement.
columnName( myColNo );
1183 myMap[myFieldName] = myFieldValue;
1185 if ( statement.
step() != SQLITE_DONE )
1187 QgsDebugMsgLevel( QStringLiteral(
"Multiple records found in srs.db" ), 4 );
1196 if ( myMap.empty() )
1199 QFileInfo myFileInfo;
1200 myFileInfo.setFile( myDatabaseFileName );
1201 if ( !myFileInfo.exists() )
1203 QgsDebugError( QStringLiteral(
"user qgis.db not found" ) );
1208 myResult = openDatabase( myDatabaseFileName, database );
1209 if ( myResult != SQLITE_OK )
1214 statement = database.
prepare( sql, myResult );
1216 if ( myResult == SQLITE_OK && statement.
step() == SQLITE_ROW )
1220 for (
int myColNo = 0; myColNo < myColumnCount; myColNo++ )
1222 myFieldName = statement.
columnName( myColNo );
1224 myMap[myFieldName] = myFieldValue;
1227 if ( statement.
step() != SQLITE_DONE )
1229 QgsDebugMsgLevel( QStringLiteral(
"Multiple records found in srs.db" ), 4 );
1260 if ( d->mDescription.isNull() )
1266 return d->mDescription;
1273 if ( !
authid().isEmpty() )
1283 id =
isValid() ? QObject::tr(
"Custom CRS" ) : QObject::tr(
"Unknown CRS" );
1285 id = QObject::tr(
"Custom CRS: %1" ).arg(
1288 else if ( !
toProj().isEmpty() )
1291 if ( !
id.isEmpty() && !std::isnan( d->mCoordinateEpoch ) )
1292 id += QStringLiteral(
" @ %1" ).arg(
qgsDoubleToString( d->mCoordinateEpoch, 3 ) );
1299 if ( d->mProjectionAcronym.isNull() )
1305 return d->mProjectionAcronym;
1311 if ( d->mEllipsoidAcronym.isNull() )
1313 if (
PJ *obj = d->threadLocalProjObject() )
1318 const QString ellipsoidAuthName( proj_get_id_auth_name( ellipsoid.get(), 0 ) );
1319 const QString ellipsoidAuthCode( proj_get_id_code( ellipsoid.get(), 0 ) );
1320 if ( !ellipsoidAuthName.isEmpty() && !ellipsoidAuthCode.isEmpty() )
1321 d->mEllipsoidAcronym = QStringLiteral(
"%1:%2" ).arg( ellipsoidAuthName, ellipsoidAuthCode );
1324 double semiMajor, semiMinor, invFlattening;
1325 int semiMinorComputed = 0;
1326 if ( proj_ellipsoid_get_parameters(
QgsProjContext::get(), ellipsoid.get(), &semiMajor, &semiMinor, &semiMinorComputed, &invFlattening ) )
1328 d->mEllipsoidAcronym = QStringLiteral(
"PARAMETER:%1:%2" ).arg(
qgsDoubleToString( semiMajor ),
1333 d->mEllipsoidAcronym.clear();
1338 return d->mEllipsoidAcronym;
1342 return d->mEllipsoidAcronym;
1356 if ( d->mProj4.isEmpty() )
1358 if (
PJ *obj = d->threadLocalProjObject() )
1364 return d->mProj4.trimmed();
1370 switch ( d->mProjType )
1372 case PJ_TYPE_UNKNOWN:
1375 case PJ_TYPE_ELLIPSOID:
1376 case PJ_TYPE_PRIME_MERIDIAN:
1377 case PJ_TYPE_GEODETIC_REFERENCE_FRAME:
1378 case PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME:
1379 case PJ_TYPE_VERTICAL_REFERENCE_FRAME:
1380 case PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME:
1381 case PJ_TYPE_DATUM_ENSEMBLE:
1382 case PJ_TYPE_CONVERSION:
1383 case PJ_TYPE_TRANSFORMATION:
1384 case PJ_TYPE_CONCATENATED_OPERATION:
1385 case PJ_TYPE_OTHER_COORDINATE_OPERATION:
1386 case PJ_TYPE_TEMPORAL_DATUM:
1387 case PJ_TYPE_ENGINEERING_DATUM:
1388 case PJ_TYPE_PARAMETRIC_DATUM:
1392 case PJ_TYPE_GEOGRAPHIC_CRS:
1396 case PJ_TYPE_GEODETIC_CRS:
1398 case PJ_TYPE_GEOCENTRIC_CRS:
1400 case PJ_TYPE_GEOGRAPHIC_2D_CRS:
1402 case PJ_TYPE_GEOGRAPHIC_3D_CRS:
1404 case PJ_TYPE_VERTICAL_CRS:
1406 case PJ_TYPE_PROJECTED_CRS:
1408 case PJ_TYPE_COMPOUND_CRS:
1410 case PJ_TYPE_TEMPORAL_CRS:
1412 case PJ_TYPE_ENGINEERING_CRS:
1414 case PJ_TYPE_BOUND_CRS:
1416 case PJ_TYPE_OTHER_CRS:
1418#if PROJ_VERSION_MAJOR>9 || (PROJ_VERSION_MAJOR==9 && PROJ_VERSION_MINOR>=2)
1419 case PJ_TYPE_DERIVED_PROJECTED_CRS:
1421 case PJ_TYPE_COORDINATE_METADATA:
1435 return proj_is_deprecated( pj );
1440 return d->mIsGeographic;
1460 return QString( proj_get_celestial_body_name( context, pj ) );
1465 if ( d->mCoordinateEpoch == epoch )
1471 d->mCoordinateEpoch = epoch;
1472 d->setPj( std::move( clone ) );
1477 return d->mCoordinateEpoch;
1496 res.mName = QString( proj_get_name( ensemble.get() ) );
1497 res.mAuthority = QString( proj_get_id_auth_name( ensemble.get(), 0 ) );
1498 res.mCode = QString( proj_get_id_code( ensemble.get(), 0 ) );
1499 res.mRemarks = QString( proj_get_remarks( ensemble.get() ) );
1500 res.mScope = QString( proj_get_scope( ensemble.get() ) );
1501 res.mAccuracy = proj_datum_ensemble_get_accuracy( context, ensemble.get() );
1503 const int memberCount = proj_datum_ensemble_get_member_count( context, ensemble.get() );
1504 for (
int i = 0; i < memberCount; ++i )
1511 details.mName = QString( proj_get_name( member.get() ) );
1512 details.mAuthority = QString( proj_get_id_auth_name( member.get(), 0 ) );
1513 details.mCode = QString( proj_get_id_code( member.get(), 0 ) );
1514 details.mRemarks = QString( proj_get_remarks( member.get() ) );
1515 details.mScope = QString( proj_get_scope( member.get() ) );
1517 res.mMembers << details;
1527 QString projString =
toProj();
1528 projString.replace( QLatin1String(
"+type=crs" ), QString() );
1531 if ( !transformation )
1534 PJ_COORD coord = proj_coord( 0, 0, 0, HUGE_VAL );
1535 coord.uv.u = point.
x() * M_PI / 180.0;
1536 coord.uv.v = point.
y() * M_PI / 180.0;
1538 proj_errno_reset( transformation.get() );
1539 const PJ_FACTORS pjFactors = proj_factors( transformation.get(), coord );
1540 if ( proj_errno( transformation.get() ) )
1545 res.mIsValid =
true;
1546 res.mMeridionalScale = pjFactors.meridional_scale;
1547 res.mParallelScale = pjFactors.parallel_scale;
1548 res.mArealScale = pjFactors.areal_scale;
1549 res.mAngularDistortion = pjFactors.angular_distortion;
1550 res.mMeridianParallelAngle = pjFactors.meridian_parallel_angle * 180 / M_PI;
1551 res.mMeridianConvergence = pjFactors.meridian_convergence * 180 / M_PI;
1552 res.mTissotSemimajor = pjFactors.tissot_semimajor;
1553 res.mTissotSemiminor = pjFactors.tissot_semiminor;
1554 res.mDxDlam = pjFactors.dx_dlam;
1555 res.mDxDphi = pjFactors.dx_dphi;
1556 res.mDyDlam = pjFactors.dy_dlam;
1557 res.mDyDphi = pjFactors.dy_dphi;
1569 QString projString =
toProj();
1570 projString.replace( QLatin1String(
"+type=crs" ), QString() );
1571 if ( projString.isEmpty() )
1575 if ( !transformation )
1578 PJ_PROJ_INFO info = proj_pj_info( transformation.get() );
1593 return d->mMapUnits;
1601 PJ *obj = d->threadLocalProjObject();
1606 double southLat = 0;
1608 double northLat = 0;
1611 &westLon, &southLat, &eastLon, &northLat,
nullptr ) )
1626 const auto parts {
authid().split(
':' ) };
1627 if ( parts.length() == 2 )
1629 if ( parts[0] == QLatin1String(
"EPSG" ) )
1630 return QStringLiteral(
"http://www.opengis.net/def/crs/EPSG/0/%1" ).arg( parts[1] ) ;
1631 else if ( parts[0] == QLatin1String(
"OGC" ) )
1633 return QStringLiteral(
"http://www.opengis.net/def/crs/OGC/1.3/%1" ).arg( parts[1] ) ;
1649 const auto parts {
authid().split(
':' ) };
1650 if ( parts.length() == 2 )
1652 if ( parts[0] == QLatin1String(
"EPSG" ) )
1653 return QStringLiteral(
"urn:ogc:def:crs:EPSG::%1" ).arg( parts[1] );
1654 else if ( parts[0] == QLatin1String(
"OGC" ) )
1656 return QStringLiteral(
"urn:ogc:def:crs:OGC:1.3:%1" ).arg( parts[1] );
1690void QgsCoordinateReferenceSystem::setProjString(
const QString &proj4String )
1693 d->mProj4 = proj4String;
1694 d->mWktPreferred.clear();
1697 QString trimmed = proj4String.trimmed();
1699 trimmed += QLatin1String(
" +type=crs" );
1709 const int errNo = proj_context_errno( ctx );
1710 QgsDebugError( QStringLiteral(
"proj string rejected: %1" ).arg( proj_context_errno_string( ctx, errNo ) ) );
1712 d->mIsValid =
false;
1716 d->mEllipsoidAcronym.clear();
1723bool QgsCoordinateReferenceSystem::setWktString(
const QString &wkt )
1726 d->mIsValid =
false;
1727 d->mWktPreferred.clear();
1729 PROJ_STRING_LIST warnings =
nullptr;
1730 PROJ_STRING_LIST grammarErrors =
nullptr;
1738 QgsDebugMsgLevel( QStringLiteral(
"\n---------------------------------------------------------------" ), 2 );
1739 QgsDebugMsgLevel( QStringLiteral(
"This CRS could *** NOT *** be set from the supplied Wkt " ), 2 );
1741 for (
auto iter = warnings; iter && *iter; ++iter )
1745 for (
auto iter = grammarErrors; iter && *iter; ++iter )
1749 QgsDebugMsgLevel( QStringLiteral(
"---------------------------------------------------------------\n" ), 2 );
1751 proj_string_list_destroy( warnings );
1752 proj_string_list_destroy( grammarErrors );
1758 if ( !sDisableWktCache )
1759 sWktCache()->insert( wkt, *
this );
1766 QString authName( proj_get_id_auth_name( d->threadLocalProjObject(), 0 ) );
1767 QString authCode( proj_get_id_code( d->threadLocalProjObject(), 0 ) );
1769 if ( authName.isEmpty() || authCode.isEmpty() )
1775 if ( !authName.isEmpty() && !authCode.isEmpty() )
1777 if ( loadFromAuthCode( authName, authCode ) )
1780 if ( !sDisableWktCache )
1781 sWktCache()->insert( wkt, *
this );
1789 d->mDescription = QString( proj_get_name( d->threadLocalProjObject() ) );
1797void QgsCoordinateReferenceSystem::setMapUnits()
1824 if ( !coordinateSystem )
1830 const int axisCount = proj_cs_get_axis_count( context, coordinateSystem.get() );
1831 if ( axisCount > 0 )
1833 const char *outUnitName =
nullptr;
1835 proj_cs_get_axis_info( context, coordinateSystem.get(), 0,
1844 const QString unitName( outUnitName );
1848 if ( unitName.compare( QLatin1String(
"degree" ), Qt::CaseInsensitive ) == 0 ||
1849 unitName.compare( QLatin1String(
"degree minute second" ), Qt::CaseInsensitive ) == 0 ||
1850 unitName.compare( QLatin1String(
"degree minute second hemisphere" ), Qt::CaseInsensitive ) == 0 ||
1851 unitName.compare( QLatin1String(
"degree minute" ), Qt::CaseInsensitive ) == 0 ||
1852 unitName.compare( QLatin1String(
"degree hemisphere" ), Qt::CaseInsensitive ) == 0 ||
1853 unitName.compare( QLatin1String(
"degree minute hemisphere" ), Qt::CaseInsensitive ) == 0 ||
1854 unitName.compare( QLatin1String(
"hemisphere degree" ), Qt::CaseInsensitive ) == 0 ||
1855 unitName.compare( QLatin1String(
"hemisphere degree minute" ), Qt::CaseInsensitive ) == 0 ||
1856 unitName.compare( QLatin1String(
"hemisphere degree minute second" ), Qt::CaseInsensitive ) == 0 ||
1857 unitName.compare( QLatin1String(
"degree (supplier to define representation)" ), Qt::CaseInsensitive ) == 0 )
1859 else if ( unitName.compare( QLatin1String(
"metre" ), Qt::CaseInsensitive ) == 0
1860 || unitName.compare( QLatin1String(
"m" ), Qt::CaseInsensitive ) == 0
1861 || unitName.compare( QLatin1String(
"meter" ), Qt::CaseInsensitive ) == 0 )
1863 else if ( unitName.compare( QLatin1String(
"US survey foot" ), Qt::CaseInsensitive ) == 0 )
1865 else if ( unitName.compare( QLatin1String(
"foot" ), Qt::CaseInsensitive ) == 0 )
1867 else if ( unitName.compare( QLatin1String(
"British yard (Sears 1922)" ), Qt::CaseInsensitive ) == 0 )
1869 else if ( unitName.compare( QLatin1String(
"British yard (Sears 1922 truncated)" ), Qt::CaseInsensitive ) == 0 )
1871 else if ( unitName.compare( QLatin1String(
"British foot (Sears 1922)" ), Qt::CaseInsensitive ) == 0 )
1873 else if ( unitName.compare( QLatin1String(
"British foot (Sears 1922 truncated)" ), Qt::CaseInsensitive ) == 0 )
1875 else if ( unitName.compare( QLatin1String(
"British chain (Sears 1922)" ), Qt::CaseInsensitive ) == 0 )
1877 else if ( unitName.compare( QLatin1String(
"British chain (Sears 1922 truncated)" ), Qt::CaseInsensitive ) == 0 )
1879 else if ( unitName.compare( QLatin1String(
"British link (Sears 1922)" ), Qt::CaseInsensitive ) == 0 )
1881 else if ( unitName.compare( QLatin1String(
"British link (Sears 1922 truncated)" ), Qt::CaseInsensitive ) == 0 )
1883 else if ( unitName.compare( QLatin1String(
"British yard (Benoit 1895 A)" ), Qt::CaseInsensitive ) == 0 )
1885 else if ( unitName.compare( QLatin1String(
"British foot (Benoit 1895 A)" ), Qt::CaseInsensitive ) == 0 )
1887 else if ( unitName.compare( QLatin1String(
"British chain (Benoit 1895 A)" ), Qt::CaseInsensitive ) == 0 )
1889 else if ( unitName.compare( QLatin1String(
"British link (Benoit 1895 A)" ), Qt::CaseInsensitive ) == 0 )
1891 else if ( unitName.compare( QLatin1String(
"British yard (Benoit 1895 B)" ), Qt::CaseInsensitive ) == 0 )
1893 else if ( unitName.compare( QLatin1String(
"British foot (Benoit 1895 B)" ), Qt::CaseInsensitive ) == 0 )
1895 else if ( unitName.compare( QLatin1String(
"British chain (Benoit 1895 B)" ), Qt::CaseInsensitive ) == 0 )
1897 else if ( unitName.compare( QLatin1String(
"British link (Benoit 1895 B)" ), Qt::CaseInsensitive ) == 0 )
1899 else if ( unitName.compare( QLatin1String(
"British foot (1865)" ), Qt::CaseInsensitive ) == 0 )
1901 else if ( unitName.compare( QLatin1String(
"British foot (1936)" ), Qt::CaseInsensitive ) == 0 )
1903 else if ( unitName.compare( QLatin1String(
"Indian foot" ), Qt::CaseInsensitive ) == 0 )
1905 else if ( unitName.compare( QLatin1String(
"Indian foot (1937)" ), Qt::CaseInsensitive ) == 0 )
1907 else if ( unitName.compare( QLatin1String(
"Indian foot (1962)" ), Qt::CaseInsensitive ) == 0 )
1909 else if ( unitName.compare( QLatin1String(
"Indian foot (1975)" ), Qt::CaseInsensitive ) == 0 )
1911 else if ( unitName.compare( QLatin1String(
"Indian yard" ), Qt::CaseInsensitive ) == 0 )
1913 else if ( unitName.compare( QLatin1String(
"Indian yard (1937)" ), Qt::CaseInsensitive ) == 0 )
1915 else if ( unitName.compare( QLatin1String(
"Indian yard (1962)" ), Qt::CaseInsensitive ) == 0 )
1917 else if ( unitName.compare( QLatin1String(
"Indian yard (1975)" ), Qt::CaseInsensitive ) == 0 )
1919 else if ( unitName.compare( QLatin1String(
"Gold Coast foot" ), Qt::CaseInsensitive ) == 0 )
1921 else if ( unitName.compare( QLatin1String(
"Clarke's foot" ), Qt::CaseInsensitive ) == 0 )
1923 else if ( unitName.compare( QLatin1String(
"Clarke's yard" ), Qt::CaseInsensitive ) == 0 )
1925 else if ( unitName.compare( QLatin1String(
"Clarke's chain" ), Qt::CaseInsensitive ) == 0 )
1927 else if ( unitName.compare( QLatin1String(
"Clarke's link" ), Qt::CaseInsensitive ) == 0 )
1929 else if ( unitName.compare( QLatin1String(
"kilometre" ), Qt::CaseInsensitive ) == 0 )
1931 else if ( unitName.compare( QLatin1String(
"centimetre" ), Qt::CaseInsensitive ) == 0 )
1933 else if ( unitName.compare( QLatin1String(
"millimetre" ), Qt::CaseInsensitive ) == 0 )
1935 else if ( unitName.compare( QLatin1String(
"Statute mile" ), Qt::CaseInsensitive ) == 0 )
1937 else if ( unitName.compare( QLatin1String(
"nautical mile" ), Qt::CaseInsensitive ) == 0 )
1939 else if ( unitName.compare( QLatin1String(
"yard" ), Qt::CaseInsensitive ) == 0 )
1941 else if ( unitName.compare( QLatin1String(
"fathom" ), Qt::CaseInsensitive ) == 0 )
1943 else if ( unitName.compare( QLatin1String(
"US survey chain" ), Qt::CaseInsensitive ) == 0 )
1945 else if ( unitName.compare( QLatin1String(
"chain" ), Qt::CaseInsensitive ) == 0 )
1947 else if ( unitName.compare( QLatin1String(
"link" ), Qt::CaseInsensitive ) == 0 )
1949 else if ( unitName.compare( QLatin1String(
"US survey link" ), Qt::CaseInsensitive ) == 0 )
1951 else if ( unitName.compare( QLatin1String(
"US survey mile" ), Qt::CaseInsensitive ) == 0 )
1953 else if ( unitName.compare( QLatin1String(
"German legal metre" ), Qt::CaseInsensitive ) == 0 )
1970 if ( d->mEllipsoidAcronym.isNull() || d->mProjectionAcronym.isNull()
1973 QgsDebugMsgLevel(
"QgsCoordinateReferenceSystem::findMatchingProj will only "
1974 "work if prj acr ellipsoid acr and proj4string are set"
1975 " and the current projection is valid!", 4 );
1985 QString mySql = QString(
"select srs_id,parameters from tbl_srs where "
1986 "projection_acronym=%1 and ellipsoid_acronym=%2 order by deprecated" )
1993 myResult = openDatabase( myDatabaseFileName, database );
1994 if ( myResult != SQLITE_OK )
1999 statement = database.
prepare( mySql, myResult );
2000 if ( myResult == SQLITE_OK )
2003 while ( statement.
step() == SQLITE_ROW )
2007 if (
toProj() == myProj4String.trimmed() )
2009 return mySrsId.toLong();
2020 myResult = openDatabase( myDatabaseFileName, database );
2021 if ( myResult != SQLITE_OK )
2026 statement = database.
prepare( mySql, myResult );
2028 if ( myResult == SQLITE_OK )
2030 while ( statement.
step() == SQLITE_ROW )
2034 if (
toProj() == myProj4String.trimmed() )
2036 return mySrsId.toLong();
2050 if ( !d->mIsValid && !srs.d->mIsValid )
2053 if ( !d->mIsValid || !srs.d->mIsValid )
2061 if ( isUser != otherIsUser )
2065 if ( !isUser && ( !d->mAuthId.isEmpty() || !srs.d->mAuthId.isEmpty() ) )
2066 return d->mAuthId == srs.d->mAuthId;
2073 return !( *
this == srs );
2078 if (
PJ *obj = d->threadLocalProjObject() )
2081 if ( isDefaultPreferredFormat && !d->mWktPreferred.isEmpty() )
2084 return d->mWktPreferred;
2087 PJ_WKT_TYPE
type = PJ_WKT1_GDAL;
2091 type = PJ_WKT1_GDAL;
2094 type = PJ_WKT1_ESRI;
2097 type = PJ_WKT2_2015;
2100 type = PJ_WKT2_2015_SIMPLIFIED;
2103 type = PJ_WKT2_2019;
2106 type = PJ_WKT2_2019_SIMPLIFIED;
2110 const QByteArray multiLineOption = QStringLiteral(
"MULTILINE=%1" ).arg( multiline ? QStringLiteral(
"YES" ) : QStringLiteral(
"NO" ) ).toLocal8Bit();
2111 const QByteArray indentatationWidthOption = QStringLiteral(
"INDENTATION_WIDTH=%1" ).arg( multiline ? QString::number( indentationWidth ) : QStringLiteral(
"0" ) ).toLocal8Bit();
2112 const char *
const options[] = {multiLineOption.constData(), indentatationWidthOption.constData(),
nullptr};
2115 if ( isDefaultPreferredFormat )
2118 d->mWktPreferred = res;
2130 QDomNode srsNode = node.namedItem( QStringLiteral(
"spatialrefsys" ) );
2132 if ( ! srsNode.isNull() )
2134 bool initialized =
false;
2137 long srsid = srsNode.namedItem( QStringLiteral(
"srsid" ) ).toElement().text().toLong( &ok );
2143 node = srsNode.namedItem( QStringLiteral(
"authid" ) );
2144 if ( !node.isNull() )
2155 node = srsNode.namedItem( QStringLiteral(
"epsg" ) );
2156 if ( !node.isNull() )
2174 const QString
description = srsNode.namedItem( QStringLiteral(
"description" ) ).toElement().text();
2176 const QString wkt = srsNode.namedItem( QStringLiteral(
"wkt" ) ).toElement().text();
2177 initialized = createFromWktInternal( wkt,
description );
2182 node = srsNode.namedItem( QStringLiteral(
"proj4" ) );
2183 const QString proj4 = node.toElement().text();
2190 node = srsNode.namedItem( QStringLiteral(
"proj4" ) );
2191 const QString proj4 = node.toElement().text();
2192 if ( !proj4.trimmed().isEmpty() )
2193 setProjString( node.toElement().text() );
2195 node = srsNode.namedItem( QStringLiteral(
"srsid" ) );
2196 d->mSrsId = node.toElement().text().toLong();
2198 node = srsNode.namedItem( QStringLiteral(
"srid" ) );
2199 d->mSRID = node.toElement().text().toLong();
2201 node = srsNode.namedItem( QStringLiteral(
"authid" ) );
2202 d->mAuthId = node.toElement().text();
2204 node = srsNode.namedItem( QStringLiteral(
"description" ) );
2205 d->mDescription = node.toElement().text();
2207 node = srsNode.namedItem( QStringLiteral(
"projectionacronym" ) );
2208 d->mProjectionAcronym = node.toElement().text();
2210 node = srsNode.namedItem( QStringLiteral(
"ellipsoidacronym" ) );
2211 d->mEllipsoidAcronym = node.toElement().text();
2213 node = srsNode.namedItem( QStringLiteral(
"geographicflag" ) );
2214 d->mIsGeographic = node.toElement().text() == QLatin1String(
"true" );
2216 d->mWktPreferred.clear();
2222 const QString epoch = srsNode.toElement().attribute( QStringLiteral(
"coordinateEpoch" ) );
2223 if ( !epoch.isEmpty() )
2225 bool epochOk =
false;
2226 d->mCoordinateEpoch = epoch.toDouble( &epochOk );
2228 d->mCoordinateEpoch = std::numeric_limits< double >::quiet_NaN();
2232 d->mCoordinateEpoch = std::numeric_limits< double >::quiet_NaN();
2235 mNativeFormat = qgsEnumKeyToValue<Qgis::CrsDefinitionFormat>( srsNode.toElement().attribute( QStringLiteral(
"nativeFormat" ) ),
Qgis::CrsDefinitionFormat::Wkt );
2240 d =
new QgsCoordinateReferenceSystemPrivate();
2248 QDomElement layerNode = node.toElement();
2249 QDomElement srsElement = doc.createElement( QStringLiteral(
"spatialrefsys" ) );
2251 srsElement.setAttribute( QStringLiteral(
"nativeFormat" ), qgsEnumValueToKey<Qgis::CrsDefinitionFormat>( mNativeFormat ) );
2253 if ( std::isfinite( d->mCoordinateEpoch ) )
2255 srsElement.setAttribute( QStringLiteral(
"coordinateEpoch" ), d->mCoordinateEpoch );
2258 QDomElement wktElement = doc.createElement( QStringLiteral(
"wkt" ) );
2260 srsElement.appendChild( wktElement );
2262 QDomElement proj4Element = doc.createElement( QStringLiteral(
"proj4" ) );
2263 proj4Element.appendChild( doc.createTextNode(
toProj() ) );
2264 srsElement.appendChild( proj4Element );
2266 QDomElement srsIdElement = doc.createElement( QStringLiteral(
"srsid" ) );
2267 srsIdElement.appendChild( doc.createTextNode( QString::number(
srsid() ) ) );
2268 srsElement.appendChild( srsIdElement );
2270 QDomElement sridElement = doc.createElement( QStringLiteral(
"srid" ) );
2271 sridElement.appendChild( doc.createTextNode( QString::number(
postgisSrid() ) ) );
2272 srsElement.appendChild( sridElement );
2274 QDomElement authidElement = doc.createElement( QStringLiteral(
"authid" ) );
2275 authidElement.appendChild( doc.createTextNode(
authid() ) );
2276 srsElement.appendChild( authidElement );
2278 QDomElement descriptionElement = doc.createElement( QStringLiteral(
"description" ) );
2279 descriptionElement.appendChild( doc.createTextNode(
description() ) );
2280 srsElement.appendChild( descriptionElement );
2282 QDomElement projectionAcronymElement = doc.createElement( QStringLiteral(
"projectionacronym" ) );
2283 projectionAcronymElement.appendChild( doc.createTextNode(
projectionAcronym() ) );
2284 srsElement.appendChild( projectionAcronymElement );
2286 QDomElement ellipsoidAcronymElement = doc.createElement( QStringLiteral(
"ellipsoidacronym" ) );
2287 ellipsoidAcronymElement.appendChild( doc.createTextNode(
ellipsoidAcronym() ) );
2288 srsElement.appendChild( ellipsoidAcronymElement );
2290 QDomElement geographicFlagElement = doc.createElement( QStringLiteral(
"geographicflag" ) );
2291 QString geoFlagText = QStringLiteral(
"false" );
2294 geoFlagText = QStringLiteral(
"true" );
2297 geographicFlagElement.appendChild( doc.createTextNode( geoFlagText ) );
2298 srsElement.appendChild( geographicFlagElement );
2300 layerNode.appendChild( srsElement );
2312QString QgsCoordinateReferenceSystem::projFromSrsId(
const int srsId )
2314 QString myDatabaseFileName;
2315 QString myProjString;
2316 QString mySql = QStringLiteral(
"select parameters from tbl_srs where srs_id = %1 order by deprecated" ).arg( srsId );
2325 QFileInfo myFileInfo;
2326 myFileInfo.setFile( myDatabaseFileName );
2327 if ( !myFileInfo.exists() )
2329 QgsDebugError( QStringLiteral(
"users qgis.db not found" ) );
2342 rc = openDatabase( myDatabaseFileName, database );
2348 statement = database.
prepare( mySql, rc );
2350 if ( rc == SQLITE_OK )
2352 if ( statement.
step() == SQLITE_ROW )
2358 return myProjString;
2365 myResult = database.
open_v2( path, SQLITE_OPEN_READONLY,
nullptr );
2367 myResult = database.
open( path );
2369 if ( myResult != SQLITE_OK )
2378 .arg( database.
errorMessage() ), QObject::tr(
"CRS" ) );
2385 sCustomSrsValidation = f;
2390 return sCustomSrsValidation;
2393void QgsCoordinateReferenceSystem::debugPrint()
2396 QgsDebugMsgLevel(
"* Valid : " + ( d->mIsValid ? QString(
"true" ) : QString(
"false" ) ), 1 );
2417 mValidationHint = html;
2422 return mValidationHint;
2432 mNativeFormat = format;
2437 return mNativeFormat;
2440long QgsCoordinateReferenceSystem::getRecordCount()
2445 long myRecordCount = 0;
2448 if ( myResult != SQLITE_OK )
2454 QString mySql = QStringLiteral(
"select count(*) from tbl_srs" );
2455 statement = database.
prepare( mySql, myResult );
2456 if ( myResult == SQLITE_OK )
2458 if ( statement.
step() == SQLITE_ROW )
2460 QString myRecordCountString = statement.
columnAsText( 0 );
2461 myRecordCount = myRecordCountString.toLong();
2464 return myRecordCount;
2470 bool isGeographic =
false;
2474 if ( !horizontalCrs )
2478 if ( coordinateSystem )
2480 const int axisCount = proj_cs_get_axis_count( pjContext, coordinateSystem.get() );
2481 if ( axisCount > 0 )
2483 const char *outUnitAuthName =
nullptr;
2484 const char *outUnitAuthCode =
nullptr;
2486 proj_cs_get_axis_info( pjContext, coordinateSystem.get(), 0,
2495 if ( outUnitAuthName && outUnitAuthCode )
2497 const char *unitCategory =
nullptr;
2498 if ( proj_uom_get_info_from_database( pjContext, outUnitAuthName, outUnitAuthCode,
nullptr,
nullptr, &unitCategory ) )
2500 isGeographic = QString( unitCategory ).compare( QLatin1String(
"angular" ), Qt::CaseInsensitive ) == 0;
2505 return isGeographic;
2510 thread_local const QRegularExpression projRegExp( QStringLiteral(
"\\+proj=(\\S+)" ) );
2511 const QRegularExpressionMatch projMatch = projRegExp.match( proj );
2512 if ( !projMatch.hasMatch() )
2514 QgsDebugMsgLevel( QStringLiteral(
"no +proj argument found [%2]" ).arg( proj ), 2 );
2517 operation = projMatch.captured( 1 );
2519 const QRegularExpressionMatch ellipseMatch = projRegExp.match( proj );
2520 if ( ellipseMatch.hasMatch() )
2522 ellipsoid = ellipseMatch.captured( 1 );
2536bool QgsCoordinateReferenceSystem::loadFromAuthCode(
const QString &auth,
const QString &code )
2542 d->mIsValid =
false;
2543 d->mWktPreferred.clear();
2555 proj4.replace( QLatin1String(
"+type=crs" ), QString() );
2556 proj4 = proj4.trimmed();
2560 d->mWktPreferred.clear();
2561 d->mDescription = QString( proj_get_name(
crs.get() ) );
2562 d->mAuthId = QStringLiteral(
"%1:%2" ).arg( auth, code );
2564 d->mAxisInvertedDirty =
true;
2569 d->mEllipsoidAcronym.clear();
2570 d->setPj( std::move(
crs ) );
2572 const QString dbVals =
sAuthIdToQgisSrsIdMap.value( QStringLiteral(
"%1:%2" ).arg( auth, code ).toUpper() );
2573 if ( !dbVals.isEmpty() )
2575 const QStringList parts = dbVals.split(
',' );
2576 d->mSrsId = parts.at( 0 ).toInt();
2577 d->mSRID = parts.at( 1 ).toInt();
2585QList<long> QgsCoordinateReferenceSystem::userSrsIds()
2587 QList<long> results;
2591 QFileInfo myInfo( db );
2592 if ( !myInfo.exists() )
2602 int result = openDatabase( db, database );
2603 if ( result != SQLITE_OK )
2605 QgsDebugError(
"failed : " + db +
" could not be opened!" );
2609 QString sql = QStringLiteral(
"select srs_id from tbl_srs where srs_id >= %1" ).arg(
Qgis::USER_CRS_START_ID );
2611 statement = database.
prepare( sql, rc );
2614 int ret = statement.
step();
2616 if ( ret == SQLITE_DONE )
2622 if ( ret == SQLITE_ROW )
2628 QgsMessageLog::logMessage( QObject::tr(
"SQLite error: %2\nSQL: %1" ).arg( sql, sqlite3_errmsg( database.get() ) ), QObject::tr(
"SpatiaLite" ) );
2636long QgsCoordinateReferenceSystem::matchToUserCrs()
const
2638 PJ *obj = d->threadLocalProjObject();
2642 const QList< long > ids = userSrsIds();
2643 for (
long id : ids )
2646 if ( candidate.
projObject() && proj_is_equivalent_to( obj, candidate.
projObject(), PJ_COMP_EQUIVALENT ) )
2654static void sync_db_proj_logger(
void * ,
int level,
const char *message )
2659 if ( level == PJ_LOG_ERROR )
2663 else if ( level == PJ_LOG_DEBUG )
2671 setlocale( LC_ALL,
"C" );
2674 int inserted = 0, updated = 0, deleted = 0, errors = 0;
2679 if ( database.
open( dbFilePath ) != SQLITE_OK )
2685 if ( sqlite3_exec( database.get(),
"BEGIN TRANSACTION",
nullptr,
nullptr,
nullptr ) != SQLITE_OK )
2693 char *errMsg =
nullptr;
2695 bool createdTypeColumn =
false;
2696 if ( sqlite3_exec( database.get(),
"ALTER TABLE tbl_srs ADD COLUMN srs_type text",
nullptr,
nullptr,
nullptr ) == SQLITE_OK )
2698 createdTypeColumn =
true;
2699 if ( sqlite3_exec( database.get(),
"CREATE INDEX srs_type ON tbl_srs(srs_type)",
nullptr,
nullptr,
nullptr ) != SQLITE_OK )
2701 QgsDebugError( QStringLiteral(
"Could not create index for srs_type" ) );
2706 if ( sqlite3_exec( database.get(),
"create table tbl_info (proj_major INT, proj_minor INT, proj_patch INT)",
nullptr,
nullptr,
nullptr ) == SQLITE_OK )
2708 QString sql = QStringLiteral(
"INSERT INTO tbl_info(proj_major, proj_minor, proj_patch) VALUES (%1, %2,%3)" )
2709 .arg( QString::number( PROJ_VERSION_MAJOR ),
2710 QString::number( PROJ_VERSION_MINOR ),
2711 QString::number( PROJ_VERSION_PATCH ) );
2712 if ( sqlite3_exec( database.get(), sql.toUtf8(),
nullptr,
nullptr, &errMsg ) != SQLITE_OK )
2714 QgsDebugError( QStringLiteral(
"Could not execute: %1 [%2/%3]\n" ).arg(
2717 errMsg ? errMsg :
"(unknown error)" ) );
2719 sqlite3_free( errMsg );
2726 QString sql = QStringLiteral(
"SELECT proj_major, proj_minor, proj_patch FROM tbl_info" );
2727 statement = database.
prepare( sql, result );
2728 if ( result != SQLITE_OK )
2733 if ( statement.
step() == SQLITE_ROW )
2738 if ( !createdTypeColumn && major == PROJ_VERSION_MAJOR && minor == PROJ_VERSION_MINOR && patch == PROJ_VERSION_PATCH )
2744 QgsDebugError( QStringLiteral(
"Could not retrieve previous CRS sync PROJ version number" ) );
2751 proj_log_func( pjContext,
nullptr, sync_db_proj_logger );
2753 PROJ_STRING_LIST authorities = proj_get_authorities_from_database( pjContext );
2755 int nextSrsId = 67218;
2756 int nextSrId = 520007218;
2757 for (
auto authIter = authorities; authIter && *authIter; ++authIter )
2759 const QString authority( *authIter );
2760 QgsDebugMsgLevel( QStringLiteral(
"Loading authority '%1'" ).arg( authority ), 2 );
2761 PROJ_STRING_LIST codes = proj_get_codes_from_database( pjContext, *authIter, PJ_TYPE_CRS,
true );
2763 QStringList allCodes;
2765 for (
auto codesIter = codes; codesIter && *codesIter; ++codesIter )
2767 const QString code( *codesIter );
2773 QgsDebugError( QStringLiteral(
"Could not load '%1:%2'" ).arg( authority, code ) );
2777 const PJ_TYPE pjType = proj_get_type(
crs.get( ) );
2779 QString srsTypeString;
2784 case PJ_TYPE_ELLIPSOID:
2785 case PJ_TYPE_PRIME_MERIDIAN:
2786 case PJ_TYPE_GEODETIC_REFERENCE_FRAME:
2787 case PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME:
2788 case PJ_TYPE_VERTICAL_REFERENCE_FRAME:
2789 case PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME:
2790 case PJ_TYPE_DATUM_ENSEMBLE:
2791 case PJ_TYPE_CONVERSION:
2792 case PJ_TYPE_TRANSFORMATION:
2793 case PJ_TYPE_CONCATENATED_OPERATION:
2794 case PJ_TYPE_OTHER_COORDINATE_OPERATION:
2795 case PJ_TYPE_TEMPORAL_DATUM:
2796 case PJ_TYPE_ENGINEERING_DATUM:
2797 case PJ_TYPE_PARAMETRIC_DATUM:
2798 case PJ_TYPE_UNKNOWN:
2802 case PJ_TYPE_GEOGRAPHIC_CRS:
2805 case PJ_TYPE_GEODETIC_CRS:
2809 case PJ_TYPE_GEOCENTRIC_CRS:
2813 case PJ_TYPE_GEOGRAPHIC_2D_CRS:
2817 case PJ_TYPE_GEOGRAPHIC_3D_CRS:
2821 case PJ_TYPE_PROJECTED_CRS:
2825 case PJ_TYPE_COMPOUND_CRS:
2829 case PJ_TYPE_TEMPORAL_CRS:
2833 case PJ_TYPE_ENGINEERING_CRS:
2837 case PJ_TYPE_BOUND_CRS:
2841 case PJ_TYPE_VERTICAL_CRS:
2845#if PROJ_VERSION_MAJOR>9 || (PROJ_VERSION_MAJOR==9 && PROJ_VERSION_MINOR>=2)
2846 case PJ_TYPE_DERIVED_PROJECTED_CRS:
2849 case PJ_TYPE_COORDINATE_METADATA:
2852 case PJ_TYPE_OTHER_CRS:
2861 proj4.replace( QLatin1String(
"+type=crs" ), QString() );
2862 proj4 = proj4.trimmed();
2864 if ( proj4.isEmpty() )
2866 QgsDebugMsgLevel( QStringLiteral(
"No proj4 for '%1:%2'" ).arg( authority, code ), 2 );
2877 if ( translatedOperation.isEmpty() && !
operation.isEmpty() )
2879 std::cout << QStringLiteral(
"Operation needs translation in QgsCoordinateReferenceSystemUtils::translateProjection: %1" ).arg(
operation ).toLocal8Bit().constData() << std::endl;
2880 qFatal(
"aborted" );
2883 const bool deprecated = proj_is_deprecated(
crs.get() );
2884 const QString name( proj_get_name(
crs.get() ) );
2886 QString sql = QStringLiteral(
"SELECT parameters,description,deprecated,srs_type,projection_acronym FROM tbl_srs WHERE auth_name='%1' AND auth_id='%2'" ).arg( authority, code );
2887 statement = database.
prepare( sql, result );
2888 if ( result != SQLITE_OK )
2897 QString dbOperation;
2898 bool dbSrsDeprecated = deprecated;
2899 if ( statement.
step() == SQLITE_ROW )
2903 dbSrsDeprecated = statement.
columnAsText( 2 ).toInt() != 0;
2908 if ( !dbSrsProj4.isEmpty() || !dbSrsDesc.isEmpty() )
2910 if ( proj4 != dbSrsProj4 || name != dbSrsDesc || deprecated != dbSrsDeprecated || dbSrsType != srsTypeString || dbOperation !=
operation )
2913 sql = QStringLiteral(
"UPDATE tbl_srs SET parameters=%1,description=%2,deprecated=%3, srs_type=%4,projection_acronym=%5 WHERE auth_name=%6 AND auth_id=%7" )
2916 .arg( deprecated ? 1 : 0 )
2921 if ( sqlite3_exec( database.get(), sql.toUtf8(),
nullptr,
nullptr, &errMsg ) != SQLITE_OK )
2923 QgsDebugError( QStringLiteral(
"Could not execute: %1 [%2/%3]\n" ).arg(
2926 errMsg ? errMsg :
"(unknown error)" ) );
2928 sqlite3_free( errMsg );
2942 const QString dbVals =
sAuthIdToQgisSrsIdMap.value( QStringLiteral(
"%1:%2" ).arg( authority, code ) );
2945 if ( !dbVals.isEmpty() )
2947 const QStringList parts = dbVals.split(
',' );
2948 srsId = parts.at( 0 );
2949 srId = parts.at( 1 );
2951 if ( srId.isEmpty() )
2953 srId = QString::number( nextSrId );
2956 if ( srsId.isEmpty() )
2958 srsId = QString::number( nextSrsId );
2962 if ( !srsId.isEmpty() )
2964 sql = QStringLiteral(
"INSERT INTO tbl_srs(srs_id, description,projection_acronym,ellipsoid_acronym,parameters,srid,auth_name,auth_id,is_geo,deprecated,srs_type) VALUES (%1, %2,%3,%4,%5,%6,%7,%8,%9,%10,%11)" )
2974 .arg( deprecated ? 1 : 0 )
2979 sql = QStringLiteral(
"INSERT INTO tbl_srs(description,projection_acronym,ellipsoid_acronym,parameters,srid,auth_name,auth_id,is_geo,deprecated,srs_type) VALUES (%1,%2,%3,%4,%5,%6,%7,%8,%9,%10)" )
2988 .arg( deprecated ? 1 : 0 )
2993 if ( sqlite3_exec( database.get(), sql.toUtf8(),
nullptr,
nullptr, &errMsg ) == SQLITE_OK )
2999 qCritical(
"Could not execute: %s [%s/%s]\n",
3000 sql.toLocal8Bit().constData(),
3001 sqlite3_errmsg( database.get() ),
3002 errMsg ? errMsg :
"(unknown error)" );
3006 sqlite3_free( errMsg );
3011 proj_string_list_destroy( codes );
3013 const QString sql = QStringLiteral(
"DELETE FROM tbl_srs WHERE auth_name='%1' AND NOT auth_id IN (%2)" ).arg( authority, allCodes.join(
',' ) );
3014 if ( sqlite3_exec( database.get(), sql.toUtf8(),
nullptr,
nullptr,
nullptr ) == SQLITE_OK )
3016 deleted = sqlite3_changes( database.get() );
3021 qCritical(
"Could not execute: %s [%s]\n",
3022 sql.toLocal8Bit().constData(),
3023 sqlite3_errmsg( database.get() ) );
3027 proj_string_list_destroy( authorities );
3029 QString sql = QStringLiteral(
"UPDATE tbl_info set proj_major=%1,proj_minor=%2,proj_patch=%3" )
3030 .arg( QString::number( PROJ_VERSION_MAJOR ),
3031 QString::number( PROJ_VERSION_MINOR ),
3032 QString::number( PROJ_VERSION_PATCH ) );
3033 if ( sqlite3_exec( database.get(), sql.toUtf8(),
nullptr,
nullptr, &errMsg ) != SQLITE_OK )
3035 QgsDebugError( QStringLiteral(
"Could not execute: %1 [%2/%3]\n" ).arg(
3038 errMsg ? errMsg :
"(unknown error)" ) );
3040 sqlite3_free( errMsg );
3044 if ( sqlite3_exec( database.get(),
"COMMIT",
nullptr,
nullptr,
nullptr ) != SQLITE_OK )
3046 QgsDebugError( QStringLiteral(
"Could not commit transaction: %1 [%2]\n" ).arg(
3048 sqlite3_errmsg( database.get() ) )
3054 QgsDebugMsgLevel( QStringLiteral(
"CRS update (inserted:%1 updated:%2 deleted:%3 errors:%4)" ).arg( inserted ).arg( updated ).arg( deleted ).arg( errors ), 4 );
3062 return updated + inserted;
3065const QHash<QString, QgsCoordinateReferenceSystem> &QgsCoordinateReferenceSystem::stringCache()
3067 return *sStringCache();
3070const QHash<QString, QgsCoordinateReferenceSystem> &QgsCoordinateReferenceSystem::projCache()
3072 return *sProj4Cache();
3075const QHash<QString, QgsCoordinateReferenceSystem> &QgsCoordinateReferenceSystem::ogcCache()
3077 return *sOgcCache();
3080const QHash<QString, QgsCoordinateReferenceSystem> &QgsCoordinateReferenceSystem::wktCache()
3082 return *sWktCache();
3085const QHash<long, QgsCoordinateReferenceSystem> &QgsCoordinateReferenceSystem::srIdCache()
3087 return *sSrIdCache();
3090const QHash<long, QgsCoordinateReferenceSystem> &QgsCoordinateReferenceSystem::srsIdCache()
3092 return *sSrsIdCache();
3102 if (
PJ *obj = d->threadLocalProjObject() )
3109 const PJ_TYPE pjType = proj_get_type( geoCrs.get( ) );
3110 if ( pjType == PJ_TYPE_GEOCENTRIC_CRS )
3116 pjContext, PJ_ELLPS2D_LONGITUDE_LATITUDE,
"Degree", 1.0 ) );
3120 pjContext,
nullptr, datum ? datum.get() :
datumEnsemble.get(),
3123 if ( !geoGraphicCrs )
3150 if (
PJ *obj = d->threadLocalProjObject() )
3204 if (
PJ *obj = d->threadLocalProjObject() )
3237 if (
PJ *obj = d->threadLocalProjObject() )
3248 if (
PJ *obj = d->threadLocalProjObject() )
3261 else if (
PJ *obj = d->threadLocalProjObject() )
3264 return geoCrs ? QStringLiteral(
"%1:%2" ).arg( proj_get_id_auth_name( geoCrs.get(), 0 ), proj_get_id_code( geoCrs.get(), 0 ) ) : QString();
3274 return d->threadLocalProjObject();
3287 d->mIsValid =
false;
3289 d->mWktPreferred.clear();
3296 switch ( proj_get_type(
object ) )
3298 case PJ_TYPE_GEODETIC_CRS:
3299 case PJ_TYPE_GEOCENTRIC_CRS:
3300 case PJ_TYPE_GEOGRAPHIC_CRS:
3301 case PJ_TYPE_GEOGRAPHIC_2D_CRS:
3302 case PJ_TYPE_GEOGRAPHIC_3D_CRS:
3303 case PJ_TYPE_VERTICAL_CRS:
3304 case PJ_TYPE_PROJECTED_CRS:
3305 case PJ_TYPE_COMPOUND_CRS:
3306 case PJ_TYPE_TEMPORAL_CRS:
3307 case PJ_TYPE_ENGINEERING_CRS:
3308 case PJ_TYPE_BOUND_CRS:
3309 case PJ_TYPE_OTHER_CRS:
3325 const QString authName( proj_get_id_auth_name( d->threadLocalProjObject(), 0 ) );
3326 const QString authCode( proj_get_id_code( d->threadLocalProjObject(), 0 ) );
3327 if ( !authName.isEmpty() && !authCode.isEmpty() &&
createFromOgcWmsCrs( QStringLiteral(
"%1:%2" ).arg( authName, authCode ) ) )
3335 d->mDescription = QString( proj_get_name( d->threadLocalProjObject() ) );
3346 QStringList projections;
3348 projections.reserve( res.size() );
3351 projections << QString::number(
crs.
srsid() );
3378 sSrIdCacheLock()->lockForWrite();
3379 if ( !sDisableSrIdCache )
3382 sDisableSrIdCache =
true;
3383 sSrIdCache()->clear();
3385 sSrIdCacheLock()->unlock();
3387 sOgcLock()->lockForWrite();
3388 if ( !sDisableOgcCache )
3391 sDisableOgcCache =
true;
3392 sOgcCache()->clear();
3394 sOgcLock()->unlock();
3396 sProj4CacheLock()->lockForWrite();
3397 if ( !sDisableProjCache )
3400 sDisableProjCache =
true;
3401 sProj4Cache()->clear();
3403 sProj4CacheLock()->unlock();
3405 sCRSWktLock()->lockForWrite();
3406 if ( !sDisableWktCache )
3409 sDisableWktCache =
true;
3410 sWktCache()->clear();
3412 sCRSWktLock()->unlock();
3414 sCRSSrsIdLock()->lockForWrite();
3415 if ( !sDisableSrsIdCache )
3418 sDisableSrsIdCache =
true;
3419 sSrsIdCache()->clear();
3421 sCRSSrsIdLock()->unlock();
3423 sCrsStringLock()->lockForWrite();
3424 if ( !sDisableStringCache )
3427 sDisableStringCache =
true;
3428 sStringCache()->clear();
3430 sCrsStringLock()->unlock();
3439 if ( !c1.d->mIsValid && !c2.d->mIsValid )
3442 if ( !c1.d->mIsValid && c2.d->mIsValid )
3445 if ( c1.d->mIsValid && !c2.d->mIsValid )
3451 if ( c1IsUser && !c2IsUser )
3454 if ( !c1IsUser && c2IsUser )
3457 if ( !c1IsUser && !c2IsUser && !c1.d->mAuthId.isEmpty() && !c2.d->mAuthId.isEmpty() )
3459 if ( c1.d->mAuthId != c2.d->mAuthId )
3460 return c1.d->mAuthId > c2.d->mAuthId;
3468 if ( c1.d->mCoordinateEpoch == c2.d->mCoordinateEpoch )
3471 if ( std::isnan( c1.d->mCoordinateEpoch ) && std::isnan( c2.d->mCoordinateEpoch ) )
3474 if ( std::isnan( c1.d->mCoordinateEpoch ) && !std::isnan( c2.d->mCoordinateEpoch ) )
3477 if ( !std::isnan( c1.d->mCoordinateEpoch ) && std::isnan( c2.d->mCoordinateEpoch ) )
3480 return c1.d->mCoordinateEpoch > c2.d->mCoordinateEpoch;
3488 if ( !c1.d->mIsValid && !c2.d->mIsValid )
3491 if ( c1.d->mIsValid && !c2.d->mIsValid )
3494 if ( !c1.d->mIsValid && c2.d->mIsValid )
3500 if ( !c1IsUser && c2IsUser )
3503 if ( c1IsUser && !c2IsUser )
3506 if ( !c1IsUser && !c2IsUser && !c1.d->mAuthId.isEmpty() && !c2.d->mAuthId.isEmpty() )
3508 if ( c1.d->mAuthId != c2.d->mAuthId )
3509 return c1.d->mAuthId < c2.d->mAuthId;
3517 if ( c1.d->mCoordinateEpoch == c2.d->mCoordinateEpoch )
3520 if ( std::isnan( c1.d->mCoordinateEpoch ) && std::isnan( c2.d->mCoordinateEpoch ) )
3523 if ( !std::isnan( c1.d->mCoordinateEpoch ) && std::isnan( c2.d->mCoordinateEpoch ) )
3526 if ( std::isnan( c1.d->mCoordinateEpoch ) && !std::isnan( c2.d->mCoordinateEpoch ) )
3529 return c1.d->mCoordinateEpoch < c2.d->mCoordinateEpoch;
3534 return !( c1 < c2 );
3538 return !( c1 > c2 );
1311 if ( d->mEllipsoidAcronym.isNull() ) {
…}
CrsIdentifierType
Available identifier string types for representing coordinate reference systems.
@ ShortString
A heavily abbreviated string, for use when a compact representation is required.
@ MediumString
A medium-length string, recommended for general purpose use.
DistanceUnit
Units of distance.
@ YardsBritishSears1922Truncated
British yards (Sears 1922 truncated)
@ MilesUSSurvey
US Survey miles.
@ LinksBritishSears1922
British links (Sears 1922)
@ YardsBritishBenoit1895A
British yards (Benoit 1895 A)
@ LinksBritishBenoit1895A
British links (Benoit 1895 A)
@ Centimeters
Centimeters.
@ YardsIndian1975
Indian yards (1975)
@ FeetUSSurvey
US Survey feet.
@ Millimeters
Millimeters.
@ FeetBritishSears1922
British feet (Sears 1922)
@ YardsClarkes
Clarke's yards.
@ YardsIndian
Indian yards.
@ FeetBritishBenoit1895B
British feet (Benoit 1895 B)
@ Miles
Terrestrial miles.
@ LinksUSSurvey
US Survey links.
@ ChainsUSSurvey
US Survey chains.
@ FeetClarkes
Clarke's feet.
@ Unknown
Unknown distance unit.
@ FeetBritish1936
British feet (1936)
@ FeetIndian1962
Indian feet (1962)
@ YardsBritishSears1922
British yards (Sears 1922)
@ FeetIndian1937
Indian feet (1937)
@ YardsIndian1937
Indian yards (1937)
@ Degrees
Degrees, for planar geographic CRS distance measurements.
@ ChainsBritishBenoit1895B
British chains (Benoit 1895 B)
@ LinksBritishSears1922Truncated
British links (Sears 1922 truncated)
@ ChainsBritishBenoit1895A
British chains (Benoit 1895 A)
@ YardsBritishBenoit1895B
British yards (Benoit 1895 B)
@ FeetBritish1865
British feet (1865)
@ YardsIndian1962
Indian yards (1962)
@ FeetBritishSears1922Truncated
British feet (Sears 1922 truncated)
@ MetersGermanLegal
German legal meter.
@ LinksBritishBenoit1895B
British links (Benoit 1895 B)
@ ChainsInternational
International chains.
@ LinksInternational
International links.
@ ChainsBritishSears1922Truncated
British chains (Sears 1922 truncated)
@ FeetIndian
Indian (geodetic) feet.
@ NauticalMiles
Nautical miles.
@ ChainsClarkes
Clarke's chains.
@ LinksClarkes
Clarke's links.
@ ChainsBritishSears1922
British chains (Sears 1922)
@ FeetIndian1975
Indian feet (1975)
@ FeetGoldCoast
Gold Coast feet.
@ FeetBritishBenoit1895A
British feet (Benoit 1895 A)
@ Critical
Critical/error message.
CrsType
Coordinate reference system types.
@ Compound
Compound (horizontal + vertical) CRS.
@ Projected
Projected CRS.
@ DerivedProjected
Derived projected CRS.
@ Engineering
Engineering CRS.
@ Geographic3d
3D geopraphic CRS
@ Geographic2d
2D geographic CRS
@ Geocentric
Geocentric CRS.
CrsDefinitionFormat
CRS definition formats.
@ Wkt
WKT format (always recommended over proj string format)
static const int USER_CRS_START_ID
Minimum ID number for a user-defined projection.
CrsAxisDirection
Coordinate reference system axis directions.
@ ColumnPositive
Column positive.
@ SouthSouthEast
South South East.
@ ColumnNegative
Column negative.
@ RowPositive
Row positive.
@ DisplayDown
Display down.
@ GeocentricZ
Geocentric (Z)
@ DisplayRight
Display right.
@ WestSouthWest
West South West.
@ RowNegative
Row negative.
@ NorthNorthEast
North North East.
@ EastNorthEast
East North East.
@ Unspecified
Unspecified.
@ NorthNorthWest
North North West.
@ GeocentricY
Geocentric (Y)
@ CounterClockwise
Counter clockwise.
@ SouthSouthWest
South South West.
@ DisplayLeft
Display left.
@ WestNorthWest
West North West.
@ EastSouthEast
East South East.
@ GeocentricX
Geocentric (X)
CrsWktVariant
Coordinate reference system WKT formatting variants.
@ Wkt2_2019Simplified
WKT2_2019 with the simplification rule of WKT2_SIMPLIFIED.
@ Wkt2_2015Simplified
Same as WKT2_2015 with the following exceptions: UNIT keyword used. ID node only on top element....
@ Wkt1Esri
WKT1 as traditionally output by ESRI software, deriving from OGC 99-049.
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
@ Wkt2_2019
Full WKT2 string, conforming to ISO 19162:2019 / OGC 18-010, with all possible nodes and new keyword ...
@ Wkt2_2015
Full WKT2 string, conforming to ISO 19162:2015(E) / OGC 12-063r5 with all possible nodes and new keyw...
@ Wkt1Gdal
WKT1 as traditionally output by GDAL, deriving from OGC 01-009. A notable departure from WKT1_GDAL wi...
static QgsCoordinateReferenceSystemRegistry * coordinateReferenceSystemRegistry()
Returns the application's coordinate reference system (CRS) registry, which handles known CRS definit...
static QString qgisUserDatabaseFilePath()
Returns the path to the user qgis.db file.
static QString srsDatabaseFilePath()
Returns the path to the srs.db file.
void removeRecent(const QgsCoordinateReferenceSystem &crs)
Removes a CRS from the list of recently used CRS.
long addUserCrs(const QgsCoordinateReferenceSystem &crs, const QString &name, Qgis::CrsDefinitionFormat nativeFormat=Qgis::CrsDefinitionFormat::Wkt)
Adds a new crs definition as a custom ("USER") CRS.
void clearRecent()
Cleans the list of recently used CRS.
QList< QgsCoordinateReferenceSystem > recentCrs()
Returns a list of recently used CRS.
void pushRecent(const QgsCoordinateReferenceSystem &crs)
Pushes a recently used CRS to the top of the recent CRS list.
QMap< QString, QgsProjOperation > projOperations() const
Returns a map of all valid PROJ operations.
static QString translateProjection(const QString &projection)
Returns a translated string for a projection method.
Represents a coordinate reference system (CRS).
static QgsCoordinateReferenceSystem fromOgcWmsCrs(const QString &ogcCrs)
Creates a CRS from a given OGC WMS-format Coordinate Reference System string.
bool createFromOgcWmsCrs(const QString &crs)
Sets this CRS to the given OGC WMS-format Coordinate Reference Systems.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
bool createFromWkt(const QString &wkt)
Sets this CRS using a WKT definition.
bool createFromString(const QString &definition)
Set up this CRS from a string definition.
bool hasVerticalAxis() const
Returns true if the CRS has a vertical axis.
void validate()
Perform some validation on this CRS.
~QgsCoordinateReferenceSystem()
QgsRectangle bounds() const
Returns the approximate bounds for the region the CRS is usable within.
QString toProj() const
Returns a Proj string representation of this CRS.
static CUSTOM_CRS_VALIDATION customCrsValidation()
Gets custom function.
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
static QgsCoordinateReferenceSystem createCompoundCrs(const QgsCoordinateReferenceSystem &horizontalCrs, const QgsCoordinateReferenceSystem &verticalCrs, QString &error)
Given a horizontal and vertical CRS, attempts to create a compound CRS from them.
static Q_INVOKABLE QgsCoordinateReferenceSystem fromEpsgId(long epsg)
Creates a CRS from a given EPSG ID.
Q_DECL_DEPRECATED bool createFromProj4(const QString &projString)
Sets this CRS by passing it a PROJ style formatted string.
static Q_DECL_DEPRECATED QStringList recentProjections()
Returns a list of recently used projections.
QString ellipsoidAcronym() const
Returns the ellipsoid acronym for the ellipsoid used by the CRS.
QString toOgcUri() const
Returns the crs as OGC URI (format: http://www.opengis.net/def/crs/OGC/1.3/CRS84) Returns an empty st...
QString toOgcUrn() const
Returns the crs as OGC URN (format: urn:ogc:def:crs:OGC:1.3:CRS84) Returns an empty string on failure...
static void setCustomCrsValidation(CUSTOM_CRS_VALIDATION f)
Sets custom function to force valid CRS.
static Q_DECL_DEPRECATED void pushRecentCoordinateReferenceSystem(const QgsCoordinateReferenceSystem &crs)
Pushes a recently used CRS to the top of the recent CRS list.
long postgisSrid() const
Returns PostGIS SRID for the CRS.
QgsCoordinateReferenceSystem horizontalCrs() const
Returns the horizontal CRS associated with this CRS object.
Q_DECL_DEPRECATED long findMatchingProj()
Walks the CRS databases (both system and user database) trying to match stored PROJ string to a datab...
static QList< long > validSrsIds()
Returns a list of all valid SRS IDs present in the CRS database.
QgsProjectionFactors factors(const QgsPoint &point) const
Calculate various cartographic properties, such as scale factors, angular distortion and meridian con...
void setValidationHint(const QString &html)
Set user hint for validation.
Q_DECL_DEPRECATED QString toProj4() const
Returns a Proj string representation of this CRS.
bool operator==(const QgsCoordinateReferenceSystem &srs) const
QString projectionAcronym() const
Returns the projection acronym for the projection used by the CRS.
QString userFriendlyIdentifier(Qgis::CrsIdentifierType type=Qgis::CrsIdentifierType::MediumString) const
Returns a user friendly identifier for the CRS.
Qgis::CrsDefinitionFormat nativeFormat() const
Returns the native format for the CRS definition.
CrsType
Enumeration of types of IDs accepted in createFromId() method.
@ InternalCrsId
Internal ID used by QGIS in the local SQLite database.
@ PostgisCrsId
SRID used in PostGIS. DEPRECATED – DO NOT USE.
bool createFromUserInput(const QString &definition)
Set up this CRS from various text formats.
QgsCoordinateReferenceSystem()
Constructs an invalid CRS object.
static int syncDatabase()
Update proj.4 parameters in our database from proj.4.
bool operator!=(const QgsCoordinateReferenceSystem &srs) const
void setNativeFormat(Qgis::CrsDefinitionFormat format)
Sets the native format for the CRS definition.
bool createFromProj(const QString &projString, bool identify=true)
Sets this CRS by passing it a PROJ style formatted string.
QgsCoordinateReferenceSystem verticalCrs() const
Returns the vertical CRS associated with this CRS object.
static Q_DECL_DEPRECATED void removeRecentCoordinateReferenceSystem(const QgsCoordinateReferenceSystem &crs)
Removes a CRS from the list of recently used CRS.
QgsDatumEnsemble datumEnsemble() const
Attempts to retrieve datum ensemble details from the CRS.
QgsCoordinateReferenceSystem toGeographicCrs() const
Returns the geographic CRS associated with this CRS object.
bool isDynamic() const
Returns true if the CRS is a dynamic CRS.
bool createFromSrsId(long srsId)
Sets this CRS by lookup of internal QGIS CRS ID in the CRS database.
Q_DECL_DEPRECATED bool createFromId(long id, CrsType type=PostgisCrsId)
Sets this CRS by lookup of the given ID in the CRS database.
static QgsCoordinateReferenceSystem fromProjObject(PJ *object)
Constructs a QgsCoordinateReferenceSystem from a PROJ PJ object.
static void invalidateCache(bool disableCache=false)
Clears the internal cache used to initialize QgsCoordinateReferenceSystem objects.
QgsCoordinateReferenceSystem & operator=(const QgsCoordinateReferenceSystem &srs)
void updateDefinition()
Updates the definition and parameters of the coordinate reference system to their latest values.
static QgsCoordinateReferenceSystem fromProj(const QString &proj)
Creates a CRS from a proj style formatted string.
static Q_DECL_DEPRECATED void setupESRIWktFix()
Make sure that ESRI WKT import is done properly.
static Q_DECL_DEPRECATED QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems()
Returns a list of recently used CRS.
QString toWkt(Qgis::CrsWktVariant variant=Qgis::CrsWktVariant::Wkt1Gdal, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
PJ * projObject() const
Returns the underlying PROJ PJ object corresponding to the CRS, or nullptr if the CRS is invalid.
void setCoordinateEpoch(double epoch)
Sets the coordinate epoch, as a decimal year.
Q_DECL_DEPRECATED bool createFromSrid(long srid)
Sets this CRS by lookup of the given PostGIS SRID in the CRS database.
long saveAsUserCrs(const QString &name, Qgis::CrsDefinitionFormat nativeFormat=Qgis::CrsDefinitionFormat::Wkt)
Saves the CRS as a new custom ("USER") CRS.
static Q_DECL_DEPRECATED void clearRecentCoordinateReferenceSystems()
Cleans the list of recently used CRS.
QString celestialBodyName() const
Attempts to retrieve the name of the celestial body associated with the CRS (e.g.
static QgsCoordinateReferenceSystem fromWkt(const QString &wkt)
Creates a CRS from a WKT spatial ref sys definition string.
bool writeXml(QDomNode &node, QDomDocument &doc) const
Stores state to the given Dom node in the given document.
static QgsCoordinateReferenceSystem createGeocentricCrs(const QString &ellipsoid)
Creates a geocentric CRS given an ellipsoid definition.
static QgsCoordinateReferenceSystem fromSrsId(long srsId)
Creates a CRS from a specified QGIS SRS ID.
double coordinateEpoch() const
Returns the coordinate epoch, as a decimal year.
QString geographicCrsAuthId() const
Returns auth id of related geographic CRS.
QString validationHint() const
Gets user hint for validation.
QgsProjOperation operation() const
Returns information about the PROJ operation associated with the coordinate reference system,...
QgsCoordinateReferenceSystem toGeocentricCrs() const
Returns a new geocentric CRS based on this CRS object.
QList< Qgis::CrsAxisDirection > axisOrdering() const
Returns an ordered list of the axis directions reflecting the native axis order for the CRS.
long srsid() const
Returns the internal CRS ID, if available.
Qgis::CrsType type() const
Returns the type of the CRS.
Qgis::DistanceUnit mapUnits
bool hasAxisInverted() const
Returns whether the axis order is inverted for the CRS compared to the order east/north (longitude/la...
bool createFromProjObject(PJ *object)
Sets this CRS by passing it a PROJ PJ object, corresponding to a PROJ CRS object.
bool isDeprecated() const
Returns true if the CRS is considered deprecated.
static Q_DECL_DEPRECATED QgsCoordinateReferenceSystem fromProj4(const QString &proj4)
Creates a CRS from a proj style formatted string.
Contains information about a member of a datum ensemble.
Contains information about a datum ensemble.
static EllipsoidParameters ellipsoidParameters(const QString &ellipsoid)
Returns the parameters for the specified ellipsoid.
Sets the current locale to the c locale for the lifetime of the object.
static void warning(const QString &msg)
Goes to qWarning.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
@ UNKNOWN
Unknown/unhandled flavor.
@ AUTH_CODE
E.g EPSG:4326.
static CRSFlavor parseCrsName(const QString &crsName, QString &authority, QString &code)
Parse a CRS name in one of the flavors of OGC services, and decompose it as authority and code.
static QString OGRSpatialReferenceToWkt(OGRSpatialReferenceH srs)
Returns a WKT string corresponding to the specified OGR srs object.
Point geometry type, with support for z-dimension and m-values.
static PJ_CONTEXT * get()
Returns a thread local instance of a proj context, safe for use in the current thread.
Contains information about a PROJ operation.
static proj_pj_unique_ptr crsToHorizontalCrs(const PJ *crs)
Given a PROJ crs (which may be a compound or bound crs, or some other type), extract the horizontal c...
@ FlagMatchBoundCrsToUnderlyingSourceCrs
Allow matching a BoundCRS object to its underlying SourceCRS.
static proj_pj_unique_ptr createCompoundCrs(const PJ *horizontalCrs, const PJ *verticalCrs, QStringList *errors=nullptr)
Given a PROJ horizontal and vertical CRS, attempt to create a compound CRS from them.
static bool isDynamic(const PJ *crs)
Returns true if the given proj coordinate system is a dynamic CRS.
static proj_pj_unique_ptr unboundCrs(const PJ *crs)
Given a PROJ crs (which may be a compound or bound crs, or some other type), ensure that it is not a ...
static bool identifyCrs(const PJ *crs, QString &authName, QString &authCode, IdentifyFlags flags=IdentifyFlags())
Attempts to identify a crs, matching it to a known authority and code within an acceptable level of t...
static bool hasVerticalAxis(const PJ *crs)
Returns true if a PROJ crs has a vertical axis.
static proj_pj_unique_ptr crsToVerticalCrs(const PJ *crs)
Given a PROJ crs (which may be a compound crs, or some other type), extract the vertical crs from it.
static proj_pj_unique_ptr crsToDatumEnsemble(const PJ *crs)
Given a PROJ crs, attempt to retrieve the datum ensemble from it.
std::unique_ptr< PJ, ProjPJDeleter > proj_pj_unique_ptr
Scoped Proj PJ object.
static bool axisOrderIsSwapped(const PJ *crs)
Returns true if the given proj coordinate system uses requires y/x coordinate order instead of x/y.
Contains various cartographic properties, such as scale factors, angular distortion and meridian conv...
A convenience class that simplifies locking and unlocking QReadWriteLocks.
void unlock()
Unlocks the lock.
void changeMode(Mode mode)
Change the mode of the lock to mode.
A rectangle specified with double values.
void setYMinimum(double y)
Set the minimum y value.
void setXMinimum(double x)
Set the minimum x value.
void setYMaximum(double y)
Set the maximum y value.
void setXMaximum(double x)
Set the maximum x value.
static QString quotedString(const QString &value)
Returns a quoted string value, surround by ' characters and with special characters correctly escaped...
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
sqlite3_statement_unique_ptr prepare(const QString &sql, int &resultCode) const
Prepares a sql statement, returning the result.
int open(const QString &path)
Opens the database at the specified file path.
QString errorMessage() const
Returns the most recent error message encountered by the database.
int open_v2(const QString &path, int flags, const char *zVfs)
Opens the database at the specified file path.
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
QString columnAsText(int column) const
Returns the column value from the current statement row as a string.
QString columnName(int column) const
Returns the name of column.
int step()
Steps to the next record in the statement, returning the sqlite3 result code.
qlonglong columnAsInt64(int column) const
Gets column value from the current statement row as a long long integer (64 bits).
int columnCount() const
Gets the number of columns that this statement returns.
#define Q_NOWARN_DEPRECATED_POP
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
#define Q_NOWARN_DEPRECATED_PUSH
bool qgsNanCompatibleEquals(double a, double b)
Compare two doubles, treating nan values as equal.
QString getFullProjString(PJ *obj)
bool operator>=(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
bool operator<(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
bool testIsGeographic(PJ *crs)
void getOperationAndEllipsoidFromProjString(const QString &proj, QString &operation, QString &ellipsoid)
QHash< QString, QgsCoordinateReferenceSystem > StringCrsCacheHash
bool operator<=(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
QHash< long, QgsCoordinateReferenceSystem > SrIdCrsCacheHash
bool operator>(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
const QMap< QString, QString > sAuthIdToQgisSrsIdMap
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)
const QgsCoordinateReferenceSystem & crs
Contains parameters for an ellipsoid.
double semiMajor
Semi-major axis, in meters.
bool valid
Whether ellipsoid parameters are valid.
double inverseFlattening
Inverse flattening.