QGIS API Documentation 3.43.0-Master (ebb4087afc0)
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 "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
367 // Misc helper functions -----------------------
368
369 // TODO QGIS 4: remove type and always use EPSG code, rename to createFromEpsg
370
376 Q_DECL_DEPRECATED bool createFromId( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
377
378 // TODO QGIS 4: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
379
393 bool createFromOgcWmsCrs( const QString &crs );
394
395 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
396
404 Q_DECL_DEPRECATED bool createFromSrid( long srid ) SIP_DEPRECATED;
405
421 bool createFromWkt( const QString &wkt );
422
438 bool createFromSrsId( long srsId );
439
466 Q_DECL_DEPRECATED bool createFromProj4( const QString &projString ) SIP_DEPRECATED;
467
497#ifndef SIP_RUN
498 bool createFromProj( const QString &projString, bool identify = true );
499#else
500 bool createFromProj( const QString &projString );
501#endif
502
522 bool createFromString( const QString &definition );
523
524 // TODO QGIS 4: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
525
544 bool createFromUserInput( const QString &definition );
545
556 Q_DECL_DEPRECATED static void setupESRIWktFix() SIP_DEPRECATED;
557
559 bool isValid() const;
560
573 void validate();
574
575 // TODO QGIS 4: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
576
585 Q_DECL_DEPRECATED long findMatchingProj() SIP_DEPRECATED;
586
587 bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
588 bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
589
596 bool readXml( const QDomNode &node );
597
604 bool writeXml( QDomNode &node, QDomDocument &doc ) const;
605
606
611 static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
612
617 static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
618
619 // Accessors -----------------------------------
620
625 long srsid() const;
626
627 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
628
633 long postgisSrid() const;
634
647 QString authid() const;
648
656 QString description() const;
657
671 QString userFriendlyIdentifier( Qgis::CrsIdentifierType type = Qgis::CrsIdentifierType::MediumString ) const;
672
679 QString projectionAcronym() const;
680
687 QString ellipsoidAcronym() const;
688
700 QString toWkt( Qgis::CrsWktVariant variant = Qgis::CrsWktVariant::Wkt1Gdal, bool multiline = false, int indentationWidth = 4 ) const;
701
714 Q_DECL_DEPRECATED QString toProj4() const SIP_DEPRECATED;
715
728 QString toProj() const;
729
735 Qgis::CrsType type() const;
736
742 bool isDeprecated() const;
743
748 bool isGeographic() const;
749
758 bool isDynamic() const;
759
770 QgsDatumEnsemble datumEnsemble() const;
771
777 QString celestialBodyName() const;
778
802 void setCoordinateEpoch( double epoch );
803
827 double coordinateEpoch() const;
828
840 QgsProjectionFactors factors( const QgsPoint &point ) const;
841
848 QgsProjOperation operation() const;
849
857 bool hasAxisInverted() const;
858
864#ifndef SIP_RUN
865 QList< Qgis::CrsAxisDirection > axisOrdering() const;
866#else
867 SIP_PYOBJECT axisOrdering() const SIP_TYPEHINT( List[Qgis.CrsAxisDirection] );
868 % MethodCode
869 // adapted from the qpymultimedia_qlist.sip file from the PyQt6 sources
870
871 const QList< Qgis::CrsAxisDirection > cppRes = sipCpp->axisOrdering();
872
873 PyObject *l = PyList_New( cppRes.size() );
874
875 if ( !l )
876 sipIsErr = 1;
877 else
878 {
879 for ( int i = 0; i < cppRes.size(); ++i )
880 {
881 PyObject *eobj = sipConvertFromEnum( static_cast<int>( cppRes.at( i ) ),
882 sipType_Qgis_CrsAxisDirection );
883
884 if ( !eobj )
885 {
886 sipIsErr = 1;
887 }
888
889 PyList_SetItem( l, i, eobj );
890 }
891
892 if ( !sipIsErr )
893 {
894 sipRes = l;
895 }
896 else
897 {
898 Py_DECREF( l );
899 }
900 }
901 % End
902#endif
903
909 Qgis::DistanceUnit mapUnits() const;
910
918 QgsRectangle bounds() const;
919
926 QString toOgcUri() const;
927
934 QString toOgcUrn() const;
935
936 // Mutators -----------------------------------
937
954 void updateDefinition();
955
959 void setValidationHint( const QString &html );
960
964 QString validationHint() const;
965
972 static int syncDatabase();
973
987 long saveAsUserCrs( const QString &name, Qgis::CrsDefinitionFormat nativeFormat = Qgis::CrsDefinitionFormat::Wkt );
988
998 void setNativeFormat( Qgis::CrsDefinitionFormat format );
999
1009 Qgis::CrsDefinitionFormat nativeFormat() const;
1010
1020 QgsCoordinateReferenceSystem toGeographicCrs() const;
1021
1032 QgsCoordinateReferenceSystem horizontalCrs() const;
1033
1046 QgsCoordinateReferenceSystem verticalCrs() const;
1047
1054 bool hasVerticalAxis() const;
1055
1057 QString geographicCrsAuthId() const;
1058
1059#ifdef SIP_RUN
1060 SIP_PYOBJECT __repr__();
1061 % MethodCode
1062 const QString str = sipCpp->isValid() ? QStringLiteral( "<QgsCoordinateReferenceSystem: %1%2>" ).arg( !sipCpp->authid().isEmpty() ? sipCpp->authid() : sipCpp->toWkt( Qgis::CrsWktVariant::Preferred ),
1063 std::isfinite( sipCpp->coordinateEpoch() ) ? QStringLiteral( " @ %1" ).arg( sipCpp->coordinateEpoch() ) : QString() )
1064 : QStringLiteral( "<QgsCoordinateReferenceSystem: invalid>" );
1065 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
1066 % End
1067#endif
1068
1069#ifndef SIP_RUN
1070
1080 PJ *projObject() const;
1081
1092 static QgsCoordinateReferenceSystem fromProjObject( PJ *object );
1093
1102 bool createFromProjObject( PJ *object );
1103#endif
1104
1111 Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
1112
1118 Q_DECL_DEPRECATED static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems() SIP_DEPRECATED;
1119
1125 Q_DECL_DEPRECATED static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1126
1132 Q_DECL_DEPRECATED static void removeRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1133
1139 Q_DECL_DEPRECATED static void clearRecentCoordinateReferenceSystems() SIP_DEPRECATED;
1140
1141#ifndef SIP_RUN
1142
1152 static void invalidateCache( bool disableCache = false );
1153#else
1154
1161 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
1162#endif
1163
1164 // Mutators -----------------------------------
1165 // We don't want to expose these to the public api since they won't create
1166 // a fully valid crs. Programmers should use the createFrom* methods rather
1167 private:
1168
1174 static QString projFromSrsId( int srsId );
1175
1181 void setProjString( const QString &projString );
1182
1186 bool setWktString( const QString &wkt );
1187
1191 void debugPrint();
1192
1194 typedef QMap<QString, QString> RecordMap;
1195
1203 RecordMap getRecord( const QString &sql );
1204
1209 static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
1210
1212 void setMapUnits();
1213
1215 static long getRecordCount();
1216
1222 bool loadFromAuthCode( const QString &auth, const QString &code );
1223
1227 static QList< long > userSrsIds();
1228
1236 long matchToUserCrs() const;
1237
1242 bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
1243
1244 bool createFromWktInternal( const QString &wkt, const QString &description );
1245
1246 QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
1247
1248 QString mValidationHint;
1249
1251
1252 friend class QgsProjContext;
1253
1254 // Only meant to be called by QgsProjContext::~QgsProjContext()
1255 static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
1256
1258 static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
1259
1260 // cache
1261
1262 static bool sDisableSrIdCache;
1263 static bool sDisableOgcCache;
1264 static bool sDisableProjCache;
1265 static bool sDisableWktCache;
1266 static bool sDisableSrsIdCache;
1267 static bool sDisableStringCache;
1268
1269 // for tests
1270 static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
1271 static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
1272 static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
1273 static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
1274 static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
1275 static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
1276
1277 friend class TestQgsCoordinateReferenceSystem;
1279 friend bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1280 friend bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1281 friend bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1282 friend bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1283
1284 bool createFromPostgisSrid( const long id );
1285};
1286
1288
1289
1290#ifndef SIP_RUN
1291inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
1292{
1293 QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
1294 mySummary += QLatin1String( "\n\t\tDescription : " );
1295 if ( !r.description().isNull() )
1296 {
1297 mySummary += r.description();
1298 }
1299 else
1300 {
1301 mySummary += QLatin1String( "Undefined" );
1302 }
1303 mySummary += QLatin1String( "\n\t\tProjection : " );
1304 if ( !r.projectionAcronym().isNull() )
1305 {
1306 mySummary += r.projectionAcronym();
1307 }
1308 else
1309 {
1310 mySummary += QLatin1String( "Undefined" );
1311 }
1312
1313 mySummary += QLatin1String( "\n\t\tEllipsoid : " );
1314 if ( !r.ellipsoidAcronym().isNull() )
1315 {
1316 mySummary += r.ellipsoidAcronym();
1317 }
1318 else
1319 {
1320 mySummary += QLatin1String( "Undefined" );
1321 }
1322
1323 mySummary += QLatin1String( "\n\t\tProjString : " );
1324 if ( !r.toProj().isNull() )
1325 {
1326 mySummary += r.toProj();
1327 }
1328 else
1329 {
1330 mySummary += QLatin1String( "Undefined" );
1331 }
1332 // Using streams we need to use local 8 Bit
1333 return os << mySummary.toLocal8Bit().data() << std::endl;
1334}
1335
1336bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1337bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1338bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1339bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1340#endif
1341
1342#endif // QGSCOORDINATEREFERENCESYSTEM_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
DistanceUnit
Units of distance.
Definition qgis.h:4821
CrsDefinitionFormat
CRS definition formats.
Definition qgis.h:3738
@ Wkt
WKT format (always recommended over proj string format)
A registry for known coordinate reference system (CRS) definitions, including any user-defined CRSes.
This class 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:6573
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:6587
#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