~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/gui/qgsspatialrefsys.h

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifndef QGSSPATIALREFSYS_H
2
 
#define QGSSPATIALREFSYS_H
3
 
 
4
 
//Standard includes
5
 
#include <ostream>
6
 
#include <istream>
7
 
 
8
 
//qt includes
9
 
#include <QString>
10
 
#include <QMap>
11
 
class QDomNode;
12
 
class QDomDocument;
13
 
 
14
 
#include <sqlite3.h>
15
 
 
16
 
//qgis includes
17
 
#include <qgis.h>
18
 
 
19
 
//gdal and ogr includes
20
 
#include <ogr_api.h>
21
 
#include <ogr_spatialref.h>
22
 
#include <cpl_error.h>
23
 
 
24
 
/*!
25
 
 * \class QgsSpatialRefSys
26
 
 * \brief Class for storing a spatial reference system (SRS)
27
 
 */ 
28
 
class QgsSpatialRefSys
29
 
{
30
 
    public:
31
 
        //! Default constructor
32
 
 
33
 
        QgsSpatialRefSys();
34
 
        /*! 
35
 
         * Constructs a SRS object from a WKT string
36
 
         * @param theWkt A String containing a valid Wkt def
37
 
         */
38
 
        explicit QgsSpatialRefSys(QString theWkt);
39
 
 
40
 
        /*!
41
 
         * Constructs a SRS object from the following component parts
42
 
         *
43
 
         * @param  long theSrsId The internal sqlite3 srs.db primary key for this srs 
44
 
         * @param  QString the Description A textual description of the srs.
45
 
         * @param  QString theProjectionAcronym The official proj4 acronym for the projection family
46
 
         * @param  QString theEllipsoidAcronym The official proj4 acronym for the ellipoid
47
 
         * @param  QString theProj4String Proj4 format specifies (excluding proj and ellips) that define this srs.
48
 
         * @param  bool theGeoFlag Whether this is a geographic or projected coordinate system
49
 
         * @param  long theSRID If available, the Postgis spatial_ref_sys identifier for this srs (defaults to 0)
50
 
         * @param  long theEpsg If available the ESPG identifier for this srs (defaults to 0)
51
 
         *
52
 
         * @note THIS CTOR WILL PROABBLY BE REMOVED!!!!!!!!!!!!!!!!
53
 
         */
54
 
        QgsSpatialRefSys(long theSrsId, 
55
 
                QString theDescription, 
56
 
                QString theProjectionAcronym, 
57
 
                QString theEllipsoidAcronym, 
58
 
                QString theProj4String,
59
 
                long theSRID,
60
 
                long theEpsg,
61
 
                bool theGeoFlag);
62
 
 
63
 
         enum SRS_TYPE {QGIS_SRSID,POSTGIS_SRID, EPSG};
64
 
         /*! Use this constructor when you want to create a SRS object using 
65
 
          *  a postgis SRID, an EPSG id or a QGIS SRS_ID.
66
 
          * @param theId The ID no valid for the chosen coordinate system id type
67
 
          * @param theType One of the types described in QgsSpatialRefSys::SRS_TYPE
68
 
          */
69
 
         QgsSpatialRefSys(const long theId, SRS_TYPE theType=POSTGIS_SRID);
70
 
 
71
 
         // Assignment operator
72
 
         QgsSpatialRefSys& operator=(const QgsSpatialRefSys& srs);
73
 
 
74
 
        // Misc helper functions -----------------------
75
 
 
76
 
        void createFromId(const long theId, SRS_TYPE theType=POSTGIS_SRID);
77
 
 
78
 
        /**
79
 
         * \brief Set up this SRS from the given OGC CRS
80
 
         *
81
 
         * Sets this SRS to the given OGC WMS-format Coordinate Reference Systems.
82
 
         *
83
 
         * \note This function only deals with EPSG labels only at this time.
84
 
         *
85
 
         * \retval FALSE if not given an EPSG label
86
 
         */
87
 
        bool createFromOgcWmsCrs(QString theCrs);
88
 
 
89
 
        /*! Set up this srs by fetching the appropriate information from the 
90
 
         * sqlite backend. First the system level read only srs.db will be checked
91
 
         * and then the users ~/.qgis/qgis.db database will be checked for a match.
92
 
         * @note Any members will be overwritten during this process.
93
 
         * @param theSrid The postgis SRID for the desired spatial reference system.
94
 
         */
95
 
        bool createFromSrid(const long theSrid);
96
 
 
97
 
        /*! Set up this srs using a WKT spatial ref sys definition. 
98
 
         * The wkt will be converted to a proj4 string using OGR helper
99
 
         * functions. After this the srs databasses will be searched for matches.
100
 
         * First the system level read only srs.db will be checked
101
 
         * and then the users ~/.qgis/qgis.db database will be checked for a match.
102
 
         * @note Any members will be overwritten during this process.
103
 
         * @note SRID and EPSG may be blank if no match can be found on srs db.
104
 
         * @param theWkt The WKT for the desired spatial reference system.
105
 
         * @return bool TRUE if sucess else false
106
 
         */
107
 
        bool createFromWkt(const QString theWkt);
108
 
 
109
 
        /*! Set up this srs by fetching the appropriate information from the 
110
 
         * sqlite backend. First the system level read only srs.db will be checked
111
 
         * and then the users ~/.qgis/qgis.db database will be checked for a match.
112
 
         * @note Any members will be overwritten during this process.
113
 
         * @param theEpsg The EPSG for the desired spatial reference system.
114
 
         * @return bool TRUE if sucess else false
115
 
         */
116
 
        bool createFromEpsg(const long theEpsg);
117
 
 
118
 
        /*! Set up this srs by fetching the appropriate information from the 
119
 
         * sqlite backend. If the srsid is < 100000, only the system srs.db 
120
 
         * will be checked. If the srsid > 100000 the srs will be retrieved from 
121
 
         * the ~/.qgis/qgis.db
122
 
         * @note Any members will be overwritten during this process.
123
 
         * @param theSrsId The QGIS SrsId for the desired spatial reference system.
124
 
         * @return bool TRUE if sucess else false
125
 
         */
126
 
        bool createFromSrsId (const long theSrsId);
127
 
 
128
 
        /*! Set up this srs by passing it a proj4 style formatted string.
129
 
         * The string will be parsed and the projection and ellipsoid
130
 
         * members set and the remainder of the proj4 string will be stored
131
 
         * in the parameters member. The reason for this is so that we
132
 
         * can easily present the user with 'natural language' representation
133
 
         * of the projection and ellipsoid by looking them up in the srs.bs sqlite 
134
 
         * database. Also having the ellpse and proj elements stripped out
135
 
         * is hepful to speed up globbing queries (see below).
136
 
         * 
137
 
         * We try to match the proj string to and srsid using the following logic: 
138
 
         *
139
 
         * - perform a whole text search on srs name (if not null). The srs name will 
140
 
         *   have been set if this method has been delegated to from createFromWkt.
141
 
         * - if the above does not match perform a whole text search on proj4 string (if not null)
142
 
         * - if none of the above match convert the proj4 string to an OGR SRS
143
 
         *   then check if its a geocs or a proj cs (using ogr isGeographic)
144
 
         *   then sequentially walk through the database (first users qgis.db srs tbl then
145
 
         *   system srs.db tbl), converting each entry into an ogr srs and using isSame
146
 
         *   or isSameGeocs (essentially calling the == overloaded operator). We'll try to 
147
 
         *   be smart about this and first parse out the proj and ellpse strings and only 
148
 
         *   check for a match in entities that have the same ellps and proj entries so 
149
 
         *   that it doesnt munch yer cpu so much.
150
 
         *
151
 
         * @note If the srs was not matched, we will create a new entry on the users tbl_srs 
152
 
         *    for this srs.
153
 
         *
154
 
         * @param theProjString A proj4 format string
155
 
         * @return bool TRUE if sucess else false
156
 
         */
157
 
        bool createFromProj4 (const QString theProjString);
158
 
 
159
 
        /*! Find out whether this SRS is correctly initialised and useable */
160
 
        bool isValid() const;
161
 
        /*! Perform some validation on this SRS. If the sts doesnt validate the
162
 
         *  default behaviour settings for layers with unknown SRS will be 
163
 
         * consulted and acted on accordingly. By hell or high water this
164
 
         * method will do its best to make sure that this SRS is valid - even
165
 
         * if that involves resorting to a hard coded default of geocs:wgs84.
166
 
         *
167
 
         * @note It is not usually neccessary to use this function, unless you
168
 
         * are trying to force theis srs to be valid.
169
 
         */
170
 
        void validate();
171
 
 
172
 
        /*! This is a globbing function to try to find a record in the database
173
 
         *  that matches a SRS defined only by a proj4string. The goal is to 
174
 
         *  learn what the tbl_srs.srs_id value is for the SRS. Internally 
175
 
         *  the source SRS is converted to and OGR srs object using the proj4string
176
 
         *  and then every record in the database that matches projection and ellipsoid
177
 
         *  will be converted to an OGR srs in turn and compared to the source SRS.
178
 
         *  There are some gotchas with using ogr isSame() srs comparison, but
179
 
         *  its more effective than using straight string comparison of proj4params.
180
 
         *  @note The ellipsoid and projection acronyms must be set as well as the proj4string!
181
 
         *  @return lomg the SrsId of the matched SRS
182
 
         */
183
 
        long findMatchingProj();
184
 
         
185
 
        /*! A string based associative array used for passing records around */
186
 
        typedef QMap<QString, QString> RecordMap;
187
 
        /*! Get a record from the srs.db or qgis.db backends, given an sql statment.
188
 
         * @note only handles queries that return a single record.
189
 
         * @note it will first try the system srs.db then the users qgis.db!
190
 
         * @param QString The sql query to execute
191
 
         * @return QMap An associative array of field name <-> value pairs
192
 
         */
193
 
         RecordMap getRecord(QString theSql);
194
 
        /*! Overloaded == operator used to compare to SRS's.
195
 
         *  Internally it will delegate to the equals method described below
196
 
         */
197
 
         bool operator==(const QgsSpatialRefSys &theSrs);
198
 
        /*! Overloaded == operator used to compare to SRS's.
199
 
         *  Internally it will use OGR isSameSRS() or isSameGeoSRS() methods as appropriate.
200
 
         *  Additionally logic may also be applied if the result from the OGR methods
201
 
         *  is inconclusive.
202
 
         */
203
 
         bool equals(QString theProj4String);
204
 
         /*! A helper to get an ogr representation of this srs
205
 
          * @return OGRSpatialReference
206
 
          */
207
 
         OGRSpatialReference toOgrSrs();
208
 
 
209
 
         /*! Restores state from the given DOM node.
210
 
         * @param theNode The node from which state will be restored
211
 
         * @return bool True on success, False on failure
212
 
         */
213
 
          bool readXML( QDomNode & theNode );
214
 
        /*! Stores state to the given DOM node in the given document.
215
 
         * Below is an example of the generated tag.
216
 
         *  <spatialrefsys>
217
 
         *      <proj4>+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs </proj4>
218
 
         *       <srsid>2585</srsid>
219
 
         *       <srid>4326</srid>
220
 
         *       <epsg>4326</epsg>
221
 
         *       <description>WGS 84</description>
222
 
         *       <projectionacronym>longlat</projectionacronym>
223
 
         *       <ellipsoidacronym>WGS84</ellipsoidacronym>
224
 
         *   </spatialrefsys>
225
 
         * @param theNode The node in which state will be restored
226
 
         * @param theDom The document in which state will be stored
227
 
         * @return bool True on success, False on failure
228
 
         */
229
 
          bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
230
 
 
231
 
         /** A static helper function to find out the proj4 string for a srsid
232
 
           * @param int theSrsId The srsid used for the lookup
233
 
           * @return QString The proj4 string
234
 
         */
235
 
         static QString getProj4FromSrsId(const int theSrsId);
236
 
 
237
 
        // Accessors -----------------------------------
238
 
 
239
 
         /*! Get the SrsId - if possible
240
 
         *  @return  long theSrsId The internal sqlite3 srs.db primary key for this srs 
241
 
         */
242
 
        long srsid() const;
243
 
        /*! Get the Postgis SRID - if possible.
244
 
        *  @return  long theSRID The internal postgis SRID for this SRS
245
 
        */
246
 
        long srid() const;
247
 
        /*! Get the Description
248
 
         * @return  QString the Description A textual description of the srs.
249
 
         * @note A zero length string will be returned if the description is uninitialised
250
 
         */
251
 
        QString description () const;
252
 
        /*! Get the Projection Acronym
253
 
         * @return  QString theProjectionAcronym The official proj4 acronym for the projection family
254
 
         * @note A zero length string will be returned if the projectionAcronym is uninitialised
255
 
         */
256
 
        QString projectionAcronym() const;
257
 
        /*! Get the Ellipsoid Acronym
258
 
         * @return  QString theEllipsoidAcronym The official proj4 acronym for the ellipoid
259
 
         * @note A zero length string will be returned if the ellipsoidAcronym is uninitialised
260
 
         */
261
 
        QString ellipsoidAcronym () const;
262
 
        /** Get the Proj Proj4String. If proj and ellps keys are found in the parameters,
263
 
         * they will be stripped out and the Projection and ellipsoid acronyms will be
264
 
         * overridden with these.
265
 
         * @return  QString theProj4String Proj4 format specifies that define this srs.
266
 
         * @note A zero length string will be returned if the proj4String is uninitialised
267
 
         */
268
 
        QString proj4String() const;
269
 
        /*! Get this Geographic? flag
270
 
         * @return  bool theGeoFlag Whether this is a geographic or projected coordinate system
271
 
         */
272
 
        bool geographicFlag () const;
273
 
        /*! Get the units that the projection is in
274
 
         * @return QGis::units that gives the units for the coordinate system
275
 
         */
276
 
        QGis::units mapUnits() const;
277
 
 
278
 
        /*! Set the postgis srid for this srs
279
 
         * @return  long theSRID the Postgis spatial_ref_sys identifier for this srs (defaults to 0)
280
 
         */
281
 
        long postgisSrid () const;
282
 
        /*! Set the EPSG identifier for this srs
283
 
         * @return  long theEpsg the ESPG identifier for this srs (defaults to 0)
284
 
         */
285
 
        long epsg () const;
286
 
 
287
 
        // Mutators -----------------------------------
288
 
 
289
 
        /*! Set the QGIS  SrsId
290
 
         *  @param  long theSrsId The internal sqlite3 srs.db primary key for this srs 
291
 
         */
292
 
        void setSrsId(long theSrsId);
293
 
        /*! Set the postgis srid
294
 
         *  @param  long theSrsId The postgis spatial_ref_sys key for this srs 
295
 
         */
296
 
        void setSrid(long theSrid);
297
 
        /*! Set the Description
298
 
         * @param  QString the Description A textual description of the srs.
299
 
         */
300
 
        void setDescription (QString theDescription);
301
 
        /* Set the Proj Proj4String. 
302
 
         * @param  QString theProj4String Proj4 format specifies (excluding proj and ellips) that define this srs.
303
 
         */
304
 
        void setProj4String (QString theProj4String);
305
 
        /*! Set this Geographic? flag
306
 
         * @param  bool theGeoFlag Whether this is a geographic or projected coordinate system
307
 
         */
308
 
        void setGeographicFlag (bool theGeoFlag);
309
 
        /*! Set the EPSG identifier for this srs
310
 
         * @param  long theEpsg the ESPG identifier for this srs (defaults to 0)
311
 
         */
312
 
        void setEpsg (long theEpsg);
313
 
        /*! Set the projection acronym
314
 
         * @param QString the acronym (must be a valid proj4 projection acronym)
315
 
         */
316
 
        void setProjectionAcronym(QString theProjectionAcronym);
317
 
        /*! Set the ellipsoid acronym
318
 
         * @param QString the acronym (must be a valid proj4 ellipsoid acronym)
319
 
         */
320
 
        void setEllipsoidAcronym(QString theEllipsoidAcronym);
321
 
    private:
322
 
        // Open SQLite db and show message if ccannot be opened
323
 
        // returns the same code as sqlite3_open
324
 
        static int openDb ( QString path, sqlite3 **db ); 
325
 
 
326
 
        //!The internal sqlite3 srs.db primary key for this srs 
327
 
        long    mSrsId;
328
 
        //!A textual description of the srs.
329
 
        QString mDescription;
330
 
        //!The official proj4 acronym for the projection family
331
 
        QString mProjectionAcronym ;
332
 
        //!The official proj4 acronym for the ellipoid
333
 
        QString mEllipsoidAcronym;
334
 
        //!Proj4 format specifies (excluding proj and ellips) that define this srs.
335
 
        QString mProj4String ;
336
 
        //!Whether this is a geographic or projected coordinate system
337
 
        bool    mGeoFlag;
338
 
        //! The map units
339
 
        QGis::units mMapUnits;
340
 
        //!If available, the Postgis spatial_ref_sys identifier for this srs (defaults to 0)
341
 
        long    mSRID;
342
 
        //!If available the ESPG identifier for this srs (defaults to 0)
343
 
        long    mEpsg ;
344
 
        //! Wehter this srs is properly defined and valid
345
 
        bool mIsValidFlag;
346
 
 
347
 
        //! Work out the projection units and set the appropriate local variable
348
 
        void setMapUnits();
349
 
};
350
 
 
351
 
 
352
 
//! Output stream operator
353
 
inline std::ostream& operator << (std::ostream& os, const QgsSpatialRefSys &r)
354
 
{
355
 
  QString mySummary ("\n\tSpatial Reference System:");
356
 
  mySummary += "\n\t\tDescription : ";
357
 
  if (!r.description().isNull()) 
358
 
  {
359
 
    mySummary += r.description();
360
 
  }
361
 
  else
362
 
  {
363
 
    mySummary += "Undefined" ;
364
 
  }
365
 
  mySummary += "\n\t\tProjection  : " ;
366
 
  if (!r.projectionAcronym().isNull()) 
367
 
  {
368
 
    mySummary += r.projectionAcronym();
369
 
  }
370
 
  else
371
 
  {
372
 
    mySummary += "Undefined" ;
373
 
  }
374
 
 
375
 
  mySummary += "\n\t\tEllipsoid   : "; 
376
 
  if (!r.ellipsoidAcronym().isNull()) 
377
 
  {
378
 
    mySummary += r.ellipsoidAcronym();
379
 
  }
380
 
  else
381
 
  {
382
 
    mySummary += "Undefined" ;
383
 
  }
384
 
 
385
 
  mySummary += "\n\t\tProj4String  : " ;
386
 
  if (!r.proj4String().isNull()) 
387
 
  {
388
 
    mySummary += r.proj4String();
389
 
  }
390
 
  else
391
 
  {
392
 
    mySummary += "Undefined" ;
393
 
  }
394
 
  // Using streams we need to use local 8 Bit
395
 
  return os << mySummary.toLocal8Bit().data() << std::endl;
396
 
}
397
 
 
398
 
 
399
 
/*
400
 
//! Input stream operator
401
 
inline std::istream& operator>> (std::istream& str, QgsSpatialRefSys& r)
402
 
{
403
 
  //std::cout << "FIXME FIXME" << __FILE__ << ":" << __LINE__ << std::endl;
404
 
  std::string s;
405
 
  str >> s;
406
 
  
407
 
  //QString srs = s.c_str();
408
 
  // split the string into the parts to created the object
409
 
  //QStringList parts = QStringList::split(QRegExp("\t"),srs);
410
 
  // r.setSrid(parts[0]);
411
 
  // r.setAuthName(parts[1]);
412
 
  // r.setAuthSrid(parts[2]);
413
 
  // r.setSrText(parts[3]);
414
 
  // r.setProjText(parts[4]);
415
 
  // r.setName(parts[5]);
416
 
  //
417
 
  return str;
418
 
  
419
 
420
 
*/
421
 
#endif // QGSSPATIALREFSYS_H