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

« back to all changes in this revision

Viewing changes to src/core/qgscoordinatetransform.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
/***************************************************************************
 
2
               QgsCoordinateTransform.h  - Coordinate Transforms
 
3
                             -------------------
 
4
    begin                : Dec 2004
 
5
    copyright            : (C) 2004 Tim Sutton
 
6
    email                : tim at linfiniti.com
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
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.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
/* $Id$ */
 
18
#ifndef QGSCOORDINATETRANSFORM_H
 
19
#define QGSCOORDINATETRANSFORM_H
 
20
 
 
21
//qt includes
 
22
#include <QObject>
 
23
 
 
24
//qgis includes
 
25
#include "qgspoint.h"
 
26
#include "qgsrectangle.h"
 
27
#include "qgscsexception.h"
 
28
#include "qgscoordinatereferencesystem.h"
 
29
class QDomNode;
 
30
class QDomDocument;
 
31
 
 
32
//non qt includes
 
33
#include <iostream>
 
34
#include <vector>
 
35
 
 
36
typedef void* projPJ;
 
37
class QString;
 
38
 
 
39
/** \ingroup core
 
40
* Class for doing transforms between two map coordinate systems.
 
41
*
 
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.
 
46
*
 
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.
 
51
*/
 
52
class CORE_EXPORT QgsCoordinateTransform: public QObject
 
53
{
 
54
    Q_OBJECT
 
55
  public:
 
56
    /*! Default constructor. Make sure you use initialised() manually if you use this one! */
 
57
    QgsCoordinateTransform() ;
 
58
 
 
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
 
62
    */
 
63
    QgsCoordinateTransform( const QgsCoordinateReferenceSystem& theSource,
 
64
                            const QgsCoordinateReferenceSystem& theDest );
 
65
 
 
66
    /** Constructs a QgsCoordinateTransform using CRS ID of source and destination CRS */
 
67
    QgsCoordinateTransform( long theSourceSrsId, long theDestSrsId );
 
68
 
 
69
    /*!
 
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
 
74
     */
 
75
    QgsCoordinateTransform( QString theSourceWkt, QString theDestWkt );
 
76
 
 
77
    /*!
 
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
 
83
     */
 
84
    QgsCoordinateTransform( long theSourceSrid,
 
85
                            QString theDestWkt,
 
86
                            QgsCoordinateReferenceSystem::CrsType theSourceCRSType = QgsCoordinateReferenceSystem::PostgisCrsId );
 
87
 
 
88
    //! destructor
 
89
    ~QgsCoordinateTransform();
 
90
 
 
91
    //! Enum used to indicate the direction (forward or inverse) of the transform
 
92
    enum TransformDirection
 
93
    {
 
94
      ForwardTransform,     /*!< Transform from source to destination CRS. */
 
95
      ReverseTransform      /*!< Transform from destination to source CRS. */
 
96
    };
 
97
 
 
98
    /*!
 
99
     * Set the source (layer) QgsCoordinateReferenceSystem
 
100
     * @param theCRS QgsCoordinateReferenceSystem representation of the layer's coordinate system
 
101
     */
 
102
    void setSourceCrs( const QgsCoordinateReferenceSystem& theCRS );
 
103
 
 
104
    /*!
 
105
     * Mutator for dest QgsCoordinateReferenceSystem
 
106
     * @param theCRS of the destination coordinate system
 
107
     */
 
108
    void setDestCRS( const QgsCoordinateReferenceSystem& theCRS );
 
109
 
 
110
    /*!
 
111
     * Get the QgsCoordinateReferenceSystem representation of the layer's coordinate system
 
112
     * @return QgsCoordinateReferenceSystem of the layer's coordinate system
 
113
     */
 
114
    QgsCoordinateReferenceSystem& sourceCrs() { return mSourceCRS; }
 
115
 
 
116
    /*!
 
117
     * Get the QgsCoordinateReferenceSystem representation of the map canvas coordinate system
 
118
     * @return QgsCoordinateReferenceSystem of the map canvas coordinate system
 
119
     */
 
120
    QgsCoordinateReferenceSystem& destCRS() { return mDestCRS; }
 
121
 
 
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
 
128
     */
 
129
    QgsPoint transform( const QgsPoint p, TransformDirection direction = ForwardTransform ) const;
 
130
 
 
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
 
138
     */
 
139
    QgsPoint transform( const double x, const double y, TransformDirection direction = ForwardTransform ) const;
 
140
 
 
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
 
150
     */
 
151
    QgsRectangle transformBoundingBox( const QgsRectangle theRect, TransformDirection direction = ForwardTransform ) const;
 
152
 
 
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
 
155
    // C style arrays.
 
156
    void transformInPlace( double& x, double& y, double &z, TransformDirection direction = ForwardTransform ) const;
 
157
 
 
158
    void transformInPlace( std::vector<double>& x, std::vector<double>& y, std::vector<double>& z,
 
159
                           TransformDirection direction = ForwardTransform ) const;
 
160
 
 
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
 
167
     */
 
168
    QgsRectangle transform( const QgsRectangle theRect, TransformDirection direction = ForwardTransform ) const;
 
169
 
 
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
 
177
     */
 
178
    void transformCoords( const int &numPoint, double *x, double *y, double *z, TransformDirection direction = ForwardTransform ) const;
 
179
 
 
180
    /*!
 
181
     * Flag to indicate whether the coordinate systems have been initialised
 
182
     * @return true if initialised, otherwise false
 
183
     */
 
184
    bool isInitialised() const {return mInitialisedFlag;};
 
185
 
 
186
    /*! See if the transform short circuits because src and dest are equivalent
 
187
     * @return bool True if it short circuits
 
188
     */
 
189
    bool isShortCircuited() {return mShortCircuit;};
 
190
 
 
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
 
196
    * selected.
 
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 );
 
