QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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
288 static QList< long > validSrsIds();
289
290 // static creators
291
299 static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
300
307 Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
308
315 Q_DECL_DEPRECATED static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 ) SIP_DEPRECATED;
316
324 static QgsCoordinateReferenceSystem fromProj( const QString &proj );
325
333 static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
334
343 static QgsCoordinateReferenceSystem fromSrsId( long srsId );
344
345 // Misc helper functions -----------------------
346
347 // TODO QGIS 4: remove type and always use EPSG code, rename to createFromEpsg
348
354 Q_DECL_DEPRECATED bool createFromId( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
355
356 // TODO QGIS 4: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
357
368 bool createFromOgcWmsCrs( const QString &crs );
369
370 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
371
379 Q_DECL_DEPRECATED bool createFromSrid( long srid ) SIP_DEPRECATED;
380
393 bool createFromWkt( const QString &wkt );
394
407 bool createFromSrsId( long srsId );
408
432 Q_DECL_DEPRECATED bool createFromProj4( const QString &projString ) SIP_DEPRECATED;
433
461#ifndef SIP_RUN
462 bool createFromProj( const QString &projString, bool identify = true );
463#else
464 bool createFromProj( const QString &projString );
465#endif
466
482 bool createFromString( const QString &definition );
483
484 // TODO QGIS 4: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
485
501 bool createFromUserInput( const QString &definition );
502
513 Q_DECL_DEPRECATED static void setupESRIWktFix() SIP_DEPRECATED;
514
516 bool isValid() const;
517
529 void validate();
530
531 // TODO QGIS 4: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
532
541 Q_DECL_DEPRECATED long findMatchingProj() SIP_DEPRECATED;
542
548 bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
549
555 bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
556
563 bool readXml( const QDomNode &node );
564
571 bool writeXml( QDomNode &node, QDomDocument &doc ) const;
572
573
578 static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
579
584 static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
585
586 // Accessors -----------------------------------
587
592 long srsid() const;
593
594 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
595
600 long postgisSrid() const;
601
614 QString authid() const;
615
623 QString description() const;
624
636
650 QString userFriendlyIdentifier( IdentifierType type = MediumString ) const;
651
658 QString projectionAcronym() const;
659
666 QString ellipsoidAcronym() const;
667
670 {
677 WKT2_2019 = WKT2_2018,
678 WKT2_2019_SIMPLIFIED = WKT2_2018_SIMPLIFIED,
679
680 WKT_PREFERRED = WKT2_2019,
681 WKT_PREFERRED_SIMPLIFIED = WKT2_2019_SIMPLIFIED,
682 WKT_PREFERRED_GDAL = WKT2_2019,
683 };
684
696 QString toWkt( WktVariant variant = WKT1_GDAL, bool multiline = false, int indentationWidth = 4 ) const;
697
710 Q_DECL_DEPRECATED QString toProj4() const SIP_DEPRECATED;
711
724 QString toProj() const;
725
731 Qgis::CrsType type() const;
732
737 bool isGeographic() const;
738
747 bool isDynamic() const;
748
763 QgsDatumEnsemble datumEnsemble() const SIP_THROW( QgsNotSupportedException );
764
774 QString celestialBodyName() const SIP_THROW( QgsNotSupportedException );
775
799 void setCoordinateEpoch( double epoch );
800
824 double coordinateEpoch() const;
825
837 QgsProjectionFactors factors( const QgsPoint &point ) const;
838
845 QgsProjOperation operation() const;
846
854 bool hasAxisInverted() const;
855
861#ifndef SIP_RUN
862 QList< Qgis::CrsAxisDirection > axisOrdering() const;
863#else
864 SIP_PYOBJECT axisOrdering() const SIP_TYPEHINT( List[Qgis.CrsAxisDirection] );
865 % MethodCode
866 // adapted from the qpymultimedia_qlist.sip file from the PyQt6 sources
867
868 const QList< Qgis::CrsAxisDirection > cppRes = sipCpp->axisOrdering();
869
870 PyObject *l = PyList_New( cppRes.size() );
871
872 if ( !l )
873 sipIsErr = 1;
874 else
875 {
876 for ( int i = 0; i < cppRes.size(); ++i )
877 {
878 PyObject *eobj = sipConvertFromEnum( static_cast<int>( cppRes.at( i ) ),
879 sipType_Qgis_CrsAxisDirection );
880
881 if ( !eobj )
882 {
883 sipIsErr = 1;
884 }
885
886 PyList_SetItem( l, i, eobj );
887 }
888
889 if ( !sipIsErr )
890 {
891 sipRes = l;
892 }
893 else
894 {
895 Py_DECREF( l );
896 }
897 }
898 % End
899#endif
900
906 Qgis::DistanceUnit mapUnits() const;
907
916 QgsRectangle bounds() const;
917
924 QString toOgcUri() const;
925
926 // Mutators -----------------------------------
927
944 void updateDefinition();
945
949 void setValidationHint( const QString &html );
950
954 QString validationHint() const;
955
962 static int syncDatabase();
963
977 long saveAsUserCrs( const QString &name, Qgis::CrsDefinitionFormat nativeFormat = Qgis::CrsDefinitionFormat::Wkt );
978
988 void setNativeFormat( Qgis::CrsDefinitionFormat format );
989
999 Qgis::CrsDefinitionFormat nativeFormat() const;
1000
1010 QgsCoordinateReferenceSystem toGeographicCrs() const;
1011
1013 QString geographicCrsAuthId() const;
1014
1015#ifdef SIP_RUN
1016 SIP_PYOBJECT __repr__();
1017 % MethodCode
1018 const QString str = sipCpp->isValid() ? QStringLiteral( "<QgsCoordinateReferenceSystem: %1%2>" ).arg( !sipCpp->authid().isEmpty() ? sipCpp->authid() : sipCpp->toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ),
1019 std::isfinite( sipCpp->coordinateEpoch() ) ? QStringLiteral( " @ %1" ).arg( sipCpp->coordinateEpoch() ) : QString() )
1020 : QStringLiteral( "<QgsCoordinateReferenceSystem: invalid>" );
1021 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
1022 % End
1023#endif
1024
1025#ifndef SIP_RUN
1026
1036 PJ *projObject() const;
1037
1048 static QgsCoordinateReferenceSystem fromProjObject( PJ *object );
1049
1058 bool createFromProjObject( PJ *object );
1059#endif
1060
1066 Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
1067
1072 static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems();
1073
1078 static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs );
1079
1084 static void removeRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs );
1085
1090 static void clearRecentCoordinateReferenceSystems();
1091
1092#ifndef SIP_RUN
1093
1104 static void invalidateCache( bool disableCache = false );
1105#else
1106
1114 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
1115#endif
1116
1117 // Mutators -----------------------------------
1118 // We don't want to expose these to the public api since they won't create
1119 // a fully valid crs. Programmers should use the createFrom* methods rather
1120 private:
1121
1127 static QString projFromSrsId( int srsId );
1128
1134 void setProjString( const QString &projString );
1135
1139 bool setWktString( const QString &wkt );
1140
1144 void debugPrint();
1145
1147 typedef QMap<QString, QString> RecordMap;
1148
1156 RecordMap getRecord( const QString &sql );
1157
1162 static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
1163
1165 void setMapUnits();
1166
1168 static long getRecordCount();
1169
1170 bool loadFromAuthCode( const QString &auth, const QString &code );
1171
1175 static QList< long > userSrsIds();
1176
1184 long matchToUserCrs() const;
1185
1190 bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
1191
1192 bool createFromWktInternal( const QString &wkt, const QString &description );
1193
1194 QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
1195
1196 QString mValidationHint;
1197
1199
1200 friend class QgsProjContext;
1201
1202 // Only meant to be called by QgsProjContext::~QgsProjContext()
1203 static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
1204
1206 static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
1207
1208 // cache
1209
1210 static bool sDisableSrIdCache;
1211 static bool sDisableOgcCache;
1212 static bool sDisableProjCache;
1213 static bool sDisableWktCache;
1214 static bool sDisableSrsIdCache;
1215 static bool sDisableStringCache;
1216
1217 // for tests
1218 static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
1219 static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
1220 static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
1221 static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
1222 static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
1223 static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
1224
1225 friend class TestQgsCoordinateReferenceSystem;
1227 friend bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1228 friend bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1229 friend bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1230 friend bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1231
1232 bool createFromPostgisSrid( const long id );
1233};
1234
1236
1237
1238#ifndef SIP_RUN
1239inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
1240{
1241 QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
1242 mySummary += QLatin1String( "\n\t\tDescription : " );
1243 if ( !r.description().isNull() )
1244 {
1245 mySummary += r.description();
1246 }
1247 else
1248 {
1249 mySummary += QLatin1String( "Undefined" );
1250 }
1251 mySummary += QLatin1String( "\n\t\tProjection : " );
1252 if ( !r.projectionAcronym().isNull() )
1253 {
1254 mySummary += r.projectionAcronym();
1255 }
1256 else
1257 {
1258 mySummary += QLatin1String( "Undefined" );
1259 }
1260
1261 mySummary += QLatin1String( "\n\t\tEllipsoid : " );
1262 if ( !r.ellipsoidAcronym().isNull() )
1263 {
1264 mySummary += r.ellipsoidAcronym();
1265 }
1266 else
1267 {
1268 mySummary += QLatin1String( "Undefined" );
1269 }
1270
1271 mySummary += QLatin1String( "\n\t\tProjString : " );
1272 if ( !r.toProj().isNull() )
1273 {
1274 mySummary += r.toProj();
1275 }
1276 else
1277 {
1278 mySummary += QLatin1String( "Undefined" );
1279 }
1280 // Using streams we need to use local 8 Bit
1281 return os << mySummary.toLocal8Bit().data() << std::endl;
1282}
1283
1284bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1285bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1286bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1287bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1288#endif
1289
1290#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:3496
CrsDefinitionFormat
CRS definition formats.
Definition qgis.h:2617
@ 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.
IdentifierType
Type of identifier string to create.
@ MediumString
A medium-length string, recommended for general purpose use.
@ FullString
Full definition – possibly a very lengthy string, e.g. with no truncation of custom WKT definitions.
@ ShortString
A heavily abbreviated string, for use when a compact representation is required.
WktVariant
WKT formatting variants, only used for builds based on Proj >= 6.
@ WKT1_GDAL
WKT1 as traditionally output by GDAL, deriving from OGC 01-009. A notable departure from WKT1_GDAL wi...
@ WKT2_2018_SIMPLIFIED
Alias for WKT2_2019_SIMPLIFIED.
@ WKT2_2015
Full WKT2 string, conforming to ISO 19162:2015(E) / OGC 12-063r5 with all possible nodes and new keyw...
@ WKT1_ESRI
WKT1 as traditionally output by ESRI software, deriving from OGC 99-049.
@ WKT2_2015_SIMPLIFIED
Same as WKT2_2015 with the following exceptions: UNIT keyword used. ID node only on top element....
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:4726
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:4740
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:227
#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:198
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