QGIS API Documentation  2.99.0-Master (7d4f81d)
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 
35 class QDomNode;
36 class QDomDocument;
37 class QgsCoordinateReferenceSystemPrivate;
38 
39 // forward declaration for sqlite3
40 typedef struct sqlite3 sqlite3 SIP_SKIP;
41 
42 #ifdef DEBUG
43 typedef struct OGRSpatialReferenceHS *OGRSpatialReferenceH SIP_SKIP;
44 #else
46 #endif
47 
50 
187 {
188  Q_GADGET
189 
190  Q_PROPERTY( QgsUnitTypes::DistanceUnit mapUnits READ mapUnits )
191 
192  public:
193 
195  enum CrsType
196  {
199  EpsgCrsId
200  };
201 
204 
206  // TODO QGIS 3: remove "POSTGIS" and "INTERNAL", allow PROJ4 without the prefix
221  explicit QgsCoordinateReferenceSystem( const QString &definition );
222  // TODO QGIS 3: remove type and always use EPSG code
229  explicit QgsCoordinateReferenceSystem( const long id, CrsType type = PostgisCrsId );
230 
233 
236 
238  operator QVariant() const
239  {
240  return QVariant::fromValue( *this );
241  }
242 
250  static QList< long > validSrsIds();
251 
252  // static creators
253 
260  static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
261 
267  Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
268 
275  static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 );
276 
283  static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
284 
292  static QgsCoordinateReferenceSystem fromSrsId( long srsId );
293 
294  // Misc helper functions -----------------------
295  // TODO QGIS 3: remove type and always use EPSG code, rename to createFromEpsg
302  bool createFromId( const long id, CrsType type = PostgisCrsId );
303  // TODO QGIS 3: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
314  bool createFromOgcWmsCrs( const QString &crs );
315  // TODO QGIS 3: remove unless really necessary - let's use EPSG codes instead
320  bool createFromSrid( const long srid );
321 
334  bool createFromWkt( const QString &wkt );
335 
345  bool createFromSrsId( const long srsId );
346 
369  bool createFromProj4( const QString &projString );
370 
384  bool createFromString( const QString &definition );
385  // TODO QGIS3: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
400  bool createFromUserInput( const QString &definition );
401 
410  static void setupESRIWktFix();
411 
413  bool isValid() const;
414 
425  void validate();
426  // TODO QGIS 3: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
433  long findMatchingProj();
434 
439  bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
440 
445  bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
446 
451  bool readXml( const QDomNode &node );
452 
458  bool writeXml( QDomNode &node, QDomDocument &doc ) const;
459 
460 
464  static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
465 
469  static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
470 
471  // Accessors -----------------------------------
472 
476  long srsid() const;
477  // TODO QGIS 3: remove unless really necessary - let's use EPSG codes instead
481  long postgisSrid() const;
482 
494  QString authid() const;
495 
503  QString description() const;
504 
510  QString projectionAcronym() const;
511 
517  QString ellipsoidAcronym() const;
518 
523  QString toWkt() const;
524 
534  QString toProj4() const;
535 
539  bool isGeographic() const;
540 
544  bool hasAxisInverted() const;
545 
548  QgsUnitTypes::DistanceUnit mapUnits() const;
549 
550  // Mutators -----------------------------------
551 
554  void setValidationHint( const QString &html );
555 
558  QString validationHint();
559 
565  static int syncDatabase();
566 
567 
571  bool saveAsUserCrs( const QString &name );
572 
574  QString geographicCrsAuthId() const;
575 
580  static QStringList recentProjections();
581 
587  static void invalidateCache();
588 
589  // Mutators -----------------------------------
590  // We don't want to expose these to the public api since they won't create
591  // a fully valid crs. Programmers should use the createFrom* methods rather
592  private:
593 
598  static QString proj4FromSrsId( const int srsId );
599 
603  void setInternalId( long srsId );
604 
608  void setSrid( long srid );
609 
613  void setDescription( const QString &description );
614 
625  void setProj4String( const QString &proj4String );
626 
630  void setGeographicFlag( bool geoFlag );
631 
635  void setEpsg( long epsg );
636 
640  void setAuthId( const QString &theID );
641 
645  void setProjectionAcronym( const QString &projectionAcronym );
646 
650  void setEllipsoidAcronym( const QString &ellipsoidAcronym );
651 
654  void debugPrint();
655 
657  typedef QMap<QString, QString> RecordMap;
658 
665  RecordMap getRecord( const QString &sql );
666 
669  static int openDatabase( const QString &path, sqlite3 **db, bool readonly = true );
670 
672  void setMapUnits();
673 
675  long getRecordCount();
676 
678  static QString quotedValue( QString value );
679 
682  bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
683 
684  static bool loadIds( QHash<int, QString> &wkts );
685  static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
687  static bool syncDatumTransform( const QString &dbPath );
688 
689  QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
690 
692  static CUSTOM_CRS_VALIDATION mCustomSrsValidation;
693 
694 
695  // cache
696 
697  static QReadWriteLock sSrIdCacheLock;
698  static QHash< long, QgsCoordinateReferenceSystem > sSrIdCache;
699  static QReadWriteLock sOgcLock;
700  static QHash< QString, QgsCoordinateReferenceSystem > sOgcCache;
701  static QReadWriteLock sProj4CacheLock;
702  static QHash< QString, QgsCoordinateReferenceSystem > sProj4Cache;
703  static QReadWriteLock sCRSWktLock;
704  static QHash< QString, QgsCoordinateReferenceSystem > sWktCache;
705  static QReadWriteLock sCRSSrsIdLock;
706  static QHash< long, QgsCoordinateReferenceSystem > sSrsIdCache;
707  static QReadWriteLock sCrsStringLock;
708  static QHash< QString, QgsCoordinateReferenceSystem > sStringCache;
709 
710  friend class TestQgsCoordinateReferenceSystem;
711 };
712 
714 
715 #ifndef SIP_RUN
717 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
718 {
719  QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
720  mySummary += QLatin1String( "\n\t\tDescription : " );
721  if ( !r.description().isNull() )
722  {
723  mySummary += r.description();
724  }
725  else
726  {
727  mySummary += QLatin1String( "Undefined" );
728  }
729  mySummary += QLatin1String( "\n\t\tProjection : " );
730  if ( !r.projectionAcronym().isNull() )
731  {
732  mySummary += r.projectionAcronym();
733  }
734  else
735  {
736  mySummary += QLatin1String( "Undefined" );
737  }
738 
739  mySummary += QLatin1String( "\n\t\tEllipsoid : " );
740  if ( !r.ellipsoidAcronym().isNull() )
741  {
742  mySummary += r.ellipsoidAcronym();
743  }
744  else
745  {
746  mySummary += QLatin1String( "Undefined" );
747  }
748 
749  mySummary += QLatin1String( "\n\t\tProj4String : " );
750  if ( !r.toProj4().isNull() )
751  {
752  mySummary += r.toProj4();
753  }
754  else
755  {
756  mySummary += QLatin1String( "Undefined" );
757  }
758  // Using streams we need to use local 8 Bit
759  return os << mySummary.toLocal8Bit().data() << std::endl;
760 }
761 #endif
762 
763 #endif // QGSCOORDINATEREFERENCESYSTEM_H
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:42
This class represents a coordinate reference system (CRS).
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
void * OGRSpatialReferenceH