QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
640  static void invalidateCache();
641 
642  // Mutators -----------------------------------
643  // We don't want to expose these to the public api since they won't create
644  // a fully valid crs. Programmers should use the createFrom* methods rather
645  private:
646 
652  static QString proj4FromSrsId( int srsId );
653 
658  void setInternalId( long srsId );
659 
664  void setSrid( long srid );
665 
670  void setDescription( const QString &description );
671 
683  void setProj4String( const QString &proj4String );
684 
689  void setGeographicFlag( bool geoFlag );
690 
695  void setEpsg( long epsg );
696 
701  void setAuthId( const QString &theID );
702 
707  void setProjectionAcronym( const QString &projectionAcronym );
708 
713  void setEllipsoidAcronym( const QString &ellipsoidAcronym );
714 
718  void debugPrint();
719 
721  typedef QMap<QString, QString> RecordMap;
722 
730  RecordMap getRecord( const QString &sql );
731 
736  static int openDatabase( const QString &path, sqlite3_database_unique_ptr &database, bool readonly = true );
737 
739  void setMapUnits();
740 
742  long getRecordCount();
743 
745  static QString quotedValue( QString value );
746 
751  bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
752 
753  static bool loadIds( QHash<int, QString> &wkts );
754  static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
756  static bool syncDatumTransform( const QString &dbPath );
757 
758  QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
759 
761  static CUSTOM_CRS_VALIDATION mCustomSrsValidation;
762 
763 
764  // cache
765 
766  static QReadWriteLock sSrIdCacheLock;
767  static QHash< long, QgsCoordinateReferenceSystem > sSrIdCache;
768  static QReadWriteLock sOgcLock;
769  static QHash< QString, QgsCoordinateReferenceSystem > sOgcCache;
770  static QReadWriteLock sProj4CacheLock;
771  static QHash< QString, QgsCoordinateReferenceSystem > sProj4Cache;
772  static QReadWriteLock sCRSWktLock;
773  static QHash< QString, QgsCoordinateReferenceSystem > sWktCache;
774  static QReadWriteLock sCRSSrsIdLock;
775  static QHash< long, QgsCoordinateReferenceSystem > sSrsIdCache;
776  static QReadWriteLock sCrsStringLock;
777  static QHash< QString, QgsCoordinateReferenceSystem > sStringCache;
778 
779  friend class TestQgsCoordinateReferenceSystem;
780 };
781 
783 
784 #ifndef SIP_RUN
786 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
787 {
788  QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
789  mySummary += QLatin1String( "\n\t\tDescription : " );
790  if ( !r.description().isNull() )
791  {
792  mySummary += r.description();
793  }
794  else
795  {
796  mySummary += QLatin1String( "Undefined" );
797  }
798  mySummary += QLatin1String( "\n\t\tProjection : " );
799  if ( !r.projectionAcronym().isNull() )
800  {
801  mySummary += r.projectionAcronym();
802  }
803  else
804  {
805  mySummary += QLatin1String( "Undefined" );
806  }
807 
808  mySummary += QLatin1String( "\n\t\tEllipsoid : " );
809  if ( !r.ellipsoidAcronym().isNull() )
810  {
811  mySummary += r.ellipsoidAcronym();
812  }
813  else
814  {
815  mySummary += QLatin1String( "Undefined" );
816  }
817 
818  mySummary += QLatin1String( "\n\t\tProj4String : " );
819  if ( !r.toProj4().isNull() )
820  {
821  mySummary += r.toProj4();
822  }
823  else
824  {
825  mySummary += QLatin1String( "Undefined" );
826  }
827  // Using streams we need to use local 8 Bit
828  return os << mySummary.toLocal8Bit().data() << std::endl;
829 }
830 #endif
831 
832 #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)
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