QGIS API Documentation  2.99.0-Master (19b062c)
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 : sherman@mrcc.com
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( const 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( const 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( const long srid );
331 
345  bool createFromWkt( const QString &wkt );
346 
357  bool createFromSrsId( const 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 
472  bool readXml( const QDomNode &node );
473 
480  bool writeXml( QDomNode &node, QDomDocument &doc ) const;
481 
482 
487  static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
488 
493  static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
494 
495  // Accessors -----------------------------------
496 
501  long srsid() const;
502  // TODO QGIS 3: remove unless really necessary - let's use EPSG codes instead
507  long postgisSrid() const;
508 
521  QString authid() const;
522 
531  QString description() const;
532 
539  QString projectionAcronym() const;
540 
547  QString ellipsoidAcronym() const;
548 
554  QString toWkt() const;
555 
566  QString toProj4() const;
567 
572  bool isGeographic() const;
573 
578  bool hasAxisInverted() const;
579 
583  QgsUnitTypes::DistanceUnit mapUnits() const;
584 
593  QgsRectangle bounds() const;
594 
595  // Mutators -----------------------------------
596 
600  void setValidationHint( const QString &html );
601 
605  QString validationHint();
606 
613  static int syncDatabase();
614 
615 
621  long saveAsUserCrs( const QString &name );
622 
624  QString geographicCrsAuthId() const;
625 
631  static QStringList recentProjections();
632 
639  static void invalidateCache();
640 
641  // Mutators -----------------------------------
642  // We don't want to expose these to the public api since they won't create
643  // a fully valid crs. Programmers should use the createFrom* methods rather
644  private:
645 
651  static QString proj4FromSrsId( const int srsId );
652 
657  void setInternalId( long srsId );
658 
663  void setSrid( long srid );
664 
669  void setDescription( const QString &description );
670 
682  void setProj4String( const QString &proj4String );
683 
688  void setGeographicFlag( bool geoFlag );
689 
694  void setEpsg( long epsg );
695 
700  void setAuthId( const QString &theID );
701 
706  void setProjectionAcronym( const QString &projectionAcronym );
707 
712  void setEllipsoidAcronym( const QString &ellipsoidAcronym );
713 
717  void debugPrint();
718 
720  typedef QMap<QString, QString> RecordMap;
721 
729  RecordMap getRecord( const QString &sql );
730 
735  static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
736 
738  void setMapUnits();
739 
741  long getRecordCount();
742 
744  static QString quotedValue( QString value );
745 
750  bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
751 
752  static bool loadIds( QHash<int, QString> &wkts );
753  static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
755  static bool syncDatumTransform( const QString &dbPath );
756 
757  QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
758 
760  static CUSTOM_CRS_VALIDATION mCustomSrsValidation;
761 
762 
763  // cache
764 
765  static QReadWriteLock sSrIdCacheLock;
766  static QHash< long, QgsCoordinateReferenceSystem > sSrIdCache;
767  static QReadWriteLock sOgcLock;
768  static QHash< QString, QgsCoordinateReferenceSystem > sOgcCache;
769  static QReadWriteLock sProj4CacheLock;
770  static QHash< QString, QgsCoordinateReferenceSystem > sProj4Cache;
771  static QReadWriteLock sCRSWktLock;
772  static QHash< QString, QgsCoordinateReferenceSystem > sWktCache;
773  static QReadWriteLock sCRSSrsIdLock;
774  static QHash< long, QgsCoordinateReferenceSystem > sSrsIdCache;
775  static QReadWriteLock sCrsStringLock;
776  static QHash< QString, QgsCoordinateReferenceSystem > sStringCache;
777 
778  friend class TestQgsCoordinateReferenceSystem;
779 };
780 
782 
783 #ifndef SIP_RUN
785 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
786 {
787  QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
788  mySummary += QLatin1String( "\n\t\tDescription : " );
789  if ( !r.description().isNull() )
790  {
791  mySummary += r.description();
792  }
793  else
794  {
795  mySummary += QLatin1String( "Undefined" );
796  }
797  mySummary += QLatin1String( "\n\t\tProjection : " );
798  if ( !r.projectionAcronym().isNull() )
799  {
800  mySummary += r.projectionAcronym();
801  }
802  else
803  {
804  mySummary += QLatin1String( "Undefined" );
805  }
806 
807  mySummary += QLatin1String( "\n\t\tEllipsoid : " );
808  if ( !r.ellipsoidAcronym().isNull() )
809  {
810  mySummary += r.ellipsoidAcronym();
811  }
812  else
813  {
814  mySummary += QLatin1String( "Undefined" );
815  }
816 
817  mySummary += QLatin1String( "\n\t\tProj4String : " );
818  if ( !r.toProj4().isNull() )
819  {
820  mySummary += r.toProj4();
821  }
822  else
823  {
824  mySummary += QLatin1String( "Undefined" );
825  }
826  // Using streams we need to use local 8 Bit
827  return os << mySummary.toLocal8Bit().data() << std::endl;
828 }
829 #endif
830 
831 #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
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