Quantum GIS API Documentation  master-693a1fe
src/core/qgscoordinatereferencesystem.h
Go to the documentation of this file.
00001 /***************************************************************************
00002                              qgscoordinatereferencesystem.h
00003 
00004                              -------------------
00005     begin                : 2007
00006     copyright            : (C) 2007 by Gary E. Sherman
00007     email                : sherman@mrcc.com
00008 ***************************************************************************/
00009 
00010 /***************************************************************************
00011  *                                                                         *
00012  *   This program is free software; you can redistribute it and/or modify  *
00013  *   it under the terms of the GNU General Public License as published by  *
00014  *   the Free Software Foundation; either version 2 of the License, or     *
00015  *   (at your option) any later version.                                   *
00016  *                                                                         *
00017  ***************************************************************************/
00018 #ifndef QGSCOORDINATEREFERENCESYSTEM_H
00019 #define QGSCOORDINATEREFERENCESYSTEM_H
00020 
00021 //Standard includes
00022 #include <ostream>
00023 
00024 //qt includes
00025 #include <QString>
00026 #include <QMap>
00027 #include <QHash>
00028 
00029 class QDomNode;
00030 class QDomDocument;
00031 
00032 // forward declaration for sqlite3
00033 typedef struct sqlite3 sqlite3;
00034 
00035 //qgis includes
00036 #include "qgis.h"
00037 
00038 #ifdef DEBUG
00039 typedef struct OGRSpatialReferenceHS *OGRSpatialReferenceH;
00040 #else
00041 typedef void *OGRSpatialReferenceH;
00042 #endif
00043 
00044 class QgsCoordinateReferenceSystem;
00045 typedef void ( *CUSTOM_CRS_VALIDATION )( QgsCoordinateReferenceSystem& );
00046 
00050 class CORE_EXPORT QgsCoordinateReferenceSystem
00051 {
00052   public:
00053 
00054     enum CrsType
00055     {
00056       InternalCrsId,
00057       PostgisCrsId,
00058       EpsgCrsId     // deprecated
00059     };
00060 
00062     QgsCoordinateReferenceSystem();
00063 
00064     ~QgsCoordinateReferenceSystem();
00065 
00071     explicit QgsCoordinateReferenceSystem( QString theDefinition );
00072 
00080     QgsCoordinateReferenceSystem( const long theId, CrsType theType = PostgisCrsId );
00081 
00083     QgsCoordinateReferenceSystem( const QgsCoordinateReferenceSystem& srs );
00084 
00086     QgsCoordinateReferenceSystem& operator=( const QgsCoordinateReferenceSystem& srs );
00087 
00088     // Misc helper functions -----------------------
00089 
00090     bool createFromId( const long theId, CrsType theType = PostgisCrsId );
00091 
00099     bool createFromOgcWmsCrs( QString theCrs );
00100 
00107     bool createFromSrid( const long theSrid );
00108 
00119     bool createFromWkt( const QString theWkt );
00120 
00129     bool createFromSrsId( const long theSrsId );
00130 
00157     bool createFromProj4( const QString theProjString );
00158 
00166     bool createFromString( const QString theDefinition );
00167 
00183     bool createFromUserInput( const QString theDefinition );
00184 
00195     static void setupESRIWktFix();
00196 
00198     bool isValid() const;
00199 
00209     void validate();
00210 
00222     long findMatchingProj();
00223 
00227     bool operator==( const QgsCoordinateReferenceSystem &theSrs ) const;
00231     bool operator!=( const QgsCoordinateReferenceSystem &theSrs ) const;
00232 
00237     bool readXML( QDomNode & theNode );
00255     bool writeXML( QDomNode & theNode, QDomDocument & theDoc ) const;
00256 
00257 
00262     static void setCustomSrsValidation( CUSTOM_CRS_VALIDATION f );
00263 
00267     static CUSTOM_CRS_VALIDATION customSrsValidation();
00268 
00269     // Accessors -----------------------------------
00270 
00274     long srsid() const;
00275 
00279     long postgisSrid() const;
00280 
00285     QString authid() const;
00286 
00291     QString description() const;
00292 
00297     QString projectionAcronym() const;
00298 
00303     QString ellipsoidAcronym() const;
00304 
00308     QString toWkt() const;
00309 
00317     QString toProj4() const;
00318 
00322     bool geographicFlag() const;
00323 
00328     bool axisInverted() const;
00329 
00333     QGis::UnitType mapUnits() const;
00334 
00335 
00336     // Mutators -----------------------------------
00339     void setValidationHint( QString html );
00340 
00343     QString validationHint();
00349     static int syncDb();
00350 
00351 
00355     bool saveAsUserCRS( QString name );
00356 
00357     // Mutators -----------------------------------
00358     // We don't want to expose these to the public api since they wont create
00359     // a fully valid crs. Programmers should use the createFrom* methods rather
00360   private:
00365     static QString proj4FromSrsId( const int theSrsId );
00366 
00370     void setInternalId( long theSrsId );
00374     void setSrid( long theSrid );
00378     void setDescription( QString theDescription );
00379     /* Set the Proj Proj4String.
00380      * @param  QString theProj4String Proj4 format specifies
00381      * (excluding proj and ellips) that define this srs.
00382      * @note some content of the PROJ4 string may be stripped off by this
00383      * method due to the parsing of the string by OSRNewSpatialReference .
00384      * For example input:
00385      * +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
00386      * Gets stored in the CRS as:
00387      * +proj=longlat +datum=WGS84 +no_defs
00388      */
00389     void setProj4String( QString theProj4String );
00393     void setGeographicFlag( bool theGeoFlag );
00394 
00398     void setEpsg( long theEpsg );
00399 
00403     void setAuthId( QString theID );
00407     void setProjectionAcronym( QString theProjectionAcronym );
00411     void setEllipsoidAcronym( QString theEllipsoidAcronym );
00412 
00415     void debugPrint();
00416 
00418     typedef QMap<QString, QString> RecordMap;
00425     RecordMap getRecord( QString theSql );
00426 
00427     // Open SQLite db and show message if cannot be opened
00428     // returns the same code as sqlite3_open
00429     static int openDb( QString path, sqlite3 **db, bool readonly = true );
00430 
00432     long    mSrsId;
00434     QString mDescription;
00436     QString mProjectionAcronym ;
00438     QString mEllipsoidAcronym;
00440     bool    mGeoFlag;
00442     QGis::UnitType mMapUnits;
00444     long    mSRID;
00446     QString mAuthId;
00448     bool mIsValidFlag;
00449 
00451     void setMapUnits();
00452 
00454     long getRecordCount();
00455 
00457     static QString quotedValue( QString value );
00458 
00459     OGRSpatialReferenceH mCRS;
00460 
00461     bool loadFromDb( QString db, QString expression, QString value );
00462 
00463     QString mValidationHint;
00464     mutable QString mWkt;
00465 
00466     static bool loadIDs( QHash<int, QString> &wkts );
00467     static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
00468 
00470     mutable int mAxisInverted;
00471 
00472     static CUSTOM_CRS_VALIDATION mCustomSrsValidation;
00473 };
00474 
00475 
00477 inline std::ostream& operator << ( std::ostream& os, const QgsCoordinateReferenceSystem &r )
00478 {
00479   QString mySummary( "\n\tSpatial Reference System:" );
00480   mySummary += "\n\t\tDescription : ";
00481   if ( !r.description().isNull() )
00482   {
00483     mySummary += r.description();
00484   }
00485   else
00486   {
00487     mySummary += "Undefined" ;
00488   }
00489   mySummary += "\n\t\tProjection  : " ;
00490   if ( !r.projectionAcronym().isNull() )
00491   {
00492     mySummary += r.projectionAcronym();
00493   }
00494   else
00495   {
00496     mySummary += "Undefined" ;
00497   }
00498 
00499   mySummary += "\n\t\tEllipsoid   : ";
00500   if ( !r.ellipsoidAcronym().isNull() )
00501   {
00502     mySummary += r.ellipsoidAcronym();
00503   }
00504   else
00505   {
00506     mySummary += "Undefined" ;
00507   }
00508 
00509   mySummary += "\n\t\tProj4String  : " ;
00510   if ( !r.toProj4().isNull() )
00511   {
00512     mySummary += r.toProj4();
00513   }
00514   else
00515   {
00516     mySummary += "Undefined" ;
00517   }
00518   // Using streams we need to use local 8 Bit
00519   return os << mySummary.toLocal8Bit().data() << std::endl;
00520 }
00521 
00522 #endif // QGSCOORDINATEREFERENCESYSTEM_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines