QGIS API Documentation  2.99.0-Master (0a63d1f)
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& theDefinition );
223 
230  // TODO QGIS 3: remove theType and always use EPSG code
231  QgsCoordinateReferenceSystem( const long theId, CrsType theType = PostgisCrsId );
232 
235 
238 
239  // static creators
240 
247  static QgsCoordinateReferenceSystem fromOgcWmsCrs( const QString& ogcCrs );
248 
254  Q_INVOKABLE static QgsCoordinateReferenceSystem fromEpsgId( long epsg );
255 
262  static QgsCoordinateReferenceSystem fromProj4( const QString& proj4 );
263 
270  static QgsCoordinateReferenceSystem fromWkt( const QString& wkt );
271 
278  static QgsCoordinateReferenceSystem fromSrsId( long srsId );
279 
280  // Misc helper functions -----------------------
281 
288  // TODO QGIS 3: remove theType and always use EPSG code, rename to createFromEpsg
289  bool createFromId( const long theId, CrsType theType = PostgisCrsId );
290 
301  // TODO QGIS 3: remove "QGIS" and "CUSTOM", only support "USER" (also returned by authid())
302  bool createFromOgcWmsCrs( const QString& theCrs );
303 
308  // TODO QGIS 3: remove unless really necessary - let's use EPSG codes instead
309  bool createFromSrid( const long theSrid );
310 
323  bool createFromWkt( const QString &theWkt );
324 
334  bool createFromSrsId( const long theSrsId );
335 
358  bool createFromProj4( const QString &theProjString );
359 
373  bool createFromString( const QString &theDefinition );
374 
389  // TODO QGIS3: rename to createFromStringOGR so it is clear it's similar to createFromString, just different backend
390  bool createFromUserInput( const QString &theDefinition );
391 
400  static void setupESRIWktFix();
401 
403  bool isValid() const;
404 
415  void validate();
416 
423  // TODO QGIS 3: seems completely obsolete now (only compares proj4 - already done in createFromProj4)
424  long findMatchingProj();
425 
430  bool operator==( const QgsCoordinateReferenceSystem &theSrs ) const;
431 
436  bool operator!=( const QgsCoordinateReferenceSystem &theSrs ) const;
437 
442  bool readXml( const QDomNode & theNode );
443 
449  bool writeXml( QDomNode & theNode, QDomDocument & theDoc ) const;
450 
451 
456  static void setCustomCrsValidation( CUSTOM_CRS_VALIDATION f );
457 
461  static CUSTOM_CRS_VALIDATION customCrsValidation();
462 
463  // Accessors -----------------------------------
464 
468  long srsid() const;
469 
473  // TODO QGIS 3: remove unless really necessary - let's use EPSG codes instead
474  long postgisSrid() const;
475 
487  QString authid() const;
488 
496  QString description() const;
497 
503  QString projectionAcronym() const;
504 
510  QString ellipsoidAcronym() const;
511 
516  QString toWkt() const;
517 
527  QString toProj4() const;
528 
532  bool isGeographic() const;
533 
537  bool hasAxisInverted() const;
538 
541  QgsUnitTypes::DistanceUnit mapUnits() const;
542 
543  // Mutators -----------------------------------
544 
547  void setValidationHint( const QString& html );
548 
551  QString validationHint();
552 
558  static int syncDb();
559 
560 
564  bool saveAsUserCrs( const QString& name );
565 
567  QString geographicCrsAuthId() const;
568 
573  static QStringList recentProjections();
574 
580  static void invalidateCache();
581 
582  // Mutators -----------------------------------
583  // We don't want to expose these to the public api since they won't create
584  // a fully valid crs. Programmers should use the createFrom* methods rather
585  private:
586 
591  static QString proj4FromSrsId( const int theSrsId );
592 
596  void setInternalId( long theSrsId );
597 
601  void setSrid( long theSrid );
602 
606  void setDescription( const QString& theDescription );
607 
618  void setProj4String( const QString& theProj4String );
619 
623  void setGeographicFlag( bool theGeoFlag );
624 
628  void setEpsg( long theEpsg );
629 
633  void setAuthId( const QString& theID );
634 
638  void setProjectionAcronym( const QString& theProjectionAcronym );
639 
643  void setEllipsoidAcronym( const QString& theEllipsoidAcronym );
644 
647  void debugPrint();
648 
650  typedef QMap<QString, QString> RecordMap;
651 
658  RecordMap getRecord( const QString& theSql );
659 
662  static int openDb( const QString& path, sqlite3 **db, bool readonly = true );
663 
665  void setMapUnits();
666 
668  long getRecordCount();
669 
671  static QString quotedValue( QString value );
672 
675  bool loadFromDb( const QString& db, const QString& expression, const QString& value );
676 
677  static bool loadIds( QHash<int, QString> &wkts );
678  static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
680  static bool syncDatumTransform( const QString& dbPath );
681 
682  QExplicitlySharedDataPointer<QgsCoordinateReferenceSystemPrivate> d;
683 
685  static CUSTOM_CRS_VALIDATION mCustomSrsValidation;
686 
687 
688  // cache
689 
690  static QReadWriteLock sSrIdCacheLock;
691  static QHash< long, QgsCoordinateReferenceSystem > sSrIdCache;
692  static QReadWriteLock sOgcLock;
693  static QHash< QString, QgsCoordinateReferenceSystem > sOgcCache;
694  static QReadWriteLock sProj4CacheLock;
695  static QHash< QString, QgsCoordinateReferenceSystem > sProj4Cache;
696  static QReadWriteLock sCRSWktLock;
697  static QHash< QString, QgsCoordinateReferenceSystem > sWktCache;
698  static QReadWriteLock sCRSSrsIdLock;
699  static QHash< long, QgsCoordinateReferenceSystem > sSrsIdCache;
700  static QReadWriteLock sCrsStringLock;
701  static QHash< QString, QgsCoordinateReferenceSystem > sStringCache;
702 
703  friend class TestQgsCoordinateReferenceSystem;
704 };
705 
706 
708 inline std::ostream& operator << ( std::ostream& os, const QgsCoordinateReferenceSystem &r )
709 {
710  QString mySummary( QStringLiteral( "\n\tSpatial Reference System:" ) );
711  mySummary += QLatin1String( "\n\t\tDescription : " );
712  if ( !r.description().isNull() )
713  {
714  mySummary += r.description();
715  }
716  else
717  {
718  mySummary += QLatin1String( "Undefined" );
719  }
720  mySummary += QLatin1String( "\n\t\tProjection : " );
721  if ( !r.projectionAcronym().isNull() )
722  {
723  mySummary += r.projectionAcronym();
724  }
725  else
726  {
727  mySummary += QLatin1String( "Undefined" );
728  }
729 
730  mySummary += QLatin1String( "\n\t\tEllipsoid : " );
731  if ( !r.ellipsoidAcronym().isNull() )
732  {
733  mySummary += r.ellipsoidAcronym();
734  }
735  else
736  {
737  mySummary += QLatin1String( "Undefined" );
738  }
739 
740  mySummary += QLatin1String( "\n\t\tProj4String : " );
741  if ( !r.toProj4().isNull() )
742  {
743  mySummary += r.toProj4();
744  }
745  else
746  {
747  mySummary += QLatin1String( "Undefined" );
748  }
749  // Using streams we need to use local 8 Bit
750  return os << mySummary.toLocal8Bit().data() << std::endl;
751 }
752 
753 #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:43
This class represents a coordinate reference system (CRS).
void(* CUSTOM_CRS_VALIDATION)(QgsCoordinateReferenceSystem &)
void * OGRSpatialReferenceH