1
#ifndef QGSSPATIALREFSYS_H
2
#define QGSSPATIALREFSYS_H
19
//gdal and ogr includes
21
#include <ogr_spatialref.h>
22
#include <cpl_error.h>
25
* \class QgsSpatialRefSys
26
* \brief Class for storing a spatial reference system (SRS)
28
class QgsSpatialRefSys
31
//! Default constructor
35
* Constructs a SRS object from a WKT string
36
* @param theWkt A String containing a valid Wkt def
38
explicit QgsSpatialRefSys(QString theWkt);
41
* Constructs a SRS object from the following component parts
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)
52
* @note THIS CTOR WILL PROABBLY BE REMOVED!!!!!!!!!!!!!!!!
54
QgsSpatialRefSys(long theSrsId,
55
QString theDescription,
56
QString theProjectionAcronym,
57
QString theEllipsoidAcronym,
58
QString theProj4String,
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
69
QgsSpatialRefSys(const long theId, SRS_TYPE theType=POSTGIS_SRID);
71
// Assignment operator
72
QgsSpatialRefSys& operator=(const QgsSpatialRefSys& srs);
74
// Misc helper functions -----------------------
76
void createFromId(const long theId, SRS_TYPE theType=POSTGIS_SRID);
79
* \brief Set up this SRS from the given OGC CRS
81
* Sets this SRS to the given OGC WMS-format Coordinate Reference Systems.
83
* \note This function only deals with EPSG labels only at this time.
85
* \retval FALSE if not given an EPSG label
87
bool createFromOgcWmsCrs(QString theCrs);
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.
95
bool createFromSrid(const long theSrid);
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
107
bool createFromWkt(const QString theWkt);
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
116
bool createFromEpsg(const long theEpsg);
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
126
bool createFromSrsId (const long theSrsId);
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).
137
* We try to match the proj string to and srsid using the following logic:
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.
151
* @note If the srs was not matched, we will create a new entry on the users tbl_srs
154
* @param theProjString A proj4 format string
155
* @return bool TRUE if sucess else false
157
bool createFromProj4 (const QString theProjString);
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.
167
* @note It is not usually neccessary to use this function, unless you
168
* are trying to force theis srs to be valid.
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
183
long findMatchingProj();
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
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
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
203
bool equals(QString theProj4String);
204
/*! A helper to get an ogr representation of this srs
205
* @return OGRSpatialReference
207
OGRSpatialReference toOgrSrs();
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
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.
217
* <proj4>+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs </proj4>
218
* <srsid>2585</srsid>
221
* <description>WGS 84</description>
222
* <projectionacronym>longlat</projectionacronym>
223
* <ellipsoidacronym>WGS84</ellipsoidacronym>
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
229
bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
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
235
static QString getProj4FromSrsId(const int theSrsId);
237
// Accessors -----------------------------------
239
/*! Get the SrsId - if possible
240
* @return long theSrsId The internal sqlite3 srs.db primary key for this srs
243
/*! Get the Postgis SRID - if possible.
244
* @return long theSRID The internal postgis SRID for this SRS
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
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
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
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
268
QString proj4String() const;
269
/*! Get this Geographic? flag
270
* @return bool theGeoFlag Whether this is a geographic or projected coordinate system
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
276
QGis::units mapUnits() const;
278
/*! Set the postgis srid for this srs
279
* @return long theSRID the Postgis spatial_ref_sys identifier for this srs (defaults to 0)
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)
287
// Mutators -----------------------------------
289
/*! Set the QGIS SrsId
290
* @param long theSrsId The internal sqlite3 srs.db primary key for this srs
292
void setSrsId(long theSrsId);
293
/*! Set the postgis srid
294
* @param long theSrsId The postgis spatial_ref_sys key for this srs
296
void setSrid(long theSrid);
297
/*! Set the Description
298
* @param QString the Description A textual description of the srs.
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.
304
void setProj4String (QString theProj4String);
305
/*! Set this Geographic? flag
306
* @param bool theGeoFlag Whether this is a geographic or projected coordinate system
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)
312
void setEpsg (long theEpsg);
313
/*! Set the projection acronym
314
* @param QString the acronym (must be a valid proj4 projection acronym)
316
void setProjectionAcronym(QString theProjectionAcronym);
317
/*! Set the ellipsoid acronym
318
* @param QString the acronym (must be a valid proj4 ellipsoid acronym)
320
void setEllipsoidAcronym(QString theEllipsoidAcronym);
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 );
326
//!The internal sqlite3 srs.db primary key for this srs
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
339
QGis::units mMapUnits;
340
//!If available, the Postgis spatial_ref_sys identifier for this srs (defaults to 0)
342
//!If available the ESPG identifier for this srs (defaults to 0)
344
//! Wehter this srs is properly defined and valid
347
//! Work out the projection units and set the appropriate local variable
352
//! Output stream operator
353
inline std::ostream& operator << (std::ostream& os, const QgsSpatialRefSys &r)
355
QString mySummary ("\n\tSpatial Reference System:");
356
mySummary += "\n\t\tDescription : ";
357
if (!r.description().isNull())
359
mySummary += r.description();
363
mySummary += "Undefined" ;
365
mySummary += "\n\t\tProjection : " ;
366
if (!r.projectionAcronym().isNull())
368
mySummary += r.projectionAcronym();
372
mySummary += "Undefined" ;
375
mySummary += "\n\t\tEllipsoid : ";
376
if (!r.ellipsoidAcronym().isNull())
378
mySummary += r.ellipsoidAcronym();
382
mySummary += "Undefined" ;
385
mySummary += "\n\t\tProj4String : " ;
386
if (!r.proj4String().isNull())
388
mySummary += r.proj4String();
392
mySummary += "Undefined" ;
394
// Using streams we need to use local 8 Bit
395
return os << mySummary.toLocal8Bit().data() << std::endl;
400
//! Input stream operator
401
inline std::istream& operator>> (std::istream& str, QgsSpatialRefSys& r)
403
//std::cout << "FIXME FIXME" << __FILE__ << ":" << __LINE__ << std::endl;
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]);
421
#endif // QGSSPATIALREFSYS_H