QGIS API Documentation  2.99.0-Master (e077efd)
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 <ostream>
23 
24 //qt includes
25 #include <QString>
26 #include <QMap>
27 #include <QHash>
28 #include <QReadWriteLock>
29 
30 class QDomNode;
31 class QDomDocument;
32 class QgsCoordinateReferenceSystemPrivate;
33 
34 // forward declaration for sqlite3
35 typedef struct sqlite3 sqlite3;
36 
37 //qgis includes
38 #include "qgis.h"
39 #include "qgsunittypes.h"
40 
41 #ifdef DEBUG
42 typedef struct OGRSpatialReferenceHS *OGRSpatialReferenceH;
43 #else
44 typedef void *OGRSpatialReferenceH;
45 #endif
46 
49 
186 {
187  Q_GADGET
188 
189  Q_PROPERTY( QgsUnitTypes::DistanceUnit mapUnits READ mapUnits )
190 
191  public:
192 
194  enum CrsType
195  {
198  EpsgCrsId
199  };
200 
203 
205 
220  // TODO QGIS 3: remove "POSTGIS" and "INTERNAL", allow PROJ4 without the prefix
221  explicit QgsCoordinateReferenceSystem( const QString& theDefinition );
222 
229  // TODO QGIS 3: remove theType and always use EPSG code
230  QgsCoordinateReferenceSystem( const long theId, CrsType theType = PostgisCrsId );
231 
234 
237 
238  // static creators
239 
246  static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString& ogcCrs );
247 
253  Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
254 
261  static QgsCoordinateReferenceSystem fromProj4( const QString& proj4 );
262 
269  static QgsCoordinateReferenceSystem fromWkt( const QString& wkt );
270 
277  static QgsCoordinateReferenceSystem fromSrsId( long srsId );
278 
279  // Misc helper functions -----------------------
280 
287  // TODO QGIS 3: remove theType and always use EPSG code, rename to createFromEpsg
288  bool createFromId( const long theId, CrsType theType = PostgisCrsId );
289 
300  // TODO QGIS 3: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
301  bool createFromOgcWmsCrs( const QString& theCrs );
302 
307  // TODO QGIS 3: remove unless really necessary - let's use EPSG codes instead
308  bool createFromSrid( const long theSrid );
309 
322  bool createFromWkt( const QString &theWkt );
323 
333  bool createFromSrsId( const long theSrsId );
334 
357  bool createFromProj4( const QString &theProjString );
358 
372  bool createFromString( const QString &theDefinition );
373 
388  // TODO QGIS3: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
389  bool createFromUserInput( const QString &theDefinition );
390 
399  static void setupESRIWktFix();
400 
402  bool isValid() const;
403 
414  void validate();
415 
422  // TODO QGIS 3: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
423  long findMatchingProj();
424 
429  bool operator==( const QgsCoordinateReferenceSystem &theSrs ) const;
430 
435  bool operator!=( const QgsCoordinateReferenceSystem &theSrs ) const;
436 
441  bool readXml( const QDomNode & theNode );
442 
448  bool writeXml( QDomNode & theNode, QDomDocument & theDoc ) const;
449 
450 
455  static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f );
456 
460  static CUSTOM_CRS_VALIDATION customCrsValidation();
461 
462  // Accessors -----------------------------------
463 
467  long srsid() const;
468 
472  // TODO QGIS 3: remove unless really necessary - let's use EPSG codes instead
473  long postgisSrid() const;
474 
486  QString authid() const;
487 
495  QString description() const;
496 
502  QString projectionAcronym() const;
503 
509  QString ellipsoidAcronym() const;
510 
515  QString toWkt() const;
516 
526  QString toProj4() const;
527 
531  bool isGeographic() const;
532 
536  bool hasAxisInverted() const;
537 
540  QgsUnitTypes::DistanceUnit mapUnits() const;
541 
542  // Mutators -----------------------------------
543 
546  void setValidationHint( const QString& html );
547 
550  QString validationHint();
551 
557  static int syncDb();
558 
559 
563  bool saveAsUserCrs( const QString& name );
564 
566  QString geographicCrsAuthId() const;
567 
572  static QStringList recentProjections();
573 
579  static void invalidateCache();
580 
581  // Mutators -----------------------------------
582  // We don't want to expose these to the public api since they wont create
583  // a fully valid crs. Programmers should use the createFrom* methods rather
584  private:
585 
590  static QString proj4FromSrsId( const int theSrsId );
591 
595  void setInternalId( long theSrsId );
596 
600  void setSrid( long theSrid );
601 
605  void setDescription( const QString& theDescription );
606 
617  void setProj4String( const QString& theProj4String );
618 
622  void setGeographicFlag( bool theGeoFlag );
623 
627  void setEpsg( long theEpsg );
628 
632  void setAuthId( const QString& theID );
633 
637  void setProjectionAcronym( const QString& theProjectionAcronym );
638 
642  void setEllipsoidAcronym( const QString& theEllipsoidAcronym );
643 
646  void debugPrint();
647 
649  typedef QMap<QString, QString> RecordMap;
650 
657  RecordMap getRecord( const QString& theSql );
658 
661  static int openDb( const QString& path, sqlite3 **db, bool readonly = true );
662 
664  void setMapUnits();
665 
667  long getRecordCount();
668 
670  static QString quotedValue( QString value );
671 
674  bool loadFromDb( const QString& db, const QString& expression, const QString& value );
675 
676  static bool loadIds( QHash<int, QString> &wkts );
677  static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
679  static bool syncDatumTransform( const QString& dbPath );
680 
681  QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
682 
684  static CUSTOM_CRS_VALIDATION mCustomSrsValidation;
685 
686 
687  // cache
688 
689  static QReadWriteLock mSrIdCacheLock;
690  static QHash< long, QgsCoordinateReferenceSystem > mSrIdCache;
691  static QReadWriteLock mOgcLock;
692  static QHash< QString, QgsCoordinateReferenceSystem > mOgcCache;
693  static QReadWriteLock mProj4CacheLock;
694  static QHash< QString, QgsCoordinateReferenceSystem > mProj4Cache;
695  static QReadWriteLock mCRSWktLock;
696  static QHash< QString, QgsCoordinateReferenceSystem > mWktCache;
697  static QReadWriteLock mCRSSrsIdLock;
698  static QHash< long, QgsCoordinateReferenceSystem > mSrsIdCache;
699  static QReadWriteLock mCrsStringLock;
700  static QHash< QString, QgsCoordinateReferenceSystem > mStringCache;
701 
702  friend class TestQgsCoordinateReferenceSystem;
703 };
704 
705 
707 inline std::ostream& operator << ( std::ostream& os, const QgsCoordinateReferenceSystem &r )
708 {
709  QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
710  mySummary += QLatin1String( "\n\t\tDescription : " );
711  if ( !r.description().isNull() )
712  {
713  mySummary += r.description();
714  }
715  else
716  {
717  mySummary += QLatin1String( "Undefined" );
718  }
719  mySummary += QLatin1String( "\n\t\tProjection : " );
720  if ( !r.projectionAcronym().isNull() )
721  {
722  mySummary += r.projectionAcronym();
723  }
724  else
725  {
726  mySummary += QLatin1String( "Undefined" );
727  }
728 
729  mySummary += QLatin1String( "\n\t\tEllipsoid : " );
730  if ( !r.ellipsoidAcronym().isNull() )
731  {
732  mySummary += r.ellipsoidAcronym();
733  }
734  else
735  {
736  mySummary += QLatin1String( "Undefined" );
737  }
738 
739  mySummary += QLatin1String( "\n\t\tProj4String : " );
740  if ( !r.toProj4().isNull() )
741  {
742  mySummary += r.toProj4();
743  }
744  else
745  {
746  mySummary += QLatin1String( "Undefined" );
747  }
748  // Using streams we need to use local 8 Bit
749  return os << mySummary.toLocal8Bit().data() << std::endl;
750 }
751 
752 #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, eg "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