QGIS API Documentation  3.5.0-Master (dabd649)
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 // forward declaration for sqlite3
44 typedef struct sqlite3 sqlite3 SIP_SKIP;
45 
46 #ifdef DEBUG
47 typedef struct OGRSpatialReferenceHS *OGRSpatialReferenceH SIP_SKIP;
48 #else
50 #endif
51 
54 
192 {
193  Q_GADGET
194 
195  Q_PROPERTY( QgsUnitTypes::DistanceUnit mapUnits READ mapUnits )
196  Q_PROPERTY( bool isGeographic READ isGeographic )
197 
198  public:
199 
201  enum CrsType
202  {
205  EpsgCrsId
206  };
207 
210 
212 
213  // TODO QGIS 4: remove "POSTGIS" and "INTERNAL", allow PROJ4 without the prefix
214 
229  explicit QgsCoordinateReferenceSystem( const QString &definition );
230 
231  // TODO QGIS 4: remove type and always use EPSG code
232 
240  explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId );
241 
244 
247 
249  operator QVariant() const
250  {
251  return QVariant::fromValue( *this );
252  }
253 
261  static QList< long > validSrsIds();
262 
263  // static creators
264 
272  static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
273 
280  Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
281 
289  static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 );
290 
298  static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
299 
308  static QgsCoordinateReferenceSystem fromSrsId( long srsId );
309 
310  // Misc helper functions -----------------------
311 
312  // TODO QGIS 4: remove type and always use EPSG code, rename to createFromEpsg
313 
320  bool createFromId( long id, CrsType type = PostgisCrsId );
321 
322  // TODO QGIS 4: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
323 
334  bool createFromOgcWmsCrs( const QString &crs );
335 
336  // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
337 
343  bool createFromSrid( long srid );
344 
358  bool createFromWkt( const QString &wkt );
359 
370  bool createFromSrsId( long srsId );
371 
395  bool createFromProj4( const QString &projString );
396 
411  bool createFromString( const QString &definition );
412  // TODO QGIS3: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
428  bool createFromUserInput( const QString &definition );
429 
439  static void setupESRIWktFix();
440 
442  bool isValid() const;
443 
455  void validate();
456 
457  // TODO QGIS 4: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
458 
466  long findMatchingProj();
467 
473  bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
474 
480  bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
481 
488  bool readXml( const QDomNode &node );
489 
496  bool writeXml( QDomNode &node, QDomDocument &doc ) const;
497 
498 
503  static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
504 
509  static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
510 
511  // Accessors -----------------------------------
512 
517  long srsid() const;
518 
519  // TODO QGIS 4: remove unless really necessary - let's use EPSG codes instead
520 
525  long postgisSrid() const;
526 
539  QString authid() const;
540 
549  QString description() const;
550 
557  QString projectionAcronym() const;
558 
565  QString ellipsoidAcronym() const;
566 
572  QString toWkt() const;
573 
584  QString toProj4() const;
585 
590  bool isGeographic() const;
591 
596  bool hasAxisInverted() const;
597 
601  QgsUnitTypes::DistanceUnit mapUnits() const;
602 
611  QgsRectangle bounds() const;
612 
613  // Mutators -----------------------------------
614 
618  void setValidationHint( const QString &html );
619 
623  QString validationHint();
624 
631  static int syncDatabase();
632 
633 
639  long saveAsUserCrs( const QString &name );
640 
642  QString geographicCrsAuthId() const;
643 
649  static QStringList recentProjections();
650 
657  static void invalidateCache();
658 
659  // Mutators -----------------------------------
660  // We don't want to expose these to the public api since they won't create
661  // a fully valid crs. Programmers should use the createFrom* methods rather
662  private:
663 
669  static QString proj4FromSrsId( int srsId );
670 
675  void setInternalId( long srsId );
676 
681  void setSrid( long srid );
682 
687  void setDescription( const QString &description );
688 
700  void setProj4String( const QString &proj4String );
701 
706  void setGeographicFlag( bool geoFlag );
707 
712  void setEpsg( long epsg );
713 
718  void setAuthId( const QString &theID );
719 
724  void setProjectionAcronym( const QString &projectionAcronym );
725 
730  void setEllipsoidAcronym( const QString &ellipsoidAcronym );
731 
735  void debugPrint();
736 
738  typedef QMap<QString, QString> RecordMap;
739 
747  RecordMap getRecord( const QString &sql );
748 
753  static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
754 
756  void setMapUnits();
757 
759  long getRecordCount();
760 
765  bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
766 
767  static bool loadIds( QHash<int, QString> &wkts );
768  static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
770  static bool syncDatumTransform( const QString &dbPath );
771 
772  QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
773 
775  static CUSTOM_CRS_VALIDATION mCustomSrsValidation;
776 
777 
778  // cache
779 
780  static QReadWriteLock sSrIdCacheLock;
781  static QHash< long, QgsCoordinateReferenceSystem > sSrIdCache;
782  static QReadWriteLock sOgcLock;
783  static QHash< QString, QgsCoordinateReferenceSystem > sOgcCache;
784  static QReadWriteLock sProj4CacheLock;
785  static QHash< QString, QgsCoordinateReferenceSystem > sProj4Cache;
786  static QReadWriteLock sCRSWktLock;
787  static QHash< QString, QgsCoordinateReferenceSystem > sWktCache;
788  static QReadWriteLock sCRSSrsIdLock;
789  static QHash< long, QgsCoordinateReferenceSystem > sSrsIdCache;
790  static QReadWriteLock sCrsStringLock;
791  static QHash< QString, QgsCoordinateReferenceSystem > sStringCache;
792 
793  friend class TestQgsCoordinateReferenceSystem;
794 };
795 
797 
798 #ifndef SIP_RUN
800 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
801 {
802  QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
803  mySummary += QLatin1String( "\n\t\tDescription : " );
804  if ( !r.description().isNull() )
805  {
806  mySummary += r.description();
807  }
808  else
809  {
810  mySummary += QLatin1String( "Undefined" );
811  }
812  mySummary += QLatin1String( "\n\t\tProjection : " );
813  if ( !r.projectionAcronym().isNull() )
814  {
815  mySummary += r.projectionAcronym();
816  }
817  else
818  {
819  mySummary += QLatin1String( "Undefined" );
820  }
821 
822  mySummary += QLatin1String( "\n\t\tEllipsoid : " );
823  if ( !r.ellipsoidAcronym().isNull() )
824  {
825  mySummary += r.ellipsoidAcronym();
826  }
827  else
828  {
829  mySummary += QLatin1String( "Undefined" );
830  }
831 
832  mySummary += QLatin1String( "\n\t\tProj4String : " );
833  if ( !r.toProj4().isNull() )
834  {
835  mySummary += r.toProj4();
836  }
837  else
838  {
839  mySummary += QLatin1String( "Undefined" );
840  }
841  // Using streams we need to use local 8 Bit
842  return os << mySummary.toLocal8Bit().data() << std::endl;
843 }
844 #endif
845 
846 #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
Q_DECLARE_METATYPE(QModelIndex)
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".
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