QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 : [email protected]
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 "qgsunittypes.h"
36 #include "qgsrectangle.h"
37 #include "qgssqliteutils.h"
38 
39 class QDomNode;
40 class QDomDocument;
41 class QgsCoordinateReferenceSystemPrivate;
42 
43 #if PROJ_VERSION_MAJOR>=6
44 #ifndef SIP_RUN
45 struct PJconsts;
46 typedef struct PJconsts PJ;
47 
48 struct projCtx_t;
49 typedef struct projCtx_t PJ_CONTEXT;
50 
51 #endif
52 #endif
53 
54 // forward declaration for sqlite3
55 typedef struct sqlite3 sqlite3 SIP_SKIP;
56 
57 #ifdef DEBUG
58 typedef struct OGRSpatialReferenceHS *OGRSpatialReferenceH SIP_SKIP;
59 #else
61 #endif
62 
65 
207 {
208  Q_GADGET
209 
210  Q_PROPERTY( QgsUnitTypes::DistanceUnit mapUnits READ mapUnits )
211  Q_PROPERTY( bool isGeographic READ isGeographic )
212 
213  public:
214 
216  enum CrsType
217  {
220  EpsgCrsId
221  };
222 
224  enum Format
225  {
226  FormatWkt = 0,
228  };
229 
232 
234 
235  // TODO QGIS 4: remove "POSTGIS" and "INTERNAL"
236 
252  explicit QgsCoordinateReferenceSystem( const QString &definition );
253 
254  // TODO QGIS 4: remove type and always use EPSG code
255 
267  Q_DECL_DEPRECATED explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
268 
271 
274 
276  operator QVariant() const
277  {
278  return QVariant::fromValue( *this );
279  }
280 
288  static QList< long > validSrsIds();
289 
290  // static creators
291 
299  static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
300 
307  Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
308 
315  Q_DECL_DEPRECATED static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 ) SIP_DEPRECATED;
316 
324  static QgsCoordinateReferenceSystem fromProj( const QString &proj );
325 
333  static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
334 
343  static QgsCoordinateReferenceSystem fromSrsId( long srsId );
344 
345  // Misc helper functions -----------------------
346 
347  // TODO QGIS 4: remove type and always use EPSG code, rename to createFromEpsg
348 
354  Q_DECL_DEPRECATED bool createFromId( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
355 
356  // TODO QGIS 4: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
357 
368  bool createFromOgcWmsCrs( const QString &crs );
369 
370  // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
371 
379  Q_DECL_DEPRECATED bool createFromSrid( long srid ) SIP_DEPRECATED;
380 
393  bool createFromWkt( const QString &wkt );
394 
407  bool createFromSrsId( long srsId );
408 
432  Q_DECL_DEPRECATED bool createFromProj4( const QString &projString ) SIP_DEPRECATED;
433 
461 #ifndef SIP_RUN
462  bool createFromProj( const QString &projString, bool identify = true );
463 #else
464  bool createFromProj( const QString &projString );
465 #endif
466 
482  bool createFromString( const QString &definition );
483  // TODO QGIS3: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
499  bool createFromUserInput( const QString &definition );
500 
511  Q_DECL_DEPRECATED static void setupESRIWktFix() SIP_DEPRECATED;
512 
514  bool isValid() const;
515 
527  void validate();
528 
529  // TODO QGIS 4: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
530 
539  Q_DECL_DEPRECATED long findMatchingProj() SIP_DEPRECATED;
540 
546  bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
547 
553  bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
554 
561  bool readXml( const QDomNode &node );
562 
569  bool writeXml( QDomNode &node, QDomDocument &doc ) const;
570 
571 
576  static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
577 
582  static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
583 
584  // Accessors -----------------------------------
585 
590  long srsid() const;
591 
592  // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
593 
598  long postgisSrid() const;
599 
612  QString authid() const;
613 
621  QString description() const;
622 
629  {
633  };
634 
648  QString userFriendlyIdentifier( IdentifierType type = MediumString ) const;
649 
656  QString projectionAcronym() const;
657 
664  QString ellipsoidAcronym() const;
665 
668  {
675  WKT2_2019 = WKT2_2018,
676  WKT2_2019_SIMPLIFIED = WKT2_2018_SIMPLIFIED,
677 
678  WKT_PREFERRED = WKT2_2019,
679  WKT_PREFERRED_SIMPLIFIED = WKT2_2019_SIMPLIFIED,
680  WKT_PREFERRED_GDAL = WKT2_2019,
681  };
682 
694  QString toWkt( WktVariant variant = WKT1_GDAL, bool multiline = false, int indentationWidth = 4 ) const;
695 
708  Q_DECL_DEPRECATED QString toProj4() const SIP_DEPRECATED;
709 
722  QString toProj() const;
723 
728  bool isGeographic() const;
729 
734  bool hasAxisInverted() const;
735 
739  QgsUnitTypes::DistanceUnit mapUnits() const;
740 
749  QgsRectangle bounds() const;
750 
751  // Mutators -----------------------------------
752 
756  void setValidationHint( const QString &html );
757 
761  QString validationHint();
762 
769  static int syncDatabase();
770 
782  long saveAsUserCrs( const QString &name, Format nativeFormat = FormatWkt );
783 
785  QString geographicCrsAuthId() const;
786 
787 #ifdef SIP_RUN
788  SIP_PYOBJECT __repr__();
789  % MethodCode
790  QString str = QStringLiteral( "<QgsCoordinateReferenceSystem: %1>" ).arg( sipCpp->authid() );
791  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
792  % End
793 #endif
794 
795 #ifndef SIP_RUN
796 #if PROJ_VERSION_MAJOR>=6
797 
807  PJ *projObject() const;
808 #endif
809 #endif
810 
816  Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
817 
822  static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems();
823 
828  static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs );
829 
830 #ifndef SIP_RUN
831 
842  static void invalidateCache( bool disableCache = false );
843 #else
844 
852  static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
853 #endif
854 
855  // Mutators -----------------------------------
856  // We don't want to expose these to the public api since they won't create
857  // a fully valid crs. Programmers should use the createFrom* methods rather
858  private:
859 
865  static QString projFromSrsId( int srsId );
866 
872  void setProjString( const QString &projString );
873 
877  bool setWktString( const QString &wkt, bool allowProjFallback = true );
878 
882  void debugPrint();
883 
885  typedef QMap<QString, QString> RecordMap;
886 
894  RecordMap getRecord( const QString &sql );
895 
900  static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
901 
903  void setMapUnits();
904 
906  long getRecordCount();
907 
908 #if PROJ_VERSION_MAJOR>=6
909  bool loadFromAuthCode( const QString &auth, const QString &code );
910 
914  static QList< long > userSrsIds();
915 
923  long matchToUserCrs() const;
924 #endif
925 
930  bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
931 
932 #if PROJ_VERSION_MAJOR<6 // not used for proj >= 6.0
933  static bool loadIds( QHash<int, QString> &wkts );
934  static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
935 
937  static bool syncDatumTransform( const QString &dbPath );
938 #endif
939 
940  QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
941 
942  QString mValidationHint;
943 
944 #if PROJ_VERSION_MAJOR>=6
945  friend class QgsProjContext;
946 
947  // Only meant to be called by QgsProjContext::~QgsProjContext()
948  static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
949 #endif
950 
952  static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
953 
954  // cache
955 
956  static bool sDisableSrIdCache;
957  static bool sDisableOgcCache;
958  static bool sDisableProjCache;
959  static bool sDisableWktCache;
960  static bool sDisableSrsIdCache;
961  static bool sDisableStringCache;
962 
963  // for tests
964  static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
965  static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
966  static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
967  static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
968  static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
969  static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
970 
971  friend class TestQgsCoordinateReferenceSystem;
972  friend class QgsPostgresProvider;
973 
974  bool createFromPostgisSrid( const long id );
975 };
976 
978 
979 #ifndef SIP_RUN
981 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
982 {
983  QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
984  mySummary += QLatin1String( "\n\t\tDescription : " );
985  if ( !r.description().isNull() )
986  {
987  mySummary += r.description();
988  }
989  else
990  {
991  mySummary += QLatin1String( "Undefined" );
992  }
993  mySummary += QLatin1String( "\n\t\tProjection : " );
994  if ( !r.projectionAcronym().isNull() )
995  {
996  mySummary += r.projectionAcronym();
997  }
998  else
999  {
1000  mySummary += QLatin1String( "Undefined" );
1001  }
1002 
1003  mySummary += QLatin1String( "\n\t\tEllipsoid : " );
1004  if ( !r.ellipsoidAcronym().isNull() )
1005  {
1006  mySummary += r.ellipsoidAcronym();
1007  }
1008  else
1009  {
1010  mySummary += QLatin1String( "Undefined" );
1011  }
1012 
1013  mySummary += QLatin1String( "\n\t\tProjString : " );
1014  if ( !r.toProj().isNull() )
1015  {
1016  mySummary += r.toProj();
1017  }
1018  else
1019  {
1020  mySummary += QLatin1String( "Undefined" );
1021  }
1022  // Using streams we need to use local 8 Bit
1023  return os << mySummary.toLocal8Bit().data() << std::endl;
1024 }
1025 #endif
1026 
1027 #endif // QGSCOORDINATEREFERENCESYSTEM_H
QgsCoordinateReferenceSystem::InternalCrsId
@ InternalCrsId
Internal ID used by QGIS in the local SQLite database.
Definition: qgscoordinatereferencesystem.h:218
QgsCoordinateReferenceSystem::description
QString description() const
Returns the descriptive name of the CRS, e.g., "WGS 84" or "GDA 94 / Vicgrid94".
Definition: qgscoordinatereferencesystem.cpp:1304
QgsCoordinateReferenceSystem::FormatProj
@ FormatProj
Proj string format.
Definition: qgscoordinatereferencesystem.h:227
qgsrectangle.h
QgsCoordinateReferenceSystem::projectionAcronym
QString projectionAcronym() const
Returns the projection acronym for the projection used by the CRS.
Definition: qgscoordinatereferencesystem.cpp:1339
sqlite3
struct sqlite3 sqlite3
Definition: qgscoordinatereferencesystem.h:55
QgsCoordinateReferenceSystem::CrsType
CrsType
Enumeration of types of IDs accepted in createFromId() method.
Definition: qgscoordinatereferencesystem.h:216
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsCoordinateReferenceSystem::WKT2_2018_SIMPLIFIED
@ WKT2_2018_SIMPLIFIED
Alias for WKT2_2019_SIMPLIFIED.
Definition: qgscoordinatereferencesystem.h:674
qgsunittypes.h
SIP_PYARGREMOVE
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:146
QgsUnitTypes::DistanceUnit
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:67
QgsCoordinateReferenceSystem::WKT1_ESRI
@ WKT1_ESRI
WKT1 as traditionally output by ESRI software, deriving from OGC 99-049.
Definition: qgscoordinatereferencesystem.h:670
QgsRectangle
Definition: qgsrectangle.h:41
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
OGRSpatialReferenceH
void * OGRSpatialReferenceH
Definition: qgscoordinatereferencesystem.h:60
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsCoordinateReferenceSystem::WktVariant
WktVariant
WKT formatting variants, only used for builds based on Proj >= 6.
Definition: qgscoordinatereferencesystem.h:667
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
CUSTOM_CRS_VALIDATION
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
Definition: qgscoordinatereferencesystem.h:64
qgis_sip.h
QgsUnitTypes
Helper functions for various unit types.
Definition: qgsunittypes.h:38
QgsCoordinateReferenceSystem::FullString
@ FullString
Full definition – possibly a very lengthy string, e.g. with no truncation of custom WKT definitions.
Definition: qgscoordinatereferencesystem.h:632
QgsCoordinateReferenceSystem::toProj
QString toProj() const
Returns a Proj string representation of this CRS.
Definition: qgscoordinatereferencesystem.cpp:1398
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsCoordinateReferenceSystem::WKT2_2015_SIMPLIFIED
@ WKT2_2015_SIMPLIFIED
Same as WKT2_2015 with the following exceptions: UNIT keyword used. ID node only on top element....
Definition: qgscoordinatereferencesystem.h:672
QgsCoordinateReferenceSystem::ShortString
@ ShortString
A heavily abbreviated string, for use when a compact representation is required.
Definition: qgscoordinatereferencesystem.h:630
QgsCoordinateReferenceSystem::WKT1_GDAL
@ WKT1_GDAL
WKT1 as traditionally output by GDAL, deriving from OGC 01-009. A notable departure from WKT1_GDAL wi...
Definition: qgscoordinatereferencesystem.h:669
PJ_CONTEXT
void PJ_CONTEXT
Definition: qgsprojutils.h:151
qgssqliteutils.h
QgsCoordinateReferenceSystem::ellipsoidAcronym
QString ellipsoidAcronym() const
Returns the ellipsoid acronym for the ellipsoid used by the CRS.
Definition: qgscoordinatereferencesystem.cpp:1351
QgsCoordinateReferenceSystem::Format
Format
Projection definition formats.
Definition: qgscoordinatereferencesystem.h:224
sqlite3_database_unique_ptr
Definition: qgssqliteutils.h:118
operator<<
std::ostream & operator<<(std::ostream &os, const QgsCoordinateReferenceSystem &r)
Output stream operator.
Definition: qgscoordinatereferencesystem.h:981
QgsCoordinateReferenceSystem::WKT2_2018
@ WKT2_2018
Alias for WKT2_2019.
Definition: qgscoordinatereferencesystem.h:673
QgsCoordinateReferenceSystem::MediumString
@ MediumString
A medium-length string, recommended for general purpose use.
Definition: qgscoordinatereferencesystem.h:631
QgsCoordinateReferenceSystem::WKT2_2015
@ WKT2_2015
Full WKT2 string, conforming to ISO 19162:2015(E) / OGC 12-063r5 with all possible nodes and new keyw...
Definition: qgscoordinatereferencesystem.h:671
QgsProjContext
Definition: qgsprojutils.h:161
QgsCoordinateReferenceSystem::PostgisCrsId
@ PostgisCrsId
SRID used in PostGIS. DEPRECATED – DO NOT USE.
Definition: qgscoordinatereferencesystem.h:219
QgsCoordinateReferenceSystem::IdentifierType
IdentifierType
Type of identifier string to create.
Definition: qgscoordinatereferencesystem.h:628