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