QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 #if PROJ_VERSION_MAJOR>=8
49 struct pj_ctx;
50 typedef struct pj_ctx PJ_CONTEXT;
51 #else
52 struct projCtx_t;
53 typedef struct projCtx_t PJ_CONTEXT;
54 #endif
55 #endif
56 #endif
57 
58 // forward declaration for sqlite3
59 typedef struct sqlite3 sqlite3 SIP_SKIP;
60 
61 #ifdef DEBUG
62 typedef struct OGRSpatialReferenceHS *OGRSpatialReferenceH SIP_SKIP;
63 #else
65 #endif
66 
69 
210 {
211  Q_GADGET
212 
213  Q_PROPERTY( QgsUnitTypes::DistanceUnit mapUnits READ mapUnits )
214  Q_PROPERTY( bool isGeographic READ isGeographic )
215 
216  public:
217 
219  enum CrsType
220  {
223  EpsgCrsId
224  };
225 
227  enum Format
228  {
229  FormatWkt = 0,
231  };
232 
235 
237 
238  // TODO QGIS 4: remove "POSTGIS" and "INTERNAL"
239 
255  explicit QgsCoordinateReferenceSystem( const QString &definition );
256 
257  // TODO QGIS 4: remove type and always use EPSG code
258 
270  Q_DECL_DEPRECATED explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
271 
274 
277 
279  operator QVariant() const
280  {
281  return QVariant::fromValue( *this );
282  }
283 
291  static QList< long > validSrsIds();
292 
293  // static creators
294 
302  static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
303 
310  Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
311 
318  Q_DECL_DEPRECATED static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 ) SIP_DEPRECATED;
319 
327  static QgsCoordinateReferenceSystem fromProj( const QString &proj );
328 
336  static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
337 
346  static QgsCoordinateReferenceSystem fromSrsId( long srsId );
347 
348  // Misc helper functions -----------------------
349 
350  // TODO QGIS 4: remove type and always use EPSG code, rename to createFromEpsg
351 
357  Q_DECL_DEPRECATED bool createFromId( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
358 
359  // TODO QGIS 4: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
360 
371  bool createFromOgcWmsCrs( const QString &crs );
372 
373  // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
374 
382  Q_DECL_DEPRECATED bool createFromSrid( long srid ) SIP_DEPRECATED;
383 
396  bool createFromWkt( const QString &wkt );
397 
410  bool createFromSrsId( long srsId );
411 
435  Q_DECL_DEPRECATED bool createFromProj4( const QString &projString ) SIP_DEPRECATED;
436 
464 #ifndef SIP_RUN
465  bool createFromProj( const QString &projString, bool identify = true );
466 #else
467  bool createFromProj( const QString &projString );
468 #endif
469 
485  bool createFromString( const QString &definition );
486 
487  // TODO QGIS 4: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
488 
504  bool createFromUserInput( const QString &definition );
505 
516  Q_DECL_DEPRECATED static void setupESRIWktFix() SIP_DEPRECATED;
517 
519  bool isValid() const;
520 
532  void validate();
533 
534  // TODO QGIS 4: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
535 
544  Q_DECL_DEPRECATED long findMatchingProj() SIP_DEPRECATED;
545 
551  bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
552 
558  bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
559 
566  bool readXml( const QDomNode &node );
567 
574  bool writeXml( QDomNode &node, QDomDocument &doc ) const;
575 
576 
581  static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
582 
587  static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
588 
589  // Accessors -----------------------------------
590 
595  long srsid() const;
596 
597  // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
598 
603  long postgisSrid() const;
604 
617  QString authid() const;
618 
626  QString description() const;
627 
634  {
638  };
639 
653  QString userFriendlyIdentifier( IdentifierType type = MediumString ) const;
654 
661  QString projectionAcronym() const;
662 
669  QString ellipsoidAcronym() const;
670 
673  {
680  WKT2_2019 = WKT2_2018,
681  WKT2_2019_SIMPLIFIED = WKT2_2018_SIMPLIFIED,
682 
683  WKT_PREFERRED = WKT2_2019,
684  WKT_PREFERRED_SIMPLIFIED = WKT2_2019_SIMPLIFIED,
685  WKT_PREFERRED_GDAL = WKT2_2019,
686  };
687 
699  QString toWkt( WktVariant variant = WKT1_GDAL, bool multiline = false, int indentationWidth = 4 ) const;
700 
713  Q_DECL_DEPRECATED QString toProj4() const SIP_DEPRECATED;
714 
727  QString toProj() const;
728 
733  bool isGeographic() const;
734 
739  bool hasAxisInverted() const;
740 
744  QgsUnitTypes::DistanceUnit mapUnits() const;
745 
754  QgsRectangle bounds() const;
755 
756  // Mutators -----------------------------------
757 
774  void updateDefinition();
775 
779  void setValidationHint( const QString &html );
780 
784  QString validationHint();
785 
792  static int syncDatabase();
793 
807  long saveAsUserCrs( const QString &name, Format nativeFormat = FormatWkt );
808 
810  QString geographicCrsAuthId() const;
811 
812 #ifdef SIP_RUN
813  SIP_PYOBJECT __repr__();
814  % MethodCode
815  const QString str = sipCpp->isValid() ? QStringLiteral( "<QgsCoordinateReferenceSystem: %1>" ).arg( !sipCpp->authid().isEmpty() ? sipCpp->authid() : sipCpp->toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) )
816  : QStringLiteral( "<QgsCoordinateReferenceSystem: invalid>" );
817  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
818  % End
819 #endif
820 
821 #ifndef SIP_RUN
822 #if PROJ_VERSION_MAJOR>=6
823 
833  PJ *projObject() const;
834 #endif
835 #endif
836 
842  Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
843 
848  static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems();
849 
854  static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs );
855 
856 #ifndef SIP_RUN
857 
868  static void invalidateCache( bool disableCache = false );
869 #else
870 
878  static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
879 #endif
880 
881  // Mutators -----------------------------------
882  // We don't want to expose these to the public api since they won't create
883  // a fully valid crs. Programmers should use the createFrom* methods rather
884  private:
885 
891  static QString projFromSrsId( int srsId );
892 
898  void setProjString( const QString &projString );
899 
903  bool setWktString( const QString &wkt, bool allowProjFallback = true );
904 
908  void debugPrint();
909 
911  typedef QMap<QString, QString> RecordMap;
912 
920  RecordMap getRecord( const QString &sql );
921 
926  static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
927 
929  void setMapUnits();
930 
932  static long getRecordCount();
933 
934 #if PROJ_VERSION_MAJOR>=6
935  bool loadFromAuthCode( const QString &auth, const QString &code );
936 
940  static QList< long > userSrsIds();
941 
949  long matchToUserCrs() const;
950 #endif
951 
956  bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
957 
958  bool createFromWktInternal( const QString &wkt, const QString &description );
959 
960 #if PROJ_VERSION_MAJOR<6 // not used for proj >= 6.0
961  static bool loadIds( QHash<int, QString> &wkts );
962  static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
963 
965  static bool syncDatumTransform( const QString &dbPath );
966 #endif
967 
968  QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
969 
970  QString mValidationHint;
971 
972 #if PROJ_VERSION_MAJOR>=6
973  friend class QgsProjContext;
974 
975  // Only meant to be called by QgsProjContext::~QgsProjContext()
976  static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
977 #endif
978 
980  static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
981 
982  // cache
983 
984  static bool sDisableSrIdCache;
985  static bool sDisableOgcCache;
986  static bool sDisableProjCache;
987  static bool sDisableWktCache;
988  static bool sDisableSrsIdCache;
989  static bool sDisableStringCache;
990 
991  // for tests
992  static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
993  static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
994  static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
995  static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
996  static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
997  static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
998 
999  friend class TestQgsCoordinateReferenceSystem;
1000  friend class QgsPostgresProvider;
1002 
1003  bool createFromPostgisSrid( const long id );
1004 };
1005 
1007 
1008 #ifndef SIP_RUN
1010 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
1011 {
1012  QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
1013  mySummary += QLatin1String( "\n\t\tDescription : " );
1014  if ( !r.description().isNull() )
1015  {
1016  mySummary += r.description();
1017  }
1018  else
1019  {
1020  mySummary += QLatin1String( "Undefined" );
1021  }
1022  mySummary += QLatin1String( "\n\t\tProjection : " );
1023  if ( !r.projectionAcronym().isNull() )
1024  {
1025  mySummary += r.projectionAcronym();
1026  }
1027  else
1028  {
1029  mySummary += QLatin1String( "Undefined" );
1030  }
1031 
1032  mySummary += QLatin1String( "\n\t\tEllipsoid : " );
1033  if ( !r.ellipsoidAcronym().isNull() )
1034  {
1035  mySummary += r.ellipsoidAcronym();
1036  }
1037  else
1038  {
1039  mySummary += QLatin1String( "Undefined" );
1040  }
1041 
1042  mySummary += QLatin1String( "\n\t\tProjString : " );
1043  if ( !r.toProj().isNull() )
1044  {
1045  mySummary += r.toProj();
1046  }
1047  else
1048  {
1049  mySummary += QLatin1String( "Undefined" );
1050  }
1051  // Using streams we need to use local 8 Bit
1052  return os << mySummary.toLocal8Bit().data() << std::endl;
1053 }
1054 #endif
1055 
1056 #endif // QGSCOORDINATEREFERENCESYSTEM_H
A registry for known coordinate reference system (CRS) definitions, including any user-defined CRSes.
This class 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 description() const
Returns the descriptive name of the CRS, e.g., "WGS 84" or "GDA 94 / Vicgrid94".
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.
IdentifierType
Type of identifier string to create.
@ MediumString
A medium-length string, recommended for general purpose use.
@ FullString
Full definition – possibly a very lengthy string, e.g. with no truncation of custom WKT definitions.
@ ShortString
A heavily abbreviated string, for use when a compact representation is required.
Format
Projection definition formats.
WktVariant
WKT formatting variants, only used for builds based on Proj >= 6.
@ WKT1_GDAL
WKT1 as traditionally output by GDAL, deriving from OGC 01-009. A notable departure from WKT1_GDAL wi...
@ WKT2_2018_SIMPLIFIED
Alias for WKT2_2019_SIMPLIFIED.
@ WKT_PREFERRED
Preferred format, matching the most recent WKT ISO standard. Currently an alias to WKT2_2019,...
@ WKT2_2015
Full WKT2 string, conforming to ISO 19162:2015(E) / OGC 12-063r5 with all possible nodes and new keyw...
@ WKT1_ESRI
WKT1 as traditionally output by ESRI software, deriving from OGC 99-049.
@ WKT2_2015_SIMPLIFIED
Same as WKT2_2015 with the following exceptions: UNIT keyword used. ID node only on top element....
Used to create and store a proj context object, correctly freeing the context upon destruction.
Definition: qgsprojutils.h:162
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Helper functions for various unit types.
Definition: qgsunittypes.h:39
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:68
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:146
struct sqlite3 sqlite3
void * OGRSpatialReferenceH
std::ostream & operator<<(std::ostream &os, const QgsCoordinateReferenceSystem &r)
Output stream operator.
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
void PJ_CONTEXT
Definition: qgsprojutils.h:151
const QgsCoordinateReferenceSystem & crs