QGIS API Documentation 4.3.0-Master (91d4a333d5d)
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
7 email : sherman@mrcc.com
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
792 bool isGeographic() const;
793
801 bool isGeographicCrossingAntimeridian() const;
802
811 bool isDynamic() const;
812
823 QgsDatumEnsemble datumEnsemble() const;
824
830 QString celestialBodyName() const;
831
855 void setCoordinateEpoch( double epoch );
856
880 double coordinateEpoch() const;
881
893 QgsProjectionFactors factors( const QgsPoint &point ) const;
894
901 QgsProjOperation operation() const;
902
910 bool hasAxisInverted() const;
911
917#ifndef SIP_RUN
918 QList< Qgis::CrsAxisDirection > axisOrdering() const;
919#else
920 // clang-format off
921 SIP_PYOBJECT axisOrdering() const SIP_TYPEHINT( List[Qgis.CrsAxisDirection] );
922 % MethodCode
923 // adapted from the qpymultimedia_qlist.sip file from the PyQt6 sources
924
925 const QList< Qgis::CrsAxisDirection > cppRes = sipCpp->axisOrdering();
926
927 PyObject *l = PyList_New( cppRes.size() );
928
929 if ( !l )
930 sipIsErr = 1;
931 else
932 {
933 for ( int i = 0; i < cppRes.size(); ++i )
934 {
935 PyObject *eobj = sipConvertFromEnum( static_cast<int>( cppRes.at( i ) ),
936 sipType_Qgis_CrsAxisDirection );
937
938 if ( !eobj )
939 {
940 sipIsErr = 1;
941 }
942
943 PyList_SetItem( l, i, eobj );
944 }
945
946 if ( !sipIsErr )
947 {
948 sipRes = l;
949 }
950 else
951 {
952 Py_DECREF( l );
953 }
954 }
955 % End
956// clang-format on
957#endif
958
964 Qgis::DistanceUnit mapUnits() const;
965
973 QgsRectangle bounds() const;
974
981 QString toOgcUri() const;
982
989 QString toOgcUrn() const;
990
991 // Mutators -----------------------------------
992
1009 void updateDefinition();
1010
1014 void setValidationHint( const QString &html );
1015
1019 QString validationHint() const;
1020
1027 static int syncDatabase();
1028
1042 long saveAsUserCrs( const QString &name, Qgis::CrsDefinitionFormat nativeFormat = Qgis::CrsDefinitionFormat::Wkt );
1043
1053 void setNativeFormat( Qgis::CrsDefinitionFormat format );
1054
1064 Qgis::CrsDefinitionFormat nativeFormat() const;
1065
1075 QgsCoordinateReferenceSystem toGeographicCrs() const;
1076
1088 QgsCoordinateReferenceSystem toGeocentricCrs() const;
1089
1100 QgsCoordinateReferenceSystem horizontalCrs() const;
1101
1114 QgsCoordinateReferenceSystem verticalCrs() const;
1115
1122 bool hasVerticalAxis() const;
1123
1125 QString geographicCrsAuthId() const;
1126
1135 bool topocentricOrigin( double &latitude SIP_OUT, double &longitude SIP_OUT ) const;
1136
1137
1146 QgsCoordinateReferenceSystem toTopocentricCrs( double latitude, double longitude ) const;
1147
1154 QgsCoordinateReferenceSystem topocentricBaseCrs() const;
1155
1156#ifdef SIP_RUN
1157 // clang-format off
1158 SIP_PYOBJECT __repr__();
1159 % MethodCode
1160 const QString str = sipCpp->isValid() ? u"<QgsCoordinateReferenceSystem: %1%2>"_s.arg( !sipCpp->authid().isEmpty() ? sipCpp->authid() : sipCpp->toWkt( Qgis::CrsWktVariant::Preferred ),
1161 std::isfinite( sipCpp->coordinateEpoch() ) ? u" @ %1"_s.arg( sipCpp->coordinateEpoch() ) : QString() )
1162 : u"<QgsCoordinateReferenceSystem: invalid>"_s;
1163 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
1164 % End
1165// clang-format on
1166#endif
1167
1168#ifndef SIP_RUN
1169
1179 PJ *projObject() const;
1180
1191 static QgsCoordinateReferenceSystem fromProjObject( PJ *object );
1192
1201 bool createFromProjObject( PJ *object );
1202#endif
1203
1211 bool isEarthCrs() const;
1212
1219 bool isSameCelestialBody( const QgsCoordinateReferenceSystem &other ) const;
1220
1227 Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
1228
1234 Q_DECL_DEPRECATED static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems() SIP_DEPRECATED;
1235
1241 Q_DECL_DEPRECATED static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1242
1248 Q_DECL_DEPRECATED static void removeRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1249
1255 Q_DECL_DEPRECATED static void clearRecentCoordinateReferenceSystems() SIP_DEPRECATED;
1256
1257#ifndef SIP_RUN
1258
1268 static void invalidateCache( bool disableCache = false );
1269#else
1270
1277 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
1278#endif
1279
1280 // Mutators -----------------------------------
1281 // We don't want to expose these to the public api since they won't create
1282 // a fully valid crs. Programmers should use the createFrom* methods rather
1283 private:
1289 static QString projFromSrsId( int srsId );
1290
1291 static const QMap<QString, QString> &authIdToQgisSrsIdMap();
1292
1298 void setProjString( const QString &projString );
1299
1303 bool setWktString( const QString &wkt );
1304
1308 void debugPrint();
1309
1311 typedef QMap<QString, QString> RecordMap;
1312
1320 RecordMap getRecord( const QString &sql );
1321
1326 static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
1327
1329 void setMapUnits();
1330
1331 void setExtraProperties();
1332
1334 static long getRecordCount();
1335
1341 bool loadFromAuthCode( const QString &auth, const QString &code );
1342
1346 static QList< long > userSrsIds();
1347
1355 long matchToUserCrs() const;
1356
1361 bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
1362
1363 bool createFromWktInternal( const QString &wkt, const QString &description );
1364
1365 QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
1366
1367 QString mValidationHint;
1368
1371 friend class QgsProjContext;
1372
1373 // Only meant to be called by QgsProjContext::~QgsProjContext()
1374 static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
1375
1377 static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
1378
1379 // cache
1380 static bool sDisableSrIdCache;
1381 static bool sDisableOgcCache;
1382 static bool sDisableProjCache;
1383 static bool sDisableWktCache;
1384 static bool sDisableSrsIdCache;
1385 static bool sDisableStringCache;
1386
1387 // for tests
1388 static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
1389 static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
1390 static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
1391 static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
1392 static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
1393 static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
1397 friend bool CORE_EXPORT operator>( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1398 friend bool CORE_EXPORT operator<( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1399 friend bool CORE_EXPORT operator>=( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1400 friend bool CORE_EXPORT operator<=( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1401
1402 bool createFromPostgisSrid( const long id );
1403};
1404
1406
1407
1408#ifndef SIP_RUN
1409inline std::ostream &operator<<( std::ostream &os, const QgsCoordinateReferenceSystem &r )
1410{
1411 QString mySummary( u"\n\tSpatial Reference System:"_s );
1412 mySummary += "\n\t\tDescription : "_L1;
1413 if ( !r.description().isNull() )
1414 {
1415 mySummary += r.description();
1416 }
1417 else
1418 {
1419 mySummary += "Undefined"_L1;
1420 }
1421 mySummary += "\n\t\tProjection : "_L1;
1422 if ( !r.projectionAcronym().isNull() )
1423 {
1424 mySummary += r.projectionAcronym();
1425 }
1426 else
1427 {
1428 mySummary += "Undefined"_L1;
1429 }
1430
1431 mySummary += "\n\t\tEllipsoid : "_L1;
1432 if ( !r.ellipsoidAcronym().isNull() )
1433 {
1434 mySummary += r.ellipsoidAcronym();
1435 }
1436 else
1437 {
1438 mySummary += "Undefined"_L1;
1439 }
1440
1441 mySummary += "\n\t\tProjString : "_L1;
1442 if ( !r.toProj().isNull() )
1443 {
1444 mySummary += r.toProj();
1445 }
1446 else
1447 {
1448 mySummary += "Undefined"_L1;
1449 }
1450 // Using streams we need to use local 8 Bit
1451 return os << mySummary.toLocal8Bit().data() << std::endl;
1452}
1453
1454bool CORE_EXPORT operator>( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1455bool CORE_EXPORT operator<( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1456bool CORE_EXPORT operator>=( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1457bool CORE_EXPORT operator<=( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1458#endif
1459
1460#endif // QGSCOORDINATEREFERENCESYSTEM_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
DistanceUnit
Units of distance.
Definition qgis.h:5580
CrsDefinitionFormat
CRS definition formats.
Definition qgis.h:4166
@ Wkt
WKT format (always recommended over proj string format).
Definition qgis.h:4167
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
Definition qgis.h:2631
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:8323
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:8339
#define SIP_TYPEHINT(type)
Definition qgis_sip.h:242
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_SKIP
Definition qgis_sip.h:138
#define SIP_PYARGREMOVE
Definition qgis_sip.h:163
#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)