QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
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 <ostream>
23
24#include "qgis_core.h"
25
26//qt includes
27#include <QString>
28#include <QMap>
29#include <QHash>
30#include <QReadWriteLock>
31#include <QExplicitlySharedDataPointer>
32#include <QObject>
33
34//qgis includes
35#include "qgis_sip.h"
36#include "qgis.h"
37#include "qgsconfig.h"
38#include "qgsrectangle.h"
39#include "qgssqliteutils.h"
40
41class QDomNode;
42class QDomDocument;
43class QgsCoordinateReferenceSystemPrivate;
47
48#ifndef SIP_RUN
49struct PJconsts;
50typedef struct PJconsts PJ;
51
52struct pj_ctx;
53typedef struct pj_ctx PJ_CONTEXT;
54#endif
55
56// forward declaration for sqlite3
57typedef struct sqlite3 sqlite3 SIP_SKIP;
58
61
212class CORE_EXPORT QgsCoordinateReferenceSystem
213{
214 Q_GADGET
217 Q_PROPERTY( bool isGeographic READ isGeographic )
218 Q_PROPERTY( QString authid READ authid )
219 Q_PROPERTY( QString description READ description )
220
221 public:
222
223 //! Enumeration of types of IDs accepted in createFromId() method
224 enum CrsType
228 EpsgCrsId
229 };
230
233
235
236 // TODO QGIS 4: remove "POSTGIS" and "INTERNAL"
237
257 explicit QgsCoordinateReferenceSystem( const QString &definition );
258
259 // TODO QGIS 4: remove type and always use EPSG code
260
272 Q_DECL_DEPRECATED explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
273
276
277 //! Allows direct construction of QVariants from QgsCoordinateReferenceSystem.
278 operator QVariant() const
279 {
280 return QVariant::fromValue( *this );
281 }
282
289 static QList< long > validSrsIds();
290
291 // static creators
292
303 static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
304
314 Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
315
326 Q_DECL_DEPRECATED static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 ) SIP_DEPRECATED;
327
335 static QgsCoordinateReferenceSystem fromProj( const QString &proj );
336
347 static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
348
360 static QgsCoordinateReferenceSystem fromSrsId( long srsId );
361
377 static QgsCoordinateReferenceSystem createCompoundCrs( const QgsCoordinateReferenceSystem &horizontalCrs, const QgsCoordinateReferenceSystem &verticalCrs, QString &error SIP_OUT );
378
394 static QgsCoordinateReferenceSystem createGeocentricCrs( const QString &ellipsoid );
395
396 // Misc helper functions -----------------------
397
398 // TODO QGIS 4: remove type and always use EPSG code, rename to createFromEpsg
399
405 Q_DECL_DEPRECATED bool createFromId( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
406
407 // TODO QGIS 4: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
408
422 bool createFromOgcWmsCrs( const QString &crs );
423
424 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
425
433 Q_DECL_DEPRECATED bool createFromSrid( long srid ) SIP_DEPRECATED;
434
450 bool createFromWkt( const QString &wkt );
451
467 bool createFromSrsId( long srsId );
468
495 Q_DECL_DEPRECATED bool createFromProj4( const QString &projString ) SIP_DEPRECATED;
496
526#ifndef SIP_RUN
527 bool createFromProj( const QString &projString, bool identify = true );
528#else
529 bool createFromProj( const QString &projString );
530#endif
531
551 bool createFromString( const QString &definition );
552
553 // TODO QGIS 4: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
554
573 bool createFromUserInput( const QString &definition );
574
585 Q_DECL_DEPRECATED static void setupESRIWktFix() SIP_DEPRECATED;
586
588 bool isValid() const;
589
602 void validate();
603
604 // TODO QGIS 4: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
605
614 Q_DECL_DEPRECATED long findMatchingProj() SIP_DEPRECATED;
615
616 bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
617 bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
618
625 bool readXml( const QDomNode &node );
626
633 bool writeXml( QDomNode &node, QDomDocument &doc ) const;
634
635
640 static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
641
646 static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
647
648 // Accessors -----------------------------------
649
654 long srsid() const;
655
656 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
657
662 long postgisSrid() const;
663
676 QString authid() const;
677
685 QString description() const;
686
700 QString userFriendlyIdentifier( Qgis::CrsIdentifierType type = Qgis::CrsIdentifierType::MediumString ) const;
701
708 QString projectionAcronym() const;
709
716 QString ellipsoidAcronym() const;
717
729 QString toWkt( Qgis::CrsWktVariant variant = Qgis::CrsWktVariant::Wkt1Gdal, bool multiline = false, int indentationWidth = 4 ) const;
730
743 Q_DECL_DEPRECATED QString toProj4() const SIP_DEPRECATED;
744
757 QString toProj() const;
758
770 std::string toJsonString( bool multiline = false, int indentationWidth = 4, const QString &schema = QString() ) const;
771
777 Qgis::CrsType type() const;
778
784 bool isDeprecated() const;
785
790 bool isGeographic() const;
791
800 bool isDynamic() const;
801
812 QgsDatumEnsemble datumEnsemble() const;
813
819 QString celestialBodyName() const;
820
844 void setCoordinateEpoch( double epoch );
845
869 double coordinateEpoch() const;
870
882 QgsProjectionFactors factors( const QgsPoint &point ) const;
883
890 QgsProjOperation operation() const;
891
899 bool hasAxisInverted() const;
900
906#ifndef SIP_RUN
907 QList< Qgis::CrsAxisDirection > axisOrdering() const;
908#else
909 SIP_PYOBJECT axisOrdering() const SIP_TYPEHINT( List[Qgis.CrsAxisDirection] );
910 % MethodCode
911 // adapted from the qpymultimedia_qlist.sip file from the PyQt6 sources
912
913 const QList< Qgis::CrsAxisDirection > cppRes = sipCpp->axisOrdering();
914
915 PyObject *l = PyList_New( cppRes.size() );
916
917 if ( !l )
918 sipIsErr = 1;
919 else
920 {
921 for ( int i = 0; i < cppRes.size(); ++i )
922 {
923 PyObject *eobj = sipConvertFromEnum( static_cast<int>( cppRes.at( i ) ),
924 sipType_Qgis_CrsAxisDirection );
925
926 if ( !eobj )
927 {
928 sipIsErr = 1;
929 }
930
931 PyList_SetItem( l, i, eobj );
932 }
933
934 if ( !sipIsErr )
935 {
936 sipRes = l;
937 }
938 else
939 {
940 Py_DECREF( l );
941 }
942 }
943 % End
944#endif
945
951 Qgis::DistanceUnit mapUnits() const;
952
960 QgsRectangle bounds() const;
961
968 QString toOgcUri() const;
969
976 QString toOgcUrn() const;
977
978 // Mutators -----------------------------------
979
996 void updateDefinition();
997
1001 void setValidationHint( const QString &html );
1002
1006 QString validationHint() const;
1007
1014 static int syncDatabase();
1015
1029 long saveAsUserCrs( const QString &name, Qgis::CrsDefinitionFormat nativeFormat = Qgis::CrsDefinitionFormat::Wkt );
1030
1040 void setNativeFormat( Qgis::CrsDefinitionFormat format );
1041
1051 Qgis::CrsDefinitionFormat nativeFormat() const;
1052
1062 QgsCoordinateReferenceSystem toGeographicCrs() const;
1063
1075 QgsCoordinateReferenceSystem toGeocentricCrs() const;
1076
1087 QgsCoordinateReferenceSystem horizontalCrs() const;
1088
1101 QgsCoordinateReferenceSystem verticalCrs() const;
1102
1109 bool hasVerticalAxis() const;
1110
1112 QString geographicCrsAuthId() const;
1113
1114#ifdef SIP_RUN
1115 SIP_PYOBJECT __repr__();
1116 % MethodCode
1117 const QString str = sipCpp->isValid() ? QStringLiteral( "<QgsCoordinateReferenceSystem: %1%2>" ).arg( !sipCpp->authid().isEmpty() ? sipCpp->authid() : sipCpp->toWkt( Qgis::CrsWktVariant::Preferred ),
1118 std::isfinite( sipCpp->coordinateEpoch() ) ? QStringLiteral( " @ %1" ).arg( sipCpp->coordinateEpoch() ) : QString() )
1119 : QStringLiteral( "<QgsCoordinateReferenceSystem: invalid>" );
1120 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
1121 % End
1122#endif
1123
1124#ifndef SIP_RUN
1125
1135 PJ *projObject() const;
1136
1147 static QgsCoordinateReferenceSystem fromProjObject( PJ *object );
1148
1157 bool createFromProjObject( PJ *object );
1158#endif
1159
1166 Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
1167
1173 Q_DECL_DEPRECATED static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems() SIP_DEPRECATED;
1174
1180 Q_DECL_DEPRECATED static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1181
1187 Q_DECL_DEPRECATED static void removeRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1188
1194 Q_DECL_DEPRECATED static void clearRecentCoordinateReferenceSystems() SIP_DEPRECATED;
1195
1196#ifndef SIP_RUN
1197
1207 static void invalidateCache( bool disableCache = false );
1208#else
1209
1216 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
1217#endif
1218
1219 // Mutators -----------------------------------
1220 // We don't want to expose these to the public api since they won't create
1221 // a fully valid crs. Programmers should use the createFrom* methods rather
1222 private:
1223
1229 static QString projFromSrsId( int srsId );
1230
1236 void setProjString( const QString &projString );
1237
1241 bool setWktString( const QString &wkt );
1242
1246 void debugPrint();
1247
1249 typedef QMap<QString, QString> RecordMap;
1250
1258 RecordMap getRecord( const QString &sql );
1259
1264 static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
1265
1267 void setMapUnits();
1268
1270 static long getRecordCount();
1271
1277 bool loadFromAuthCode( const QString &auth, const QString &code );
1278
1282 static QList< long > userSrsIds();
1283
1291 long matchToUserCrs() const;
1292
1297 bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
1298
1299 bool createFromWktInternal( const QString &wkt, const QString &description );
1300
1301 QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
1302
1303 QString mValidationHint;
1304
1307 friend class QgsProjContext;
1308
1309 // Only meant to be called by QgsProjContext::~QgsProjContext()
1310 static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
1311
1313 static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
1314
1315 // cache
1316
1317 static bool sDisableSrIdCache;
1318 static bool sDisableOgcCache;
1319 static bool sDisableProjCache;
1320 static bool sDisableWktCache;
1321 static bool sDisableSrsIdCache;
1322 static bool sDisableStringCache;
1323
1324 // for tests
1325 static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
1326 static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
1327 static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
1328 static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
1329 static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
1330 static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
1334 friend bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1335 friend bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1336 friend bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1337 friend bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1338
1339 bool createFromPostgisSrid( const long id );
1340};
1341
1343
1344
1345#ifndef SIP_RUN
1346inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
1347{
1348 QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
1349 mySummary += QLatin1String( "\n\t\tDescription : " );
1350 if ( !r.description().isNull() )
1351 {
1352 mySummary += r.description();
1353 }
1354 else
1355 {
1356 mySummary += QLatin1String( "Undefined" );
1357 }
1358 mySummary += QLatin1String( "\n\t\tProjection : " );
1359 if ( !r.projectionAcronym().isNull() )
1360 {
1361 mySummary += r.projectionAcronym();
1362 }
1363 else
1364 {
1365 mySummary += QLatin1String( "Undefined" );
1366 }
1367
1368 mySummary += QLatin1String( "\n\t\tEllipsoid : " );
1369 if ( !r.ellipsoidAcronym().isNull() )
1370 {
1371 mySummary += r.ellipsoidAcronym();
1372 }
1373 else
1374 {
1375 mySummary += QLatin1String( "Undefined" );
1376 }
1377
1378 mySummary += QLatin1String( "\n\t\tProjString : " );
1379 if ( !r.toProj().isNull() )
1380 {
1381 mySummary += r.toProj();
1382 }
1383 else
1384 {
1385 mySummary += QLatin1String( "Undefined" );
1386 }
1387 // Using streams we need to use local 8 Bit
1388 return os << mySummary.toLocal8Bit().data() << std::endl;
1389}
1390
1391bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1392bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1393bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1394bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1395#endif
1396
1397#endif // QGSCOORDINATEREFERENCESYSTEM_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:56
DistanceUnit
Units of distance.
Definition qgis.h:5013
CrsDefinitionFormat
CRS definition formats.
Definition qgis.h:3891
@ Wkt
WKT format (always recommended over proj string format).
Definition qgis.h:3892
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
Definition qgis.h:2439
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.
QgsCoordinateReferenceSystem()
Constructs an invalid CRS object.
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
Contains information about a PROJ operation.
Contains various cartographic properties, such as scale factors, angular distortion and meridian conv...
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:7023
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:7039
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:240
#define SIP_DEPRECATED
Definition qgis_sip.h:114
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_PYARGREMOVE
Definition qgis_sip.h:159
#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)