1
/***************************************************************************
2
QgsCoordinateTransform.h - Coordinate Transforms
5
copyright : (C) 2004 Tim Sutton
6
email : tim at linfiniti.com
7
***************************************************************************/
9
/***************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
16
***************************************************************************/
17
/* $Id: qgscoordinatetransform.h 5429 2006-05-08 15:55:34Z rblazek $ */
18
#ifndef QGSCOORDINATETRANSFORM_H
19
#define QGSCOORDINATETRANSFORM_H
27
#include "qgscsexception.h"
28
#include "qgsspatialrefsys.h"
41
/*! \class QgsCoordinateTransform
42
* \brief Class for doing transforms between two map coordinate systems.
44
* This class can convert map coordinates to a different spatial reference system.
45
* It is normally associated with a map layer and is used to transform between the
46
* layer's coordinate system and the coordinate system of the map canvas, although
47
* it can be used in a more general sense to transform coordinates.
49
* All references to source and destination coordinate systems refer to
50
* layer and map canvas respectively. All operations are from the perspective
51
* of the layer. For example, a forward transformation transforms coordinates from the
52
* layers coordinate system to the map canvas.
54
class QgsCoordinateTransform: public QObject
58
/*! Default constructor. Make sure you use initialised() manually if you use this one! */
59
QgsCoordinateTransform() ;
61
/** Constructs a QgsCoordinateTransform using QgsSpatialRefSys objects.
62
* @param theSource SRS, typically of the layer's coordinate system
63
* @param theDest SRS, typically of the map canvas coordinate system
65
QgsCoordinateTransform(const QgsSpatialRefSys& theSource,
66
const QgsSpatialRefSys& theDest);
69
* Constructs a QgsCoordinateTransform using the Well Known Text representation
70
* of the layer and map canvas coordinate systems
71
* @param theSourceWKT WKT, typically of the layer's coordinate system
72
* @param theDestWKT WKT, typically of the map canvas coordinate system
74
QgsCoordinateTransform(QString theSourceWKT, QString theDestWKT );
77
* Constructs a QgsCoordinateTransform using a Spatial Reference Id
78
* of the layer and map canvas coordinate system as Wkt
79
* @param theSourceSrid Spatial Ref Id of the layer's coordinate system
80
* @param theSourceWKT WKT of the map canvas coordinate system
81
* @param theSourceSRSType On of the enum members defined in QgsSpatialRefSys::SRS_TYPE
83
QgsCoordinateTransform(long theSourceSrid,
85
QgsSpatialRefSys::SRS_TYPE theSourceSRSType = QgsSpatialRefSys::POSTGIS_SRID );
88
~QgsCoordinateTransform();
90
//! Enum used to indicate the direction (forward or inverse) of the transform
91
enum TransformDirection{
92
FORWARD, /*!< Transform from source to destination SRS. */
93
INVERSE /*!< Transform from destination to source SRS. */
97
* Set the source (layer) QgsSpatialRefSys
98
* @param theSRS QgsSpatialRefSys representation of the layer's coordinate system
100
void setSourceSRS(const QgsSpatialRefSys& theSRS);
103
* Mutator for dest QgsSpatialRefSys
104
* @param theSRS of the destination coordinate system
106
void setDestSRS(const QgsSpatialRefSys& theSRS);
109
* Get the QgsSpatialRefSys representation of the layer's coordinate system
110
* @return QgsSpatialRefSys of the layer's coordinate system
112
QgsSpatialRefSys& sourceSRS() { return mSourceSRS; }
115
* Get the QgsSpatialRefSys representation of the map canvas coordinate system
116
* @return QgsSpatialRefSys of the map canvas coordinate system
118
QgsSpatialRefSys& destSRS() { return mDestSRS; }
120
/*! Transform the point from Source Coordinate System to Destination Coordinate System
121
* If the direction is FORWARD then coordinates are transformed from layer CS --> map canvas CS,
122
* otherwise points are transformed from map canvas CS to layerCS.
123
* @param p Point to transform
124
* @param direction TransformDirection (defaults to FORWARD)
125
* @return QgsPoint in Destination Coordinate System
127
QgsPoint transform(const QgsPoint p,TransformDirection direction=FORWARD);
129
/*! Transform the point specified by x,y from Source Coordinate System to Destination Coordinate System
130
* If the direction is FORWARD then coordinates are transformed from layer CS --> map canvas CS,
131
* otherwise points are transformed from map canvas CS to layerCS.
132
* @param x x cordinate of point to transform
133
* @param y y coordinate of point to transform
134
* @param direction TransformDirection (defaults to FORWARD)
135
* @return QgsPoint in Destination Coordinate System
137
QgsPoint transform(const double x, const double y,TransformDirection direction=FORWARD);
139
/*! Transform a QgsRect to the dest Coordinate system
140
* If the direction is FORWARD then coordinates are transformed from layer CS --> map canvas CS,
141
* otherwise points are transformed from map canvas CS to layerCS.
142
* It assumes that rect is a bounding box, and creates a bounding box
143
* in the proejcted CS, so that all points in source rectangle is within
144
* returned rectangle.
145
* @param QgsRect rect to transform
146
* @param direction TransformDirection (defaults to FORWARD)
147
* @return QgsRect in Destination Coordinate System
149
QgsRect transformBoundingBox(const QgsRect theRect,TransformDirection direction=FORWARD);
151
// Same as for the other transform() functions, but alters the x
152
// and y variables in place. The second one works with good old-fashioned
154
void transformInPlace(double& x, double& y, double &z, TransformDirection direction = FORWARD);
156
void transformInPlace(std::vector<double>& x, std::vector<double>& y, std::vector<double>& z,
157
TransformDirection direction = FORWARD);
159
/*! Transform a QgsRect to the dest Coordinate system
160
* If the direction is FORWARD then coordinates are transformed from layer CS --> map canvas CS,
161
* otherwise points are transformed from map canvas CS to layerCS.
162
* @param QgsRect rect to transform
163
* @param direction TransformDirection (defaults to FORWARD)
164
* @return QgsRect in Destination Coordinate System
166
QgsRect transform(const QgsRect theRect,TransformDirection direction=FORWARD);
168
/*! Transform an array of coordinates to a different Coordinate System
169
* If the direction is FORWARD then coordinates are transformed from layer CS --> map canvas CS,
170
* otherwise points are transformed from map canvas CS to layerCS.
171
* @param x x cordinate of point to transform
172
* @param y y coordinate of point to transform
173
* @param direction TransformDirection (defaults to FORWARD)
174
* @return QgsRect in Destination Coordinate System
176
void transformCoords( const int &numPoint, double *x, double *y, double *z,TransformDirection direction=FORWARD);
179
* Flag to indicate whether the coordinate systems have been initialised
180
* @return true if initialised, otherwise false
182
bool isInitialised() {return mInitialisedFlag;};
184
/*! See if the transform short circuits because src and dest are equivalent
185
* @return bool True if it short circuits
187
bool isShortCircuited() {return mShortCircuit;};
190
/*! Change the destination coordinate system by passing it a qgis srsid
191
* A QGIS srsid is a unique key value to an entry on the tbl_srs in the
192
* srs.db sqlite database.
193
* @note This slot will usually be called if the
194
* project properties change and a different coordinate system is
196
* @note This coord transform will be reinitialised when this slot is called
197
* to check if short circuiting is needed or not etc.
198
* @param theSRSID - A long representing the srsid of the srs to be used */
199
void setDestSRSID (long theSRSID);
201
//!initialise is used to actually create the Transformer instance
204
/*! Restores state from the given DOM node.
205
* @param theNode The node from which state will be restored
206
* @return bool True on success, False on failure
208
bool readXML( QDomNode & theNode );
210
/*! Stores state to the given DOM node in the given document
211
* @param theNode The node in which state will be restored
212
* @param theDom The document in which state will be stored
213
* @return bool True on success, False on failure
215
bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
218
/** Signal when an invalid pj_transform() has occured */
219
void invalidTransformInput();
224
* Flag to indicate that the source and destination coordinate systems are
225
* equal and not transformation needs to be done
230
* flag to show whether the transform is properly initialised or not
232
bool mInitialisedFlag;
235
* QgsSpatialRefSys of the source (layer) coordinate system
237
QgsSpatialRefSys mSourceSRS;
240
* QgsSpatialRefSys of the destination (map canvas) coordinate system
242
QgsSpatialRefSys mDestSRS;
245
* Proj4 data structure of the source projection (layer coordinate system)
247
projPJ mSourceProjection;
250
* Proj4 data structure of the destination projection (map canvas coordinate system)
252
projPJ mDestinationProjection;
255
* Finder for PROJ grid files.
260
//! Output stream operator
261
inline std::ostream& operator << (std::ostream& os, const QgsCoordinateTransform &r)
263
QString mySummary ("\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:");
264
mySummary += "\n\tInitialised? : ";
266
if (r.isInitialised())
274
mySummary += "\n\tShort Circuit? : " ;
275
if (r.isShortCircuited())
284
mySummary += "\n\tSource Spatial Ref Sys : ";
287
mySummary << r.sourceSRS();
291
mySummary += "Undefined" ;
294
mySummary += "\n\tDest Spatial Ref Sys : " ;
297
mySummary << r.destSRS();
301
mySummary += "Undefined" ;
304
mySummary+=("\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n");
305
return os << mySummary.toLocal8Bit().data() << std::endl;
309
#endif // QGSCOORDINATETRANSFORM_H