QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
1170 Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
1171
1177 Q_DECL_DEPRECATED static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems() SIP_DEPRECATED;
1178
1184 Q_DECL_DEPRECATED static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1185
1191 Q_DECL_DEPRECATED static void removeRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
1192
1198 Q_DECL_DEPRECATED static void clearRecentCoordinateReferenceSystems() SIP_DEPRECATED;
1199
1200#ifndef SIP_RUN
1201
1211 static void invalidateCache( bool disableCache = false );
1212#else
1213
1220 static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
1221#endif
1222
1223 // Mutators -----------------------------------
1224 // We don't want to expose these to the public api since they won't create
1225 // a fully valid crs. Programmers should use the createFrom* methods rather
1226 private:
1232 static QString projFromSrsId( int srsId );
1233
1239 void setProjString( const QString &projString );
1240
1244 bool setWktString( const QString &wkt );
1245
1249 void debugPrint();
1250
1252 typedef QMap<QString, QString> RecordMap;
1253
1261 RecordMap getRecord( const QString &sql );
1262
1267 static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
1268
1270 void setMapUnits();
1271
1273 static long getRecordCount();
1274
1280 bool loadFromAuthCode( const QString &auth, const QString &code );
1281
1285 static QList< long > userSrsIds();
1286
1294 long matchToUserCrs() const;
1295
1300 bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
1301
1302 bool createFromWktInternal( const QString &wkt, const QString &description );
1303
1304 QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
1305
1306 QString mValidationHint;
1307
1310 friend class QgsProjContext;
1311
1312 // Only meant to be called by QgsProjContext::~QgsProjContext()
1313 static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
1314
1316 static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
1317
1318 // cache
1319
1320 static bool sDisableSrIdCache;
1321 static bool sDisableOgcCache;
1322 static bool sDisableProjCache;
1323 static bool sDisableWktCache;
1324 static bool sDisableSrsIdCache;
1325 static bool sDisableStringCache;
1326
1327 // for tests
1328 static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
1329 static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
1330 static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
1331 static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
1332 static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
1333 static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
1337 friend bool CORE_EXPORT operator>( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1338 friend bool CORE_EXPORT operator<( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1339 friend bool CORE_EXPORT operator>=( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1340 friend bool CORE_EXPORT operator<=( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1341
1342 bool createFromPostgisSrid( const long id );
1343};
1344
1346
1347
1348#ifndef SIP_RUN
1349inline std::ostream &operator<<( std::ostream &os, const QgsCoordinateReferenceSystem &r )
1350{
1351 QString mySummary( u"\n\tSpatial Reference System:"_s );
1352 mySummary += "\n\t\tDescription : "_L1;
1353 if ( !r.description().isNull() )
1354 {
1355 mySummary += r.description();
1356 }
1357 else
1358 {
1359 mySummary += "Undefined"_L1;
1360 }
1361 mySummary += "\n\t\tProjection : "_L1;
1362 if ( !r.projectionAcronym().isNull() )
1363 {
1364 mySummary += r.projectionAcronym();
1365 }
1366 else
1367 {
1368 mySummary += "Undefined"_L1;
1369 }
1370
1371 mySummary += "\n\t\tEllipsoid : "_L1;
1372 if ( !r.ellipsoidAcronym().isNull() )
1373 {
1374 mySummary += r.ellipsoidAcronym();
1375 }
1376 else
1377 {
1378 mySummary += "Undefined"_L1;
1379 }
1380
1381 mySummary += "\n\t\tProjString : "_L1;
1382 if ( !r.toProj().isNull() )
1383 {
1384 mySummary += r.toProj();
1385 }
1386 else
1387 {
1388 mySummary += "Undefined"_L1;
1389 }
1390 // Using streams we need to use local 8 Bit
1391 return os << mySummary.toLocal8Bit().data() << std::endl;
1392}
1393
1394bool CORE_EXPORT operator>( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1395bool CORE_EXPORT operator<( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1396bool CORE_EXPORT operator>=( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1397bool CORE_EXPORT operator<=( const QgsCoordinateReferenceSystem &c1, const QgsCoordinateReferenceSystem &c2 );
1398#endif
1399
1400#endif // QGSCOORDINATEREFERENCESYSTEM_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
DistanceUnit
Units of distance.
Definition qgis.h:5170
CrsDefinitionFormat
CRS definition formats.
Definition qgis.h:4008
@ Wkt
WKT format (always recommended over proj string format).
Definition qgis.h:4009
@ Preferred
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
Definition qgis.h:2527
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:7380
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:7396
#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)