QGIS API Documentation 4.1.0-Master (467af3bbe65)
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 <ostream>
23
24#include "qgis_core.h"
25
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
30//qt includes
31#include <QString>
32#include <QMap>
33#include <QHash>
34#include <QReadWriteLock>
35#include <QExplicitlySharedDataPointer>
36#include <QObject>
37
38//qgis includes
39#include "qgis_sip.h"
40#include "qgis.h"
41#include "qgsconfig.h"
42#include "qgsrectangle.h"
43#include "qgssqliteutils.h"
44
45class QDomNode;
46class QDomDocument;
47class QgsCoordinateReferenceSystemPrivate;
51
52#ifndef SIP_RUN
53struct PJconsts;
54typedef struct PJconsts PJ;
55
56struct pj_ctx;
57typedef struct pj_ctx PJ_CONTEXT;
58#endif
59
60// forward declaration for sqlite3
61typedef struct sqlite3 sqlite3 SIP_SKIP;
62
65
216class CORE_EXPORT QgsCoordinateReferenceSystem
217{
218 Q_GADGET
221 Q_PROPERTY( bool isGeographic READ isGeographic )
222 Q_PROPERTY( QString authid READ authid )
223 Q_PROPERTY( QString description READ description )
224
225 public:
226 //! Enumeration of types of IDs accepted in createFromId() method
227 enum CrsType
231 EpsgCrsId
232 };
233
236
238
239 // TODO QGIS 5: remove "POSTGIS" and "INTERNAL"
240
260 explicit QgsCoordinateReferenceSystem( const QString &definition );
261
262 // TODO QGIS 5: remove type and always use EPSG code
263
275 Q_DECL_DEPRECATED explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
276
279
281 operator QVariant() const { return QVariant::fromValue( *this ); }
282
289 static QList< long > validSrsIds();
290
291 // static creators
292
303 static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
304
314 Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
315
326 Q_DECL_DEPRECATED static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 ) SIP_DEPRECATED;
327
335 static QgsCoordinateReferenceSystem fromProj( const QString &proj );
336
347 static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
348
360 static QgsCoordinateReferenceSystem fromSrsId( long srsId );
361
377 static QgsCoordinateReferenceSystem createCompoundCrs( const QgsCoordinateReferenceSystem &horizontalCrs, const QgsCoordinateReferenceSystem &verticalCrs, QString &error SIP_OUT );
378
394 static QgsCoordinateReferenceSystem createGeocentricCrs( const QString &ellipsoid );
395
396 // Misc helper functions -----------------------
397
398 // TODO QGIS 5: remove type and always use EPSG code, rename to createFromEpsg
399
405 Q_DECL_DEPRECATED bool createFromId( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
406
407 // TODO QGIS 5: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
408
422 bool createFromOgcWmsCrs( const QString &crs );
423
424 // TODO QGIS 5: remove unless really necessary - let's use EPSG codes instead
425
433 Q_DECL_DEPRECATED bool createFromSrid( long srid ) SIP_DEPRECATED;
434
450 bool createFromWkt( const QString &wkt );
451
467 bool createFromSrsId( long srsId );
468
495 Q_DECL_DEPRECATED bool createFromProj4( const QString &projString ) SIP_DEPRECATED;
496
526#ifndef SIP_RUN
527 bool createFromProj( const QString &projString, bool identify = true );
528#else
529 bool createFromProj( const QString &projString );
530#endif
531
551 bool createFromString( const QString &definition );
552
553 // TODO QGIS 5: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
554
573 bool createFromUserInput( const QString &definition );
574
585 Q_DECL_DEPRECATED static void setupESRIWktFix() SIP_DEPRECATED;
586
588 bool isValid() const;
589
602 void validate();
603
604 // TODO QGIS 5: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
605
614 Q_DECL_DEPRECATED long findMatchingProj() SIP_DEPRECATED;
615
616 bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
617 bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
618
625 bool readXml( const QDomNode &node );
626
633 bool writeXml( QDomNode &node, QDomDocument &doc ) const;
634
635
640 static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
641
646 static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
647
648 // Accessors -----------------------------------
649
654 long srsid() const;
655
656 // TODO QGIS 5: remove unless really necessary - let's use EPSG codes instead
657
662 long postgisSrid() const;
663
676 QString authid() const;
677
685 QString description() const;
686
700 QString userFriendlyIdentifier( Qgis::CrsIdentifierType type = Qgis::CrsIdentifierType::MediumString ) const;
701
708 QString projectionAcronym() const;
709
716 QString ellipsoidAcronym() const;
717
729 QString toWkt( Qgis::CrsWktVariant variant = Qgis::CrsWktVariant::Wkt1Gdal, bool multiline = false, int indentationWidth = 4 ) const;
730
743 Q_DECL_DEPRECATED QString toProj4() const SIP_DEPRECATED;
744
757 QString toProj() const;
758
770 std::string toJsonString( bool multiline = false, int indentationWidth = 4, const QString &schema = QString() ) const;
771
777 Qgis::CrsType type() const;
778
784 bool isDeprecated() const;
785
790 bool isGeographic() const;
791
800 bool isDynamic() const;
801
812 QgsDatumEnsemble datumEnsemble() const;
813
819 QString celestialBodyName() const;
820
844 void setCoordinateEpoch( double epoch );
845
869 double coordinateEpoch() const;
870
882 QgsProjectionFactors factors( const QgsPoint &point ) const;
883
890 QgsProjOperation operation() const;
891
899 bool hasAxisInverted() const;
900
906#ifndef SIP_RUN
907 QList< Qgis::CrsAxisDirection > axisOrdering() const;
908#else
909 // clang-format off
910 SIP_PYOBJECT axisOrdering() const SIP_TYPEHINT( List[Qgis.CrsAxisDirection] );
911 % MethodCode
912 // adapted from the qpymultimedia_qlist.sip file from the PyQt6 sources
913
914 const QList< Qgis::CrsAxisDirection > cppRes = sipCpp->axisOrdering();
915
916 PyObject *l = PyList_New( cppRes.size() );
917
918 if ( !l )
919 sipIsErr = 1;
920 else
921 {
922 for ( int i = 0; i < cppRes.size(); ++i )
923 {
924 PyObject *eobj = sipConvertFromEnum( static_cast<int>( cppRes.at( i ) ),
925 sipType_Qgis_CrsAxisDirection );
926
927 if ( !eobj )
928 {
929 sipIsErr = 1;
930 }
931
932 PyList_SetItem( l, i, eobj );
933 }
934
935 if ( !sipIsErr )
936 {
937 sipRes = l;
938 }
939 else
940 {
941 Py_DECREF( l );
942 }
943 }
944 % End
945// clang-format on
946#endif
947
953 Qgis::DistanceUnit mapUnits() const;
954
962 QgsRectangle bounds() const;
963
970 QString toOgcUri() const;
971
978 QString toOgcUrn() const;
979
980 // Mutators -----------------------------------
981
998 void updateDefinition();
999
1003 void setValidationHint( const QString &html );
1004
1008 QString validationHint() const;
1009
1016 static int syncDatabase();
1017
1031 long saveAsUserCrs( const QString &name, Qgis::CrsDefinitionFormat nativeFormat = Qgis::CrsDefinitionFormat::Wkt );
1032
1042 void setNativeFormat( Qgis::CrsDefinitionFormat format );
1043
1053 Qgis::CrsDefinitionFormat nativeFormat() const;
1054
1064 QgsCoordinateReferenceSystem toGeographicCrs() const;
1065
1077 QgsCoordinateReferenceSystem toGeocentricCrs() const;
1078
1089 QgsCoordinateReferenceSystem horizontalCrs() const;
1090
1103 QgsCoordinateReferenceSystem verticalCrs() const;
1104
1111 bool hasVerticalAxis() const;
1112
1114 QString geographicCrsAuthId() const;
1115
1116#ifdef SIP_RUN
1117 // clang-format off
1118 SIP_PYOBJECT __repr__();
1119 % MethodCode
1120 const QString str = sipCpp->isValid() ? u"<QgsCoordinateReferenceSystem: %1%2>"_s.arg( !sipCpp->authid().isEmpty() ? sipCpp->authid() : sipCpp->toWkt( Qgis::CrsWktVariant::Preferred ),
1121 std::isfinite( sipCpp->coordinateEpoch() ) ? u" @ %1"_s.arg( sipCpp->coordinateEpoch() ) : QString() )
1122 : u"<QgsCoordinateReferenceSystem: invalid>"_s;
1123 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
1124 % End
1125// clang-format on
1126#endif
1127
1128#ifndef SIP_RUN
1129
1139 PJ *projObject() const;
1140
1151 static QgsCoordinateReferenceSystem fromProjObject( PJ *object );
1152
1161 bool createFromProjObject( PJ *object );
1162#endif
1163
1171 bool isEarthCrs() const;
1172
1179 bool isSameCelestialBody( const QgsCoordinateReferenceSystem &other ) const;
1180
1187 Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
1188
1194 Q_DECL_DEPRECATED static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems() SIP_DEPRECATED;
1195
1201 Q_DECL_DEPRECATED static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1202
1208 Q_DECL_DEPRECATED static void removeRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1209
1215 Q_DECL_DEPRECATED static void clearRecentCoordinateReferenceSystems() SIP_DEPRECATED;
1216
1217#ifndef SIP_RUN
1218
1228 static void invalidateCache( bool disableCache = false );
1229#else
1230
1237 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
1238#endif
1239
1240 // Mutators -----------------------------------
1241 // We don't want to expose these to the public api since they won't create
1242 // a fully valid crs. Programmers should use the createFrom* methods rather
1243 private:
1249 static QString projFromSrsId( int srsId );
1250
1256 void setProjString( const QString &projString );
1257
1261 bool setWktString( const QString &wkt );
1262
1266 void debugPrint();
1267
1269 typedef QMap<QString, QString> RecordMap;
1270
1278 RecordMap getRecord( const QString &sql );
1279
1284 static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
1285
1287 void setMapUnits();
1288
1290 static long getRecordCount();
1291
1297 bool loadFromAuthCode( const QString &auth, const QString &code );
1298
1302 static QList< long > userSrsIds();
1303
1311 long matchToUserCrs() const;
1312
1317 bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
1318
1319 bool createFromWktInternal( const QString &wkt, const QString &description );
1320
1321 QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
1322
1323 QString mValidationHint;
1324
1327 friend class QgsProjContext;
1328
1329 // Only meant to be called by QgsProjContext::~QgsProjContext()
1330 static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
1331
1333 static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
1334
1335 // cache
1336
1337 static bool sDisableSrIdCache;
1338 static bool sDisableOgcCache;
1339 static bool sDisableProjCache;
1340 static bool sDisableWktCache;
1341 static bool sDisableSrsIdCache;
1342 static bool sDisableStringCache;
1343
1344 // for tests
1345 static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
1346 static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
1347 static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
1348 static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
1349 static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
1350 static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
1354 friend bool CORE_EXPORT operator>( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1355 friend bool CORE_EXPORT operator<( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1356 friend bool CORE_EXPORT operator>=( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1357 friend bool CORE_EXPORT operator<=( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1358
1359 bool createFromPostgisSrid( const long id );
1360};
1361
1363
1364
1365#ifndef SIP_RUN
1366inline std::ostream &operator<<( std::ostream &os, const QgsCoordinateReferenceSystem &r )
1367{
1368 QString mySummary( u"\n\tSpatial Reference System:"_s );
1369 mySummary += "\n\t\tDescription : "_L1;
1370 if ( !r.description().isNull() )
1371 {
1372 mySummary += r.description();
1373 }
1374 else
1375 {
1376 mySummary += "Undefined"_L1;
1377 }
1378 mySummary += "\n\t\tProjection : "_L1;
1379 if ( !r.projectionAcronym().isNull() )
1380 {
1381 mySummary += r.projectionAcronym();
1382 }
1383 else
1384 {
1385 mySummary += "Undefined"_L1;
1386 }
1387
1388 mySummary += "\n\t\tEllipsoid : "_L1;
1389 if ( !r.ellipsoidAcronym().isNull() )
1390 {
1391 mySummary += r.ellipsoidAcronym();
1392 }
1393 else
1394 {
1395 mySummary += "Undefined"_L1;
1396 }
1397
1398 mySummary += "\n\t\tProjString : "_L1;
1399 if ( !r.toProj().isNull() )
1400 {
1401 mySummary += r.toProj();
1402 }
1403 else
1404 {
1405 mySummary += "Undefined"_L1;
1406 }
1407 // Using streams we need to use local 8 Bit
1408 return os << mySummary.toLocal8Bit().data() << std::endl;
1409}
1410
1411bool CORE_EXPORT operator>( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1412bool CORE_EXPORT operator<( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1413bool CORE_EXPORT operator>=( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1414bool CORE_EXPORT operator<=( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1415#endif
1416
1417#endif // QGSCOORDINATEREFERENCESYSTEM_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
DistanceUnit
Units of distance.
Definition qgis.h:5269
CrsDefinitionFormat
CRS definition formats.
Definition qgis.h:4060
@ Wkt
WKT format (always recommended over proj string format).
Definition qgis.h:4061
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
Definition qgis.h:2579
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.
QgsCoordinateReferenceSystem()
Constructs an invalid CRS object.
Contains information about a datum ensemble.
Definition qgsdatums.h:100
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
Contains information about a PROJ operation.
Contains various cartographic properties, such as scale factors, angular distortion and meridian conv...
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:7497
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:7513
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:239
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_PYARGREMOVE
Definition qgis_sip.h:158
#define SIP_OUT
Definition qgis_sip.h:57
bool operator>=(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
bool operator<=(const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2)
struct sqlite3 sqlite3
struct pj_ctx PJ_CONTEXT
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)