QGIS API Documentation 3.41.0-Master (3291e9ff48b)
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
51#if PROJ_VERSION_MAJOR>=8
52struct pj_ctx;
53typedef struct pj_ctx PJ_CONTEXT;
54#else
55struct projCtx_t;
56typedef struct projCtx_t PJ_CONTEXT;
57#endif
58#endif
59
60// forward declaration for sqlite3
61typedef struct sqlite3 sqlite3 SIP_SKIP;
62
63#ifdef DEBUG
64typedef struct OGRSpatialReferenceHS *OGRSpatialReferenceH SIP_SKIP;
65#else
67#endif
68
71
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
378 // Misc helper functions -----------------------
379
380 // TODO QGIS 4: remove type and always use EPSG code, rename to createFromEpsg
381
387 Q_DECL_DEPRECATED bool createFromId( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
388
389 // TODO QGIS 4: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
390
404 bool createFromOgcWmsCrs( const QString &crs );
405
406 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
407
415 Q_DECL_DEPRECATED bool createFromSrid( long srid ) SIP_DEPRECATED;
416
432 bool createFromWkt( const QString &wkt );
433
449 bool createFromSrsId( long srsId );
450
477 Q_DECL_DEPRECATED bool createFromProj4( const QString &projString ) SIP_DEPRECATED;
478
508#ifndef SIP_RUN
509 bool createFromProj( const QString &projString, bool identify = true );
510#else
511 bool createFromProj( const QString &projString );
512#endif
513
533 bool createFromString( const QString &definition );
534
535 // TODO QGIS 4: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
536
555 bool createFromUserInput( const QString &definition );
556
567 Q_DECL_DEPRECATED static void setupESRIWktFix() SIP_DEPRECATED;
568
570 bool isValid() const;
571
584 void validate();
585
586 // TODO QGIS 4: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
587
596 Q_DECL_DEPRECATED long findMatchingProj() SIP_DEPRECATED;
597
598 bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
599 bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
600
607 bool readXml( const QDomNode &node );
608
615 bool writeXml( QDomNode &node, QDomDocument &doc ) const;
616
617
622 static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
623
628 static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
629
630 // Accessors -----------------------------------
631
636 long srsid() const;
637
638 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
639
644 long postgisSrid() const;
645
658 QString authid() const;
659
667 QString description() const;
668
682 QString userFriendlyIdentifier( Qgis::CrsIdentifierType type = Qgis::CrsIdentifierType::MediumString ) const;
683
690 QString projectionAcronym() const;
691
698 QString ellipsoidAcronym() const;
699
711 QString toWkt( Qgis::CrsWktVariant variant = Qgis::CrsWktVariant::Wkt1Gdal, bool multiline = false, int indentationWidth = 4 ) const;
712
725 Q_DECL_DEPRECATED QString toProj4() const SIP_DEPRECATED;
726
739 QString toProj() const;
740
746 Qgis::CrsType type() const;
747
753 bool isDeprecated() const;
754
759 bool isGeographic() const;
760
769 bool isDynamic() const;
770
785 QgsDatumEnsemble datumEnsemble() const SIP_THROW( QgsNotSupportedException );
786
796 QString celestialBodyName() const SIP_THROW( QgsNotSupportedException );
797
821 void setCoordinateEpoch( double epoch );
822
846 double coordinateEpoch() const;
847
859 QgsProjectionFactors factors( const QgsPoint &point ) const;
860
867 QgsProjOperation operation() const;
868
876 bool hasAxisInverted() const;
877
883#ifndef SIP_RUN
884 QList< Qgis::CrsAxisDirection > axisOrdering() const;
885#else
886 SIP_PYOBJECT axisOrdering() const SIP_TYPEHINT( List[Qgis.CrsAxisDirection] );
887 % MethodCode
888 // adapted from the qpymultimedia_qlist.sip file from the PyQt6 sources
889
890 const QList< Qgis::CrsAxisDirection > cppRes = sipCpp->axisOrdering();
891
892 PyObject *l = PyList_New( cppRes.size() );
893
894 if ( !l )
895 sipIsErr = 1;
896 else
897 {
898 for ( int i = 0; i < cppRes.size(); ++i )
899 {
900 PyObject *eobj = sipConvertFromEnum( static_cast<int>( cppRes.at( i ) ),
901 sipType_Qgis_CrsAxisDirection );
902
903 if ( !eobj )
904 {
905 sipIsErr = 1;
906 }
907
908 PyList_SetItem( l, i, eobj );
909 }
910
911 if ( !sipIsErr )
912 {
913 sipRes = l;
914 }
915 else
916 {
917 Py_DECREF( l );
918 }
919 }
920 % End
921#endif
922
928 Qgis::DistanceUnit mapUnits() const;
929
937 QgsRectangle bounds() const;
938
945 QString toOgcUri() const;
946
953 QString toOgcUrn() const;
954
955 // Mutators -----------------------------------
956
973 void updateDefinition();
974
978 void setValidationHint( const QString &html );
979
983 QString validationHint() const;
984
991 static int syncDatabase();
992
1006 long saveAsUserCrs( const QString &name, Qgis::CrsDefinitionFormat nativeFormat = Qgis::CrsDefinitionFormat::Wkt );
1007
1017 void setNativeFormat( Qgis::CrsDefinitionFormat format );
1018
1028 Qgis::CrsDefinitionFormat nativeFormat() const;
1029
1039 QgsCoordinateReferenceSystem toGeographicCrs() const;
1040
1051 QgsCoordinateReferenceSystem horizontalCrs() const;
1052
1065 QgsCoordinateReferenceSystem verticalCrs() const;
1066
1073 bool hasVerticalAxis() const;
1074
1076 QString geographicCrsAuthId() const;
1077
1078#ifdef SIP_RUN
1079 SIP_PYOBJECT __repr__();
1080 % MethodCode
1081 const QString str = sipCpp->isValid() ? QStringLiteral( "<QgsCoordinateReferenceSystem: %1%2>" ).arg( !sipCpp->authid().isEmpty() ? sipCpp->authid() : sipCpp->toWkt( Qgis::CrsWktVariant::Preferred ),
1082 std::isfinite( sipCpp->coordinateEpoch() ) ? QStringLiteral( " @ %1" ).arg( sipCpp->coordinateEpoch() ) : QString() )
1083 : QStringLiteral( "<QgsCoordinateReferenceSystem: invalid>" );
1084 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
1085 % End
1086#endif
1087
1088#ifndef SIP_RUN
1089
1099 PJ *projObject() const;
1100
1111 static QgsCoordinateReferenceSystem fromProjObject( PJ *object );
1112
1121 bool createFromProjObject( PJ *object );
1122#endif
1123
1130 Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
1131
1137 Q_DECL_DEPRECATED static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems() SIP_DEPRECATED;
1138
1144 Q_DECL_DEPRECATED static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1145
1151 Q_DECL_DEPRECATED static void removeRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1152
1158 Q_DECL_DEPRECATED static void clearRecentCoordinateReferenceSystems() SIP_DEPRECATED;
1159
1160#ifndef SIP_RUN
1161
1171 static void invalidateCache( bool disableCache = false );
1172#else
1173
1180 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
1181#endif
1182
1183 // Mutators -----------------------------------
1184 // We don't want to expose these to the public api since they won't create
1185 // a fully valid crs. Programmers should use the createFrom* methods rather
1186 private:
1187
1193 static QString projFromSrsId( int srsId );
1194
1200 void setProjString( const QString &projString );
1201
1205 bool setWktString( const QString &wkt );
1206
1210 void debugPrint();
1211
1213 typedef QMap<QString, QString> RecordMap;
1214
1222 RecordMap getRecord( const QString &sql );
1223
1228 static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
1229
1231 void setMapUnits();
1232
1234 static long getRecordCount();
1235
1241 bool loadFromAuthCode( const QString &auth, const QString &code );
1242
1246 static QList< long > userSrsIds();
1247
1255 long matchToUserCrs() const;
1256
1261 bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
1262
1263 bool createFromWktInternal( const QString &wkt, const QString &description );
1264
1265 QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
1266
1267 QString mValidationHint;
1268
1270
1271 friend class QgsProjContext;
1272
1273 // Only meant to be called by QgsProjContext::~QgsProjContext()
1274 static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
1275
1277 static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
1278
1279 // cache
1280
1281 static bool sDisableSrIdCache;
1282 static bool sDisableOgcCache;
1283 static bool sDisableProjCache;
1284 static bool sDisableWktCache;
1285 static bool sDisableSrsIdCache;
1286 static bool sDisableStringCache;
1287
1288 // for tests
1289 static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
1290 static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
1291 static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
1292 static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
1293 static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
1294 static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
1295
1296 friend class TestQgsCoordinateReferenceSystem;
1298 friend bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1299 friend bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1300 friend bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1301 friend bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1302
1303 bool createFromPostgisSrid( const long id );
1304};
1305
1307
1308
1309#ifndef SIP_RUN
1310inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
1311{
1312 QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
1313 mySummary += QLatin1String( "\n\t\tDescription : " );
1314 if ( !r.description().isNull() )
1315 {
1316 mySummary += r.description();
1317 }
1318 else
1319 {
1320 mySummary += QLatin1String( "Undefined" );
1321 }
1322 mySummary += QLatin1String( "\n\t\tProjection : " );
1323 if ( !r.projectionAcronym().isNull() )
1324 {
1325 mySummary += r.projectionAcronym();
1326 }
1327 else
1328 {
1329 mySummary += QLatin1String( "Undefined" );
1330 }
1331
1332 mySummary += QLatin1String( "\n\t\tEllipsoid : " );
1333 if ( !r.ellipsoidAcronym().isNull() )
1334 {
1335 mySummary += r.ellipsoidAcronym();
1336 }
1337 else
1338 {
1339 mySummary += QLatin1String( "Undefined" );
1340 }
1341
1342 mySummary += QLatin1String( "\n\t\tProjString : " );
1343 if ( !r.toProj().isNull() )
1344 {
1345 mySummary += r.toProj();
1346 }
1347 else
1348 {
1349 mySummary += QLatin1String( "Undefined" );
1350 }
1351 // Using streams we need to use local 8 Bit
1352 return os << mySummary.toLocal8Bit().data() << std::endl;
1353}
1354
1355bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1356bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1357bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1358bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1359#endif
1360
1361#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:4677
CrsDefinitionFormat
CRS definition formats.
Definition qgis.h:3613
@ 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
Custom exception class which is raised when an operation is not supported.
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...
#define str(x)
Definition qgis.cpp:39
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:6345
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:6359
#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
#define SIP_THROW(name,...)
Definition qgis_sip.h:203
bool operator>=(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
bool operator<=(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
struct sqlite3 sqlite3
void * OGRSpatialReferenceH
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
struct projCtx_t PJ_CONTEXT
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