QGIS API Documentation 3.43.0-Master (56aa1fd18d7)
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
212{
213 Q_GADGET
214
215 Q_PROPERTY( Qgis::DistanceUnit mapUnits READ mapUnits )
216 Q_PROPERTY( bool isGeographic READ isGeographic )
217 Q_PROPERTY( QString authid READ authid )
218 Q_PROPERTY( QString description READ description )
219
220 public:
221
224 {
227 EpsgCrsId
228 };
229
232
234
235 // TODO QGIS 4: remove "POSTGIS" and "INTERNAL"
236
256 explicit QgsCoordinateReferenceSystem( const QString &definition );
257
258 // TODO QGIS 4: remove type and always use EPSG code
259
271 Q_DECL_DEPRECATED explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
272
275
277 operator QVariant() const
278 {
279 return QVariant::fromValue( *this );
280 }
281
288 static QList< long > validSrsIds();
289
290 // static creators
291
302 static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
303
313 Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
314
325 Q_DECL_DEPRECATED static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 ) SIP_DEPRECATED;
326
334 static QgsCoordinateReferenceSystem fromProj( const QString &proj );
335
346 static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
347
359 static QgsCoordinateReferenceSystem fromSrsId( long srsId );
360
376 static QgsCoordinateReferenceSystem createCompoundCrs( const QgsCoordinateReferenceSystem &horizontalCrs, const QgsCoordinateReferenceSystem &verticalCrs, QString &error SIP_OUT );
377
393 static QgsCoordinateReferenceSystem createGeocentricCrs( const QString &ellipsoid );
394
395 // Misc helper functions -----------------------
396
397 // TODO QGIS 4: remove type and always use EPSG code, rename to createFromEpsg
398
404 Q_DECL_DEPRECATED bool createFromId( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
405
406 // TODO QGIS 4: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
407
421 bool createFromOgcWmsCrs( const QString &crs );
422
423 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
424
432 Q_DECL_DEPRECATED bool createFromSrid( long srid ) SIP_DEPRECATED;
433
449 bool createFromWkt( const QString &wkt );
450
466 bool createFromSrsId( long srsId );
467
494 Q_DECL_DEPRECATED bool createFromProj4( const QString &projString ) SIP_DEPRECATED;
495
525#ifndef SIP_RUN
526 bool createFromProj( const QString &projString, bool identify = true );
527#else
528 bool createFromProj( const QString &projString );
529#endif
530
550 bool createFromString( const QString &definition );
551
552 // TODO QGIS 4: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
553
572 bool createFromUserInput( const QString &definition );
573
584 Q_DECL_DEPRECATED static void setupESRIWktFix() SIP_DEPRECATED;
585
587 bool isValid() const;
588
601 void validate();
602
603 // TODO QGIS 4: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
604
613 Q_DECL_DEPRECATED long findMatchingProj() SIP_DEPRECATED;
614
615 bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
616 bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
617
624 bool readXml( const QDomNode &node );
625
632 bool writeXml( QDomNode &node, QDomDocument &doc ) const;
633
634
639 static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
640
645 static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
646
647 // Accessors -----------------------------------
648
653 long srsid() const;
654
655 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
656
661 long postgisSrid() const;
662
675 QString authid() const;
676
684 QString description() const;
685
699 QString userFriendlyIdentifier( Qgis::CrsIdentifierType type = Qgis::CrsIdentifierType::MediumString ) const;
700
707 QString projectionAcronym() const;
708
715 QString ellipsoidAcronym() const;
716
728 QString toWkt( Qgis::CrsWktVariant variant = Qgis::CrsWktVariant::Wkt1Gdal, bool multiline = false, int indentationWidth = 4 ) const;
729
742 Q_DECL_DEPRECATED QString toProj4() const SIP_DEPRECATED;
743
756 QString toProj() const;
757
763 Qgis::CrsType type() const;
764
770 bool isDeprecated() const;
771
776 bool isGeographic() const;
777
786 bool isDynamic() const;
787
798 QgsDatumEnsemble datumEnsemble() const;
799
805 QString celestialBodyName() const;
806
830 void setCoordinateEpoch( double epoch );
831
855 double coordinateEpoch() const;
856
868 QgsProjectionFactors factors( const QgsPoint &point ) const;
869
876 QgsProjOperation operation() const;
877
885 bool hasAxisInverted() const;
886
892#ifndef SIP_RUN
893 QList< Qgis::CrsAxisDirection > axisOrdering() const;
894#else
895 SIP_PYOBJECT axisOrdering() const SIP_TYPEHINT( List[Qgis.CrsAxisDirection] );
896 % MethodCode
897 // adapted from the qpymultimedia_qlist.sip file from the PyQt6 sources
898
899 const QList< Qgis::CrsAxisDirection > cppRes = sipCpp->axisOrdering();
900
901 PyObject *l = PyList_New( cppRes.size() );
902
903 if ( !l )
904 sipIsErr = 1;
905 else
906 {
907 for ( int i = 0; i < cppRes.size(); ++i )
908 {
909 PyObject *eobj = sipConvertFromEnum( static_cast<int>( cppRes.at( i ) ),
910 sipType_Qgis_CrsAxisDirection );
911
912 if ( !eobj )
913 {
914 sipIsErr = 1;
915 }
916
917 PyList_SetItem( l, i, eobj );
918 }
919
920 if ( !sipIsErr )
921 {
922 sipRes = l;
923 }
924 else
925 {
926 Py_DECREF( l );
927 }
928 }
929 % End
930#endif
931
937 Qgis::DistanceUnit mapUnits() const;
938
946 QgsRectangle bounds() const;
947
954 QString toOgcUri() const;
955
962 QString toOgcUrn() const;
963
964 // Mutators -----------------------------------
965
982 void updateDefinition();
983
987 void setValidationHint( const QString &html );
988
992 QString validationHint() const;
993
1000 static int syncDatabase();
1001
1015 long saveAsUserCrs( const QString &name, Qgis::CrsDefinitionFormat nativeFormat = Qgis::CrsDefinitionFormat::Wkt );
1016
1026 void setNativeFormat( Qgis::CrsDefinitionFormat format );
1027
1037 Qgis::CrsDefinitionFormat nativeFormat() const;
1038
1048 QgsCoordinateReferenceSystem toGeographicCrs() const;
1049
1061 QgsCoordinateReferenceSystem toGeocentricCrs() const;
1062
1073 QgsCoordinateReferenceSystem horizontalCrs() const;
1074
1087 QgsCoordinateReferenceSystem verticalCrs() const;
1088
1095 bool hasVerticalAxis() const;
1096
1098 QString geographicCrsAuthId() const;
1099
1100#ifdef SIP_RUN
1101 SIP_PYOBJECT __repr__();
1102 % MethodCode
1103 const QString str = sipCpp->isValid() ? QStringLiteral( "<QgsCoordinateReferenceSystem: %1%2>" ).arg( !sipCpp->authid().isEmpty() ? sipCpp->authid() : sipCpp->toWkt( Qgis::CrsWktVariant::Preferred ),
1104 std::isfinite( sipCpp->coordinateEpoch() ) ? QStringLiteral( " @ %1" ).arg( sipCpp->coordinateEpoch() ) : QString() )
1105 : QStringLiteral( "<QgsCoordinateReferenceSystem: invalid>" );
1106 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
1107 % End
1108#endif
1109
1110#ifndef SIP_RUN
1111
1121 PJ *projObject() const;
1122
1133 static QgsCoordinateReferenceSystem fromProjObject( PJ *object );
1134
1143 bool createFromProjObject( PJ *object );
1144#endif
1145
1152 Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
1153
1159 Q_DECL_DEPRECATED static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems() SIP_DEPRECATED;
1160
1166 Q_DECL_DEPRECATED static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1167
1173 Q_DECL_DEPRECATED static void removeRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1174
1180 Q_DECL_DEPRECATED static void clearRecentCoordinateReferenceSystems() SIP_DEPRECATED;
1181
1182#ifndef SIP_RUN
1183
1193 static void invalidateCache( bool disableCache = false );
1194#else
1195
1202 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
1203#endif
1204
1205 // Mutators -----------------------------------
1206 // We don't want to expose these to the public api since they won't create
1207 // a fully valid crs. Programmers should use the createFrom* methods rather
1208 private:
1209
1215 static QString projFromSrsId( int srsId );
1216
1222 void setProjString( const QString &projString );
1223
1227 bool setWktString( const QString &wkt );
1228
1232 void debugPrint();
1233
1235 typedef QMap<QString, QString> RecordMap;
1236
1244 RecordMap getRecord( const QString &sql );
1245
1250 static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
1251
1253 void setMapUnits();
1254
1256 static long getRecordCount();
1257
1263 bool loadFromAuthCode( const QString &auth, const QString &code );
1264
1268 static QList< long > userSrsIds();
1269
1277 long matchToUserCrs() const;
1278
1283 bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
1284
1285 bool createFromWktInternal( const QString &wkt, const QString &description );
1286
1287 QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
1288
1289 QString mValidationHint;
1290
1292
1293 friend class QgsProjContext;
1294
1295 // Only meant to be called by QgsProjContext::~QgsProjContext()
1296 static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
1297
1299 static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
1300
1301 // cache
1302
1303 static bool sDisableSrIdCache;
1304 static bool sDisableOgcCache;
1305 static bool sDisableProjCache;
1306 static bool sDisableWktCache;
1307 static bool sDisableSrsIdCache;
1308 static bool sDisableStringCache;
1309
1310 // for tests
1311 static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
1312 static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
1313 static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
1314 static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
1315 static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
1316 static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
1317
1318 friend class TestQgsCoordinateReferenceSystem;
1320 friend bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1321 friend bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1322 friend bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1323 friend bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1324
1325 bool createFromPostgisSrid( const long id );
1326};
1327
1329
1330
1331#ifndef SIP_RUN
1332inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
1333{
1334 QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
1335 mySummary += QLatin1String( "\n\t\tDescription : " );
1336 if ( !r.description().isNull() )
1337 {
1338 mySummary += r.description();
1339 }
1340 else
1341 {
1342 mySummary += QLatin1String( "Undefined" );
1343 }
1344 mySummary += QLatin1String( "\n\t\tProjection : " );
1345 if ( !r.projectionAcronym().isNull() )
1346 {
1347 mySummary += r.projectionAcronym();
1348 }
1349 else
1350 {
1351 mySummary += QLatin1String( "Undefined" );
1352 }
1353
1354 mySummary += QLatin1String( "\n\t\tEllipsoid : " );
1355 if ( !r.ellipsoidAcronym().isNull() )
1356 {
1357 mySummary += r.ellipsoidAcronym();
1358 }
1359 else
1360 {
1361 mySummary += QLatin1String( "Undefined" );
1362 }
1363
1364 mySummary += QLatin1String( "\n\t\tProjString : " );
1365 if ( !r.toProj().isNull() )
1366 {
1367 mySummary += r.toProj();
1368 }
1369 else
1370 {
1371 mySummary += QLatin1String( "Undefined" );
1372 }
1373 // Using streams we need to use local 8 Bit
1374 return os << mySummary.toLocal8Bit().data() << std::endl;
1375}
1376
1377bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1378bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1379bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1380bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1381#endif
1382
1383#endif // QGSCOORDINATEREFERENCESYSTEM_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:54
DistanceUnit
Units of distance.
Definition qgis.h:4859
CrsDefinitionFormat
CRS definition formats.
Definition qgis.h:3757
@ 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:6709
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:6723
#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