QGIS API Documentation  2.6.0-Brighton
 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 
353  // Mutators -----------------------------------
354  // We don't want to expose these to the public api since they wont create
355  // a fully valid crs. Programmers should use the createFrom* methods rather
356  private:
361  static QString proj4FromSrsId( const int theSrsId );
362 
366  void setInternalId( long theSrsId );
370  void setSrid( long theSrid );
374  void setDescription( QString theDescription );
375  /* Set the Proj Proj4String.
376  * @param QString theProj4String Proj4 format specifies
377  * (excluding proj and ellips) that define this srs.
378  * @note some content of the PROJ4 string may be stripped off by this
379  * method due to the parsing of the string by OSRNewSpatialReference .
380  * For example input:
381  * +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
382  * Gets stored in the CRS as:
383  * +proj=longlat +datum=WGS84 +no_defs
384  */
385  void setProj4String( QString theProj4String );
389  void setGeographicFlag( bool theGeoFlag );
390 
394  void setEpsg( long theEpsg );
395 
399  void setAuthId( QString theID );
403  void setProjectionAcronym( QString theProjectionAcronym );
407  void setEllipsoidAcronym( QString theEllipsoidAcronym );
408 
411  void debugPrint();
412 
414  typedef QMap<QString, QString> RecordMap;
421  RecordMap getRecord( QString theSql );
422 
423  // Open SQLite db and show message if cannot be opened
424  // returns the same code as sqlite3_open
425  static int openDb( QString path, sqlite3 **db, bool readonly = true );
426 
428  long mSrsId;
430  QString mDescription;
432  QString mProjectionAcronym;
434  QString mEllipsoidAcronym;
436  bool mGeoFlag;
438  QGis::UnitType mMapUnits;
440  long mSRID;
442  QString mAuthId;
444  bool mIsValidFlag;
445 
447  void setMapUnits();
448 
450  long getRecordCount();
451 
453  static QString quotedValue( QString value );
454 
456 
457  bool loadFromDb( QString db, QString expression, QString value );
458 
459  QString mValidationHint;
460  mutable QString mWkt;
461  mutable QString mProj4;
462 
463  static bool loadIDs( QHash<int, QString> &wkts );
464  static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
465  static bool syncDatumTransform( const QString& dbPath );
466 
468  mutable int mAxisInverted;
469 
470  static CUSTOM_CRS_VALIDATION mCustomSrsValidation;
471 };
472 
473 
475 inline std::ostream& operator << ( std::ostream& os, const QgsCoordinateReferenceSystem &r )
476 {
477  QString mySummary( "\n\tSpatial Reference System:" );
478  mySummary += "\n\t\tDescription : ";
479  if ( !r.description().isNull() )
480  {
481  mySummary += r.description();
482  }
483  else
484  {
485  mySummary += "Undefined";
486  }
487  mySummary += "\n\t\tProjection : ";
488  if ( !r.projectionAcronym().isNull() )
489  {
490  mySummary += r.projectionAcronym();
491  }
492  else
493  {
494  mySummary += "Undefined";
495  }
496 
497  mySummary += "\n\t\tEllipsoid : ";
498  if ( !r.ellipsoidAcronym().isNull() )
499  {
500  mySummary += r.ellipsoidAcronym();
501  }
502  else
503  {
504  mySummary += "Undefined";
505  }
506 
507  mySummary += "\n\t\tProj4String : ";
508  if ( !r.toProj4().isNull() )
509  {
510  mySummary += r.toProj4();
511  }
512  else
513  {
514  mySummary += "Undefined";
515  }
516  // Using streams we need to use local 8 Bit
517  return os << mySummary.toLocal8Bit().data() << std::endl;
518 }
519 
520 #endif // QGSCOORDINATEREFERENCESYSTEM_H