QGIS API Documentation 3.99.0-Master (d270888f95f)
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#include <QString>
27
28using namespace Qt::StringLiterals;
29
30//qt includes
31#include <QString>
32#include <QMap>
33#include <QHash>
34#include <QReadWriteLock>
35#include <QExplicitlySharedDataPointer>
36#include <QObject>
37
38//qgis includes
39#include "qgis_sip.h"
40#include "qgis.h"
41#include "qgsconfig.h"
42#include "qgsrectangle.h"
43#include "qgssqliteutils.h"
44
45class QDomNode;
46class QDomDocument;
47class QgsCoordinateReferenceSystemPrivate;
51
52#ifndef SIP_RUN
53struct PJconsts;
54typedef struct PJconsts PJ;
55
56struct pj_ctx;
57typedef struct pj_ctx PJ_CONTEXT;
58#endif
59
60// forward declaration for sqlite3
61typedef struct sqlite3 sqlite3 SIP_SKIP;
62
65
216class CORE_EXPORT QgsCoordinateReferenceSystem
217{
218 Q_GADGET
221 Q_PROPERTY( bool isGeographic READ isGeographic )
222 Q_PROPERTY( QString authid READ authid )
223 Q_PROPERTY( QString description READ description )
224
225 public:
226
227 //! Enumeration of types of IDs accepted in createFromId() method
228 enum CrsType
232 EpsgCrsId
233 };
234
237
239
240 // TODO QGIS 5: remove "POSTGIS" and "INTERNAL"
241
261 explicit QgsCoordinateReferenceSystem( const QString &definition );
262
263 // TODO QGIS 5: remove type and always use EPSG code
264
276 Q_DECL_DEPRECATED explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
277
280
281 //! Allows direct construction of QVariants from QgsCoordinateReferenceSystem.
282 operator QVariant() const
283 {
284 return QVariant::fromValue( *this );
285 }
286
293 static QList< long > validSrsIds();
294
295 // static creators
296
307 static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
308
318 Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
319
330 Q_DECL_DEPRECATED static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 ) SIP_DEPRECATED;
331
339 static QgsCoordinateReferenceSystem fromProj( const QString &proj );
340
351 static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
352
364 static QgsCoordinateReferenceSystem fromSrsId( long srsId );
365
381 static QgsCoordinateReferenceSystem createCompoundCrs( const QgsCoordinateReferenceSystem &horizontalCrs, const QgsCoordinateReferenceSystem &verticalCrs, QString &error SIP_OUT );
382
398 static QgsCoordinateReferenceSystem createGeocentricCrs( const QString &ellipsoid );
399
400 // Misc helper functions -----------------------
401
402 // TODO QGIS 5: remove type and always use EPSG code, rename to createFromEpsg
403
409 Q_DECL_DEPRECATED bool createFromId( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
410
411 // TODO QGIS 5: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
412
426 bool createFromOgcWmsCrs( const QString &crs );
427
428 // TODO QGIS 5: remove unless really necessary - let's use EPSG codes instead
429
437 Q_DECL_DEPRECATED bool createFromSrid( long srid ) SIP_DEPRECATED;
438
454 bool createFromWkt( const QString &wkt );
455
471 bool createFromSrsId( long srsId );
472
499 Q_DECL_DEPRECATED bool createFromProj4( const QString &projString ) SIP_DEPRECATED;
500
530#ifndef SIP_RUN
531 bool createFromProj( const QString &projString, bool identify = true );
532#else
533 bool createFromProj( const QString &projString );
534#endif
535
555 bool createFromString( const QString &definition );
556
557 // TODO QGIS 5: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
558
577 bool createFromUserInput( const QString &definition );
578
589 Q_DECL_DEPRECATED static void setupESRIWktFix() SIP_DEPRECATED;
590
592 bool isValid() const;
593
606 void validate();
607
608 // TODO QGIS 5: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
609
618 Q_DECL_DEPRECATED long findMatchingProj() SIP_DEPRECATED;
619
620 bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
621 bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
622
629 bool readXml( const QDomNode &node );
630
637 bool writeXml( QDomNode &node, QDomDocument &doc ) const;
638
639
644 static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
645
650 static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
651
652 // Accessors -----------------------------------
653
658 long srsid() const;
659
660 // TODO QGIS 5: remove unless really necessary - let's use EPSG codes instead
661
666 long postgisSrid() const;
667
680 QString authid() const;
681
689 QString description() const;
690
704 QString userFriendlyIdentifier( Qgis::CrsIdentifierType type = Qgis::CrsIdentifierType::MediumString ) const;
705
712 QString projectionAcronym() const;
713
720 QString ellipsoidAcronym() const;
721
733 QString toWkt( Qgis::CrsWktVariant variant = Qgis::CrsWktVariant::Wkt1Gdal, bool multiline = false, int indentationWidth = 4 ) const;
734
747 Q_DECL_DEPRECATED QString toProj4() const SIP_DEPRECATED;
748
761 QString toProj() const;
762
774 std::string toJsonString( bool multiline = false, int indentationWidth = 4, const QString &schema = QString() ) const;
775
781 Qgis::CrsType type() const;
782
788 bool isDeprecated() const;
789
794 bool isGeographic() const;
795
804 bool isDynamic() const;
805
816 QgsDatumEnsemble datumEnsemble() const;
817
823 QString celestialBodyName() const;
824
848 void setCoordinateEpoch( double epoch );
849
873 double coordinateEpoch() const;
874
886 QgsProjectionFactors factors( const QgsPoint &point ) const;
887
894 QgsProjOperation operation() const;
895
903 bool hasAxisInverted() const;
904
910#ifndef SIP_RUN
911 QList< Qgis::CrsAxisDirection > axisOrdering() const;
912#else
913 SIP_PYOBJECT axisOrdering() const SIP_TYPEHINT( List[Qgis.CrsAxisDirection] );
914 % MethodCode
915 // adapted from the qpymultimedia_qlist.sip file from the PyQt6 sources
916
917 const QList< Qgis::CrsAxisDirection > cppRes = sipCpp->axisOrdering();
918
919 PyObject *l = PyList_New( cppRes.size() );
920
921 if ( !l )
922 sipIsErr = 1;
923 else
924 {
925 for ( int i = 0; i < cppRes.size(); ++i )
926 {
927 PyObject *eobj = sipConvertFromEnum( static_cast<int>( cppRes.at( i ) ),
928 sipType_Qgis_CrsAxisDirection );
929
930 if ( !eobj )
931 {
932 sipIsErr = 1;
933 }
934
935 PyList_SetItem( l, i, eobj );
936 }
937
938 if ( !sipIsErr )
939 {
940 sipRes = l;
941 }
942 else
943 {
944 Py_DECREF( l );
945 }
946 }
947 % End
948#endif
949
955 Qgis::DistanceUnit mapUnits() const;
956
964 QgsRectangle bounds() const;
965
972 QString toOgcUri() const;
973
980 QString toOgcUrn() const;
981
982 // Mutators -----------------------------------
983
1000 void updateDefinition();
1001
1005 void setValidationHint( const QString &html );
1006
1010 QString validationHint() const;
1011
1018 static int syncDatabase();
1019
1033 long saveAsUserCrs( const QString &name, Qgis::CrsDefinitionFormat nativeFormat = Qgis::CrsDefinitionFormat::Wkt );
1034
1044 void setNativeFormat( Qgis::CrsDefinitionFormat format );
1045
1055 Qgis::CrsDefinitionFormat nativeFormat() const;
1056
1066 QgsCoordinateReferenceSystem toGeographicCrs() const;
1067
1079 QgsCoordinateReferenceSystem toGeocentricCrs() const;
1080
1091 QgsCoordinateReferenceSystem horizontalCrs() const;
1092
1105 QgsCoordinateReferenceSystem verticalCrs() const;
1106
1113 bool hasVerticalAxis() const;
1114
1116 QString geographicCrsAuthId() const;
1117
1118#ifdef SIP_RUN
1119 SIP_PYOBJECT __repr__();
1120 % MethodCode
1121 const QString str = sipCpp->isValid() ? u"<QgsCoordinateReferenceSystem: %1%2>"_s.arg( !sipCpp->authid().isEmpty() ? sipCpp->authid() : sipCpp->toWkt( Qgis::CrsWktVariant::Preferred ),
1122 std::isfinite( sipCpp->coordinateEpoch() ) ? u" @ %1"_s.arg( sipCpp->coordinateEpoch() ) : QString() )
1123 : u"<QgsCoordinateReferenceSystem: invalid>"_s;
1124 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
1125 % End
1126#endif
1127
1128#ifndef SIP_RUN
1129
1139 PJ *projObject() const;
1140
1151 static QgsCoordinateReferenceSystem fromProjObject( PJ *object );
1152
1161 bool createFromProjObject( PJ *object );
1162#endif
1163
1170 Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
1171
1177 Q_DECL_DEPRECATED static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems() SIP_DEPRECATED;
1178
1184 Q_DECL_DEPRECATED static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1185
1191 Q_DECL_DEPRECATED static void removeRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1192
1198 Q_DECL_DEPRECATED static void clearRecentCoordinateReferenceSystems() SIP_DEPRECATED;
1199
1200#ifndef SIP_RUN
1201
1211 static void invalidateCache( bool disableCache = false );
1212#else
1213
1220 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
1221#endif
1222
1223 // Mutators -----------------------------------
1224 // We don't want to expose these to the public api since they won't create
1225 // a fully valid crs. Programmers should use the createFrom* methods rather
1226 private:
1227
1233 static QString projFromSrsId( int srsId );
1234
1240 void setProjString( const QString &projString );
1241
1245 bool setWktString( const QString &wkt );
1246
1250 void debugPrint();
1251
1253 typedef QMap<QString, QString> RecordMap;
1254
1262 RecordMap getRecord( const QString &sql );
1263
1268 static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
1269
1271 void setMapUnits();
1272
1274 static long getRecordCount();
1275
1281 bool loadFromAuthCode( const QString &auth, const QString &code );
1282
1286 static QList< long > userSrsIds();
1287
1295 long matchToUserCrs() const;
1296
1301 bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
1302
1303 bool createFromWktInternal( const QString &wkt, const QString &description );
1304
1305 QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
1306
1307 QString mValidationHint;
1308
1311 friend class QgsProjContext;
1312
1313 // Only meant to be called by QgsProjContext::~QgsProjContext()
1314 static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
1315
1317 static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
1318
1319 // cache
1320
1321 static bool sDisableSrIdCache;
1322 static bool sDisableOgcCache;
1323 static bool sDisableProjCache;
1324 static bool sDisableWktCache;
1325 static bool sDisableSrsIdCache;
1326 static bool sDisableStringCache;
1327
1328 // for tests
1329 static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
1330 static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
1331 static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
1332 static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
1333 static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
1334 static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
1338 friend bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1339 friend bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1340 friend bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1341 friend bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1342
1343 bool createFromPostgisSrid( const long id );
1344};
1345
1347
1348
1349#ifndef SIP_RUN
1350inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
1351{
1352 QString mySummary( u"\n\tSpatial Reference System:"_s );
1353 mySummary += "\n\t\tDescription : "_L1;
1354 if ( !r.description().isNull() )
1355 {
1356 mySummary += r.description();
1357 }
1358 else
1359 {
1360 mySummary += "Undefined"_L1;
1361 }
1362 mySummary += "\n\t\tProjection : "_L1;
1363 if ( !r.projectionAcronym().isNull() )
1364 {
1365 mySummary += r.projectionAcronym();
1366 }
1367 else
1368 {
1369 mySummary += "Undefined"_L1;
1370 }
1371
1372 mySummary += "\n\t\tEllipsoid : "_L1;
1373 if ( !r.ellipsoidAcronym().isNull() )
1374 {
1375 mySummary += r.ellipsoidAcronym();
1376 }
1377 else
1378 {
1379 mySummary += "Undefined"_L1;
1380 }
1381
1382 mySummary += "\n\t\tProjString : "_L1;
1383 if ( !r.toProj().isNull() )
1384 {
1385 mySummary += r.toProj();
1386 }
1387 else
1388 {
1389 mySummary += "Undefined"_L1;
1390 }
1391 // Using streams we need to use local 8 Bit
1392 return os << mySummary.toLocal8Bit().data() << std::endl;
1393}
1394
1395bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1396bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1397bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1398bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1399#endif
1400
1401#endif // QGSCOORDINATEREFERENCESYSTEM_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:59
DistanceUnit
Units of distance.
Definition qgis.h:5085
CrsDefinitionFormat
CRS definition formats.
Definition qgis.h:3950
@ Wkt
WKT format (always recommended over proj string format).
Definition qgis.h:3951
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
Definition qgis.h:2497
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:97
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
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:7314
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:7330
#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)