QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 #endif
48 #endif
49 
50 // forward declaration for sqlite3
51 typedef struct sqlite3 sqlite3 SIP_SKIP;
52 
53 #ifdef DEBUG
54 typedef struct OGRSpatialReferenceHS *OGRSpatialReferenceH SIP_SKIP;
55 #else
57 #endif
58 
61 
199 {
200  Q_GADGET
201 
202  Q_PROPERTY( QgsUnitTypes::DistanceUnit mapUnits READ mapUnits )
203  Q_PROPERTY( bool isGeographic READ isGeographic )
204 
205  public:
206 
208  enum CrsType
209  {
212  EpsgCrsId
213  };
214 
217 
219 
220  // TODO QGIS 4: remove "POSTGIS" and "INTERNAL", allow PROJ4 without the prefix
221 
236  explicit QgsCoordinateReferenceSystem( const QString &definition );
237 
238  // TODO QGIS 4: remove type and always use EPSG code
239 
247  explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId );
248 
251 
254 
256  operator QVariant() const
257  {
258  return QVariant::fromValue( *this );
259  }
260 
268  static QList< long > validSrsIds();
269 
270  // static creators
271 
279  static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
280 
287  Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
288 
296  static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 );
297 
305  static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
306 
315  static QgsCoordinateReferenceSystem fromSrsId( long srsId );
316 
317  // Misc helper functions -----------------------
318 
319  // TODO QGIS 4: remove type and always use EPSG code, rename to createFromEpsg
320 
327  bool createFromId( long id, CrsType type = PostgisCrsId );
328 
329  // TODO QGIS 4: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
330 
341  bool createFromOgcWmsCrs( const QString &crs );
342 
343  // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
344 
350  bool createFromSrid( long srid );
351 
365  bool createFromWkt( const QString &wkt );
366 
377  bool createFromSrsId( long srsId );
378 
402  bool createFromProj4( const QString &projString );
403 
418  bool createFromString( const QString &definition );
419  // TODO QGIS3: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
435  bool createFromUserInput( const QString &definition );
436 
446  static void setupESRIWktFix();
447 
449  bool isValid() const;
450 
462  void validate();
463 
464  // TODO QGIS 4: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
465 
473  long findMatchingProj();
474 
480  bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
481 
487  bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
488 
495  bool readXml( const QDomNode &node );
496 
503  bool writeXml( QDomNode &node, QDomDocument &doc ) const;
504 
505 
510  static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
511 
516  static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
517 
518  // Accessors -----------------------------------
519 
524  long srsid() const;
525 
526  // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
527 
532  long postgisSrid() const;
533 
546  QString authid() const;
547 
556  QString description() const;
557 
564  QString projectionAcronym() const;
565 
572  QString ellipsoidAcronym() const;
573 
579  QString toWkt() const;
580 
591  QString toProj4() const;
592 
597  bool isGeographic() const;
598 
603  bool hasAxisInverted() const;
604 
608  QgsUnitTypes::DistanceUnit mapUnits() const;
609 
618  QgsRectangle bounds() const;
619 
620  // Mutators -----------------------------------
621 
625  void setValidationHint( const QString &html );
626 
630  QString validationHint();
631 
638  static int syncDatabase();
639 
640 
646  long saveAsUserCrs( const QString &name );
647 
649  QString geographicCrsAuthId() const;
650 
651 #ifndef SIP_RUN
652 #if PROJ_VERSION_MAJOR>=6
653 
663  PJ *projObject() const;
664 #endif
665 #endif
666 
672  static QStringList recentProjections();
673 
680  static void invalidateCache();
681 
682  // Mutators -----------------------------------
683  // We don't want to expose these to the public api since they won't create
684  // a fully valid crs. Programmers should use the createFrom* methods rather
685  private:
686 
692  static QString proj4FromSrsId( int srsId );
693 
698  void setInternalId( long srsId );
699 
704  void setSrid( long srid );
705 
710  void setDescription( const QString &description );
711 
723  void setProj4String( const QString &proj4String );
724 
729  void setGeographicFlag( bool geoFlag );
730 
735  void setEpsg( long epsg );
736 
741  void setAuthId( const QString &theID );
742 
747  void setProjectionAcronym( const QString &projectionAcronym );
748 
753  void setEllipsoidAcronym( const QString &ellipsoidAcronym );
754 
758  void debugPrint();
759 
761  typedef QMap<QString, QString> RecordMap;
762 
770  RecordMap getRecord( const QString &sql );
771 
776  static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
777 
779  void setMapUnits();
780 
782  long getRecordCount();
783 
784 #if PROJ_VERSION_MAJOR>=6
785  bool loadFromAuthCode( const QString &auth, const QString &code );
786 #endif
787 
792  bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
793 
794 #if PROJ_VERSION_MAJOR<6 // not used for proj >= 6.0
795  static bool loadIds( QHash<int, QString> &wkts );
796  static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
797 
799  static bool syncDatumTransform( const QString &dbPath );
800 #endif
801 
802  QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
803 
805  static CUSTOM_CRS_VALIDATION mCustomSrsValidation;
806 
807 
808  // cache
809 
810  static QReadWriteLock sSrIdCacheLock;
811  static QHash< long, QgsCoordinateReferenceSystem > sSrIdCache;
812  static QReadWriteLock sOgcLock;
813  static QHash< QString, QgsCoordinateReferenceSystem > sOgcCache;
814  static QReadWriteLock sProj4CacheLock;
815  static QHash< QString, QgsCoordinateReferenceSystem > sProj4Cache;
816  static QReadWriteLock sCRSWktLock;
817  static QHash< QString, QgsCoordinateReferenceSystem > sWktCache;
818  static QReadWriteLock sCRSSrsIdLock;
819  static QHash< long, QgsCoordinateReferenceSystem > sSrsIdCache;
820  static QReadWriteLock sCrsStringLock;
821  static QHash< QString, QgsCoordinateReferenceSystem > sStringCache;
822 
823  friend class TestQgsCoordinateReferenceSystem;
824 };
825 
827 
828 #ifndef SIP_RUN
830 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
831 {
832  QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
833  mySummary += QLatin1String( "\n\t\tDescription : " );
834  if ( !r.description().isNull() )
835  {
836  mySummary += r.description();
837  }
838  else
839  {
840  mySummary += QLatin1String( "Undefined" );
841  }
842  mySummary += QLatin1String( "\n\t\tProjection : " );
843  if ( !r.projectionAcronym().isNull() )
844  {
845  mySummary += r.projectionAcronym();
846  }
847  else
848  {
849  mySummary += QLatin1String( "Undefined" );
850  }
851 
852  mySummary += QLatin1String( "\n\t\tEllipsoid : " );
853  if ( !r.ellipsoidAcronym().isNull() )
854  {
855  mySummary += r.ellipsoidAcronym();
856  }
857  else
858  {
859  mySummary += QLatin1String( "Undefined" );
860  }
861 
862  mySummary += QLatin1String( "\n\t\tProj4String : " );
863  if ( !r.toProj4().isNull() )
864  {
865  mySummary += r.toProj4();
866  }
867  else
868  {
869  mySummary += QLatin1String( "Undefined" );
870  }
871  // Using streams we need to use local 8 Bit
872  return os << mySummary.toLocal8Bit().data() << std::endl;
873 }
874 #endif
875 
876 #endif // QGSCOORDINATEREFERENCESYSTEM_H
A rectangle specified with double values.
Definition: qgsrectangle.h:41
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QString toProj4() const
Returns a Proj4 string representation of this CRS.
const QgsCoordinateReferenceSystem & crs
Internal ID used by QGIS in the local SQLite database.
QString ellipsoidAcronym() const
Returns the ellipsoid acronym for the ellipsoid used by the CRS.
#define SIP_SKIP
Definition: qgis_sip.h:119
QString description() const
Returns the descriptive name of the CRS, e.g., "WGS 84" or "GDA 94 / Vicgrid94".
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
std::ostream & operator<<(std::ostream &os, const QgsCoordinateReferenceSystem &r)
Output stream operator.
CrsType
Enumeration of types of IDs accepted in createFromId() method.
struct sqlite3 sqlite3
QString projectionAcronym() const
Returns the projection acronym for the projection used by the CRS.
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:54
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
This class represents a coordinate reference system (CRS).
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
void * OGRSpatialReferenceH