QGIS API Documentation  3.12.1-București (121cc00ff0)
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 
203 {
204  Q_GADGET
205 
206  Q_PROPERTY( QgsUnitTypes::DistanceUnit mapUnits READ mapUnits )
207  Q_PROPERTY( bool isGeographic READ isGeographic )
208 
209  public:
210 
212  enum CrsType
213  {
216  EpsgCrsId
217  };
218 
220  enum Format
221  {
222  FormatWkt = 0,
224  };
225 
228 
230 
231  // TODO QGIS 4: remove "POSTGIS" and "INTERNAL"
232 
247  explicit QgsCoordinateReferenceSystem( const QString &definition );
248 
249  // TODO QGIS 4: remove type and always use EPSG code
250 
260  Q_DECL_DEPRECATED explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
261 
264 
267 
269  operator QVariant() const
270  {
271  return QVariant::fromValue( *this );
272  }
273 
281  static QList< long > validSrsIds();
282 
283  // static creators
284 
292  static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
293 
300  Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
301 
308  Q_DECL_DEPRECATED static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 ) SIP_DEPRECATED;
309 
317  static QgsCoordinateReferenceSystem fromProj( const QString &proj );
318 
326  static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
327 
336  static QgsCoordinateReferenceSystem fromSrsId( long srsId );
337 
338  // Misc helper functions -----------------------
339 
340  // TODO QGIS 4: remove type and always use EPSG code, rename to createFromEpsg
341 
347  Q_DECL_DEPRECATED bool createFromId( long id, CrsType type = PostgisCrsId ) SIP_DEPRECATED;
348 
349  // TODO QGIS 4: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
350 
361  bool createFromOgcWmsCrs( const QString &crs );
362 
363  // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
364 
372  Q_DECL_DEPRECATED bool createFromSrid( long srid ) SIP_DEPRECATED;
373 
386  bool createFromWkt( const QString &wkt );
387 
400  bool createFromSrsId( long srsId );
401 
427  Q_DECL_DEPRECATED bool createFromProj4( const QString &projString ) SIP_DEPRECATED;
428 
454  bool createFromProj( const QString &projString );
455 
470  bool createFromString( const QString &definition );
471  // TODO QGIS3: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
487  bool createFromUserInput( const QString &definition );
488 
499  Q_DECL_DEPRECATED static void setupESRIWktFix() SIP_DEPRECATED;
500 
502  bool isValid() const;
503 
515  void validate();
516 
517  // TODO QGIS 4: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
518 
527  Q_DECL_DEPRECATED long findMatchingProj() SIP_DEPRECATED;
528 
534  bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
535 
541  bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
542 
549  bool readXml( const QDomNode &node );
550 
557  bool writeXml( QDomNode &node, QDomDocument &doc ) const;
558 
559 
564  static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
565 
570  static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
571 
572  // Accessors -----------------------------------
573 
578  long srsid() const;
579 
580  // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
581 
586  long postgisSrid() const;
587 
600  QString authid() const;
601 
609  QString description() const;
610 
617  {
621  };
622 
636  QString userFriendlyIdentifier( IdentifierType type = MediumString ) const;
637 
644  QString projectionAcronym() const;
645 
652  QString ellipsoidAcronym() const;
653 
656  {
663  };
664 
676  QString toWkt( WktVariant variant = WKT1_GDAL, bool multiline = false, int indentationWidth = 4 ) const;
677 
690  Q_DECL_DEPRECATED QString toProj4() const SIP_DEPRECATED;
691 
704  QString toProj() const;
705 
710  bool isGeographic() const;
711 
716  bool hasAxisInverted() const;
717 
721  QgsUnitTypes::DistanceUnit mapUnits() const;
722 
731  QgsRectangle bounds() const;
732 
733  // Mutators -----------------------------------
734 
738  void setValidationHint( const QString &html );
739 
743  QString validationHint();
744 
751  static int syncDatabase();
752 
764  long saveAsUserCrs( const QString &name, Format nativeFormat = FormatWkt );
765 
767  QString geographicCrsAuthId() const;
768 
769 #ifdef SIP_RUN
770  SIP_PYOBJECT __repr__();
771  % MethodCode
772  QString str = QStringLiteral( "<QgsCoordinateReferenceSystem: %1>" ).arg( sipCpp->authid() );
773  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
774  % End
775 #endif
776 
777 #ifndef SIP_RUN
778 #if PROJ_VERSION_MAJOR>=6
779 
789  PJ *projObject() const;
790 #endif
791 #endif
792 
798  Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
799 
804  static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems();
805 
810  static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs );
811 
812 #ifndef SIP_RUN
813 
824  static void invalidateCache( bool disableCache = false );
825 #else
826 
834  static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
835 #endif
836 
837  // Mutators -----------------------------------
838  // We don't want to expose these to the public api since they won't create
839  // a fully valid crs. Programmers should use the createFrom* methods rather
840  private:
841 
847  static QString projFromSrsId( int srsId );
848 
854  void setProjString( const QString &projString );
855 
859  bool setWktString( const QString &wkt, bool allowProjFallback = true );
860 
864  void debugPrint();
865 
867  typedef QMap<QString, QString> RecordMap;
868 
876  RecordMap getRecord( const QString &sql );
877 
882  static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
883 
885  void setMapUnits();
886 
888  long getRecordCount();
889 
890 #if PROJ_VERSION_MAJOR>=6
891  bool loadFromAuthCode( const QString &auth, const QString &code );
892 
896  static QList< long > userSrsIds();
897 
905  long matchToUserCrs() const;
906 #endif
907 
912  bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
913 
914 #if PROJ_VERSION_MAJOR<6 // not used for proj >= 6.0
915  static bool loadIds( QHash<int, QString> &wkts );
916  static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
917 
919  static bool syncDatumTransform( const QString &dbPath );
920 #endif
921 
922  QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
923 
924  QString mValidationHint;
925 
926 #if PROJ_VERSION_MAJOR>=6
927  friend class QgsProjContext;
928 
929  // Only meant to be called by QgsProjContext::~QgsProjContext()
930  static void removeFromCacheObjectsBelongingToCurrentThread( PJ_CONTEXT *pj_context );
931 #endif
932 
934  static CUSTOM_CRS_VALIDATION sCustomSrsValidation;
935 
936  // cache
937 
938  static bool sDisableSrIdCache;
939  static bool sDisableOgcCache;
940  static bool sDisableProjCache;
941  static bool sDisableWktCache;
942  static bool sDisableSrsIdCache;
943  static bool sDisableStringCache;
944 
945  // for tests
946  static const QHash< QString, QgsCoordinateReferenceSystem > &stringCache();
947  static const QHash< QString, QgsCoordinateReferenceSystem > &projCache();
948  static const QHash< QString, QgsCoordinateReferenceSystem > &ogcCache();
949  static const QHash< QString, QgsCoordinateReferenceSystem > &wktCache();
950  static const QHash< long, QgsCoordinateReferenceSystem > &srsIdCache();
951  static const QHash< long, QgsCoordinateReferenceSystem > &srIdCache();
952 
953  friend class TestQgsCoordinateReferenceSystem;
954  friend class QgsPostgresProvider;
955 
956  bool createFromPostgisSrid( const long id );
957 };
958 
960 
961 #ifndef SIP_RUN
963 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
964 {
965  QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
966  mySummary += QLatin1String( "\n\t\tDescription : " );
967  if ( !r.description().isNull() )
968  {
969  mySummary += r.description();
970  }
971  else
972  {
973  mySummary += QLatin1String( "Undefined" );
974  }
975  mySummary += QLatin1String( "\n\t\tProjection : " );
976  if ( !r.projectionAcronym().isNull() )
977  {
978  mySummary += r.projectionAcronym();
979  }
980  else
981  {
982  mySummary += QLatin1String( "Undefined" );
983  }
984 
985  mySummary += QLatin1String( "\n\t\tEllipsoid : " );
986  if ( !r.ellipsoidAcronym().isNull() )
987  {
988  mySummary += r.ellipsoidAcronym();
989  }
990  else
991  {
992  mySummary += QLatin1String( "Undefined" );
993  }
994 
995  mySummary += QLatin1String( "\n\t\tProjString : " );
996  if ( !r.toProj().isNull() )
997  {
998  mySummary += r.toProj();
999  }
1000  else
1001  {
1002  mySummary += QLatin1String( "Undefined" );
1003  }
1004  // Using streams we need to use local 8 Bit
1005  return os << mySummary.toLocal8Bit().data() << std::endl;
1006 }
1007 #endif
1008 
1009 #endif // QGSCOORDINATEREFERENCESYSTEM_H
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QString toProj() const
Returns a Proj string representation of this CRS.
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Helper functions for various unit types.
Definition: qgsunittypes.h:38
Full WKT2 string, conforming to ISO 19162:2015(E) / OGC 12-063r5 with all possible nodes and new keyw...
SRID used in PostGIS. DEPRECATED – DO NOT USE.
const QgsCoordinateReferenceSystem & crs
Full definition – possibly a very lengthy string, e.g. with no truncation of custom WKT definitions...
WKT2_2018 with the simplification rule of WKT2_SIMPLIFIED.
Internal ID used by QGIS in the local SQLite database.
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:161
QString ellipsoidAcronym() const
Returns the ellipsoid acronym for the ellipsoid used by the CRS.
Format
Projection definition formats.
WKT1 as traditionally output by GDAL, deriving from OGC 01-009. A notable departure from WKT1_GDAL wi...
#define SIP_SKIP
Definition: qgis_sip.h:126
QString description() const
Returns the descriptive name of the CRS, e.g., "WGS 84" or "GDA 94 / Vicgrid94".
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
IdentifierType
Type of identifier string to create.
std::ostream & operator<<(std::ostream &os, const QgsCoordinateReferenceSystem &r)
Output stream operator.
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
CrsType
Enumeration of types of IDs accepted in createFromId() method.
struct sqlite3 sqlite3
Full WKT2 string, conforming to ISO 19162:2018 / OGC 18-010, with all possible nodes and new keyword ...
A medium-length string, recommended for general purpose use.
QString projectionAcronym() const
Returns the projection acronym for the projection used by the CRS.
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:66
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:146
void PJ_CONTEXT
Definition: qgsprojutils.h:151
This class represents a coordinate reference system (CRS).
A heavily abbreviated string, for use when a compact representation is required.
WktVariant
WKT formatting variants, only used for builds based on Proj >= 6.
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
void * OGRSpatialReferenceH
WKT1 as traditionally output by ESRI software, deriving from OGC 99-049.