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
***************************************************************************/
18
#ifndef QGSCOORDINATETRANSFORM_H
19
#define QGSCOORDINATETRANSFORM_H
26
#include "qgsrectangle.h"
27
#include "qgscsexception.h"
28
#include "qgscoordinatereferencesystem.h"
40
* Class for doing transforms between two map coordinate systems.
42
* This class can convert map coordinates to a different coordinate reference system.
43
* It is normally associated with a map layer and is used to transform between the
44
* layer's coordinate system and the coordinate system of the map canvas, although
45
* it can be used in a more general sense to transform coordinates.
47
* All references to source and destination coordinate systems refer to
48
* layer and map canvas respectively. All operations are from the perspective
49
* of the layer. For example, a forward transformation transforms coordinates from the
50
* layers coordinate system to the map canvas.
52
class CORE_EXPORT QgsCoordinateTransform: public QObject
56
/*! Default constructor. Make sure you use initialised() manually if you use this one! */
57
QgsCoordinateTransform() ;
59
/** Constructs a QgsCoordinateTransform using QgsCoordinateReferenceSystem objects.
60
* @param theSource CRS, typically of the layer's coordinate system
61
* @param theDest CRS, typically of the map canvas coordinate system
63
QgsCoordinateTransform( const QgsCoordinateReferenceSystem& theSource,
64
const QgsCoordinateReferenceSystem& theDest );
66
/** Constructs a QgsCoordinateTransform using CRS ID of source and destination CRS */
67
QgsCoordinateTransform( long theSourceSrsId, long theDestSrsId );
70
* Constructs a QgsCoordinateTransform using the Well Known Text representation
71
* of the layer and map canvas coordinate systems
72
* @param theSourceWkt Wkt, typically of the layer's coordinate system
73
* @param theDestWkt Wkt, typically of the map canvas coordinate system
75
QgsCoordinateTransform( QString theSourceWkt, QString theDestWkt );
78
* Constructs a QgsCoordinateTransform using a Spatial Reference Id
79
* of the layer and map canvas coordinate system as Wkt
80
* @param theSourceSrid Spatial Ref Id of the layer's coordinate system
81
* @param theSourceWkt Wkt of the map canvas coordinate system
82
* @param theSourceCRSType On of the enum members defined in QgsCoordinateReferenceSystem::CrsType
84
QgsCoordinateTransform( long theSourceSrid,
86
QgsCoordinateReferenceSystem::CrsType theSourceCRSType = QgsCoordinateReferenceSystem::PostgisCrsId );
89
~QgsCoordinateTransform();
91
//! Enum used to indicate the direction (forward or inverse) of the transform
92
enum TransformDirection
94
ForwardTransform, /*!< Transform from source to destination CRS. */
95
ReverseTransform /*!< Transform from destination to source CRS. */
99
* Set the source (layer) QgsCoordinateReferenceSystem
100
* @param theCRS QgsCoordinateReferenceSystem representation of the layer's coordinate system
102
void setSourceCrs( const QgsCoordinateReferenceSystem& theCRS );
105
* Mutator for dest QgsCoordinateReferenceSystem
106
* @param theCRS of the destination coordinate system
108
void setDestCRS( const QgsCoordinateReferenceSystem& theCRS );
111
* Get the QgsCoordinateReferenceSystem representation of the layer's coordinate system
112
* @return QgsCoordinateReferenceSystem of the layer's coordinate system
114
QgsCoordinateReferenceSystem& sourceCrs() { return mSourceCRS; }
117
* Get the QgsCoordinateReferenceSystem representation of the map canvas coordinate system
118
* @return QgsCoordinateReferenceSystem of the map canvas coordinate system
120
QgsCoordinateReferenceSystem& destCRS() { return mDestCRS; }
122
/*! Transform the point from Source Coordinate System to Destination Coordinate System
123
* If the direction is ForwardTransform then coordinates are transformed from layer CS --> map canvas CS,
124
* otherwise points are transformed from map canvas CS to layerCS.
125
* @param p Point to transform
126
* @param direction TransformDirection (defaults to ForwardTransform)
127
* @return QgsPoint in Destination Coordinate System
129
QgsPoint transform( const QgsPoint p, TransformDirection direction = ForwardTransform ) const;
131
/*! Transform the point specified by x,y from Source Coordinate System to Destination Coordinate System
132
* If the direction is ForwardTransform then coordinates are transformed from layer CS --> map canvas CS,
133
* otherwise points are transformed from map canvas CS to layerCS.
134
* @param x x cordinate of point to transform
135
* @param y y coordinate of point to transform
136
* @param direction TransformDirection (defaults to ForwardTransform)
137
* @return QgsPoint in Destination Coordinate System
139
QgsPoint transform( const double x, const double y, TransformDirection direction = ForwardTransform ) const;
141
/*! Transform a QgsRectangle to the dest Coordinate system
142
* If the direction is ForwardTransform then coordinates are transformed from layer CS --> map canvas CS,
143
* otherwise points are transformed from map canvas CS to layerCS.
144
* It assumes that rect is a bounding box, and creates a bounding box
145
* in the proejcted CS, so that all points in source rectangle is within
146
* returned rectangle.
147
* @param QgsRectangle rect to transform
148
* @param direction TransformDirection (defaults to ForwardTransform)
149
* @return QgsRectangle in Destination Coordinate System
151
QgsRectangle transformBoundingBox( const QgsRectangle theRect, TransformDirection direction = ForwardTransform ) const;
153
// Same as for the other transform() functions, but alters the x
154
// and y variables in place. The second one works with good old-fashioned
156
void transformInPlace( double& x, double& y, double &z, TransformDirection direction = ForwardTransform ) const;
158
void transformInPlace( std::vector<double>& x, std::vector<double>& y, std::vector<double>& z,
159
TransformDirection direction = ForwardTransform ) const;
161
/*! Transform a QgsRectangle to the dest Coordinate system
162
* If the direction is ForwardTransform then coordinates are transformed from layer CS --> map canvas CS,
163
* otherwise points are transformed from map canvas CS to layerCS.
164
* @param QgsRectangle rect to transform
165
* @param direction TransformDirection (defaults to ForwardTransform)
166
* @return QgsRectangle in Destination Coordinate System
168
QgsRectangle transform( const QgsRectangle theRect, TransformDirection direction = ForwardTransform ) const;
170
/*! Transform an array of coordinates to a different Coordinate System
171
* If the direction is ForwardTransform then coordinates are transformed from layer CS --> map canvas CS,
172
* otherwise points are transformed from map canvas CS to layerCS.
173
* @param x x cordinate of point to transform
174
* @param y y coordinate of point to transform
175
* @param direction TransformDirection (defaults to ForwardTransform)
176
* @return QgsRectangle in Destination Coordinate System
178
void transformCoords( const int &numPoint, double *x, double *y, double *z, TransformDirection direction = ForwardTransform ) const;
181
* Flag to indicate whether the coordinate systems have been initialised
182
* @return true if initialised, otherwise false
184
bool isInitialised() const {return mInitialisedFlag;};
186
/*! See if the transform short circuits because src and dest are equivalent
187
* @return bool True if it short circuits
189
bool isShortCircuited() {return mShortCircuit;};
191
/*! Change the destination coordinate system by passing it a qgis srsid
192
* A QGIS srsid is a unique key value to an entry on the tbl_srs in the
193
* srs.db sqlite database.
194
* @note This slot will usually be called if the
195
* project properties change and a different coordinate system is
197
* @note This coord transform will be reinitialised when this slot is called
198
* to check if short circuiting is needed or not etc.
199
* @param theCRSID - A long representing the srsid of the srs to be used */
200
void setDestCRSID( long theCRSID );
203
//!initialise is used to actually create the Transformer instance
206
/*! Restores state from the given Dom node.
207
* @param theNode The node from which state will be restored
208
* @return bool True on success, False on failure
210
bool readXML( QDomNode & theNode );
212
/*! Stores state to the given Dom node in the given document
213
* @param theNode The node in which state will be restored
214
* @param theDom The document in which state will be stored
215
* @return bool True on success, False on failure
217
bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
220
/** Signal when an invalid pj_transform() has occured */
221
void invalidTransformInput() const;
226
* Flag to indicate that the source and destination coordinate systems are
227
* equal and not transformation needs to be done
232
* flag to show whether the transform is properly initialised or not
234
bool mInitialisedFlag;
237
* QgsCoordinateReferenceSystem of the source (layer) coordinate system
239
QgsCoordinateReferenceSystem mSourceCRS;
242
* QgsCoordinateReferenceSystem of the destination (map canvas) coordinate system
244
QgsCoordinateReferenceSystem mDestCRS;
247
* Proj4 data structure of the source projection (layer coordinate system)
249
projPJ mSourceProjection;
252
* Proj4 data structure of the destination projection (map canvas coordinate system)
254
projPJ mDestinationProjection;
257
* Finder for PROJ grid files.
262
//! Output stream operator
263
inline std::ostream& operator << ( std::ostream& os, const QgsCoordinateTransform &r )
265
QString mySummary( "\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:" );
266
mySummary += "\n\tInitialised? : ";
268
if ( r.isInitialised() )
270
//do nothing this is a dummy
273
if (r.isInitialised())
281
mySummary += "\n\tShort Circuit? : " ;
282
if (r.isShortCircuited())
291
mySummary += "\n\tSource Spatial Ref Sys : ";
294
mySummary << r.sourceCrs();
298
mySummary += "Undefined" ;
301
mySummary += "\n\tDest Spatial Ref Sys : " ;
304
mySummary << r.destCRS();
308
mySummary += "Undefined" ;
311
mySummary += ( "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n" );
312
return os << mySummary.toLocal8Bit().data() << std::endl;
316
#endif // QGSCOORDINATETRANSFORM_H