QGIS API Documentation  2.99.0-Master (a411669)
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 
36 class QDomNode;
37 class QDomDocument;
38 class QgsCoordinateReferenceSystemPrivate;
39 
40 // forward declaration for sqlite3
41 typedef struct sqlite3 sqlite3 SIP_SKIP;
42 
43 #ifdef DEBUG
44 typedef struct OGRSpatialReferenceHS *OGRSpatialReferenceH SIP_SKIP;
45 #else
47 #endif
48 
51 
189 {
190  Q_GADGET
191 
192  Q_PROPERTY( QgsUnitTypes::DistanceUnit mapUnits READ mapUnits )
193 
194  public:
195 
197  enum CrsType
198  {
201  EpsgCrsId
202  };
203 
206 
208  // TODO QGIS 3: remove "POSTGIS" and "INTERNAL", allow PROJ4 without the prefix
223  explicit QgsCoordinateReferenceSystem( const QString &definition );
224  // TODO QGIS 3: remove type and always use EPSG code
232  explicit QgsCoordinateReferenceSystem( const long id, CrsType type = PostgisCrsId );
233 
236 
239 
241  operator QVariant() const
242  {
243  return QVariant::fromValue( *this );
244  }
245 
253  static QList< long > validSrsIds();
254 
255  // static creators
256 
264  static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
265 
272  Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
273 
281  static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 );
282 
290  static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
291 
300  static QgsCoordinateReferenceSystem fromSrsId( long srsId );
301 
302  // Misc helper functions -----------------------
303  // TODO QGIS 3: remove type and always use EPSG code, rename to createFromEpsg
310  bool createFromId( const long id, CrsType type = PostgisCrsId );
311  // TODO QGIS 3: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
322  bool createFromOgcWmsCrs( const QString &crs );
323  // TODO QGIS 3: remove unless really necessary - let's use EPSG codes instead
329  bool createFromSrid( const long srid );
330 
344  bool createFromWkt( const QString &wkt );
345 
356  bool createFromSrsId( const long srsId );
357 
381  bool createFromProj4( const QString &projString );
382 
397  bool createFromString( const QString &definition );
398  // TODO QGIS3: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
414  bool createFromUserInput( const QString &definition );
415 
425  static void setupESRIWktFix();
426 
428  bool isValid() const;
429 
441  void validate();
442  // TODO QGIS 3: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
450  long findMatchingProj();
451 
457  bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
458 
464  bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
465 
471  bool readXml( const QDomNode &node );
472 
479  bool writeXml( QDomNode &node, QDomDocument &doc ) const;
480 
481 
486  static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
487 
492  static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
493 
494  // Accessors -----------------------------------
495 
500  long srsid() const;
501  // TODO QGIS 3: remove unless really necessary - let's use EPSG codes instead
506  long postgisSrid() const;
507 
520  QString authid() const;
521 
530  QString description() const;
531 
538  QString projectionAcronym() const;
539 
546  QString ellipsoidAcronym() const;
547 
553  QString toWkt() const;
554 
565  QString toProj4() const;
566 
571  bool isGeographic() const;
572 
577  bool hasAxisInverted() const;
578 
582  QgsUnitTypes::DistanceUnit mapUnits() const;
583 
592  QgsRectangle bounds() const;
593 
594  // Mutators -----------------------------------
595 
599  void setValidationHint( const QString &html );
600 
604  QString validationHint();
605 
612  static int syncDatabase();
613 
614 
619  bool saveAsUserCrs( const QString &name );
620 
622  QString geographicCrsAuthId() const;
623 
629  static QStringList recentProjections();
630 
637  static void invalidateCache();
638 
639  // Mutators -----------------------------------
640  // We don't want to expose these to the public api since they won't create
641  // a fully valid crs. Programmers should use the createFrom* methods rather
642  private:
643 
649  static QString proj4FromSrsId( const int srsId );
650 
655  void setInternalId( long srsId );
656 
661  void setSrid( long srid );
662 
667  void setDescription( const QString &description );
668 
680  void setProj4String( const QString &proj4String );
681 
686  void setGeographicFlag( bool geoFlag );
687 
692  void setEpsg( long epsg );
693 
698  void setAuthId( const QString &theID );
699 
704  void setProjectionAcronym( const QString &projectionAcronym );
705 
710  void setEllipsoidAcronym( const QString &ellipsoidAcronym );
711 
715  void debugPrint();
716 
718  typedef QMap<QString, QString> RecordMap;
719 
727  RecordMap getRecord( const QString &sql );
728 
733  static int openDatabase( const QString &path, sqlite3 **db, bool readonly = true );
734 
736  void setMapUnits();
737 
739  long getRecordCount();
740 
742  static QString quotedValue( QString value );
743 
748  bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
749 
750  static bool loadIds( QHash<int, QString> &wkts );
751  static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
753  static bool syncDatumTransform( const QString &dbPath );
754 
755  QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
756 
758  static CUSTOM_CRS_VALIDATION mCustomSrsValidation;
759 
760 
761  // cache
762 
763  static QReadWriteLock sSrIdCacheLock;
764  static QHash< long, QgsCoordinateReferenceSystem > sSrIdCache;
765  static QReadWriteLock sOgcLock;
766  static QHash< QString, QgsCoordinateReferenceSystem > sOgcCache;
767  static QReadWriteLock sProj4CacheLock;
768  static QHash< QString, QgsCoordinateReferenceSystem > sProj4Cache;
769  static QReadWriteLock sCRSWktLock;
770  static QHash< QString, QgsCoordinateReferenceSystem > sWktCache;
771  static QReadWriteLock sCRSSrsIdLock;
772  static QHash< long, QgsCoordinateReferenceSystem > sSrsIdCache;
773  static QReadWriteLock sCrsStringLock;
774  static QHash< QString, QgsCoordinateReferenceSystem > sStringCache;
775 
776  friend class TestQgsCoordinateReferenceSystem;
777 };
778 
780 
781 #ifndef SIP_RUN
783 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
784 {
785  QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
786  mySummary += QLatin1String( "\n\t\tDescription : " );
787  if ( !r.description().isNull() )
788  {
789  mySummary += r.description();
790  }
791  else
792  {
793  mySummary += QLatin1String( "Undefined" );
794  }
795  mySummary += QLatin1String( "\n\t\tProjection : " );
796  if ( !r.projectionAcronym().isNull() )
797  {
798  mySummary += r.projectionAcronym();
799  }
800  else
801  {
802  mySummary += QLatin1String( "Undefined" );
803  }
804 
805  mySummary += QLatin1String( "\n\t\tEllipsoid : " );
806  if ( !r.ellipsoidAcronym().isNull() )
807  {
808  mySummary += r.ellipsoidAcronym();
809  }
810  else
811  {
812  mySummary += QLatin1String( "Undefined" );
813  }
814 
815  mySummary += QLatin1String( "\n\t\tProj4String : " );
816  if ( !r.toProj4().isNull() )
817  {
818  mySummary += r.toProj4();
819  }
820  else
821  {
822  mySummary += QLatin1String( "Undefined" );
823  }
824  // Using streams we need to use local 8 Bit
825  return os << mySummary.toLocal8Bit().data() << std::endl;
826 }
827 #endif
828 
829 #endif // QGSCOORDINATEREFERENCESYSTEM_H
A rectangle specified with double values.
Definition: qgsrectangle.h:39
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.
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:43
This class represents a coordinate reference system (CRS).
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
void * OGRSpatialReferenceH