QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
29 class QDomNode;
30 class QDomDocument;
31 
32 // forward declaration for sqlite3
33 typedef struct sqlite3 sqlite3;
34 
35 //qgis includes
36 #include "qgis.h"
37 
38 #ifdef DEBUG
39 typedef struct OGRSpatialReferenceHS *OGRSpatialReferenceH;
40 #else
41 typedef void *OGRSpatialReferenceH;
42 #endif
43 
46 
51 {
52  public:
53 
54  enum CrsType
55  {
58  EpsgCrsId // deprecated
59  };
60 
63 
65 
71  explicit QgsCoordinateReferenceSystem( QString theDefinition );
72 
80  QgsCoordinateReferenceSystem( const long theId, CrsType theType = PostgisCrsId );
81 
84 
87 
88  // Misc helper functions -----------------------
89 
90  bool createFromId( const long theId, CrsType theType = PostgisCrsId );
91 
99  bool createFromOgcWmsCrs( QString theCrs );
100 
107  bool createFromSrid( const long theSrid );
108 
119  bool createFromWkt( const QString &theWkt );
120 
129  bool createFromSrsId( const long theSrsId );
130 
157  bool createFromProj4( const QString &theProjString );
158 
166  bool createFromString( const QString &theDefinition );
167 
181  bool createFromUserInput( const QString &theDefinition );
182 
191  static void setupESRIWktFix();
192 
194  bool isValid() const;
195 
205  void validate();
206 
218  long findMatchingProj();
219 
223  bool operator==( const QgsCoordinateReferenceSystem &theSrs ) const;
227  bool operator!=( const QgsCoordinateReferenceSystem &theSrs ) const;
228 
233  bool readXML( QDomNode & theNode );
251  bool writeXML( QDomNode & theNode, QDomDocument & theDoc ) const;
252 
253 
258  static void setCustomSrsValidation( CUSTOM_CRS_VALIDATION f );
259 
263  static CUSTOM_CRS_VALIDATION customSrsValidation();
264 
265  // Accessors -----------------------------------
266 
270  long srsid() const;
271 
275  long postgisSrid() const;
276 
280  QString authid() const;
281 
286  QString description() const;
287 
292  QString projectionAcronym() const;
293 
298  QString ellipsoidAcronym() const;
299 
303  QString toWkt() const;
304 
312  QString toProj4() const;
313 
317  bool geographicFlag() const;
318 
322  bool axisInverted() const;
323 
327  QGis::UnitType mapUnits() const;
328 
329 
330  // Mutators -----------------------------------
333  void setValidationHint( QString html );
334 
337  QString validationHint();
342  static int syncDb();
343 
344 
348  bool saveAsUserCRS( QString name );
349 
351  QString geographicCRSAuthId() const;
352 
357  static QStringList recentProjections();
358 
359  // Mutators -----------------------------------
360  // We don't want to expose these to the public api since they wont create
361  // a fully valid crs. Programmers should use the createFrom* methods rather
362  private:
367  static QString proj4FromSrsId( const int theSrsId );
368 
372  void setInternalId( long theSrsId );
376  void setSrid( long theSrid );
380  void setDescription( QString theDescription );
381  /* Set the Proj Proj4String.
382  * @param QString theProj4String Proj4 format specifies
383  * (excluding proj and ellips) that define this srs.
384  * @note some content of the PROJ4 string may be stripped off by this
385  * method due to the parsing of the string by OSRNewSpatialReference .
386  * For example input:
387  * +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
388  * Gets stored in the CRS as:
389  * +proj=longlat +datum=WGS84 +no_defs
390  */
391  void setProj4String( QString theProj4String );
395  void setGeographicFlag( bool theGeoFlag );
396 
400  void setEpsg( long theEpsg );
401 
405  void setAuthId( QString theID );
409  void setProjectionAcronym( QString theProjectionAcronym );
413  void setEllipsoidAcronym( QString theEllipsoidAcronym );
414 
417  void debugPrint();
418 
420  typedef QMap<QString, QString> RecordMap;
427  RecordMap getRecord( QString theSql );
428 
429  // Open SQLite db and show message if cannot be opened
430  // returns the same code as sqlite3_open
431  static int openDb( QString path, sqlite3 **db, bool readonly = true );
432 
434  long mSrsId;
436  QString mDescription;
438  QString mProjectionAcronym;
440  QString mEllipsoidAcronym;
442  bool mGeoFlag;
444  QGis::UnitType mMapUnits;
446  long mSRID;
448  QString mAuthId;
450  bool mIsValidFlag;
451 
453  void setMapUnits();
454 
456  long getRecordCount();
457 
459  static QString quotedValue( QString value );
460 
462 
463  bool loadFromDb( QString db, QString expression, QString value );
464 
465  QString mValidationHint;
466  mutable QString mWkt;
467  mutable QString mProj4;
468 
469  static bool loadIDs( QHash<int, QString> &wkts );
470  static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
471  static bool syncDatumTransform( const QString& dbPath );
472 
474  mutable int mAxisInverted;
475 
476  static CUSTOM_CRS_VALIDATION mCustomSrsValidation;
477 };
478 
479 
481 inline std::ostream& operator << ( std::ostream& os, const QgsCoordinateReferenceSystem &r )
482 {
483  QString mySummary( "\n\tSpatial Reference System:" );
484  mySummary += "\n\t\tDescription : ";
485  if ( !r.description().isNull() )
486  {
487  mySummary += r.description();
488  }
489  else
490  {
491  mySummary += "Undefined";
492  }
493  mySummary += "\n\t\tProjection : ";
494  if ( !r.projectionAcronym().isNull() )
495  {
496  mySummary += r.projectionAcronym();
497  }
498  else
499  {
500  mySummary += "Undefined";
501  }
502 
503  mySummary += "\n\t\tEllipsoid : ";
504  if ( !r.ellipsoidAcronym().isNull() )
505  {
506  mySummary += r.ellipsoidAcronym();
507  }
508  else
509  {
510  mySummary += "Undefined";
511  }
512 
513  mySummary += "\n\t\tProj4String : ";
514  if ( !r.toProj4().isNull() )
515  {
516  mySummary += r.toProj4();
517  }
518  else
519  {
520  mySummary += "Undefined";
521  }
522  // Using streams we need to use local 8 Bit
523  return os << mySummary.toLocal8Bit().data() << std::endl;
524 }
525 
526 #endif // QGSCOORDINATEREFERENCESYSTEM_H