QGIS API Documentation  2.2.0-Valmiera
 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 
183  bool createFromUserInput( const QString &theDefinition );
184 
195  static void setupESRIWktFix();
196 
198  bool isValid() const;
199 
209  void validate();
210 
222  long findMatchingProj();
223 
227  bool operator==( const QgsCoordinateReferenceSystem &theSrs ) const;
231  bool operator!=( const QgsCoordinateReferenceSystem &theSrs ) const;
232 
237  bool readXML( QDomNode & theNode );
255  bool writeXML( QDomNode & theNode, QDomDocument & theDoc ) const;
256 
257 
262  static void setCustomSrsValidation( CUSTOM_CRS_VALIDATION f );
263 
267  static CUSTOM_CRS_VALIDATION customSrsValidation();
268 
269  // Accessors -----------------------------------
270 
274  long srsid() const;
275 
279  long postgisSrid() const;
280 
285  QString authid() const;
286 
291  QString description() const;
292 
297  QString projectionAcronym() const;
298 
303  QString ellipsoidAcronym() const;
304 
308  QString toWkt() const;
309 
317  QString toProj4() const;
318 
322  bool geographicFlag() const;
323 
328  bool axisInverted() const;
329 
333  QGis::UnitType mapUnits() const;
334 
335 
336  // Mutators -----------------------------------
339  void setValidationHint( QString html );
340 
343  QString validationHint();
349  static int syncDb();
350 
351 
355  bool saveAsUserCRS( QString name );
356 
358  QString geographicCRSAuthId() const;
359 
360  // Mutators -----------------------------------
361  // We don't want to expose these to the public api since they wont create
362  // a fully valid crs. Programmers should use the createFrom* methods rather
363  private:
368  static QString proj4FromSrsId( const int theSrsId );
369 
373  void setInternalId( long theSrsId );
377  void setSrid( long theSrid );
381  void setDescription( QString theDescription );
382  /* Set the Proj Proj4String.
383  * @param QString theProj4String Proj4 format specifies
384  * (excluding proj and ellips) that define this srs.
385  * @note some content of the PROJ4 string may be stripped off by this
386  * method due to the parsing of the string by OSRNewSpatialReference .
387  * For example input:
388  * +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
389  * Gets stored in the CRS as:
390  * +proj=longlat +datum=WGS84 +no_defs
391  */
392  void setProj4String( QString theProj4String );
396  void setGeographicFlag( bool theGeoFlag );
397 
401  void setEpsg( long theEpsg );
402 
406  void setAuthId( QString theID );
410  void setProjectionAcronym( QString theProjectionAcronym );
414  void setEllipsoidAcronym( QString theEllipsoidAcronym );
415 
418  void debugPrint();
419 
421  typedef QMap<QString, QString> RecordMap;
428  RecordMap getRecord( QString theSql );
429 
430  // Open SQLite db and show message if cannot be opened
431  // returns the same code as sqlite3_open
432  static int openDb( QString path, sqlite3 **db, bool readonly = true );
433 
435  long mSrsId;
437  QString mDescription;
443  bool mGeoFlag;
447  long mSRID;
449  QString mAuthId;
452 
454  void setMapUnits();
455 
457  long getRecordCount();
458 
460  static QString quotedValue( QString value );
461 
463 
464  bool loadFromDb( QString db, QString expression, QString value );
465 
467  mutable QString mWkt;
468  mutable QString mProj4;
469 
470  static bool loadIDs( QHash<int, QString> &wkts );
471  static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
472  static bool syncDatumTransform( const QString& dbPath );
473 
475  mutable int mAxisInverted;
476 
478 };
479 
480 
482 inline std::ostream& operator << ( std::ostream& os, const QgsCoordinateReferenceSystem &r )
483 {
484  QString mySummary( "\n\tSpatial Reference System:" );
485  mySummary += "\n\t\tDescription : ";
486  if ( !r.description().isNull() )
487  {
488  mySummary += r.description();
489  }
490  else
491  {
492  mySummary += "Undefined" ;
493  }
494  mySummary += "\n\t\tProjection : " ;
495  if ( !r.projectionAcronym().isNull() )
496  {
497  mySummary += r.projectionAcronym();
498  }
499  else
500  {
501  mySummary += "Undefined" ;
502  }
503 
504  mySummary += "\n\t\tEllipsoid : ";
505  if ( !r.ellipsoidAcronym().isNull() )
506  {
507  mySummary += r.ellipsoidAcronym();
508  }
509  else
510  {
511  mySummary += "Undefined" ;
512  }
513 
514  mySummary += "\n\t\tProj4String : " ;
515  if ( !r.toProj4().isNull() )
516  {
517  mySummary += r.toProj4();
518  }
519  else
520  {
521  mySummary += "Undefined" ;
522  }
523  // Using streams we need to use local 8 Bit
524  return os << mySummary.toLocal8Bit().data() << std::endl;
525 }
526 
527 #endif // QGSCOORDINATEREFERENCESYSTEM_H