QGIS API Documentation 3.41.0-Master (88383c3d16f)
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
51struct pj_ctx;
52typedef struct pj_ctx PJ_CONTEXT;
53#endif
54
55// forward declaration for sqlite3
56typedef struct sqlite3 sqlite3 SIP_SKIP;
57
58#ifdef DEBUG
59typedef struct OGRSpatialReferenceHS *OGRSpatialReferenceH SIP_SKIP;
60#else
62#endif
63
66
207{
208 Q_GADGET
209
210 Q_PROPERTY( Qgis::DistanceUnit mapUnits READ mapUnits )
211 Q_PROPERTY( bool isGeographic READ isGeographic )
212 Q_PROPERTY( QString authid READ authid )
213 Q_PROPERTY( QString description READ description )
214
215 public:
216
219 {
222 EpsgCrsId
223 };
224
227
229
230 // TODO QGIS 4: remove "POSTGIS" and "INTERNAL"
231
251 explicit QgsCoordinateReferenceSystem( const QString &definition );
252
253 // TODO QGIS 4: remove type and always use EPSG code
254
266 Q_DECL_DEPRECATED explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
267
270
272 operator QVariant() const
273 {
274 return QVariant::fromValue( *this );
275 }
276
283 static QList< long > validSrsIds();
284
285 // static creators
286
297 static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
298
308 Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
309
320 Q_DECL_DEPRECATED static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 ) SIP_DEPRECATED;
321
329 static QgsCoordinateReferenceSystem fromProj( const QString &proj );
330
341 static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
342
354 static QgsCoordinateReferenceSystem fromSrsId( long srsId );
355
371 static QgsCoordinateReferenceSystem createCompoundCrs( const QgsCoordinateReferenceSystem &horizontalCrs, const QgsCoordinateReferenceSystem &verticalCrs, QString &error SIP_OUT );
372
373 // Misc helper functions -----------------------
374
375 // TODO QGIS 4: remove type and always use EPSG code, rename to createFromEpsg
376
382 Q_DECL_DEPRECATED bool createFromId( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
383
384 // TODO QGIS 4: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
385
399 bool createFromOgcWmsCrs( const QString &crs );
400
401 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
402
410 Q_DECL_DEPRECATED bool createFromSrid( long srid ) SIP_DEPRECATED;
411
427 bool createFromWkt( const QString &wkt );
428
444 bool createFromSrsId( long srsId );
445
472 Q_DECL_DEPRECATED bool createFromProj4( const QString &projString ) SIP_DEPRECATED;
473
503#ifndef SIP_RUN
504 bool createFromProj( const QString &projString, bool identify = true );
505#else
506 bool createFromProj( const QString &projString );
507#endif
508
528 bool createFromString( const QString &definition );
529
530 // TODO QGIS 4: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
531
550 bool createFromUserInput( const QString &definition );
551
562 Q_DECL_DEPRECATED static void setupESRIWktFix() SIP_DEPRECATED;
563
565 bool isValid() const;
566
579 void validate();
580
581 // TODO QGIS 4: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
582
591 Q_DECL_DEPRECATED long findMatchingProj() SIP_DEPRECATED;
592
593 bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
594 bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
595
602 bool readXml( const QDomNode &node );
603
610 bool writeXml( QDomNode &node, QDomDocument &doc ) const;
611
612
617 static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
618
623 static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
624
625 // Accessors -----------------------------------
626
631 long srsid() const;
632
633 // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
634
639 long postgisSrid() const;
640
653 QString authid() const;
654
662 QString description() const;
663
677 QString userFriendlyIdentifier( Qgis::CrsIdentifierType type = Qgis::CrsIdentifierType::MediumString ) const;
678
685 QString projectionAcronym() const;
686
693 QString ellipsoidAcronym() const;
694
706 QString toWkt( Qgis::CrsWktVariant variant = Qgis::CrsWktVariant::Wkt1Gdal, bool multiline = false, int indentationWidth = 4 ) const;
707
720 Q_DECL_DEPRECATED QString toProj4() const SIP_DEPRECATED;
721
734 QString toProj() const;
735
741 Qgis::CrsType type() const;
742
748 bool isDeprecated() const;
749
754 bool isGeographic() const;
755
764 bool isDynamic() const;
765
776 QgsDatumEnsemble datumEnsemble() const;
777
783 QString celestialBodyName() const;
784
808 void setCoordinateEpoch( double epoch );
809
833 double coordinateEpoch() const;
834
846 QgsProjectionFactors factors( const QgsPoint &point ) const;
847
854 QgsProjOperation operation() const;
855
863 bool hasAxisInverted() const;
864
870#ifndef SIP_RUN
871 QList< Qgis::CrsAxisDirection > axisOrdering() const;
872#else
873 SIP_PYOBJECT axisOrdering() const SIP_TYPEHINT( List[Qgis.CrsAxisDirection] );
874 % MethodCode
875 // adapted from the qpymultimedia_qlist.sip file from the PyQt6 sources
876
877 const QList< Qgis::CrsAxisDirection > cppRes = sipCpp->axisOrdering();
878
879 PyObject *l = PyList_New( cppRes.size() );
880
881 if ( !l )
882 sipIsErr = 1;
883 else
884 {
885 for ( int i = 0; i < cppRes.size(); ++i )
886 {
887 PyObject *eobj = sipConvertFromEnum( static_cast<int>( cppRes.at( i ) ),
888 sipType_Qgis_CrsAxisDirection );
889
890 if ( !eobj )
891 {
892 sipIsErr = 1;
893 }
894
895 PyList_SetItem( l, i, eobj );
896 }
897
898 if ( !sipIsErr )
899 {
900 sipRes = l;
901 }
902 else
903 {
904 Py_DECREF( l );
905 }
906 }
907 % End
908#endif
909
915 Qgis::DistanceUnit mapUnits() const;
916
924 QgsRectangle bounds() const;
925
932 QString toOgcUri() const;
933
940 QString toOgcUrn() const;
941
942 // Mutators -----------------------------------
943
960 void updateDefinition();
961
965 void setValidationHint( const QString &html );
966
970 QString validationHint() const;
971
978 static int syncDatabase();
979
993 long saveAsUserCrs( const QString &name, Qgis::CrsDefinitionFormat nativeFormat = Qgis::CrsDefinitionFormat::Wkt );
994
1004 void setNativeFormat( Qgis::CrsDefinitionFormat format );
1005
1015 Qgis::CrsDefinitionFormat nativeFormat() const;
1016
1026 QgsCoordinateReferenceSystem toGeographicCrs() const;
1027
1038 QgsCoordinateReferenceSystem horizontalCrs() const;
1039
1052 QgsCoordinateReferenceSystem verticalCrs() const;
1053
1060 bool hasVerticalAxis() const;
1061
1063 QString geographicCrsAuthId() const;
1064
1065#ifdef SIP_RUN
1066 SIP_PYOBJECT __repr__();
1067 % MethodCode
1068 const QString str = sipCpp->isValid() ? QStringLiteral( "<QgsCoordinateReferenceSystem: %1%2>" ).arg( !sipCpp->authid().isEmpty() ? sipCpp->authid() : sipCpp->toWkt( Qgis::CrsWktVariant::Preferred ),
1069 std::isfinite( sipCpp->coordinateEpoch() ) ? QStringLiteral( " @ %1" ).arg( sipCpp->coordinateEpoch() ) : QString() )
1070 : QStringLiteral( "<QgsCoordinateReferenceSystem: invalid>" );
1071 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
1072 % End
1073#endif
1074
1075#ifndef SIP_RUN
1076
1086 PJ *projObject() const;
1087
1098 static QgsCoordinateReferenceSystem fromProjObject( PJ *object );
1099
1108 bool createFromProjObject( PJ *object );
1109#endif
1110
1117 Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
1118
1124 Q_DECL_DEPRECATED static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems() SIP_DEPRECATED;
1125
1131 Q_DECL_DEPRECATED static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1132
1138 Q_DECL_DEPRECATED static void removeRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1139
1145 Q_DECL_DEPRECATED static void clearRecentCoordinateReferenceSystems() SIP_DEPRECATED;
1146
1147#ifndef SIP_RUN
1148
1158 static void invalidateCache( bool disableCache = false );
1159#else
1160
1167 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
1168#endif
1169
1170 // Mutators -----------------------------------
1171 // We don't want to expose these to the public api since they won't create
1172 // a fully valid crs. Programmers should use the createFrom* methods rather
1173 private:
1174
1180 static QString projFromSrsId( int srsId );
1181
1187 void setProjString( const QString &projString );
1188
1192 bool setWktString( const QString &wkt );
1193
1197 void debugPrint();
1198
1200 typedef QMap<QString, QString> RecordMap;
1201
1209 RecordMap getRecord( const QString &sql );
1210
1215 static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
1216
1218 void setMapUnits();
1219
1221 static long getRecordCount();
1222
1228 bool loadFromAuthCode( const QString &auth, const QString &code );
1229
1233 static QList< long > userSrsIds();
1234
1242 long matchToUserCrs() const;
1243
1248 bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
1249
1250 bool createFromWktInternal( const QString &wkt, const QString &description );
1251
1252 QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
1253
1254 QString mValidationHint;
1255
1257
1258 friend class QgsProjContext;
1259
1260 // Only meant to be called by QgsProjContext::~QgsProjContext()
1261 static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
1262
1264 static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
1265
1266 // cache
1267
1268 static bool sDisableSrIdCache;
1269 static bool sDisableOgcCache;
1270 static bool sDisableProjCache;
1271 static bool sDisableWktCache;
1272 static bool sDisableSrsIdCache;
1273 static bool sDisableStringCache;
1274
1275 // for tests
1276 static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
1277 static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
1278 static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
1279 static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
1280 static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
1281 static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
1282
1283 friend class TestQgsCoordinateReferenceSystem;
1285 friend bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1286 friend bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1287 friend bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1288 friend bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1289
1290 bool createFromPostgisSrid( const long id );
1291};
1292
1294
1295
1296#ifndef SIP_RUN
1297inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
1298{
1299 QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
1300 mySummary += QLatin1String( "\n\t\tDescription : " );
1301 if ( !r.description().isNull() )
1302 {
1303 mySummary += r.description();
1304 }
1305 else
1306 {
1307 mySummary += QLatin1String( "Undefined" );
1308 }
1309 mySummary += QLatin1String( "\n\t\tProjection : " );
1310 if ( !r.projectionAcronym().isNull() )
1311 {
1312 mySummary += r.projectionAcronym();
1313 }
1314 else
1315 {
1316 mySummary += QLatin1String( "Undefined" );
1317 }
1318
1319 mySummary += QLatin1String( "\n\t\tEllipsoid : " );
1320 if ( !r.ellipsoidAcronym().isNull() )
1321 {
1322 mySummary += r.ellipsoidAcronym();
1323 }
1324 else
1325 {
1326 mySummary += QLatin1String( "Undefined" );
1327 }
1328
1329 mySummary += QLatin1String( "\n\t\tProjString : " );
1330 if ( !r.toProj().isNull() )
1331 {
1332 mySummary += r.toProj();
1333 }
1334 else
1335 {
1336 mySummary += QLatin1String( "Undefined" );
1337 }
1338 // Using streams we need to use local 8 Bit
1339 return os << mySummary.toLocal8Bit().data() << std::endl;
1340}
1341
1342bool CORE_EXPORT operator> ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1343bool CORE_EXPORT operator< ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1344bool CORE_EXPORT operator>= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1345bool CORE_EXPORT operator<= ( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1346#endif
1347
1348#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:4760
CrsDefinitionFormat
CRS definition formats.
Definition qgis.h:3696
@ 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
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...
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:6512
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:6526
#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
bool operator>=(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
bool operator<=(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
struct sqlite3 sqlite3
struct pj_ctx PJ_CONTEXT
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
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