QGIS API Documentation 3.43.0-Master (b60ef06885e)
qgscoordinatereferencesystem.h
Go to the documentation of this file.
1/***************************************************************************
2 qgscoordinatereferencesystem.h
3
4 -------------------
5 begin : 2007
6 copyright : (C) 2007 by Gary E. Sherman
7 email : sherman@mrcc.com
8***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18#ifndef QGSCOORDINATEREFERENCESYSTEM_H
19#define QGSCOORDINATEREFERENCESYSTEM_H
20
21//Standard includes
22#include "qgis_core.h"
23#include <ostream>
24
25//qt includes
26#include <QString>
27#include <QMap>
28#include <QHash>
29#include <QReadWriteLock>
30#include <QExplicitlySharedDataPointer>
31#include <QObject>
32
33//qgis includes
34#include "qgis_sip.h"
35#include "qgis.h"
36#include "qgsconfig.h"
37#include "qgsrectangle.h"
38#include "qgssqliteutils.h"
39
40class QDomNode;
41class QDomDocument;
42class QgsCoordinateReferenceSystemPrivate;
46
47#ifndef SIP_RUN
48struct PJconsts;
49typedef struct PJconsts PJ;
50
51struct pj_ctx;
52typedef struct pj_ctx PJ_CONTEXT;
53#endif
54
55// forward declaration for sqlite3
56typedef struct sqlite3 sqlite3 SIP_SKIP;
57
60
201{
202 Q_GADGET
203
204 Q_PROPERTY( Qgis::DistanceUnit mapUnits READ mapUnits )
205 Q_PROPERTY( bool isGeographic READ isGeographic )
206 Q_PROPERTY( QString authid READ authid )
207 Q_PROPERTY( QString description READ description )
208
209 public:
210
213 {
216 EpsgCrsId
217 };
218
221
223
224 // TODO QGIS 4: remove "POSTGIS" and "INTERNAL"
225
245 explicit QgsCoordinateReferenceSystem( const QString &definition );
246
247 // TODO QGIS 4: remove type and always use EPSG code
248
260 Q_DECL_DEPRECATED explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
261
264
266 operator QVariant() const
267 {
268 return QVariant::fromValue( *this );
269 }
270
277 static QList< long > validSrsIds();
278
279 // static creators
280
291 static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
292
302 Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
303
314 Q_DECL_DEPRECATED static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 ) SIP_DEPRECATED;
315
323 static QgsCoordinateReferenceSystem fromProj( const QString &proj );
324
335 static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
336
348 static QgsCoordinateReferenceSystem fromSrsId( long srsId );
349
365 static QgsCoordinateReferenceSystem createCompoundCrs( const QgsCoordinateReferenceSystem &horizontalCrs, const QgsCoordinateReferenceSystem &verticalCrs, QString &error SIP_OUT );
366
382 static QgsCoordinateReferenceSystem createGeocentricCrs( const QString &ellipsoid );
383
384 // Misc helper functions -----------------------
385
386 // TODO QGIS 4: remove type and always use EPSG code, rename to createFromEpsg
387
393 Q_DECL_DEPRECATED bool createFromId( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
394
395 // TODO QGIS 4: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
396
410 bool createFromOgcWmsCrs( const QString &crs );
411
412 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
413
421 Q_DECL_DEPRECATED bool createFromSrid( long srid ) SIP_DEPRECATED;
422
438 bool createFromWkt( const QString &wkt );
439
455 bool createFromSrsId( long srsId );
456
483 Q_DECL_DEPRECATED bool createFromProj4( const QString &projString ) SIP_DEPRECATED;
484
514#ifndef SIP_RUN
515 bool createFromProj( const QString &projString, bool identify = true );
516#else
517 bool createFromProj( const QString &projString );
518#endif
519
539 bool createFromString( const QString &definition );
540
541 // TODO QGIS 4: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
542
561 bool createFromUserInput( const QString &definition );
562
573 Q_DECL_DEPRECATED static void setupESRIWktFix() SIP_DEPRECATED;
574
576 bool isValid() const;
577
590 void validate();
591
592 // TODO QGIS 4: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
593
602 Q_DECL_DEPRECATED long findMatchingProj() SIP_DEPRECATED;
603
604 bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
605 bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
606
613 bool readXml( const QDomNode &node );
614
621 bool writeXml( QDomNode &node, QDomDocument &doc ) const;
622
623
628 static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
629
634 static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
635
636 // Accessors -----------------------------------
637
642 long srsid() const;
643
644 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
645
650 long postgisSrid() const;
651
664 QString authid() const;
665
673 QString description() const;
674
688 QString userFriendlyIdentifier( Qgis::CrsIdentifierType type = Qgis::CrsIdentifierType::MediumString ) const;
689
696 QString projectionAcronym() const;
697
704 QString ellipsoidAcronym() const;
705
717 QString toWkt( Qgis::CrsWktVariant variant = Qgis::CrsWktVariant::Wkt1Gdal, bool multiline = false, int indentationWidth = 4 ) const;
718
731 Q_DECL_DEPRECATED QString toProj4() const SIP_DEPRECATED;
732
745 QString toProj() const;
746
752 Qgis::CrsType type() const;
753
759 bool isDeprecated() const;
760
765 bool isGeographic() const;
766
775 bool isDynamic() const;
776
787 QgsDatumEnsemble datumEnsemble() const;
788
794 QString celestialBodyName() const;
795
819 void setCoordinateEpoch( double epoch );
820
844 double coordinateEpoch() const;
845
857 QgsProjectionFactors factors( const QgsPoint &point ) const;
858
865 QgsProjOperation operation() const;
866
874 bool hasAxisInverted() const;
875
881#ifndef SIP_RUN
882 QList< Qgis::CrsAxisDirection > axisOrdering() const;
883#else
884 SIP_PYOBJECT axisOrdering() const SIP_TYPEHINT( List[Qgis.CrsAxisDirection] );
885 % MethodCode
886 // adapted from the qpymultimedia_qlist.sip file from the PyQt6 sources
887
888 const QList< Qgis::CrsAxisDirection > cppRes = sipCpp->axisOrdering();
889
890 PyObject *l = PyList_New( cppRes.size() );
891
892 if ( !l )
893 sipIsErr = 1;
894 else
895 {
896 for ( int i = 0; i < cppRes.size(); ++i )
897 {
898 PyObject *eobj = sipConvertFromEnum( static_cast<int>( cppRes.at( i ) ),
899 sipType_Qgis_CrsAxisDirection );
900
901 if ( !eobj )
902 {
903 sipIsErr = 1;
904 }
905
906 PyList_SetItem( l, i, eobj );
907 }
908
909 if ( !sipIsErr )
910 {
911 sipRes = l;
912 }
913 else
914 {
915 Py_DECREF( l );
916 }
917 }
918 % End
919#endif
920
926 Qgis::DistanceUnit mapUnits() const;
927
935 QgsRectangle bounds() const;
936
943 QString toOgcUri() const;
944
951 QString toOgcUrn() const;
952
953 // Mutators -----------------------------------
954
971 void updateDefinition();
972
976 void setValidationHint( const QString &html );
977
981 QString validationHint() const;
982
989 static int syncDatabase();
990
1004 long saveAsUserCrs( const QString &name, Qgis::CrsDefinitionFormat nativeFormat = Qgis::CrsDefinitionFormat::Wkt );
1005
1015 void setNativeFormat( Qgis::CrsDefinitionFormat format );
1016
1026 Qgis::CrsDefinitionFormat nativeFormat() const;
1027
1037 QgsCoordinateReferenceSystem toGeographicCrs() const;
1038
1050 QgsCoordinateReferenceSystem toGeocentricCrs() const;
1051
1062 QgsCoordinateReferenceSystem horizontalCrs() const;
1063
1076 QgsCoordinateReferenceSystem verticalCrs() const;
1077
1084 bool hasVerticalAxis() const;
1085
1087 QString geographicCrsAuthId() const;
1088
1089#ifdef SIP_RUN
1090 SIP_PYOBJECT __repr__();
1091 % MethodCode
1092 const QString str = sipCpp->isValid() ? QStringLiteral( "<QgsCoordinateReferenceSystem: %1%2>" ).arg( !sipCpp->authid().isEmpty() ? sipCpp->authid() : sipCpp->toWkt( Qgis::CrsWktVariant::Preferred ),
1093 std::isfinite( sipCpp->coordinateEpoch() ) ? QStringLiteral( " @ %1" ).arg( sipCpp->coordinateEpoch() ) : QString() )
1094 : QStringLiteral( "<QgsCoordinateReferenceSystem: invalid>" );
1095 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
1096 % End
1097#endif
1098
1099#ifndef SIP_RUN
1100
1110 PJ *projObject() const;
1111
1122 static QgsCoordinateReferenceSystem fromProjObject( PJ *object );
1123
1132 bool createFromProjObject( PJ *object );
1133#endif
1134
1141 Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
1142
1148 Q_DECL_DEPRECATED static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems() SIP_DEPRECATED;
1149
1155 Q_DECL_DEPRECATED static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1156
1162 Q_DECL_DEPRECATED static void removeRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1163
1169 Q_DECL_DEPRECATED static void clearRecentCoordinateReferenceSystems() SIP_DEPRECATED;
1170
1171#ifndef SIP_RUN
1172
1182 static void invalidateCache( bool disableCache = false );
1183#else
1184
1191 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
1192#endif
1193
1194 // Mutators -----------------------------------
1195 // We don't want to expose these to the public api since they won't create
1196 // a fully valid crs. Programmers should use the createFrom* methods rather
1197 private:
1198
1204 static QString projFromSrsId( int srsId );
1205
1211 void setProjString( const QString &projString );
1212
1216 bool setWktString( const QString &wkt );
1217
1221 void debugPrint();
1222
1224 typedef QMap<QString, QString> RecordMap;
1225
1233 RecordMap getRecord( const QString &sql );
1234
1239 static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
1240
1242 void setMapUnits();
1243
1245 static long getRecordCount();
1246
1252 bool loadFromAuthCode( const QString &auth, const QString &code );
1253
1257 static QList< long > userSrsIds();
1258
1266 long matchToUserCrs() const;
1267
1272 bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
1273
1274 bool createFromWktInternal( const QString &wkt, const QString &description );
1275
1276 QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
1277
1278 QString mValidationHint;
1279
1281
1282 friend class QgsProjContext;
1283
1284 // Only meant to be called by QgsProjContext::~QgsProjContext()
1285 static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
1286
1288 static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
1289
1290 // cache
1291
1292 static bool sDisableSrIdCache;
1293 static bool sDisableOgcCache;
1294 static bool sDisableProjCache;
1295 static bool sDisableWktCache;
1296 static bool sDisableSrsIdCache;
1297 static bool sDisableStringCache;
1298
1299 // for tests
1300 static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
1301 static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
1302 static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
1303 static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
1304 static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
1305 static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
1306
1307 friend class TestQgsCoordinateReferenceSystem;
1309 friend bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1310 friend bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1311 friend bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1312 friend bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1313
1314 bool createFromPostgisSrid( const long id );
1315};
1316
1318
1319
1320#ifndef SIP_RUN
1321inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
1322{
1323 QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
1324 mySummary += QLatin1String( "\n\t\tDescription : " );
1325 if ( !r.description().isNull() )
1326 {
1327 mySummary += r.description();
1328 }
1329 else
1330 {
1331 mySummary += QLatin1String( "Undefined" );
1332 }
1333 mySummary += QLatin1String( "\n\t\tProjection : " );
1334 if ( !r.projectionAcronym().isNull() )
1335 {
1336 mySummary += r.projectionAcronym();
1337 }
1338 else
1339 {
1340 mySummary += QLatin1String( "Undefined" );
1341 }
1342
1343 mySummary += QLatin1String( "\n\t\tEllipsoid : " );
1344 if ( !r.ellipsoidAcronym().isNull() )
1345 {
1346 mySummary += r.ellipsoidAcronym();
1347 }
1348 else
1349 {
1350 mySummary += QLatin1String( "Undefined" );
1351 }
1352
1353 mySummary += QLatin1String( "\n\t\tProjString : " );
1354 if ( !r.toProj().isNull() )
1355 {
1356 mySummary += r.toProj();
1357 }
1358 else
1359 {
1360 mySummary += QLatin1String( "Undefined" );
1361 }
1362 // Using streams we need to use local 8 Bit
1363 return os << mySummary.toLocal8Bit().data() << std::endl;
1364}
1365
1366bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1367bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1368bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1369bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1370#endif
1371
1372#endif // QGSCOORDINATEREFERENCESYSTEM_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:54
DistanceUnit
Units of distance.
Definition qgis.h:4843
CrsDefinitionFormat
CRS definition formats.
Definition qgis.h:3742
@ Wkt
WKT format (always recommended over proj string format)
A registry for known coordinate reference system (CRS) definitions, including any user-defined CRSes.
Represents a coordinate reference system (CRS).
QString toProj() const
Returns a Proj string representation of this CRS.
QString ellipsoidAcronym() const
Returns the ellipsoid acronym for the ellipsoid used by the CRS.
QString projectionAcronym() const
Returns the projection acronym for the projection used by the CRS.
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.
Contains information about a datum ensemble.
Definition qgsdatums.h:95
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
Used to create and store a proj context object, correctly freeing the context upon destruction.
Contains information about a PROJ operation.
Contains various cartographic properties, such as scale factors, angular distortion and meridian conv...
A rectangle specified with double values.
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
bool operator>(const QVariant &v1, const QVariant &v2)
Compares two QVariant values and returns whether the first is greater than the second.
Definition qgis.h:6673
bool operator<(const QVariant &v1, const QVariant &v2)
Compares two QVariant values and returns whether the first is less than the second.
Definition qgis.h:6687
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:232
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_PYARGREMOVE
Definition qgis_sip.h:151
#define SIP_OUT
Definition qgis_sip.h:58
bool operator>=(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
bool operator<=(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
struct sqlite3 sqlite3
struct pj_ctx PJ_CONTEXT
bool CORE_EXPORT operator<(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
bool CORE_EXPORT operator>(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
bool CORE_EXPORT operator<=(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
struct PJconsts PJ
bool CORE_EXPORT operator>=(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
std::ostream & operator<<(std::ostream &os, const QgsCoordinateReferenceSystem &r)
Output stream operator.
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
const QgsCoordinateReferenceSystem & crs
bool hasAxisInverted