QGIS API Documentation  2.99.0-Master (314842d)
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 class QDomNode;
32 class QDomDocument;
33 class QgsCoordinateReferenceSystemPrivate;
34 
35 // forward declaration for sqlite3
36 typedef struct sqlite3 sqlite3;
37 
38 //qgis includes
39 #include "qgis.h"
40 #include "qgsunittypes.h"
41 
42 #ifdef DEBUG
43 typedef struct OGRSpatialReferenceHS *OGRSpatialReferenceH;
44 #else
45 typedef void *OGRSpatialReferenceH;
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 
221  // TODO QGIS 3: remove "POSTGIS" and "INTERNAL", allow PROJ4 without the prefix
222  explicit QgsCoordinateReferenceSystem( const QString &definition );
223 
230  // TODO QGIS 3: remove type and always use EPSG code
231  QgsCoordinateReferenceSystem( const long id, CrsType type = PostgisCrsId );
232 
235 
238 
246  static QList< long > validSrsIds();
247 
248  // static creators
249 
256  static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString &ogcCrs );
257 
263  Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
264 
271  static QgsCoordinateReferenceSystem fromProj4( const QString &proj4 );
272 
279  static QgsCoordinateReferenceSystem fromWkt( const QString &wkt );
280 
288  static QgsCoordinateReferenceSystem fromSrsId( long srsId );
289 
290  // Misc helper functions -----------------------
291 
298  // TODO QGIS 3: remove type and always use EPSG code, rename to createFromEpsg
299  bool createFromId( const long id, CrsType type = PostgisCrsId );
300 
311  // TODO QGIS 3: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
312  bool createFromOgcWmsCrs( const QString &crs );
313 
318  // TODO QGIS 3: remove unless really necessary - let's use EPSG codes instead
319  bool createFromSrid( const long srid );
320 
333  bool createFromWkt( const QString &wkt );
334 
344  bool createFromSrsId( const long srsId );
345 
368  bool createFromProj4( const QString &projString );
369 
383  bool createFromString( const QString &definition );
384 
399  // 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 
433  // TODO QGIS 3: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
434  long findMatchingProj();
435 
440  bool operator==( const QgsCoordinateReferenceSystem &srs ) const;
441 
446  bool operator!=( const QgsCoordinateReferenceSystem &srs ) const;
447 
452  bool readXml( const QDomNode &node );
453 
459  bool writeXml( QDomNode &node, QDomDocument &doc ) const;
460 
461 
466  static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f );
467 
471  static CUSTOM_CRS_VALIDATION customCrsValidation();
472 
473  // Accessors -----------------------------------
474 
478  long srsid() const;
479 
483  // TODO QGIS 3: remove unless really necessary - let's use EPSG codes instead
484  long postgisSrid() const;
485 
497  QString authid() const;
498 
506  QString description() const;
507 
513  QString projectionAcronym() const;
514 
520  QString ellipsoidAcronym() const;
521 
526  QString toWkt() const;
527 
537  QString toProj4() const;
538 
542  bool isGeographic() const;
543 
547  bool hasAxisInverted() const;
548 
551  QgsUnitTypes::DistanceUnit mapUnits() const;
552 
553  // Mutators -----------------------------------
554 
557  void setValidationHint( const QString &html );
558 
561  QString validationHint();
562 
568  static int syncDatabase();
569 
570 
574  bool saveAsUserCrs( const QString &name );
575 
577  QString geographicCrsAuthId() const;
578 
583  static QStringList recentProjections();
584 
590  static void invalidateCache();
591 
592  // Mutators -----------------------------------
593  // We don't want to expose these to the public api since they won't create
594  // a fully valid crs. Programmers should use the createFrom* methods rather
595  private:
596 
601  static QString proj4FromSrsId( const int srsId );
602 
606  void setInternalId( long srsId );
607 
611  void setSrid( long srid );
612 
616  void setDescription( const QString &description );
617 
628  void setProj4String( const QString &proj4String );
629 
633  void setGeographicFlag( bool geoFlag );
634 
638  void setEpsg( long epsg );
639 
643  void setAuthId( const QString &theID );
644 
648  void setProjectionAcronym( const QString &projectionAcronym );
649 
653  void setEllipsoidAcronym( const QString &ellipsoidAcronym );
654 
657  void debugPrint();
658 
660  typedef QMap<QString, QString> RecordMap;
661 
668  RecordMap getRecord( const QString &sql );
669 
672  static int openDatabase( const QString &path, sqlite3 **db, bool readonly = true );
673 
675  void setMapUnits();
676 
678  long getRecordCount();
679 
681  static QString quotedValue( QString value );
682 
685  bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
686 
687  static bool loadIds( QHash<int, QString> &wkts );
688  static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
690  static bool syncDatumTransform( const QString &dbPath );
691 
692  QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
693 
695  static CUSTOM_CRS_VALIDATION mCustomSrsValidation;
696 
697 
698  // cache
699 
700  static QReadWriteLock sSrIdCacheLock;
701  static QHash< long, QgsCoordinateReferenceSystem > sSrIdCache;
702  static QReadWriteLock sOgcLock;
703  static QHash< QString, QgsCoordinateReferenceSystem > sOgcCache;
704  static QReadWriteLock sProj4CacheLock;
705  static QHash< QString, QgsCoordinateReferenceSystem > sProj4Cache;
706  static QReadWriteLock sCRSWktLock;
707  static QHash< QString, QgsCoordinateReferenceSystem > sWktCache;
708  static QReadWriteLock sCRSSrsIdLock;
709  static QHash< long, QgsCoordinateReferenceSystem > sSrsIdCache;
710  static QReadWriteLock sCrsStringLock;
711  static QHash< QString, QgsCoordinateReferenceSystem > sStringCache;
712 
713  friend class TestQgsCoordinateReferenceSystem;
714 };
715 
716 
718 inline std::ostream &operator << ( std::ostream &os, const QgsCoordinateReferenceSystem &r )
719 {
720  QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
721  mySummary += QLatin1String( "\n\t\tDescription : " );
722  if ( !r.description().isNull() )
723  {
724  mySummary += r.description();
725  }
726  else
727  {
728  mySummary += QLatin1String( "Undefined" );
729  }
730  mySummary += QLatin1String( "\n\t\tProjection : " );
731  if ( !r.projectionAcronym().isNull() )
732  {
733  mySummary += r.projectionAcronym();
734  }
735  else
736  {
737  mySummary += QLatin1String( "Undefined" );
738  }
739 
740  mySummary += QLatin1String( "\n\t\tEllipsoid : " );
741  if ( !r.ellipsoidAcronym().isNull() )
742  {
743  mySummary += r.ellipsoidAcronym();
744  }
745  else
746  {
747  mySummary += QLatin1String( "Undefined" );
748  }
749 
750  mySummary += QLatin1String( "\n\t\tProj4String : " );
751  if ( !r.toProj4().isNull() )
752  {
753  mySummary += r.toProj4();
754  }
755  else
756  {
757  mySummary += QLatin1String( "Undefined" );
758  }
759  // Using streams we need to use local 8 Bit
760  return os << mySummary.toLocal8Bit().data() << std::endl;
761 }
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.
Internal ID used by QGIS in the local SQLite database.
QString ellipsoidAcronym() const
Returns the ellipsoid acronym for the ellipsoid used by the CRS.
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