QGIS API Documentation 3.38.0-Grenoble (exported)
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
252 explicit QgsCoordinateReferenceSystem( const QString &definition );
253
254 // TODO QGIS 4: remove type and always use EPSG code
255
267 Q_DECL_DEPRECATED explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
268
271
274
276 operator QVariant() const
277 {
278 return QVariant::fromValue( *this );
279 }
280
287 static QList< long > validSrsIds();
288
289 // static creators
290
297 static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
298
304 Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
305
312 Q_DECL_DEPRECATED static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 ) SIP_DEPRECATED;
313
321 static QgsCoordinateReferenceSystem fromProj( const QString &proj );
322
329 static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
330
338 static QgsCoordinateReferenceSystem fromSrsId( long srsId );
339
355 static QgsCoordinateReferenceSystem createCompoundCrs( const QgsCoordinateReferenceSystem &horizontalCrs, const QgsCoordinateReferenceSystem &verticalCrs, QString &error SIP_OUT );
356
357 // Misc helper functions -----------------------
358
359 // TODO QGIS 4: remove type and always use EPSG code, rename to createFromEpsg
360
366 Q_DECL_DEPRECATED bool createFromId( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
367
368 // TODO QGIS 4: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
369
380 bool createFromOgcWmsCrs( const QString &crs );
381
382 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
383
391 Q_DECL_DEPRECATED bool createFromSrid( long srid ) SIP_DEPRECATED;
392
405 bool createFromWkt( const QString &wkt );
406
419 bool createFromSrsId( long srsId );
420
444 Q_DECL_DEPRECATED bool createFromProj4( const QString &projString ) SIP_DEPRECATED;
445
473#ifndef SIP_RUN
474 bool createFromProj( const QString &projString, bool identify = true );
475#else
476 bool createFromProj( const QString &projString );
477#endif
478
494 bool createFromString( const QString &definition );
495
496 // TODO QGIS 4: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
497
513 bool createFromUserInput( const QString &definition );
514
525 Q_DECL_DEPRECATED static void setupESRIWktFix() SIP_DEPRECATED;
526
528 bool isValid() const;
529
541 void validate();
542
543 // TODO QGIS 4: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
544
553 Q_DECL_DEPRECATED long findMatchingProj() SIP_DEPRECATED;
554
560 bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
561
567 bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
568
575 bool readXml( const QDomNode &node );
576
583 bool writeXml( QDomNode &node, QDomDocument &doc ) const;
584
585
590 static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
591
596 static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
597
598 // Accessors -----------------------------------
599
604 long srsid() const;
605
606 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
607
612 long postgisSrid() const;
613
626 QString authid() const;
627
635 QString description() const;
636
650 QString userFriendlyIdentifier( Qgis::CrsIdentifierType type = Qgis::CrsIdentifierType::MediumString ) const;
651
658 QString projectionAcronym() const;
659
666 QString ellipsoidAcronym() const;
667
679 QString toWkt( Qgis::CrsWktVariant variant = Qgis::CrsWktVariant::Wkt1Gdal, bool multiline = false, int indentationWidth = 4 ) const;
680
693 Q_DECL_DEPRECATED QString toProj4() const SIP_DEPRECATED;
694
707 QString toProj() const;
708
714 Qgis::CrsType type() const;
715
721 bool isDeprecated() const;
722
727 bool isGeographic() const;
728
737 bool isDynamic() const;
738
753 QgsDatumEnsemble datumEnsemble() const SIP_THROW( QgsNotSupportedException );
754
764 QString celestialBodyName() const SIP_THROW( QgsNotSupportedException );
765
789 void setCoordinateEpoch( double epoch );
790
814 double coordinateEpoch() const;
815
827 QgsProjectionFactors factors( const QgsPoint &point ) const;
828
835 QgsProjOperation operation() const;
836
844 bool hasAxisInverted() const;
845
851#ifndef SIP_RUN
852 QList< Qgis::CrsAxisDirection > axisOrdering() const;
853#else
854 SIP_PYOBJECT axisOrdering() const SIP_TYPEHINT( List[Qgis.CrsAxisDirection] );
855 % MethodCode
856 // adapted from the qpymultimedia_qlist.sip file from the PyQt6 sources
857
858 const QList< Qgis::CrsAxisDirection > cppRes = sipCpp->axisOrdering();
859
860 PyObject *l = PyList_New( cppRes.size() );
861
862 if ( !l )
863 sipIsErr = 1;
864 else
865 {
866 for ( int i = 0; i < cppRes.size(); ++i )
867 {
868 PyObject *eobj = sipConvertFromEnum( static_cast<int>( cppRes.at( i ) ),
869 sipType_Qgis_CrsAxisDirection );
870
871 if ( !eobj )
872 {
873 sipIsErr = 1;
874 }
875
876 PyList_SetItem( l, i, eobj );
877 }
878
879 if ( !sipIsErr )
880 {
881 sipRes = l;
882 }
883 else
884 {
885 Py_DECREF( l );
886 }
887 }
888 % End
889#endif
890
896 Qgis::DistanceUnit mapUnits() const;
897
905 QgsRectangle bounds() const;
906
913 QString toOgcUri() const;
914
921 QString toOgcUrn() const;
922
923 // Mutators -----------------------------------
924
941 void updateDefinition();
942
946 void setValidationHint( const QString &html );
947
951 QString validationHint() const;
952
959 static int syncDatabase();
960
974 long saveAsUserCrs( const QString &name, Qgis::CrsDefinitionFormat nativeFormat = Qgis::CrsDefinitionFormat::Wkt );
975
985 void setNativeFormat( Qgis::CrsDefinitionFormat format );
986
996 Qgis::CrsDefinitionFormat nativeFormat() const;
997
1007 QgsCoordinateReferenceSystem toGeographicCrs() const;
1008
1019 QgsCoordinateReferenceSystem horizontalCrs() const;
1020
1033 QgsCoordinateReferenceSystem verticalCrs() const;
1034
1041 bool hasVerticalAxis() const;
1042
1044 QString geographicCrsAuthId() const;
1045
1046#ifdef SIP_RUN
1047 SIP_PYOBJECT __repr__();
1048 % MethodCode
1049 const QString str = sipCpp->isValid() ? QStringLiteral( "<QgsCoordinateReferenceSystem: %1%2>" ).arg( !sipCpp->authid().isEmpty() ? sipCpp->authid() : sipCpp->toWkt( Qgis::CrsWktVariant::Preferred ),
1050 std::isfinite( sipCpp->coordinateEpoch() ) ? QStringLiteral( " @ %1" ).arg( sipCpp->coordinateEpoch() ) : QString() )
1051 : QStringLiteral( "<QgsCoordinateReferenceSystem: invalid>" );
1052 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
1053 % End
1054#endif
1055
1056#ifndef SIP_RUN
1057
1067 PJ *projObject() const;
1068
1079 static QgsCoordinateReferenceSystem fromProjObject( PJ *object );
1080
1089 bool createFromProjObject( PJ *object );
1090#endif
1091
1098 Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
1099
1105 Q_DECL_DEPRECATED static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems() SIP_DEPRECATED;
1106
1112 Q_DECL_DEPRECATED static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1113
1119 Q_DECL_DEPRECATED static void removeRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1120
1126 Q_DECL_DEPRECATED static void clearRecentCoordinateReferenceSystems() SIP_DEPRECATED;
1127
1128#ifndef SIP_RUN
1129
1139 static void invalidateCache( bool disableCache = false );
1140#else
1141
1148 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
1149#endif
1150
1151 // Mutators -----------------------------------
1152 // We don't want to expose these to the public api since they won't create
1153 // a fully valid crs. Programmers should use the createFrom* methods rather
1154 private:
1155
1161 static QString projFromSrsId( int srsId );
1162
1168 void setProjString( const QString &projString );
1169
1173 bool setWktString( const QString &wkt );
1174
1178 void debugPrint();
1179
1181 typedef QMap<QString, QString> RecordMap;
1182
1190 RecordMap getRecord( const QString &sql );
1191
1196 static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
1197
1199 void setMapUnits();
1200
1202 static long getRecordCount();
1203
1204 bool loadFromAuthCode( const QString &auth, const QString &code );
1205
1209 static QList< long > userSrsIds();
1210
1218 long matchToUserCrs() const;
1219
1224 bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
1225
1226 bool createFromWktInternal( const QString &wkt, const QString &description );
1227
1228 QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
1229
1230 QString mValidationHint;
1231
1233
1234 friend class QgsProjContext;
1235
1236 // Only meant to be called by QgsProjContext::~QgsProjContext()
1237 static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
1238
1240 static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
1241
1242 // cache
1243
1244 static bool sDisableSrIdCache;
1245 static bool sDisableOgcCache;
1246 static bool sDisableProjCache;
1247 static bool sDisableWktCache;
1248 static bool sDisableSrsIdCache;
1249 static bool sDisableStringCache;
1250
1251 // for tests
1252 static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
1253 static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
1254 static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
1255 static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
1256 static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
1257 static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
1258
1259 friend class TestQgsCoordinateReferenceSystem;
1261 friend bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1262 friend bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1263 friend bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1264 friend bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1265
1266 bool createFromPostgisSrid( const long id );
1267};
1268
1270
1271
1272#ifndef SIP_RUN
1273inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
1274{
1275 QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
1276 mySummary += QLatin1String( "\n\t\tDescription : " );
1277 if ( !r.description().isNull() )
1278 {
1279 mySummary += r.description();
1280 }
1281 else
1282 {
1283 mySummary += QLatin1String( "Undefined" );
1284 }
1285 mySummary += QLatin1String( "\n\t\tProjection : " );
1286 if ( !r.projectionAcronym().isNull() )
1287 {
1288 mySummary += r.projectionAcronym();
1289 }
1290 else
1291 {
1292 mySummary += QLatin1String( "Undefined" );
1293 }
1294
1295 mySummary += QLatin1String( "\n\t\tEllipsoid : " );
1296 if ( !r.ellipsoidAcronym().isNull() )
1297 {
1298 mySummary += r.ellipsoidAcronym();
1299 }
1300 else
1301 {
1302 mySummary += QLatin1String( "Undefined" );
1303 }
1304
1305 mySummary += QLatin1String( "\n\t\tProjString : " );
1306 if ( !r.toProj().isNull() )
1307 {
1308 mySummary += r.toProj();
1309 }
1310 else
1311 {
1312 mySummary += QLatin1String( "Undefined" );
1313 }
1314 // Using streams we need to use local 8 Bit
1315 return os << mySummary.toLocal8Bit().data() << std::endl;
1316}
1317
1318bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1319bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1320bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1321bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1322#endif
1323
1324#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:4358
CrsDefinitionFormat
CRS definition formats.
Definition qgis.h:3317
@ 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:38
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:5832
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:5846
#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