201
 
 
202
  public slots:
 
203
    //!initialise is used to actually create the Transformer instance
 
204
    void initialise();
 
205
 
 
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
 
209
    */
 
210
    bool readXML( QDomNode & theNode );
 
211
 
 
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
 
216
    */
 
217
    bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
 
218
 
 
219
  signals:
 
220
    /** Signal when an invalid pj_transform() has occured */
 
221
    void  invalidTransformInput() const;
 
222
 
 
223
  private:
 
224
 
 
225
    /*!
 
226
     * Flag to indicate that the source and destination coordinate systems are
 
227
     * equal and not transformation needs to be done
 
228
     */
 
229
    bool mShortCircuit;
 
230
 
 
231
    /*!
 
232
     * flag to show whether the transform is properly initialised or not
 
233
     */
 
234
    bool mInitialisedFlag;
 
235
 
 
236
    /*!
 
237
     * QgsCoordinateReferenceSystem of the source (layer) coordinate system
 
238
     */
 
239
    QgsCoordinateReferenceSystem mSourceCRS;
 
240
 
 
241
    /*!
 
242
     * QgsCoordinateReferenceSystem of the destination (map canvas) coordinate system
 
243
     */
 
244
    QgsCoordinateReferenceSystem mDestCRS;
 
245
 
 
246
    /*!
 
247
     * Proj4 data structure of the source projection (layer coordinate system)
 
248
     */
 
249
    projPJ mSourceProjection;
 
250
 
 
251
    /*!
 
252
     * Proj4 data structure of the destination projection (map canvas coordinate system)
 
253
     */
 
254
    projPJ mDestinationProjection;
 
255
 
 
256
    /*!
 
257
     * Finder for PROJ grid files.
 
258
     */
 
259
    void setFinder();
 
260
};
 
261
 
 
262
//! Output stream operator
 
263
inline std::ostream& operator << ( std::ostream& os, const QgsCoordinateTransform &r )
 
264
{
 
265
  QString mySummary( "\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:" );
 
266
  mySummary += "\n\tInitialised? : ";
 
267
  //prevent warnings
 
268
  if ( r.isInitialised() )
 
269
  {
 
270
    //do nothing this is a dummy
 
271
  }
 
272
  /*
 
273
    if (r.isInitialised())
 
274
    {
 
275
      mySummary += "Yes";
 
276
    }
 
277
    else
 
278
    {
 
279
      mySummary += "No" ;
 
280
    }
 
281
    mySummary += "\n\tShort Circuit?  : " ;
 
282
    if (r.isShortCircuited())
 
283
    {
 
284
      mySummary += "Yes";
 
285
    }
 
286
    else
 
287
    {
 
288
      mySummary += "No" ;
 
289
    }
 
290
 
 
291
    mySummary += "\n\tSource Spatial Ref Sys  : ";
 
292
    if (r.sourceCrs())
 
293
    {
 
294
      mySummary << r.sourceCrs();
 
295
    }
 
296
    else
 
297
    {
 
298
      mySummary += "Undefined" ;
 
299
    }
 
300
 
 
301
    mySummary += "\n\tDest Spatial Ref Sys  : " ;
 
302
    if (r.destCRS())
 
303
    {
 
304
      mySummary << r.destCRS();
 
305
    }
 
306
    else
 
307
    {
 
308
      mySummary += "Undefined" ;
 
309
    }
 
310
  */
 
311
  mySummary += ( "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n" );
 
312
  return os << mySummary.toLocal8Bit().data() << std::endl;
 
313
}
 
314
 
 
315
 
 
316
#endif // QGSCOORDINATETRANSFORM_H