QGIS API Documentation  2.99.0-Master (cb63e82)
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 
465  static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f ) SIP_SKIP;
466 
470  static CUSTOM_CRS_VALIDATION customCrsValidation() SIP_SKIP;
471 
472  // Accessors -----------------------------------
473 
477  long srsid() const;
478  // TODO QGIS 3: remove unless really necessary - let's use EPSG codes instead
482  long postgisSrid() const;
483 
495  QString authid() const;
496 
504  QString description() const;
505 
511  QString projectionAcronym() const;
512 
518  QString ellipsoidAcronym() const;
519 
524  QString toWkt() const;
525 
535  QString toProj4() const;
536 
540  bool isGeographic() const;
541 
545  bool hasAxisInverted() const;
546 
549  QgsUnitTypes::DistanceUnit mapUnits() const;
550 
551  // Mutators -----------------------------------
552 
555  void setValidationHint( const QString &html );
556 
559  QString validationHint();
560 
566  static int syncDatabase();
567 
568 
572  bool saveAsUserCrs( const QString &name );
573 
575  QString geographicCrsAuthId() const;
576 
581  static QStringList recentProjections();
582 
588  static void invalidateCache();
589 
590  // Mutators -----------------------------------
591  // We don't want to expose these to the public api since they won't create
592  // a fully valid crs. Programmers should use the createFrom* methods rather
593  private:
594 
599  static QString proj4FromSrsId( const int srsId );
600 
604  void setInternalId( long srsId );
605 
609  void setSrid( long srid );
610 
614  void setDescription( const QString &description );
615 
626  void setProj4String( const QString &proj4String );
627 
631  void setGeographicFlag( bool geoFlag );
632 
636  void setEpsg( long epsg );
637 
641  void setAuthId( const QString &theID );
642 
646  void setProjectionAcronym( const QString &projectionAcronym );
647 
651  void setEllipsoidAcronym( const QString &ellipsoidAcronym );
652 
655  void debugPrint();
656 
658  typedef QMap<QString, QString> RecordMap;
659 
666  RecordMap getRecord( const QString &sql );
667 
670  static int openDatabase( const QString &path, sqlite3 **db, bool readonly = true );
671 
673  void setMapUnits();
674 
676  long getRecordCount();
677 
679  static QString quotedValue( QString value );
680 
683  bool loadFromDatabase( const QString &db, const QString &expression, const QString &value );
684 
685  static bool loadIds( QHash<int, QString> &wkts );
686  static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
688  static bool syncDatumTransform( const QString &dbPath );
689 
690  QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
691 
693  static CUSTOM_CRS_VALIDATION mCustomSrsValidation;
694 
695 
696  // cache
697 
698  static QReadWriteLock sSrIdCacheLock;
699  static QHash< long, QgsCoordinateReferenceSystem > sSrIdCache;
700  static QReadWriteLock sOgcLock;
701  static QHash< QString, QgsCoordinateReferenceSystem > sOgcCache;
702  static QReadWriteLock sProj4CacheLock;
703  static QHash< QString, QgsCoordinateReferenceSystem > sProj4Cache;
704  static QReadWriteLock sCRSWktLock;
705  static QHash< QString, QgsCoordinateReferenceSystem > sWktCache;
706  static QReadWriteLock sCRSSrsIdLock;
707  static QHash< long, QgsCoordinateReferenceSystem > sSrsIdCache;
708  static QReadWriteLock sCrsStringLock;
709  static QHash< QString, QgsCoordinateReferenceSystem > sStringCache;
710 
711  friend class TestQgsCoordinateReferenceSystem;
712 };
713 
715 
716 #ifndef SIP_RUN
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 #endif
763 
764 #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(QgsMimeDataUtils::UriList)
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
#define SIP_SKIP
Definition: qgis.h:457
This class represents a coordinate reference system (CRS).
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
void * OGRSpatialReferenceH