QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
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
340 // Misc helper functions -----------------------
341
342 // TODO QGIS 4: remove type and always use EPSG code, rename to createFromEpsg
343
349 Q_DECL_DEPRECATED bool createFromId( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
350
351 // TODO QGIS 4: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
352
363 bool createFromOgcWmsCrs( const QString &crs );
364
365 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
366
374 Q_DECL_DEPRECATED bool createFromSrid( long srid ) SIP_DEPRECATED;
375
388 bool createFromWkt( const QString &wkt );
389
402 bool createFromSrsId( long srsId );
403
427 Q_DECL_DEPRECATED bool createFromProj4( const QString &projString ) SIP_DEPRECATED;
428
456#ifndef SIP_RUN
457 bool createFromProj( const QString &projString, bool identify = true );
458#else
459 bool createFromProj( const QString &projString );
460#endif
461
477 bool createFromString( const QString &definition );
478
479 // TODO QGIS 4: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
480
496 bool createFromUserInput( const QString &definition );
497
508 Q_DECL_DEPRECATED static void setupESRIWktFix() SIP_DEPRECATED;
509
511 bool isValid() const;
512
524 void validate();
525
526 // TODO QGIS 4: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
527
536 Q_DECL_DEPRECATED long findMatchingProj() SIP_DEPRECATED;
537
543 bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
544
550 bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
551
558 bool readXml( const QDomNode &node );
559
566 bool writeXml( QDomNode &node, QDomDocument &doc ) const;
567
568
573 static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
574
579 static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
580
581 // Accessors -----------------------------------
582
587 long srsid() const;
588
589 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
590
595 long postgisSrid() const;
596
609 QString authid() const;
610
618 QString description() const;
619
633 QString userFriendlyIdentifier( Qgis::CrsIdentifierType type = Qgis::CrsIdentifierType::MediumString ) const;
634
641 QString projectionAcronym() const;
642
649 QString ellipsoidAcronym() const;
650
662 QString toWkt( Qgis::CrsWktVariant variant = Qgis::CrsWktVariant::Wkt1Gdal, bool multiline = false, int indentationWidth = 4 ) const;
663
676 Q_DECL_DEPRECATED QString toProj4() const SIP_DEPRECATED;
677
690 QString toProj() const;
691
697 Qgis::CrsType type() const;
698
704 bool isDeprecated() const;
705
710 bool isGeographic() const;
711
720 bool isDynamic() const;
721
736 QgsDatumEnsemble datumEnsemble() const SIP_THROW( QgsNotSupportedException );
737
747 QString celestialBodyName() const SIP_THROW( QgsNotSupportedException );
748
772 void setCoordinateEpoch( double epoch );
773
797 double coordinateEpoch() const;
798
810 QgsProjectionFactors factors( const QgsPoint &point ) const;
811
818 QgsProjOperation operation() const;
819
827 bool hasAxisInverted() const;
828
834#ifndef SIP_RUN
835 QList< Qgis::CrsAxisDirection > axisOrdering() const;
836#else
837 SIP_PYOBJECT axisOrdering() const SIP_TYPEHINT( List[Qgis.CrsAxisDirection] );
838 % MethodCode
839 // adapted from the qpymultimedia_qlist.sip file from the PyQt6 sources
840
841 const QList< Qgis::CrsAxisDirection > cppRes = sipCpp->axisOrdering();
842
843 PyObject *l = PyList_New( cppRes.size() );
844
845 if ( !l )
846 sipIsErr = 1;
847 else
848 {
849 for ( int i = 0; i < cppRes.size(); ++i )
850 {
851 PyObject *eobj = sipConvertFromEnum( static_cast<int>( cppRes.at( i ) ),
852 sipType_Qgis_CrsAxisDirection );
853
854 if ( !eobj )
855 {
856 sipIsErr = 1;
857 }
858
859 PyList_SetItem( l, i, eobj );
860 }
861
862 if ( !sipIsErr )
863 {
864 sipRes = l;
865 }
866 else
867 {
868 Py_DECREF( l );
869 }
870 }
871 % End
872#endif
873
879 Qgis::DistanceUnit mapUnits() const;
880
888 QgsRectangle bounds() const;
889
896 QString toOgcUri() const;
897
898 // Mutators -----------------------------------
899
916 void updateDefinition();
917
921 void setValidationHint( const QString &html );
922
926 QString validationHint() const;
927
934 static int syncDatabase();
935
949 long saveAsUserCrs( const QString &name, Qgis::CrsDefinitionFormat nativeFormat = Qgis::CrsDefinitionFormat::Wkt );
950
960 void setNativeFormat( Qgis::CrsDefinitionFormat format );
961
971 Qgis::CrsDefinitionFormat nativeFormat() const;
972
982 QgsCoordinateReferenceSystem toGeographicCrs() const;
983
985 QString geographicCrsAuthId() const;
986
987#ifdef SIP_RUN
988 SIP_PYOBJECT __repr__();
989 % MethodCode
990 const QString str = sipCpp->isValid() ? QStringLiteral( "<QgsCoordinateReferenceSystem: %1%2>" ).arg( !sipCpp->authid().isEmpty() ? sipCpp->authid() : sipCpp->toWkt( Qgis::CrsWktVariant::Preferred ),
991 std::isfinite( sipCpp->coordinateEpoch() ) ? QStringLiteral( " @ %1" ).arg( sipCpp->coordinateEpoch() ) : QString() )
992 : QStringLiteral( "<QgsCoordinateReferenceSystem: invalid>" );
993 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
994 % End
995#endif
996
997#ifndef SIP_RUN
998
1008 PJ *projObject() const;
1009
1020 static QgsCoordinateReferenceSystem fromProjObject( PJ *object );
1021
1030 bool createFromProjObject( PJ *object );
1031#endif
1032
1039 Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
1040
1046 Q_DECL_DEPRECATED static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems() SIP_DEPRECATED;
1047
1053 Q_DECL_DEPRECATED static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1054
1060 Q_DECL_DEPRECATED static void removeRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1061
1067 Q_DECL_DEPRECATED static void clearRecentCoordinateReferenceSystems() SIP_DEPRECATED;
1068
1069#ifndef SIP_RUN
1070
1080 static void invalidateCache( bool disableCache = false );
1081#else
1082
1089 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
1090#endif
1091
1092 // Mutators -----------------------------------
1093 // We don't want to expose these to the public api since they won't create
1094 // a fully valid crs. Programmers should use the createFrom* methods rather
1095 private:
1096
1102 static QString projFromSrsId( int srsId );
1103
1109 void setProjString( const QString &projString );
1110
1114 bool setWktString( const QString &wkt );
1115
1119 void debugPrint();
1120
1122 typedef QMap<QString, QString> RecordMap;
1123
1131 RecordMap getRecord( const QString &sql );
1132
1137 static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
1138
1140 void setMapUnits();
1141
1143 static long getRecordCount();
1144
1145 bool loadFromAuthCode( const QString &auth, const QString &code );
1146
1150 static QList< long > userSrsIds();
1151
1159 long matchToUserCrs() const;
1160
1165 bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
1166
1167 bool createFromWktInternal( const QString &wkt, const QString &description );
1168
1169 QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
1170
1171 QString mValidationHint;
1172
1174
1175 friend class QgsProjContext;
1176
1177 // Only meant to be called by QgsProjContext::~QgsProjContext()
1178 static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
1179
1181 static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
1182
1183 // cache
1184
1185 static bool sDisableSrIdCache;
1186 static bool sDisableOgcCache;
1187 static bool sDisableProjCache;
1188 static bool sDisableWktCache;
1189 static bool sDisableSrsIdCache;
1190 static bool sDisableStringCache;
1191
1192 // for tests
1193 static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
1194 static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
1195 static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
1196 static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
1197 static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
1198 static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
1199
1200 friend class TestQgsCoordinateReferenceSystem;
1202 friend bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1203 friend bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1204 friend bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1205 friend bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1206
1207 bool createFromPostgisSrid( const long id );
1208};
1209
1211
1212
1213#ifndef SIP_RUN
1214inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
1215{
1216 QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
1217 mySummary += QLatin1String( "\n\t\tDescription : " );
1218 if ( !r.description().isNull() )
1219 {
1220 mySummary += r.description();
1221 }
1222 else
1223 {
1224 mySummary += QLatin1String( "Undefined" );
1225 }
1226 mySummary += QLatin1String( "\n\t\tProjection : " );
1227 if ( !r.projectionAcronym().isNull() )
1228 {
1229 mySummary += r.projectionAcronym();
1230 }
1231 else
1232 {
1233 mySummary += QLatin1String( "Undefined" );
1234 }
1235
1236 mySummary += QLatin1String( "\n\t\tEllipsoid : " );
1237 if ( !r.ellipsoidAcronym().isNull() )
1238 {
1239 mySummary += r.ellipsoidAcronym();
1240 }
1241 else
1242 {
1243 mySummary += QLatin1String( "Undefined" );
1244 }
1245
1246 mySummary += QLatin1String( "\n\t\tProjString : " );
1247 if ( !r.toProj().isNull() )
1248 {
1249 mySummary += r.toProj();
1250 }
1251 else
1252 {
1253 mySummary += QLatin1String( "Undefined" );
1254 }
1255 // Using streams we need to use local 8 Bit
1256 return os << mySummary.toLocal8Bit().data() << std::endl;
1257}
1258
1259bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1260bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1261bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1262bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1263#endif
1264
1265#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:4062
CrsDefinitionFormat
CRS definition formats.
Definition qgis.h:3145
@ 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:5523
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:5537
#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_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