19#include "moc_qgscoordinatereferencesystem.cpp"
32#include <QRegularExpression>
52#include <proj_experimental.h>
55#include <ogr_srs_api.h>
67bool QgsCoordinateReferenceSystem::sDisableSrIdCache =
false;
71bool QgsCoordinateReferenceSystem::sDisableOgcCache =
false;
75bool QgsCoordinateReferenceSystem::sDisableProjCache =
false;
79bool QgsCoordinateReferenceSystem::sDisableWktCache =
false;
83bool QgsCoordinateReferenceSystem::sDisableSrsIdCache =
false;
87bool QgsCoordinateReferenceSystem::sDisableStringCache =
false;
96 if (
const char *proj4src = proj_as_proj_string(
QgsProjContext::get(), boundCrs.get(), PJ_PROJ_4,
nullptr ) )
98 return QString( proj4src );
115 d =
new QgsCoordinateReferenceSystemPrivate();
121 d =
new QgsCoordinateReferenceSystemPrivate();
129 , mValidationHint( srs.mValidationHint )
130 , mNativeFormat( srs.mNativeFormat )
137 mValidationHint = srs.mValidationHint;
138 mNativeFormat = srs.mNativeFormat;
148 const auto constDbs = dbs;
149 for (
const QString &db : constDbs )
151 QFileInfo myInfo( db );
152 if ( !myInfo.exists() )
162 int result = openDatabase( db, database );
163 if ( result != SQLITE_OK )
169 QString sql = QStringLiteral(
"select srs_id from tbl_srs" );
171 statement = database.
prepare( sql, rc );
175 int ret = statement.
step();
177 if ( ret == SQLITE_DONE )
183 if ( ret == SQLITE_ROW )
189 QgsMessageLog::logMessage( QObject::tr(
"SQLite error: %2\nSQL: %1" ).arg( sql, sqlite3_errmsg( database.get() ) ), QObject::tr(
"SpatiaLite" ) );
194 std::sort( results.begin(), results.end() );
250 if ( horizontalObj && verticalObj )
257 QStringList formattedErrorList;
258 for (
const QString &rawError : std::as_const( errors ) )
260 QString formattedError = rawError;
261 formattedError.replace( QLatin1String(
"proj_create_compound_crs: " ), QString() );
262 formattedErrorList.append( formattedError );
264 error = formattedErrorList.join(
'\n' );
291 QgsDebugError( QStringLiteral(
"Unexpected case reached!" ) );
298 if ( definition.isEmpty() )
302 if ( !sDisableStringCache )
304 QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = sStringCache()->constFind( definition );
305 if ( crsIt != sStringCache()->constEnd() )
308 *
this = crsIt.value();
315 const thread_local QRegularExpression reCrsId( QStringLiteral(
"^(epsg|esri|osgeo|ignf|ogc|nkg|zangi|iau_2015|iau2000|postgis|internal|user)\\:(\\w+)$" ), QRegularExpression::CaseInsensitiveOption );
316 QRegularExpressionMatch match = reCrsId.match( definition );
317 if ( match.capturedStart() == 0 )
319 QString authName = match.captured( 1 ).toLower();
320 if ( authName == QLatin1String(
"epsg" ) )
324 else if ( authName == QLatin1String(
"postgis" ) )
326 const long id = match.captured( 2 ).toLong();
331 else if ( authName == QLatin1String(
"esri" )
332 || authName == QLatin1String(
"osgeo" )
333 || authName == QLatin1String(
"ignf" )
334 || authName == QLatin1String(
"zangi" )
335 || authName == QLatin1String(
"iau2000" )
336 || authName == QLatin1String(
"ogc" )
337 || authName == QLatin1String(
"nkg" )
338 || authName == QLatin1String(
"iau_2015" )
345 const long id = match.captured( 2 ).toLong();
353 const thread_local QRegularExpression reCrsStr( QStringLiteral(
"^(?:(wkt|proj4|proj)\\:)?(.+)$" ), QRegularExpression::CaseInsensitiveOption );
354 match = reCrsStr.match( definition );
355 if ( match.capturedStart() == 0 )
357 if ( match.captured( 1 ).startsWith( QLatin1String(
"proj" ), Qt::CaseInsensitive ) )
369 if ( !sDisableStringCache )
370 sStringCache()->insert( definition, *
this );
376 if ( definition.isEmpty() )
382 if ( OSRSetFromUserInput(
crs, definition.toLocal8Bit().constData() ) == OGRERR_NONE )
385 OSRDestroySpatialReference(
crs );
395 const char *configOld = CPLGetConfigOption(
"GDAL_FIX_ESRI_WKT",
"" );
396 const char *configNew =
"GEOGCS";
398 if ( strcmp( configOld,
"" ) == 0 )
400 CPLSetConfigOption(
"GDAL_FIX_ESRI_WKT", configNew );
401 if ( strcmp( configNew, CPLGetConfigOption(
"GDAL_FIX_ESRI_WKT",
"" ) ) != 0 )
403 .arg( configNew, CPLGetConfigOption(
"GDAL_FIX_ESRI_WKT",
"" ) ) );
404 QgsDebugMsgLevel( QStringLiteral(
"set GDAL_FIX_ESRI_WKT : %1" ).arg( configNew ), 4 );
408 QgsDebugMsgLevel( QStringLiteral(
"GDAL_FIX_ESRI_WKT was already set : %1" ).arg( configNew ), 4 );
418 if ( !sDisableOgcCache )
420 QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = sOgcCache()->constFind(
crs );
421 if ( crsIt != sOgcCache()->constEnd() )
424 *
this = crsIt.value();
430 QString wmsCrs =
crs;
435 const QString authorityLower = authority.toLower();
437 ( authorityLower == QLatin1String(
"user" ) ||
438 authorityLower == QLatin1String(
"custom" ) ||
439 authorityLower == QLatin1String(
"qgis" ) ) )
444 if ( !sDisableOgcCache )
445 sOgcCache()->insert(
crs, *
this );
451 wmsCrs = authority +
':' + code;
455 const QString legacyKey = wmsCrs.toLower();
458 if ( it.key().compare( legacyKey, Qt::CaseInsensitive ) == 0 )
460 const QStringList parts = it.key().split(
':' );
461 const QString auth = parts.at( 0 );
462 const QString code = parts.at( 1 );
463 if ( loadFromAuthCode( auth, code ) )
466 if ( !sDisableOgcCache )
467 sOgcCache()->insert(
crs, *
this );
476 if ( !sDisableOgcCache )
477 sOgcCache()->insert(
crs, *
this );
482 if ( wmsCrs.compare( QLatin1String(
"CRS:27" ), Qt::CaseInsensitive ) == 0 ||
483 wmsCrs.compare( QLatin1String(
"OGC:CRS27" ), Qt::CaseInsensitive ) == 0 )
490 if ( wmsCrs.compare( QLatin1String(
"CRS:83" ), Qt::CaseInsensitive ) == 0 ||
491 wmsCrs.compare( QLatin1String(
"OGC:CRS83" ), Qt::CaseInsensitive ) == 0 )
498 if ( wmsCrs.compare( QLatin1String(
"CRS:84" ), Qt::CaseInsensitive ) == 0 ||
499 wmsCrs.compare( QLatin1String(
"OGC:CRS84" ), Qt::CaseInsensitive ) == 0 )
503 d->mAxisInverted =
false;
504 d->mAxisInvertedDirty =
false;
508 if ( !sDisableOgcCache )
509 sOgcCache()->insert(
crs, *
this );
516 if ( !authority.isEmpty() && !code.isEmpty() && loadFromAuthCode( authority, code ) )
519 if ( !sDisableOgcCache )
520 sOgcCache()->insert(
crs, *
this );
525 if ( !sDisableOgcCache )
535 if ( d->mIsValid || !sCustomSrsValidation )
539 if ( sCustomSrsValidation )
540 sCustomSrsValidation( *
this );
546 if ( !sDisableSrIdCache )
548 QHash< long, QgsCoordinateReferenceSystem >::const_iterator crsIt = sSrIdCache()->constFind(
id );
549 if ( crsIt != sSrIdCache()->constEnd() )
552 *
this = crsIt.value();
561 if ( it.value().endsWith( QStringLiteral(
",%1" ).arg(
id ) ) )
563 const QStringList parts = it.key().split(
':' );
564 const QString auth = parts.at( 0 );
565 const QString code = parts.at( 1 );
566 if ( loadFromAuthCode( auth, code ) )
569 if ( !sDisableSrIdCache )
570 sSrIdCache()->insert(
id, *
this );
580 if ( !sDisableSrIdCache )
581 sSrIdCache()->insert(
id, *
this );
589 if ( !sDisableSrsIdCache )
591 QHash< long, QgsCoordinateReferenceSystem >::const_iterator crsIt = sSrsIdCache()->constFind(
id );
592 if ( crsIt != sSrsIdCache()->constEnd() )
595 *
this = crsIt.value();
604 if ( it.value().startsWith( QString::number(
id ) +
',' ) )
606 const QStringList parts = it.key().split(
':' );
607 const QString auth = parts.at( 0 );
608 const QString code = parts.at( 1 );
609 if ( loadFromAuthCode( auth, code ) )
612 if ( !sDisableSrsIdCache )
613 sSrsIdCache()->insert(
id, *
this );
621 QStringLiteral(
"srs_id" ), QString::number(
id ) );
624 if ( !sDisableSrsIdCache )
625 sSrsIdCache()->insert(
id, *
this );
629bool QgsCoordinateReferenceSystem::loadFromDatabase(
const QString &db,
const QString &expression,
const QString &value )
633 QgsDebugMsgLevel(
"load CRS from " + db +
" where " + expression +
" is " + value, 3 );
635 d->mWktPreferred.clear();
637 QFileInfo myInfo( db );
638 if ( !myInfo.exists() )
648 myResult = openDatabase( db, database );
649 if ( myResult != SQLITE_OK )
666 QString mySql =
"select srs_id,description,projection_acronym,"
667 "ellipsoid_acronym,parameters,srid,auth_name||':'||auth_id,is_geo,wkt "
669 statement = database.
prepare( mySql, myResult );
672 if ( myResult == SQLITE_OK && statement.
step() == SQLITE_ROW )
677 d->mEllipsoidAcronym.clear();
679 d->mWktPreferred.clear();
682 d->mIsGeographic = statement.
columnAsText( 7 ).toInt() != 0;
684 d->mAxisInvertedDirty =
true;
686 if ( d->mSrsId >=
USER_CRS_START_ID && ( d->mAuthId.isEmpty() || d->mAuthId == QChar(
':' ) ) )
688 d->mAuthId = QStringLiteral(
"USER:%1" ).arg( d->mSrsId );
690 else if ( !d->mAuthId.startsWith( QLatin1String(
"USER:" ), Qt::CaseInsensitive ) )
692 QStringList parts = d->mAuthId.split(
':' );
693 QString auth = parts.at( 0 );
694 QString code = parts.at( 1 );
701 d->mIsValid = d->hasPj();
707 if ( !wkt.isEmpty() )
715 setProjString( d->mProj4 );
725void QgsCoordinateReferenceSystem::removeFromCacheObjectsBelongingToCurrentThread(
PJ_CONTEXT *pj_context )
732 if ( !sDisableSrIdCache )
735 if ( !sDisableSrIdCache )
737 for (
auto it = sSrIdCache()->begin(); it != sSrIdCache()->end(); )
739 auto &v = it.value();
740 if ( v.d->removeObjectsBelongingToCurrentThread( pj_context ) )
741 it = sSrIdCache()->erase( it );
747 if ( !sDisableOgcCache )
750 if ( !sDisableOgcCache )
752 for (
auto it = sOgcCache()->begin(); it != sOgcCache()->end(); )
754 auto &v = it.value();
755 if ( v.d->removeObjectsBelongingToCurrentThread( pj_context ) )
756 it = sOgcCache()->erase( it );
762 if ( !sDisableProjCache )
765 if ( !sDisableProjCache )
767 for (
auto it = sProj4Cache()->begin(); it != sProj4Cache()->end(); )
769 auto &v = it.value();
770 if ( v.d->removeObjectsBelongingToCurrentThread( pj_context ) )
771 it = sProj4Cache()->erase( it );
777 if ( !sDisableWktCache )
780 if ( !sDisableWktCache )
782 for (
auto it = sWktCache()->begin(); it != sWktCache()->end(); )
784 auto &v = it.value();
785 if ( v.d->removeObjectsBelongingToCurrentThread( pj_context ) )
786 it = sWktCache()->erase( it );
792 if ( !sDisableSrsIdCache )
795 if ( !sDisableSrsIdCache )
797 for (
auto it = sSrsIdCache()->begin(); it != sSrsIdCache()->end(); )
799 auto &v = it.value();
800 if ( v.d->removeObjectsBelongingToCurrentThread( pj_context ) )
801 it = sSrsIdCache()->erase( it );
807 if ( !sDisableStringCache )
810 if ( !sDisableStringCache )
812 for (
auto it = sStringCache()->begin(); it != sStringCache()->end(); )
814 auto &v = it.value();
815 if ( v.d->removeObjectsBelongingToCurrentThread( pj_context ) )
816 it = sStringCache()->erase( it );
826 if ( d->mAxisInvertedDirty )
829 d->mAxisInvertedDirty =
false;
832 return d->mAxisInverted;
849 const thread_local QMap< Qgis::CrsAxisDirection, QString > mapping =
892 QList< Qgis::CrsAxisDirection > res;
893 const int axisCount = proj_cs_get_axis_count( context, pjCs.get() );
896 res.reserve( axisCount );
898 for (
int i = 0; i < axisCount; ++i )
900 const char *outDirection =
nullptr;
901 proj_cs_get_axis_info( context, pjCs.get(), i,
911 const thread_local QRegularExpression rx( QStringLiteral(
"([^\\s]+).*" ) );
912 const QRegularExpressionMatch match = rx.match( QString( outDirection ) );
913 if ( !match.hasMatch() )
916 const QString direction = match.captured( 1 );
918 for (
auto it = mapping.constBegin(); it != mapping.constEnd(); ++it )
920 if ( it.value().compare( direction, Qt::CaseInsensitive ) == 0 )
935 return createFromWktInternal( wkt, QString() );
938bool QgsCoordinateReferenceSystem::createFromWktInternal(
const QString &wkt,
const QString &description )
946 if ( !sDisableWktCache )
948 QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = sWktCache()->constFind( wkt );
949 if ( crsIt != sWktCache()->constEnd() )
952 *
this = crsIt.value();
954 if ( !
description.isEmpty() && d->mDescription.isEmpty() )
959 sWktCache()->insert( wkt, *
this );
968 d->mWktPreferred.clear();
971 QgsDebugMsgLevel( QStringLiteral(
"theWkt is uninitialized, operation failed" ), 4 );
976 QgsCoordinateReferenceSystem::RecordMap record = getRecord(
"select * from tbl_srs where wkt=" +
QgsSqliteUtils::quotedString( wkt ) +
" order by deprecated" );
977 if ( !record.empty() )
979 long srsId = record[QStringLiteral(
"srs_id" )].toLong();
992 if ( d->mSrsId == 0 )
995 long id = matchToUserCrs();
1004 if ( !sDisableWktCache )
1005 sWktCache()->insert( wkt, *
this );
1023 if ( projString.isEmpty() )
1028 if ( projString.trimmed().isEmpty() )
1030 d->mIsValid =
false;
1032 d->mWktPreferred.clear();
1037 if ( !sDisableProjCache )
1039 QHash< QString, QgsCoordinateReferenceSystem >::const_iterator crsIt = sProj4Cache()->constFind( projString );
1040 if ( crsIt != sProj4Cache()->constEnd() )
1043 *
this = crsIt.value();
1057 QString myProj4String = projString.trimmed();
1058 myProj4String.remove( QStringLiteral(
"+type=crs" ) );
1059 myProj4String = myProj4String.trimmed();
1061 d->mIsValid =
false;
1062 d->mWktPreferred.clear();
1067 const QString projCrsString = myProj4String + ( myProj4String.contains( QStringLiteral(
"+type=crs" ) ) ? QString() : QStringLiteral(
" +type=crs" ) );
1075 const QString
authid = QStringLiteral(
"%1:%2" ).arg( authName, authCode );
1079 if ( !sDisableProjCache )
1080 sProj4Cache()->insert( projString, *
this );
1087 QgsCoordinateReferenceSystem::RecordMap myRecord = getRecord(
"select * from tbl_srs where parameters=" +
QgsSqliteUtils::quotedString( myProj4String ) +
" order by deprecated" );
1089 if ( !myRecord.empty() )
1091 id = myRecord[QStringLiteral(
"srs_id" )].toLong();
1100 setProjString( myProj4String );
1103 id = matchToUserCrs();
1112 setProjString( myProj4String );
1116 if ( !sDisableProjCache )
1117 sProj4Cache()->insert( projString, *
this );
1123QgsCoordinateReferenceSystem::RecordMap QgsCoordinateReferenceSystem::getRecord(
const QString &sql )
1125 QString myDatabaseFileName;
1126 QgsCoordinateReferenceSystem::RecordMap myMap;
1127 QString myFieldName;
1128 QString myFieldValue;
1135 QFileInfo myInfo( myDatabaseFileName );
1136 if ( !myInfo.exists() )
1138 QgsDebugError(
"failed : " + myDatabaseFileName +
" does not exist!" );
1143 myResult = openDatabase( myDatabaseFileName, database );
1144 if ( myResult != SQLITE_OK )
1149 statement = database.
prepare( sql, myResult );
1151 if ( myResult == SQLITE_OK && statement.
step() == SQLITE_ROW )
1155 for (
int myColNo = 0; myColNo < myColumnCount; myColNo++ )
1157 myFieldName = statement.
columnName( myColNo );
1159 myMap[myFieldName] = myFieldValue;
1161 if ( statement.
step() != SQLITE_DONE )
1163 QgsDebugMsgLevel( QStringLiteral(
"Multiple records found in srs.db" ), 4 );
1172 if ( myMap.empty() )
1175 QFileInfo myFileInfo;
1176 myFileInfo.setFile( myDatabaseFileName );
1177 if ( !myFileInfo.exists() )
1179 QgsDebugError( QStringLiteral(
"user qgis.db not found" ) );
1184 myResult = openDatabase( myDatabaseFileName, database );
1185 if ( myResult != SQLITE_OK )
1190 statement = database.
prepare( sql, myResult );
1192 if ( myResult == SQLITE_OK && statement.
step() == SQLITE_ROW )
1196 for (
int myColNo = 0; myColNo < myColumnCount; myColNo++ )
1198 myFieldName = statement.
columnName( myColNo );
1200 myMap[myFieldName] = myFieldValue;
1203 if ( statement.
step() != SQLITE_DONE )
1205 QgsDebugMsgLevel( QStringLiteral(
"Multiple records found in srs.db" ), 4 );
1236 if ( d->mDescription.isNull() )
1242 return d->mDescription;
1249 if ( !
authid().isEmpty() )
1259 id =
isValid() ? QObject::tr(
"Custom CRS" ) : QObject::tr(
"Unknown CRS" );
1261 id = QObject::tr(
"Custom CRS: %1" ).arg(
1264 else if ( !
toProj().isEmpty() )
1267 if ( !
id.isEmpty() && !std::isnan( d->mCoordinateEpoch ) )
1268 id += QStringLiteral(
" @ %1" ).arg(
qgsDoubleToString( d->mCoordinateEpoch, 3 ) );
1275 if ( d->mProjectionAcronym.isNull() )
1281 return d->mProjectionAcronym;
1287 if ( d->mEllipsoidAcronym.isNull() )
1289 if (
PJ *obj = d->threadLocalProjObject() )
1294 const QString ellipsoidAuthName( proj_get_id_auth_name( ellipsoid.get(), 0 ) );
1295 const QString ellipsoidAuthCode( proj_get_id_code( ellipsoid.get(), 0 ) );
1296 if ( !ellipsoidAuthName.isEmpty() && !ellipsoidAuthCode.isEmpty() )
1297 d->mEllipsoidAcronym = QStringLiteral(
"%1:%2" ).arg( ellipsoidAuthName, ellipsoidAuthCode );
1300 double semiMajor, semiMinor, invFlattening;
1301 int semiMinorComputed = 0;
1302 if ( proj_ellipsoid_get_parameters(
QgsProjContext::get(), ellipsoid.get(), &semiMajor, &semiMinor, &semiMinorComputed, &invFlattening ) )
1304 d->mEllipsoidAcronym = QStringLiteral(
"PARAMETER:%1:%2" ).arg(
qgsDoubleToString( semiMajor ),
1309 d->mEllipsoidAcronym.clear();
1314 return d->mEllipsoidAcronym;
1318 return d->mEllipsoidAcronym;
1332 if ( d->mProj4.isEmpty() )
1334 if (
PJ *obj = d->threadLocalProjObject() )
1340 return d->mProj4.trimmed();
1346 switch ( d->mProjType )
1348 case PJ_TYPE_UNKNOWN:
1351 case PJ_TYPE_ELLIPSOID:
1352 case PJ_TYPE_PRIME_MERIDIAN:
1353 case PJ_TYPE_GEODETIC_REFERENCE_FRAME:
1354 case PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME:
1355 case PJ_TYPE_VERTICAL_REFERENCE_FRAME:
1356 case PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME:
1357 case PJ_TYPE_DATUM_ENSEMBLE:
1358 case PJ_TYPE_CONVERSION:
1359 case PJ_TYPE_TRANSFORMATION:
1360 case PJ_TYPE_CONCATENATED_OPERATION:
1361 case PJ_TYPE_OTHER_COORDINATE_OPERATION:
1362 case PJ_TYPE_TEMPORAL_DATUM:
1363 case PJ_TYPE_ENGINEERING_DATUM:
1364 case PJ_TYPE_PARAMETRIC_DATUM:
1368 case PJ_TYPE_GEOGRAPHIC_CRS:
1372 case PJ_TYPE_GEODETIC_CRS:
1374 case PJ_TYPE_GEOCENTRIC_CRS:
1376 case PJ_TYPE_GEOGRAPHIC_2D_CRS:
1378 case PJ_TYPE_GEOGRAPHIC_3D_CRS:
1380 case PJ_TYPE_VERTICAL_CRS:
1382 case PJ_TYPE_PROJECTED_CRS:
1384 case PJ_TYPE_COMPOUND_CRS:
1386 case PJ_TYPE_TEMPORAL_CRS:
1388 case PJ_TYPE_ENGINEERING_CRS:
1390 case PJ_TYPE_BOUND_CRS:
1392 case PJ_TYPE_OTHER_CRS:
1394#if PROJ_VERSION_MAJOR>9 || (PROJ_VERSION_MAJOR==9 && PROJ_VERSION_MINOR>=2)
1395 case PJ_TYPE_DERIVED_PROJECTED_CRS:
1397 case PJ_TYPE_COORDINATE_METADATA:
1411 return proj_is_deprecated( pj );
1416 return d->mIsGeographic;
1436 return QString( proj_get_celestial_body_name( context, pj ) );
1441 if ( d->mCoordinateEpoch == epoch )
1447 d->mCoordinateEpoch = epoch;
1448 d->setPj( std::move( clone ) );
1453 return d->mCoordinateEpoch;
1472 res.mName = QString( proj_get_name( ensemble.get() ) );
1473 res.mAuthority = QString( proj_get_id_auth_name( ensemble.get(), 0 ) );
1474 res.mCode = QString( proj_get_id_code( ensemble.get(), 0 ) );
1475 res.mRemarks = QString( proj_get_remarks( ensemble.get() ) );
1476 res.mScope = QString( proj_get_scope( ensemble.get() ) );
1477 res.mAccuracy = proj_datum_ensemble_get_accuracy( context, ensemble.get() );
1479 const int memberCount = proj_datum_ensemble_get_member_count( context, ensemble.get() );
1480 for (
int i = 0; i < memberCount; ++i )
1487 details.mName = QString( proj_get_name( member.get() ) );
1488 details.mAuthority = QString( proj_get_id_auth_name( member.get(), 0 ) );
1489 details.mCode = QString( proj_get_id_code( member.get(), 0 ) );
1490 details.mRemarks = QString( proj_get_remarks( member.get() ) );
1491 details.mScope = QString( proj_get_scope( member.get() ) );
1493 res.mMembers << details;
1503 QString projString =
toProj();
1504 projString.replace( QLatin1String(
"+type=crs" ), QString() );
1507 if ( !transformation )
1510 PJ_COORD coord = proj_coord( 0, 0, 0, HUGE_VAL );
1511 coord.uv.u = point.
x() * M_PI / 180.0;
1512 coord.uv.v = point.
y() * M_PI / 180.0;
1514 proj_errno_reset( transformation.get() );
1515 const PJ_FACTORS pjFactors = proj_factors( transformation.get(), coord );
1516 if ( proj_errno( transformation.get() ) )
1521 res.mIsValid =
true;
1522 res.mMeridionalScale = pjFactors.meridional_scale;
1523 res.mParallelScale = pjFactors.parallel_scale;
1524 res.mArealScale = pjFactors.areal_scale;
1525 res.mAngularDistortion = pjFactors.angular_distortion;
1526 res.mMeridianParallelAngle = pjFactors.meridian_parallel_angle * 180 / M_PI;
1527 res.mMeridianConvergence = pjFactors.meridian_convergence * 180 / M_PI;
1528 res.mTissotSemimajor = pjFactors.tissot_semimajor;
1529 res.mTissotSemiminor = pjFactors.tissot_semiminor;
1530 res.mDxDlam = pjFactors.dx_dlam;
1531 res.mDxDphi = pjFactors.dx_dphi;
1532 res.mDyDlam = pjFactors.dy_dlam;
1533 res.mDyDphi = pjFactors.dy_dphi;
1545 QString projString =
toProj();
1546 projString.replace( QLatin1String(
"+type=crs" ), QString() );
1547 if ( projString.isEmpty() )
1551 if ( !transformation )
1554 PJ_PROJ_INFO info = proj_pj_info( transformation.get() );
1569 return d->mMapUnits;
1577 PJ *obj = d->threadLocalProjObject();
1582 double southLat = 0;
1584 double northLat = 0;
1587 &westLon, &southLat, &eastLon, &northLat,
nullptr ) )
1602 const auto parts {
authid().split(
':' ) };
1603 if ( parts.length() == 2 )
1605 if ( parts[0] == QLatin1String(
"EPSG" ) )
1606 return QStringLiteral(
"http://www.opengis.net/def/crs/EPSG/0/%1" ).arg( parts[1] ) ;
1607 else if ( parts[0] == QLatin1String(
"OGC" ) )
1609 return QStringLiteral(
"http://www.opengis.net/def/crs/OGC/1.3/%1" ).arg( parts[1] ) ;
1625 const auto parts {
authid().split(
':' ) };
1626 if ( parts.length() == 2 )
1628 if ( parts[0] == QLatin1String(
"EPSG" ) )
1629 return QStringLiteral(
"urn:ogc:def:crs:EPSG::%1" ).arg( parts[1] );
1630 else if ( parts[0] == QLatin1String(
"OGC" ) )
1632 return QStringLiteral(
"urn:ogc:def:crs:OGC:1.3:%1" ).arg( parts[1] );
1663void QgsCoordinateReferenceSystem::setProjString(
const QString &proj4String )
1666 d->mProj4 = proj4String;
1667 d->mWktPreferred.clear();
1670 QString trimmed = proj4String.trimmed();
1672 trimmed += QLatin1String(
" +type=crs" );
1682 const int errNo = proj_context_errno( ctx );
1683 QgsDebugError( QStringLiteral(
"proj string rejected: %1" ).arg( proj_context_errno_string( ctx, errNo ) ) );
1685 d->mIsValid =
false;
1689 d->mEllipsoidAcronym.clear();
1696bool QgsCoordinateReferenceSystem::setWktString(
const QString &wkt )
1699 d->mIsValid =
false;
1700 d->mWktPreferred.clear();
1702 PROJ_STRING_LIST warnings =
nullptr;
1703 PROJ_STRING_LIST grammarErrors =
nullptr;
1711 QgsDebugMsgLevel( QStringLiteral(
"\n---------------------------------------------------------------" ), 2 );
1712 QgsDebugMsgLevel( QStringLiteral(
"This CRS could *** NOT *** be set from the supplied Wkt " ), 2 );
1714 for (
auto iter = warnings; iter && *iter; ++iter )
1718 for (
auto iter = grammarErrors; iter && *iter; ++iter )
1722 QgsDebugMsgLevel( QStringLiteral(
"---------------------------------------------------------------\n" ), 2 );
1724 proj_string_list_destroy( warnings );
1725 proj_string_list_destroy( grammarErrors );
1731 if ( !sDisableWktCache )
1732 sWktCache()->insert( wkt, *
this );
1739 QString authName( proj_get_id_auth_name( d->threadLocalProjObject(), 0 ) );
1740 QString authCode( proj_get_id_code( d->threadLocalProjObject(), 0 ) );
1742 if ( authName.isEmpty() || authCode.isEmpty() )
1748 if ( !authName.isEmpty() && !authCode.isEmpty() )
1750 if ( loadFromAuthCode( authName, authCode ) )
1753 if ( !sDisableWktCache )
1754 sWktCache()->insert( wkt, *
this );
1762 d->mDescription = QString( proj_get_name( d->threadLocalProjObject() ) );
1770void QgsCoordinateReferenceSystem::setMapUnits()
1797 if ( !coordinateSystem )
1803 const int axisCount = proj_cs_get_axis_count( context, coordinateSystem.get() );
1804 if ( axisCount > 0 )
1806 const char *outUnitName =
nullptr;
1808 proj_cs_get_axis_info( context, coordinateSystem.get(), 0,
1817 const QString unitName( outUnitName );
1821 if ( unitName.compare( QLatin1String(
"degree" ), Qt::CaseInsensitive ) == 0 ||
1822 unitName.compare( QLatin1String(
"degree minute second" ), Qt::CaseInsensitive ) == 0 ||
1823 unitName.compare( QLatin1String(
"degree minute second hemisphere" ), Qt::CaseInsensitive ) == 0 ||
1824 unitName.compare( QLatin1String(
"degree minute" ), Qt::CaseInsensitive ) == 0 ||
1825 unitName.compare( QLatin1String(
"degree hemisphere" ), Qt::CaseInsensitive ) == 0 ||
1826 unitName.compare( QLatin1String(
"degree minute hemisphere" ), Qt::CaseInsensitive ) == 0 ||
1827 unitName.compare( QLatin1String(
"hemisphere degree" ), Qt::CaseInsensitive ) == 0 ||
1828 unitName.compare( QLatin1String(
"hemisphere degree minute" ), Qt::CaseInsensitive ) == 0 ||
1829 unitName.compare( QLatin1String(
"hemisphere degree minute second" ), Qt::CaseInsensitive ) == 0 ||
1830 unitName.compare( QLatin1String(
"degree (supplier to define representation)" ), Qt::CaseInsensitive ) == 0 )
1832 else if ( unitName.compare( QLatin1String(
"metre" ), Qt::CaseInsensitive ) == 0
1833 || unitName.compare( QLatin1String(
"m" ), Qt::CaseInsensitive ) == 0
1834 || unitName.compare( QLatin1String(
"meter" ), Qt::CaseInsensitive ) == 0 )
1836 else if ( unitName.compare( QLatin1String(
"US survey foot" ), Qt::CaseInsensitive ) == 0 )
1838 else if ( unitName.compare( QLatin1String(
"foot" ), Qt::CaseInsensitive ) == 0 )
1840 else if ( unitName.compare( QLatin1String(
"British yard (Sears 1922)" ), Qt::CaseInsensitive ) == 0 )
1842 else if ( unitName.compare( QLatin1String(
"British yard (Sears 1922 truncated)" ), Qt::CaseInsensitive ) == 0 )
1844 else if ( unitName.compare( QLatin1String(
"British foot (Sears 1922)" ), Qt::CaseInsensitive ) == 0 )
1846 else if ( unitName.compare( QLatin1String(
"British foot (Sears 1922 truncated)" ), Qt::CaseInsensitive ) == 0 )
1848 else if ( unitName.compare( QLatin1String(
"British chain (Sears 1922)" ), Qt::CaseInsensitive ) == 0 )
1850 else if ( unitName.compare( QLatin1String(
"British chain (Sears 1922 truncated)" ), Qt::CaseInsensitive ) == 0 )
1852 else if ( unitName.compare( QLatin1String(
"British link (Sears 1922)" ), Qt::CaseInsensitive ) == 0 )
1854 else if ( unitName.compare( QLatin1String(
"British link (Sears 1922 truncated)" ), Qt::CaseInsensitive ) == 0 )
1856 else if ( unitName.compare( QLatin1String(
"British yard (Benoit 1895 A)" ), Qt::CaseInsensitive ) == 0 )
1858 else if ( unitName.compare( QLatin1String(
"British foot (Benoit 1895 A)" ), Qt::CaseInsensitive ) == 0 )
1860 else if ( unitName.compare( QLatin1String(
"British chain (Benoit 1895 A)" ), Qt::CaseInsensitive ) == 0 )
1862 else if ( unitName.compare( QLatin1String(
"British link (Benoit 1895 A)" ), Qt::CaseInsensitive ) == 0 )
1864 else if ( unitName.compare( QLatin1String(
"British yard (Benoit 1895 B)" ), Qt::CaseInsensitive ) == 0 )
1866 else if ( unitName.compare( QLatin1String(
"British foot (Benoit 1895 B)" ), Qt::CaseInsensitive ) == 0 )
1868 else if ( unitName.compare( QLatin1String(
"British chain (Benoit 1895 B)" ), Qt::CaseInsensitive ) == 0 )
1870 else if ( unitName.compare( QLatin1String(
"British link (Benoit 1895 B)" ), Qt::CaseInsensitive ) == 0 )
1872 else if ( unitName.compare( QLatin1String(
"British foot (1865)" ), Qt::CaseInsensitive ) == 0 )
1874 else if ( unitName.compare( QLatin1String(
"British foot (1936)" ), Qt::CaseInsensitive ) == 0 )
1876 else if ( unitName.compare( QLatin1String(
"Indian foot" ), Qt::CaseInsensitive ) == 0 )
1878 else if ( unitName.compare( QLatin1String(
"Indian foot (1937)" ), Qt::CaseInsensitive ) == 0 )
1880 else if ( unitName.compare( QLatin1String(
"Indian foot (1962)" ), Qt::CaseInsensitive ) == 0 )
1882 else if ( unitName.compare( QLatin1String(
"Indian foot (1975)" ), Qt::CaseInsensitive ) == 0 )
1884 else if ( unitName.compare( QLatin1String(
"Indian yard" ), Qt::CaseInsensitive ) == 0 )
1886 else if ( unitName.compare( QLatin1String(
"Indian yard (1937)" ), Qt::CaseInsensitive ) == 0 )
1888 else if ( unitName.compare( QLatin1String(
"Indian yard (1962)" ), Qt::CaseInsensitive ) == 0 )
1890 else if ( unitName.compare( QLatin1String(
"Indian yard (1975)" ), Qt::CaseInsensitive ) == 0 )
1892 else if ( unitName.compare( QLatin1String(
"Gold Coast foot" ), Qt::CaseInsensitive ) == 0 )
1894 else if ( unitName.compare( QLatin1String(
"Clarke's foot" ), Qt::CaseInsensitive ) == 0 )
1896 else if ( unitName.compare( QLatin1String(
"Clarke's yard" ), Qt::CaseInsensitive ) == 0 )
1898 else if ( unitName.compare( QLatin1String(
"Clarke's chain" ), Qt::CaseInsensitive ) == 0 )
1900 else if ( unitName.compare( QLatin1String(
"Clarke's link" ), Qt::CaseInsensitive ) == 0 )
1902 else if ( unitName.compare( QLatin1String(
"kilometre" ), Qt::CaseInsensitive ) == 0 )
1904 else if ( unitName.compare( QLatin1String(
"centimetre" ), Qt::CaseInsensitive ) == 0 )
1906 else if ( unitName.compare( QLatin1String(
"millimetre" ), Qt::CaseInsensitive ) == 0 )
1908 else if ( unitName.compare( QLatin1String(
"Statute mile" ), Qt::CaseInsensitive ) == 0 )
1910 else if ( unitName.compare( QLatin1String(
"nautical mile" ), Qt::CaseInsensitive ) == 0 )
1912 else if ( unitName.compare( QLatin1String(
"yard" ), Qt::CaseInsensitive ) == 0 )
1914 else if ( unitName.compare( QLatin1String(
"fathom" ), Qt::CaseInsensitive ) == 0 )
1916 else if ( unitName.compare( QLatin1String(
"US survey chain" ), Qt::CaseInsensitive ) == 0 )
1918 else if ( unitName.compare( QLatin1String(
"chain" ), Qt::CaseInsensitive ) == 0 )
1920 else if ( unitName.compare( QLatin1String(
"link" ), Qt::CaseInsensitive ) == 0 )
1922 else if ( unitName.compare( QLatin1String(
"US survey link" ), Qt::CaseInsensitive ) == 0 )
1924 else if ( unitName.compare( QLatin1String(
"US survey mile" ), Qt::CaseInsensitive ) == 0 )
1926 else if ( unitName.compare( QLatin1String(
"German legal metre" ), Qt::CaseInsensitive ) == 0 )
1943 if ( d->mEllipsoidAcronym.isNull() || d->mProjectionAcronym.isNull()
1946 QgsDebugMsgLevel(
"QgsCoordinateReferenceSystem::findMatchingProj will only "
1947 "work if prj acr ellipsoid acr and proj4string are set"
1948 " and the current projection is valid!", 4 );
1958 QString mySql = QString(
"select srs_id,parameters from tbl_srs where "
1959 "projection_acronym=%1 and ellipsoid_acronym=%2 order by deprecated" )
1966 myResult = openDatabase( myDatabaseFileName, database );
1967 if ( myResult != SQLITE_OK )
1972 statement = database.
prepare( mySql, myResult );
1973 if ( myResult == SQLITE_OK )
1976 while ( statement.
step() == SQLITE_ROW )
1980 if (
toProj() == myProj4String.trimmed() )
1982 return mySrsId.toLong();
1993 myResult = openDatabase( myDatabaseFileName, database );
1994 if ( myResult != SQLITE_OK )
1999 statement = database.
prepare( mySql, myResult );
2001 if ( myResult == SQLITE_OK )
2003 while ( statement.
step() == SQLITE_ROW )
2007 if (
toProj() == myProj4String.trimmed() )
2009 return mySrsId.toLong();
2023 if ( !d->mIsValid && !srs.d->mIsValid )
2026 if ( !d->mIsValid || !srs.d->mIsValid )
2034 if ( isUser != otherIsUser )
2038 if ( !isUser && ( !d->mAuthId.isEmpty() || !srs.d->mAuthId.isEmpty() ) )
2039 return d->mAuthId == srs.d->mAuthId;
2046 return !( *
this == srs );
2051 if (
PJ *obj = d->threadLocalProjObject() )
2054 if ( isDefaultPreferredFormat && !d->mWktPreferred.isEmpty() )
2057 return d->mWktPreferred;
2060 PJ_WKT_TYPE
type = PJ_WKT1_GDAL;
2064 type = PJ_WKT1_GDAL;
2067 type = PJ_WKT1_ESRI;
2070 type = PJ_WKT2_2015;
2073 type = PJ_WKT2_2015_SIMPLIFIED;
2076 type = PJ_WKT2_2019;
2079 type = PJ_WKT2_2019_SIMPLIFIED;
2083 const QByteArray multiLineOption = QStringLiteral(
"MULTILINE=%1" ).arg( multiline ? QStringLiteral(
"YES" ) : QStringLiteral(
"NO" ) ).toLocal8Bit();
2084 const QByteArray indentatationWidthOption = QStringLiteral(
"INDENTATION_WIDTH=%1" ).arg( multiline ? QString::number( indentationWidth ) : QStringLiteral(
"0" ) ).toLocal8Bit();
2085 const char *
const options[] = {multiLineOption.constData(), indentatationWidthOption.constData(),
nullptr};
2088 if ( isDefaultPreferredFormat )
2091 d->mWktPreferred = res;
2103 QDomNode srsNode = node.namedItem( QStringLiteral(
"spatialrefsys" ) );
2105 if ( ! srsNode.isNull() )
2107 bool initialized =
false;
2110 long srsid = srsNode.namedItem( QStringLiteral(
"srsid" ) ).toElement().text().toLong( &ok );
2116 node = srsNode.namedItem( QStringLiteral(
"authid" ) );
2117 if ( !node.isNull() )
2128 node = srsNode.namedItem( QStringLiteral(
"epsg" ) );
2129 if ( !node.isNull() )
2147 const QString
description = srsNode.namedItem( QStringLiteral(
"description" ) ).toElement().text();
2149 const QString wkt = srsNode.namedItem( QStringLiteral(
"wkt" ) ).toElement().text();
2150 initialized = createFromWktInternal( wkt,
description );
2155 node = srsNode.namedItem( QStringLiteral(
"proj4" ) );
2156 const QString proj4 = node.toElement().text();
2163 node = srsNode.namedItem( QStringLiteral(
"proj4" ) );
2164 const QString proj4 = node.toElement().text();
2165 if ( !proj4.trimmed().isEmpty() )
2166 setProjString( node.toElement().text() );
2168 node = srsNode.namedItem( QStringLiteral(
"srsid" ) );
2169 d->mSrsId = node.toElement().text().toLong();
2171 node = srsNode.namedItem( QStringLiteral(
"srid" ) );
2172 d->mSRID = node.toElement().text().toLong();
2174 node = srsNode.namedItem( QStringLiteral(
"authid" ) );
2175 d->mAuthId = node.toElement().text();
2177 node = srsNode.namedItem( QStringLiteral(
"description" ) );
2178 d->mDescription = node.toElement().text();
2180 node = srsNode.namedItem( QStringLiteral(
"projectionacronym" ) );
2181 d->mProjectionAcronym = node.toElement().text();
2183 node = srsNode.namedItem( QStringLiteral(
"ellipsoidacronym" ) );
2184 d->mEllipsoidAcronym = node.toElement().text();
2186 node = srsNode.namedItem( QStringLiteral(
"geographicflag" ) );
2187 d->mIsGeographic = node.toElement().text() == QLatin1String(
"true" );
2189 d->mWktPreferred.clear();
2195 const QString epoch = srsNode.toElement().attribute( QStringLiteral(
"coordinateEpoch" ) );
2196 if ( !epoch.isEmpty() )
2198 bool epochOk =
false;
2199 d->mCoordinateEpoch = epoch.toDouble( &epochOk );
2201 d->mCoordinateEpoch = std::numeric_limits< double >::quiet_NaN();
2205 d->mCoordinateEpoch = std::numeric_limits< double >::quiet_NaN();
2208 mNativeFormat = qgsEnumKeyToValue<Qgis::CrsDefinitionFormat>( srsNode.toElement().attribute( QStringLiteral(
"nativeFormat" ) ),
Qgis::CrsDefinitionFormat::Wkt );
2213 d =
new QgsCoordinateReferenceSystemPrivate();
2221 QDomElement layerNode = node.toElement();
2222 QDomElement srsElement = doc.createElement( QStringLiteral(
"spatialrefsys" ) );
2224 srsElement.setAttribute( QStringLiteral(
"nativeFormat" ), qgsEnumValueToKey<Qgis::CrsDefinitionFormat>( mNativeFormat ) );
2226 if ( std::isfinite( d->mCoordinateEpoch ) )
2228 srsElement.setAttribute( QStringLiteral(
"coordinateEpoch" ), d->mCoordinateEpoch );
2231 QDomElement wktElement = doc.createElement( QStringLiteral(
"wkt" ) );
2233 srsElement.appendChild( wktElement );
2235 QDomElement proj4Element = doc.createElement( QStringLiteral(
"proj4" ) );
2236 proj4Element.appendChild( doc.createTextNode(
toProj() ) );
2237 srsElement.appendChild( proj4Element );
2239 QDomElement srsIdElement = doc.createElement( QStringLiteral(
"srsid" ) );
2240 srsIdElement.appendChild( doc.createTextNode( QString::number(
srsid() ) ) );
2241 srsElement.appendChild( srsIdElement );
2243 QDomElement sridElement = doc.createElement( QStringLiteral(
"srid" ) );
2244 sridElement.appendChild( doc.createTextNode( QString::number(
postgisSrid() ) ) );
2245 srsElement.appendChild( sridElement );
2247 QDomElement authidElement = doc.createElement( QStringLiteral(
"authid" ) );
2248 authidElement.appendChild( doc.createTextNode(
authid() ) );
2249 srsElement.appendChild( authidElement );
2251 QDomElement descriptionElement = doc.createElement( QStringLiteral(
"description" ) );
2252 descriptionElement.appendChild( doc.createTextNode(
description() ) );
2253 srsElement.appendChild( descriptionElement );
2255 QDomElement projectionAcronymElement = doc.createElement( QStringLiteral(
"projectionacronym" ) );
2256 projectionAcronymElement.appendChild( doc.createTextNode(
projectionAcronym() ) );
2257 srsElement.appendChild( projectionAcronymElement );
2259 QDomElement ellipsoidAcronymElement = doc.createElement( QStringLiteral(
"ellipsoidacronym" ) );
2260 ellipsoidAcronymElement.appendChild( doc.createTextNode(
ellipsoidAcronym() ) );
2261 srsElement.appendChild( ellipsoidAcronymElement );
2263 QDomElement geographicFlagElement = doc.createElement( QStringLiteral(
"geographicflag" ) );
2264 QString geoFlagText = QStringLiteral(
"false" );
2267 geoFlagText = QStringLiteral(
"true" );
2270 geographicFlagElement.appendChild( doc.createTextNode( geoFlagText ) );
2271 srsElement.appendChild( geographicFlagElement );
2273 layerNode.appendChild( srsElement );
2285QString QgsCoordinateReferenceSystem::projFromSrsId(
const int srsId )
2287 QString myDatabaseFileName;
2288 QString myProjString;
2289 QString mySql = QStringLiteral(
"select parameters from tbl_srs where srs_id = %1 order by deprecated" ).arg( srsId );
2298 QFileInfo myFileInfo;
2299 myFileInfo.setFile( myDatabaseFileName );
2300 if ( !myFileInfo.exists() )
2302 QgsDebugError( QStringLiteral(
"users qgis.db not found" ) );
2315 rc = openDatabase( myDatabaseFileName, database );
2321 statement = database.
prepare( mySql, rc );
2323 if ( rc == SQLITE_OK )
2325 if ( statement.
step() == SQLITE_ROW )
2331 return myProjString;
2338 myResult = database.
open_v2( path, SQLITE_OPEN_READONLY,
nullptr );
2340 myResult = database.
open( path );
2342 if ( myResult != SQLITE_OK )
2351 .arg( database.
errorMessage() ), QObject::tr(
"CRS" ) );
2358 sCustomSrsValidation = f;
2363 return sCustomSrsValidation;
2366void QgsCoordinateReferenceSystem::debugPrint()
2369 QgsDebugMsgLevel(
"* Valid : " + ( d->mIsValid ? QString(
"true" ) : QString(
"false" ) ), 1 );
2390 mValidationHint = html;
2395 return mValidationHint;
2405 mNativeFormat = format;
2410 return mNativeFormat;
2413long QgsCoordinateReferenceSystem::getRecordCount()
2418 long myRecordCount = 0;
2421 if ( myResult != SQLITE_OK )
2427 QString mySql = QStringLiteral(
"select count(*) from tbl_srs" );
2428 statement = database.
prepare( mySql, myResult );
2429 if ( myResult == SQLITE_OK )
2431 if ( statement.
step() == SQLITE_ROW )
2433 QString myRecordCountString = statement.
columnAsText( 0 );
2434 myRecordCount = myRecordCountString.toLong();
2437 return myRecordCount;
2443 bool isGeographic =
false;
2447 if ( !horizontalCrs )
2451 if ( coordinateSystem )
2453 const int axisCount = proj_cs_get_axis_count( pjContext, coordinateSystem.get() );
2454 if ( axisCount > 0 )
2456 const char *outUnitAuthName =
nullptr;
2457 const char *outUnitAuthCode =
nullptr;
2459 proj_cs_get_axis_info( pjContext, coordinateSystem.get(), 0,
2468 if ( outUnitAuthName && outUnitAuthCode )
2470 const char *unitCategory =
nullptr;
2471 if ( proj_uom_get_info_from_database( pjContext, outUnitAuthName, outUnitAuthCode,
nullptr,
nullptr, &unitCategory ) )
2473 isGeographic = QString( unitCategory ).compare( QLatin1String(
"angular" ), Qt::CaseInsensitive ) == 0;
2478 return isGeographic;
2483 thread_local const QRegularExpression projRegExp( QStringLiteral(
"\\+proj=(\\S+)" ) );
2484 const QRegularExpressionMatch projMatch = projRegExp.match( proj );
2485 if ( !projMatch.hasMatch() )
2487 QgsDebugMsgLevel( QStringLiteral(
"no +proj argument found [%2]" ).arg( proj ), 2 );
2490 operation = projMatch.captured( 1 );
2492 const QRegularExpressionMatch ellipseMatch = projRegExp.match( proj );
2493 if ( ellipseMatch.hasMatch() )
2495 ellipsoid = ellipseMatch.captured( 1 );
2509bool QgsCoordinateReferenceSystem::loadFromAuthCode(
const QString &auth,
const QString &code )
2515 d->mIsValid =
false;
2516 d->mWktPreferred.clear();
2528 proj4.replace( QLatin1String(
"+type=crs" ), QString() );
2529 proj4 = proj4.trimmed();
2533 d->mWktPreferred.clear();
2534 d->mDescription = QString( proj_get_name(
crs.get() ) );
2535 d->mAuthId = QStringLiteral(
"%1:%2" ).arg( auth, code );
2537 d->mAxisInvertedDirty =
true;
2542 d->mEllipsoidAcronym.clear();
2543 d->setPj( std::move(
crs ) );
2545 const QString dbVals =
sAuthIdToQgisSrsIdMap.value( QStringLiteral(
"%1:%2" ).arg( auth, code ).toUpper() );
2546 if ( !dbVals.isEmpty() )
2548 const QStringList parts = dbVals.split(
',' );
2549 d->mSrsId = parts.at( 0 ).toInt();
2550 d->mSRID = parts.at( 1 ).toInt();
2558QList<long> QgsCoordinateReferenceSystem::userSrsIds()
2560 QList<long> results;
2564 QFileInfo myInfo( db );
2565 if ( !myInfo.exists() )
2575 int result = openDatabase( db, database );
2576 if ( result != SQLITE_OK )
2578 QgsDebugError(
"failed : " + db +
" could not be opened!" );
2582 QString sql = QStringLiteral(
"select srs_id from tbl_srs where srs_id >= %1" ).arg(
USER_CRS_START_ID );
2584 statement = database.
prepare( sql, rc );
2587 int ret = statement.
step();
2589 if ( ret == SQLITE_DONE )
2595 if ( ret == SQLITE_ROW )
2601 QgsMessageLog::logMessage( QObject::tr(
"SQLite error: %2\nSQL: %1" ).arg( sql, sqlite3_errmsg( database.get() ) ), QObject::tr(
"SpatiaLite" ) );
2609long QgsCoordinateReferenceSystem::matchToUserCrs()
const
2611 PJ *obj = d->threadLocalProjObject();
2615 const QList< long > ids = userSrsIds();
2616 for (
long id : ids )
2619 if ( candidate.
projObject() && proj_is_equivalent_to( obj, candidate.
projObject(), PJ_COMP_EQUIVALENT ) )
2627static void sync_db_proj_logger(
void * ,
int level,
const char *message )
2632 if ( level == PJ_LOG_ERROR )
2636 else if ( level == PJ_LOG_DEBUG )
2644 setlocale( LC_ALL,
"C" );
2647 int inserted = 0, updated = 0, deleted = 0, errors = 0;
2652 if ( database.
open( dbFilePath ) != SQLITE_OK )
2658 if ( sqlite3_exec( database.get(),
"BEGIN TRANSACTION",
nullptr,
nullptr,
nullptr ) != SQLITE_OK )
2666 char *errMsg =
nullptr;
2668 bool createdTypeColumn =
false;
2669 if ( sqlite3_exec( database.get(),
"ALTER TABLE tbl_srs ADD COLUMN srs_type text",
nullptr,
nullptr,
nullptr ) == SQLITE_OK )
2671 createdTypeColumn =
true;
2672 if ( sqlite3_exec( database.get(),
"CREATE INDEX srs_type ON tbl_srs(srs_type)",
nullptr,
nullptr,
nullptr ) != SQLITE_OK )
2674 QgsDebugError( QStringLiteral(
"Could not create index for srs_type" ) );
2679 if ( sqlite3_exec( database.get(),
"create table tbl_info (proj_major INT, proj_minor INT, proj_patch INT)",
nullptr,
nullptr,
nullptr ) == SQLITE_OK )
2681 QString sql = QStringLiteral(
"INSERT INTO tbl_info(proj_major, proj_minor, proj_patch) VALUES (%1, %2,%3)" )
2682 .arg( QString::number( PROJ_VERSION_MAJOR ),
2683 QString::number( PROJ_VERSION_MINOR ),
2684 QString::number( PROJ_VERSION_PATCH ) );
2685 if ( sqlite3_exec( database.get(), sql.toUtf8(),
nullptr,
nullptr, &errMsg ) != SQLITE_OK )
2687 QgsDebugError( QStringLiteral(
"Could not execute: %1 [%2/%3]\n" ).arg(
2690 errMsg ? errMsg :
"(unknown error)" ) );
2692 sqlite3_free( errMsg );
2699 QString sql = QStringLiteral(
"SELECT proj_major, proj_minor, proj_patch FROM tbl_info" );
2700 statement = database.
prepare( sql, result );
2701 if ( result != SQLITE_OK )
2706 if ( statement.
step() == SQLITE_ROW )
2711 if ( !createdTypeColumn && major == PROJ_VERSION_MAJOR && minor == PROJ_VERSION_MINOR && patch == PROJ_VERSION_PATCH )
2717 QgsDebugError( QStringLiteral(
"Could not retrieve previous CRS sync PROJ version number" ) );
2724 proj_log_func( pjContext,
nullptr, sync_db_proj_logger );
2726 PROJ_STRING_LIST authorities = proj_get_authorities_from_database( pjContext );
2728 int nextSrsId = 67218;
2729 int nextSrId = 520007218;
2730 for (
auto authIter = authorities; authIter && *authIter; ++authIter )
2732 const QString authority( *authIter );
2733 QgsDebugMsgLevel( QStringLiteral(
"Loading authority '%1'" ).arg( authority ), 2 );
2734 PROJ_STRING_LIST codes = proj_get_codes_from_database( pjContext, *authIter, PJ_TYPE_CRS,
true );
2736 QStringList allCodes;
2738 for (
auto codesIter = codes; codesIter && *codesIter; ++codesIter )
2740 const QString code( *codesIter );
2746 QgsDebugError( QStringLiteral(
"Could not load '%1:%2'" ).arg( authority, code ) );
2750 const PJ_TYPE pjType = proj_get_type(
crs.get( ) );
2752 QString srsTypeString;
2757 case PJ_TYPE_ELLIPSOID:
2758 case PJ_TYPE_PRIME_MERIDIAN:
2759 case PJ_TYPE_GEODETIC_REFERENCE_FRAME:
2760 case PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME:
2761 case PJ_TYPE_VERTICAL_REFERENCE_FRAME:
2762 case PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME:
2763 case PJ_TYPE_DATUM_ENSEMBLE:
2764 case PJ_TYPE_CONVERSION:
2765 case PJ_TYPE_TRANSFORMATION:
2766 case PJ_TYPE_CONCATENATED_OPERATION:
2767 case PJ_TYPE_OTHER_COORDINATE_OPERATION:
2768 case PJ_TYPE_TEMPORAL_DATUM:
2769 case PJ_TYPE_ENGINEERING_DATUM:
2770 case PJ_TYPE_PARAMETRIC_DATUM:
2771 case PJ_TYPE_UNKNOWN:
2775 case PJ_TYPE_GEOGRAPHIC_CRS:
2778 case PJ_TYPE_GEODETIC_CRS:
2782 case PJ_TYPE_GEOCENTRIC_CRS:
2786 case PJ_TYPE_GEOGRAPHIC_2D_CRS:
2790 case PJ_TYPE_GEOGRAPHIC_3D_CRS:
2794 case PJ_TYPE_PROJECTED_CRS:
2798 case PJ_TYPE_COMPOUND_CRS:
2802 case PJ_TYPE_TEMPORAL_CRS:
2806 case PJ_TYPE_ENGINEERING_CRS:
2810 case PJ_TYPE_BOUND_CRS:
2814 case PJ_TYPE_VERTICAL_CRS:
2818#if PROJ_VERSION_MAJOR>9 || (PROJ_VERSION_MAJOR==9 && PROJ_VERSION_MINOR>=2)
2819 case PJ_TYPE_DERIVED_PROJECTED_CRS:
2822 case PJ_TYPE_COORDINATE_METADATA:
2825 case PJ_TYPE_OTHER_CRS:
2834 proj4.replace( QLatin1String(
"+type=crs" ), QString() );
2835 proj4 = proj4.trimmed();
2837 if ( proj4.isEmpty() )
2839 QgsDebugMsgLevel( QStringLiteral(
"No proj4 for '%1:%2'" ).arg( authority, code ), 2 );
2850 if ( translatedOperation.isEmpty() && !
operation.isEmpty() )
2852 std::cout << QStringLiteral(
"Operation needs translation in QgsCoordinateReferenceSystemUtils::translateProjection: %1" ).arg(
operation ).toLocal8Bit().constData() << std::endl;
2853 qFatal(
"aborted" );
2856 const bool deprecated = proj_is_deprecated(
crs.get() );
2857 const QString name( proj_get_name(
crs.get() ) );
2859 QString sql = QStringLiteral(
"SELECT parameters,description,deprecated,srs_type FROM tbl_srs WHERE auth_name='%1' AND auth_id='%2'" ).arg( authority, code );
2860 statement = database.
prepare( sql, result );
2861 if ( result != SQLITE_OK )
2870 bool dbSrsDeprecated = deprecated;
2871 if ( statement.
step() == SQLITE_ROW )
2875 dbSrsDeprecated = statement.
columnAsText( 2 ).toInt() != 0;
2879 if ( !dbSrsProj4.isEmpty() || !dbSrsDesc.isEmpty() )
2881 if ( proj4 != dbSrsProj4 || name != dbSrsDesc || deprecated != dbSrsDeprecated || dbSrsType != srsTypeString )
2884 sql = QStringLiteral(
"UPDATE tbl_srs SET parameters=%1,description=%2,deprecated=%3, srs_type=%4 WHERE auth_name=%5 AND auth_id=%6" )
2887 .arg( deprecated ? 1 : 0 )
2891 if ( sqlite3_exec( database.get(), sql.toUtf8(),
nullptr,
nullptr, &errMsg ) != SQLITE_OK )
2893 QgsDebugError( QStringLiteral(
"Could not execute: %1 [%2/%3]\n" ).arg(
2896 errMsg ? errMsg :
"(unknown error)" ) );
2898 sqlite3_free( errMsg );
2912 const QString dbVals =
sAuthIdToQgisSrsIdMap.value( QStringLiteral(
"%1:%2" ).arg( authority, code ) );
2915 if ( !dbVals.isEmpty() )
2917 const QStringList parts = dbVals.split(
',' );
2918 srsId = parts.at( 0 );
2919 srId = parts.at( 1 );
2921 if ( srId.isEmpty() )
2923 srId = QString::number( nextSrId );
2926 if ( srsId.isEmpty() )
2928 srsId = QString::number( nextSrsId );
2932 if ( !srsId.isEmpty() )
2934 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)" )
2944 .arg( deprecated ? 1 : 0 )
2949 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)" )
2958 .arg( deprecated ? 1 : 0 )
2963 if ( sqlite3_exec( database.get(), sql.toUtf8(),
nullptr,
nullptr, &errMsg ) == SQLITE_OK )
2969 qCritical(
"Could not execute: %s [%s/%s]\n",
2970 sql.toLocal8Bit().constData(),
2971 sqlite3_errmsg( database.get() ),
2972 errMsg ? errMsg :
"(unknown error)" );
2976 sqlite3_free( errMsg );
2981 proj_string_list_destroy( codes );
2983 const QString sql = QStringLiteral(
"DELETE FROM tbl_srs WHERE auth_name='%1' AND NOT auth_id IN (%2)" ).arg( authority, allCodes.join(
',' ) );
2984 if ( sqlite3_exec( database.get(), sql.toUtf8(),
nullptr,
nullptr,
nullptr ) == SQLITE_OK )
2986 deleted = sqlite3_changes( database.get() );
2991 qCritical(
"Could not execute: %s [%s]\n",
2992 sql.toLocal8Bit().constData(),
2993 sqlite3_errmsg( database.get() ) );
2997 proj_string_list_destroy( authorities );
2999 QString sql = QStringLiteral(
"UPDATE tbl_info set proj_major=%1,proj_minor=%2,proj_patch=%3" )
3000 .arg( QString::number( PROJ_VERSION_MAJOR ),
3001 QString::number( PROJ_VERSION_MINOR ),
3002 QString::number( PROJ_VERSION_PATCH ) );
3003 if ( sqlite3_exec( database.get(), sql.toUtf8(),
nullptr,
nullptr, &errMsg ) != SQLITE_OK )
3005 QgsDebugError( QStringLiteral(
"Could not execute: %1 [%2/%3]\n" ).arg(
3008 errMsg ? errMsg :
"(unknown error)" ) );
3010 sqlite3_free( errMsg );
3014 if ( sqlite3_exec( database.get(),
"COMMIT",
nullptr,
nullptr,
nullptr ) != SQLITE_OK )
3016 QgsDebugError( QStringLiteral(
"Could not commit transaction: %1 [%2]\n" ).arg(
3018 sqlite3_errmsg( database.get() ) )
3024 QgsDebugMsgLevel( QStringLiteral(
"CRS update (inserted:%1 updated:%2 deleted:%3 errors:%4)" ).arg( inserted ).arg( updated ).arg( deleted ).arg( errors ), 4 );
3032 return updated + inserted;
3035const QHash<QString, QgsCoordinateReferenceSystem> &QgsCoordinateReferenceSystem::stringCache()
3037 return *sStringCache();
3040const QHash<QString, QgsCoordinateReferenceSystem> &QgsCoordinateReferenceSystem::projCache()
3042 return *sProj4Cache();
3045const QHash<QString, QgsCoordinateReferenceSystem> &QgsCoordinateReferenceSystem::ogcCache()
3047 return *sOgcCache();
3050const QHash<QString, QgsCoordinateReferenceSystem> &QgsCoordinateReferenceSystem::wktCache()
3052 return *sWktCache();
3055const QHash<long, QgsCoordinateReferenceSystem> &QgsCoordinateReferenceSystem::srIdCache()
3057 return *sSrIdCache();
3060const QHash<long, QgsCoordinateReferenceSystem> &QgsCoordinateReferenceSystem::srsIdCache()
3062 return *sSrsIdCache();
3072 if (
PJ *obj = d->threadLocalProjObject() )
3118 if (
PJ *obj = d->threadLocalProjObject() )
3151 if (
PJ *obj = d->threadLocalProjObject() )
3162 if (
PJ *obj = d->threadLocalProjObject() )
3175 else if (
PJ *obj = d->threadLocalProjObject() )
3178 return geoCrs ? QStringLiteral(
"%1:%2" ).arg( proj_get_id_auth_name( geoCrs.get(), 0 ), proj_get_id_code( geoCrs.get(), 0 ) ) : QString();
3188 return d->threadLocalProjObject();
3201 d->mIsValid =
false;
3203 d->mWktPreferred.clear();
3210 switch ( proj_get_type(
object ) )
3212 case PJ_TYPE_GEODETIC_CRS:
3213 case PJ_TYPE_GEOCENTRIC_CRS:
3214 case PJ_TYPE_GEOGRAPHIC_CRS:
3215 case PJ_TYPE_GEOGRAPHIC_2D_CRS:
3216 case PJ_TYPE_GEOGRAPHIC_3D_CRS:
3217 case PJ_TYPE_VERTICAL_CRS:
3218 case PJ_TYPE_PROJECTED_CRS:
3219 case PJ_TYPE_COMPOUND_CRS:
3220 case PJ_TYPE_TEMPORAL_CRS:
3221 case PJ_TYPE_ENGINEERING_CRS:
3222 case PJ_TYPE_BOUND_CRS:
3223 case PJ_TYPE_OTHER_CRS:
3239 const QString authName( proj_get_id_auth_name( d->threadLocalProjObject(), 0 ) );
3240 const QString authCode( proj_get_id_code( d->threadLocalProjObject(), 0 ) );
3241 if ( !authName.isEmpty() && !authCode.isEmpty() &&
createFromOgcWmsCrs( QStringLiteral(
"%1:%2" ).arg( authName, authCode ) ) )
3249 d->mDescription = QString( proj_get_name( d->threadLocalProjObject() ) );
3260 QStringList projections;
3262 projections.reserve( res.size() );
3265 projections << QString::number(
crs.
srsid() );
3292 sSrIdCacheLock()->lockForWrite();
3293 if ( !sDisableSrIdCache )
3296 sDisableSrIdCache =
true;
3297 sSrIdCache()->clear();
3299 sSrIdCacheLock()->unlock();
3301 sOgcLock()->lockForWrite();
3302 if ( !sDisableOgcCache )
3305 sDisableOgcCache =
true;
3306 sOgcCache()->clear();
3308 sOgcLock()->unlock();
3310 sProj4CacheLock()->lockForWrite();
3311 if ( !sDisableProjCache )
3314 sDisableProjCache =
true;
3315 sProj4Cache()->clear();
3317 sProj4CacheLock()->unlock();
3319 sCRSWktLock()->lockForWrite();
3320 if ( !sDisableWktCache )
3323 sDisableWktCache =
true;
3324 sWktCache()->clear();
3326 sCRSWktLock()->unlock();
3328 sCRSSrsIdLock()->lockForWrite();
3329 if ( !sDisableSrsIdCache )
3332 sDisableSrsIdCache =
true;
3333 sSrsIdCache()->clear();
3335 sCRSSrsIdLock()->unlock();
3337 sCrsStringLock()->lockForWrite();
3338 if ( !sDisableStringCache )
3341 sDisableStringCache =
true;
3342 sStringCache()->clear();
3344 sCrsStringLock()->unlock();
3353 if ( !c1.d->mIsValid && !c2.d->mIsValid )
3356 if ( !c1.d->mIsValid && c2.d->mIsValid )
3359 if ( c1.d->mIsValid && !c2.d->mIsValid )
3365 if ( c1IsUser && !c2IsUser )
3368 if ( !c1IsUser && c2IsUser )
3371 if ( !c1IsUser && !c2IsUser && !c1.d->mAuthId.isEmpty() && !c2.d->mAuthId.isEmpty() )
3373 if ( c1.d->mAuthId != c2.d->mAuthId )
3374 return c1.d->mAuthId > c2.d->mAuthId;
3382 if ( c1.d->mCoordinateEpoch == c2.d->mCoordinateEpoch )
3385 if ( std::isnan( c1.d->mCoordinateEpoch ) && std::isnan( c2.d->mCoordinateEpoch ) )
3388 if ( std::isnan( c1.d->mCoordinateEpoch ) && !std::isnan( c2.d->mCoordinateEpoch ) )
3391 if ( !std::isnan( c1.d->mCoordinateEpoch ) && std::isnan( c2.d->mCoordinateEpoch ) )
3394 return c1.d->mCoordinateEpoch > c2.d->mCoordinateEpoch;
3402 if ( !c1.d->mIsValid && !c2.d->mIsValid )
3405 if ( c1.d->mIsValid && !c2.d->mIsValid )
3408 if ( !c1.d->mIsValid && c2.d->mIsValid )
3414 if ( !c1IsUser && c2IsUser )
3417 if ( c1IsUser && !c2IsUser )
3420 if ( !c1IsUser && !c2IsUser && !c1.d->mAuthId.isEmpty() && !c2.d->mAuthId.isEmpty() )
3422 if ( c1.d->mAuthId != c2.d->mAuthId )
3423 return c1.d->mAuthId < c2.d->mAuthId;
3431 if ( c1.d->mCoordinateEpoch == c2.d->mCoordinateEpoch )
3434 if ( std::isnan( c1.d->mCoordinateEpoch ) && std::isnan( c2.d->mCoordinateEpoch ) )
3437 if ( !std::isnan( c1.d->mCoordinateEpoch ) && std::isnan( c2.d->mCoordinateEpoch ) )
3440 if ( std::isnan( c1.d->mCoordinateEpoch ) && !std::isnan( c2.d->mCoordinateEpoch ) )
3443 return c1.d->mCoordinateEpoch < c2.d->mCoordinateEpoch;
3448 return !( c1 < c2 );
3452 return !( c1 > c2 );
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)
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.
This class 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 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,...
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 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...
The QgsReadWriteLocker class is a convenience class that simplifies locking and unlocking QReadWriteL...
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.
const int USER_CRS_START_ID
Magick number that determines whether a projection crsid is a system (srs.db) or user (~/....
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 * OGRSpatialReferenceH
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