QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
31 //qgis includes
32 #include "qgis.h"
33 #include "qgsunittypes.h"
34 #include "qgsrectangle.h"
35 #include "qgssqliteutils.h"
36 
37 class QDomNode;
38 class QDomDocument;
39 class QgsCoordinateReferenceSystemPrivate;
40 
41 // forward declaration for sqlite3
42 typedef struct sqlite3 sqlite3 SIP_SKIP;
43 
44 #ifdef DEBUG
45 typedef struct OGRSpatialReferenceHS *OGRSpatialReferenceH SIP_SKIP;
46 #else
48 #endif
49 
52 
190 {
191  Q_GADGET
192 
193  Q_PROPERTY( QgsUnitTypes::DistanceUnit mapUnits READ mapUnits )
194 
195  public:
196 
198  enum CrsType
199  {
202  EpsgCrsId
203  };
204 
207 
209  // TODO QGIS 3: remove "POSTGIS" and "INTERNAL", allow PROJ4 without the prefix
224  explicit QgsCoordinateReferenceSystem( const QString &definition );
225  // TODO QGIS 3: remove type and always use EPSG code
233  explicit QgsCoordinateReferenceSystem( long id, CrsType type = PostgisCrsId );
234 
237 
240 
242  operator QVariant() const
243  {
244  return QVariant::fromValue( *this );
245  }
246 
254  static QList< long > validSrsIds();
255 
256  // static creators
257 
265  static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
266 
273  Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
274 
282  static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 );
283 
291  static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
292 
301  static QgsCoordinateReferenceSystem fromSrsId( long srsId );
302 
303  // Misc helper functions -----------------------
304  // TODO QGIS 3: remove type and always use EPSG code, rename to createFromEpsg
311  bool createFromId( long id, CrsType type = PostgisCrsId );
312  // TODO QGIS 3: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
323  bool createFromOgcWmsCrs( const QString &crs );
324  // TODO QGIS 3: remove unless really necessary - let's use EPSG codes instead
330  bool createFromSrid( long srid );
331 
345  bool createFromWkt( const QString &wkt );
346 
357  bool createFromSrsId( long srsId );
358 
382  bool createFromProj4( const QString &projString );
383 
398  bool createFromString( const QString &definition );
399  // TODO QGIS3: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
415  bool createFromUserInput( const QString &definition );
416 
426  static void setupESRIWktFix();
427 
429  bool isValid() const;
430 
442  void validate();
443  // TODO QGIS 3: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
451  long findMatchingProj();
452 
458  bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
459 
465  bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
466 
473  bool readXml( const QDomNode &node );
474 
481  bool writeXml( QDomNode &node, QDomDocument &doc ) const;
482 
483 
488  static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
489 
494  static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
495 
496  // Accessors -----------------------------------
497 
502  long srsid() const;
503  // TODO QGIS 3: remove unless really necessary - let's use EPSG codes instead
508  long postgisSrid() const;
509 
522  QString authid() const;
523 
532  QString description() const;
533 
540  QString projectionAcronym() const;
541 
548  QString ellipsoidAcronym() const;
549 
555  QString toWkt() const;
556 
567  QString toProj4() const;
568 
573  bool isGeographic() const;
574 
579  bool hasAxisInverted() const;
580 
584  QgsUnitTypes::DistanceUnit mapUnits() const;
585 
594  QgsRectangle bounds() const;
595 
596  // Mutators -----------------------------------
597 
601  void setValidationHint( const QString &html );
602 
606  QString validationHint();
607 
614  static int syncDatabase();
615 
616 
622  long saveAsUserCrs( const QString &name );
623 
625  QString geographicCrsAuthId() const;
626 
632  static QStringList recentProjections();
633 
634 #ifndef SIP_RUN
635 
646  static void invalidateCache( bool disableCache = false );
647 #else
648 
656  static void invalidateCache( bool disableCache SIP_PYARGREMOVE = false );
657 #endif
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 bool sDisableSrIdCache;
783 
784  static QReadWriteLock sOgcLock;
785  static QHash< QString, QgsCoordinateReferenceSystem > sOgcCache;
786  static bool sDisableOgcCache;
787 
788  static QReadWriteLock sProj4CacheLock;
789  static QHash< QString, QgsCoordinateReferenceSystem > sProj4Cache;
790  static bool sDisableProj4Cache;
791 
792  static QReadWriteLock sCRSWktLock;
793  static QHash< QString, QgsCoordinateReferenceSystem > sWktCache;
794  static bool sDisableWktCache;
795 
796  static QReadWriteLock sCRSSrsIdLock;
797  static QHash< long, QgsCoordinateReferenceSystem > sSrsIdCache;
798  static bool sDisableSrsIdCache;
799 
800  static QReadWriteLock sCrsStringLock;
801  static QHash< QString, QgsCoordinateReferenceSystem > sStringCache;
802  static bool sDisableStringCache;
803 
804  friend class TestQgsCoordinateReferenceSystem;
805 };
806 
808 
809 #ifndef SIP_RUN
811 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
812 {
813  QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
814  mySummary += QLatin1String( "\n\t\tDescription : " );
815  if ( !r.description().isNull() )
816  {
817  mySummary += r.description();
818  }
819  else
820  {
821  mySummary += QLatin1String( "Undefined" );
822  }
823  mySummary += QLatin1String( "\n\t\tProjection : " );
824  if ( !r.projectionAcronym().isNull() )
825  {
826  mySummary += r.projectionAcronym();
827  }
828  else
829  {
830  mySummary += QLatin1String( "Undefined" );
831  }
832 
833  mySummary += QLatin1String( "\n\t\tEllipsoid : " );
834  if ( !r.ellipsoidAcronym().isNull() )
835  {
836  mySummary += r.ellipsoidAcronym();
837  }
838  else
839  {
840  mySummary += QLatin1String( "Undefined" );
841  }
842 
843  mySummary += QLatin1String( "\n\t\tProj4String : " );
844  if ( !r.toProj4().isNull() )
845  {
846  mySummary += r.toProj4();
847  }
848  else
849  {
850  mySummary += QLatin1String( "Undefined" );
851  }
852  // Using streams we need to use local 8 Bit
853  return os << mySummary.toLocal8Bit().data() << std::endl;
854 }
855 #endif
856 
857 #endif // QGSCOORDINATEREFERENCESYSTEM_H
A rectangle specified with double values.
Definition: qgsrectangle.h:40
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
const QgsCoordinateReferenceSystem & crs
Q_DECLARE_METATYPE(QModelIndex)
Internal ID used by QGIS in the local SQLite database.
#define SIP_SKIP
Definition: qgis_sip.h:119
std::ostream & operator<<(std::ostream &os, const QgsCoordinateReferenceSystem &r)
Output stream operator.
QString ellipsoidAcronym() const
Returns the ellipsoid acronym for the ellipsoid used by the CRS.
CrsType
Enumeration of types of IDs accepted in createFromId() method.
struct sqlite3 sqlite3
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:53
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:139
QString projectionAcronym() const
Returns the projection acronym for the projection used by the CRS.
This class represents a coordinate reference system (CRS).
QString description() const
Returns the descriptive name of the CRS, e.g., "WGS 84" or "GDA 94 / Vicgrid94".
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
void * OGRSpatialReferenceH
QString toProj4() const
Returns a Proj4 string representation of this CRS.