QGIS API Documentation 4.3.0-Master (7c7bd4d7018)
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
1124 bool topocentricOrigin( double &latitude SIP_OUT, double &longitude SIP_OUT ) const;
1125
1126
1135 QgsCoordinateReferenceSystem toTopocentricCrs( double latitude, double longitude ) const;
1136
1143 QgsCoordinateReferenceSystem topocentricBaseCrs() const;
1144
1145#ifdef SIP_RUN
1146 // clang-format off
1147 SIP_PYOBJECT __repr__();
1148 % MethodCode
1149 const QString str = sipCpp->isValid() ? u"<QgsCoordinateReferenceSystem: %1%2>"_s.arg( !sipCpp->authid().isEmpty() ? sipCpp->authid() : sipCpp->toWkt( Qgis::CrsWktVariant::Preferred ),
1150 std::isfinite( sipCpp->coordinateEpoch() ) ? u" @ %1"_s.arg( sipCpp->coordinateEpoch() ) : QString() )
1151 : u"<QgsCoordinateReferenceSystem: invalid>"_s;
1152 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
1153 % End
1154// clang-format on
1155#endif
1156
1157#ifndef SIP_RUN
1158
1168 PJ *projObject() const;
1169
1180 static QgsCoordinateReferenceSystem fromProjObject( PJ *object );
1181
1190 bool createFromProjObject( PJ *object );
1191#endif
1192
1200 bool isEarthCrs() const;
1201
1208 bool isSameCelestialBody( const QgsCoordinateReferenceSystem &other ) const;
1209
1216 Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
1217
1223 Q_DECL_DEPRECATED static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems() SIP_DEPRECATED;
1224
1230 Q_DECL_DEPRECATED static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1231
1237 Q_DECL_DEPRECATED static void removeRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1238
1244 Q_DECL_DEPRECATED static void clearRecentCoordinateReferenceSystems() SIP_DEPRECATED;
1245
1246#ifndef SIP_RUN
1247
1257 static void invalidateCache( bool disableCache = false );
1258#else
1259
1266 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
1267#endif
1268
1269 // Mutators -----------------------------------
1270 // We don't want to expose these to the public api since they won't create
1271 // a fully valid crs. Programmers should use the createFrom* methods rather
1272 private:
1278 static QString projFromSrsId( int srsId );
1279
1285 void setProjString( const QString &projString );
1286
1290 bool setWktString( const QString &wkt );
1291
1295 void debugPrint();
1296
1298 typedef QMap<QString, QString> RecordMap;
1299
1307 RecordMap getRecord( const QString &sql );
1308
1313 static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
1314
1316 void setMapUnits();
1317
1319 static long getRecordCount();
1320
1326 bool loadFromAuthCode( const QString &auth, const QString &code );
1327
1331 static QList< long > userSrsIds();
1332
1340 long matchToUserCrs() const;
1341
1346 bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
1347
1348 bool createFromWktInternal( const QString &wkt, const QString &description );
1349
1350 QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
1351
1352 QString mValidationHint;
1353
1356 friend class QgsProjContext;
1357
1358 // Only meant to be called by QgsProjContext::~QgsProjContext()
1359 static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
1360
1362 static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
1363
1364 // cache
1365
1366 static bool sDisableSrIdCache;
1367 static bool sDisableOgcCache;
1368 static bool sDisableProjCache;
1369 static bool sDisableWktCache;
1370 static bool sDisableSrsIdCache;
1371 static bool sDisableStringCache;
1372
1373 // for tests
1374 static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
1375 static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
1376 static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
1377 static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
1378 static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
1379 static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
1383 friend bool CORE_EXPORT operator>( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1384 friend bool CORE_EXPORT operator<( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1385 friend bool CORE_EXPORT operator>=( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1386 friend bool CORE_EXPORT operator<=( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1387
1388 bool createFromPostgisSrid( const long id );
1389};
1390
1392
1393
1394#ifndef SIP_RUN
1395inline std::ostream &operator<<( std::ostream &os, const QgsCoordinateReferenceSystem &r )
1396{
1397 QString mySummary( u"\n\tSpatial Reference System:"_s );
1398 mySummary += "\n\t\tDescription : "_L1;
1399 if ( !r.description().isNull() )
1400 {
1401 mySummary += r.description();
1402 }
1403 else
1404 {
1405 mySummary += "Undefined"_L1;
1406 }
1407 mySummary += "\n\t\tProjection : "_L1;
1408 if ( !r.projectionAcronym().isNull() )
1409 {
1410 mySummary += r.projectionAcronym();
1411 }
1412 else
1413 {
1414 mySummary += "Undefined"_L1;
1415 }
1416
1417 mySummary += "\n\t\tEllipsoid : "_L1;
1418 if ( !r.ellipsoidAcronym().isNull() )
1419 {
1420 mySummary += r.ellipsoidAcronym();
1421 }
1422 else
1423 {
1424 mySummary += "Undefined"_L1;
1425 }
1426
1427 mySummary += "\n\t\tProjString : "_L1;
1428 if ( !r.toProj().isNull() )
1429 {
1430 mySummary += r.toProj();
1431 }
1432 else
1433 {
1434 mySummary += "Undefined"_L1;
1435 }
1436 // Using streams we need to use local 8 Bit
1437 return os << mySummary.toLocal8Bit().data() << std::endl;
1438}
1439
1440bool CORE_EXPORT operator>( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1441bool CORE_EXPORT operator<( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1442bool CORE_EXPORT operator>=( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1443bool CORE_EXPORT operator<=( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1444#endif
1445
1446#endif // QGSCOORDINATEREFERENCESYSTEM_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
DistanceUnit
Units of distance.
Definition qgis.h:5437
CrsDefinitionFormat
CRS definition formats.
Definition qgis.h:4076
@ Wkt
WKT format (always recommended over proj string format).
Definition qgis.h:4077
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
Definition qgis.h:2581
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:7814
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:7830
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:237
#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)