~ubuntu-branches/ubuntu/hardy/qgis/hardy

« back to all changes in this revision

Viewing changes to src/gui/qgscoordinatetransform.h

  • Committer: Bazaar Package Importer
  • Author(s): William Grant
  • Date: 2007-05-06 13:42:32 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070506134232-pyli6t388w5asd8x
Tags: 0.8.0-3ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - debian/rules, debian/qgis.install, debian/qgis.dirs debian/qgis.desktop:
    Add and install .desktop.
* debian/qgis.desktop: Remove Applications category; it's not real.
* Modify Maintainer value to match Debian-Maintainer-Field Spec

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: qgscoordinatetransform.h 5429 2006-05-08 15:55:34Z rblazek $ */
 
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 "qgsrect.h"
 
27
#include "qgscsexception.h"
 
28
#include "qgsspatialrefsys.h"
 
29
class QDomNode;
 
30
class QDomDocument;
 
31
 
 
32
//non qt includes
 
33
#include <iostream>
 
34
#include <vector>
 
35
 
 
36
extern "C"{
 
37
#include <proj_api.h>
 
38
}
 
39
class QString;
 
40
 
 
41
/*! \class QgsCoordinateTransform
 
42
* \brief Class for doing transforms between two map coordinate systems.
 
43
*
 
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.
 
48
*
 
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.
 
53
*/
 
54
class QgsCoordinateTransform: public QObject
 
55
{
 
56
  Q_OBJECT
 
57
 public:
 
58
    /*! Default constructor. Make sure you use initialised() manually if you use this one! */
 
59
    QgsCoordinateTransform() ;
 
60
 
 
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
 
64
   */
 
65
  QgsCoordinateTransform(const QgsSpatialRefSys& theSource, 
 
66
                         const QgsSpatialRefSys& theDest);
 
67
 
 
68
    /*!
 
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
 
73
     */
 
74
    QgsCoordinateTransform(QString theSourceWKT, QString theDestWKT  );
 
75
 
 
76
    /*!
 
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
 
82
     */
 
83
    QgsCoordinateTransform(long theSourceSrid,  
 
84
                           QString theDestWKT,
 
85
                           QgsSpatialRefSys::SRS_TYPE theSourceSRSType = QgsSpatialRefSys::POSTGIS_SRID  );
 
86
 
 
87
     //! destructor
 
88
    ~QgsCoordinateTransform();
 
89
 
 
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. */
 
94
    };
 
95
    
 
96
    /*! 
 
97
     * Set the source (layer) QgsSpatialRefSys
 
98
     * @param theSRS QgsSpatialRefSys representation of the layer's coordinate system
 
99
     */
 
100
    void setSourceSRS(const QgsSpatialRefSys& theSRS);
 
101
 
 
102
    /*! 
 
103
     * Mutator for dest QgsSpatialRefSys 
 
104
     * @param theSRS of the destination coordinate system
 
105
     */
 
106
    void setDestSRS(const QgsSpatialRefSys& theSRS);
 
107
 
 
108
    /*!
 
109
     * Get the QgsSpatialRefSys representation of the layer's coordinate system
 
110
     * @return QgsSpatialRefSys of the layer's coordinate system
 
111
     */
 
112
    QgsSpatialRefSys& sourceSRS() { return mSourceSRS; }
 
113
 
 
114
    /*! 
 
115
     * Get the QgsSpatialRefSys representation of the map canvas coordinate system
 
116
     * @return QgsSpatialRefSys of the map canvas coordinate system
 
117
     */
 
118
    QgsSpatialRefSys& destSRS() { return mDestSRS; }
 
119
 
 
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
 
126
     */    
 
127
   QgsPoint transform(const QgsPoint p,TransformDirection direction=FORWARD);
 
128
 
 
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
 
136
     */
 
137
   QgsPoint transform(const double x, const double y,TransformDirection direction=FORWARD);
 
138
 
 
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
 
148
     */        
 
149
   QgsRect transformBoundingBox(const QgsRect theRect,TransformDirection direction=FORWARD);
 
150
 
 
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
 
153
   // C style arrays.
 
154
   void transformInPlace(double& x, double& y, double &z, TransformDirection direction = FORWARD);
 
155
 
 
156
   void transformInPlace(std::vector<double>& x, std::vector<double>& y, std::vector<double>& z, 
 
157
       TransformDirection direction = FORWARD);
 
158
 
 
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
 
165
     */        
 
166
   QgsRect transform(const QgsRect theRect,TransformDirection direction=FORWARD);
 
167
    
 
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
 
175
     */        
 
176
   void transformCoords( const int &numPoint, double *x, double *y, double *z,TransformDirection direction=FORWARD);
 
177
 
 
178
  /*! 
 
179
   * Flag to indicate whether the coordinate systems have been initialised
 
180
   * @return true if initialised, otherwise false
 
181
   */
 
182
   bool isInitialised() {return mInitialisedFlag;};
 
183
 
 
184
   /*! See if the transform short circuits because src and dest are equivalent
 
185
    * @return bool True if it short circuits
 
186
    */
 
187
    bool isShortCircuited() {return mShortCircuit;};
 
188
 
 
189
 public slots:
 
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 
 
195
    * selected. 
 
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);
 
200
 
 
201
    //!initialise is used to actually create the Transformer instance
 
202
    void initialise();
 
203
 
 
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
 
207
    */
 
208
    bool readXML( QDomNode & theNode );
 
209
 
 
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
 
214
    */
 
215
    bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
 
216
 
 
217
 signals:
 
218
    /** Signal when an invalid pj_transform() has occured */
 
219
    void  invalidTransformInput();
 
220
 
 
221
 private:
 
222
 
 
223
    /*! 
 
224
     * Flag to indicate that the source and destination coordinate systems are
 
225
     * equal and not transformation needs to be done
 
226
     */
 
227
    bool mShortCircuit;
 
228
 
 
229
    /*!
 
230
     * flag to show whether the transform is properly initialised or not
 
231
     */
 
232
    bool mInitialisedFlag;
 
233
 
 
234
    /*! 
 
235
     * QgsSpatialRefSys of the source (layer) coordinate system 
 
236
     */
 
237
    QgsSpatialRefSys mSourceSRS;
 
238
 
 
239
    /*! 
 
240
     * QgsSpatialRefSys of the destination (map canvas) coordinate system 
 
241
     */
 
242
    QgsSpatialRefSys mDestSRS;
 
243
 
 
244
    /*!
 
245
     * Proj4 data structure of the source projection (layer coordinate system)
 
246
     */
 
247
    projPJ mSourceProjection;
 
248
 
 
249
    /*!
 
250
     * Proj4 data structure of the destination projection (map canvas coordinate system)
 
251
     */
 
252
    projPJ mDestinationProjection;
 
253
 
 
254
    /*!
 
255
     * Finder for PROJ grid files.
 
256
     */
 
257
    void setFinder();
 
258
};
 
259
 
 
260
//! Output stream operator
 
261
inline std::ostream& operator << (std::ostream& os, const QgsCoordinateTransform &r)
 
262
{
 
263
  QString mySummary ("\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:");
 
264
  mySummary += "\n\tInitialised? : ";
 
265
/*
 
266
  if (r.isInitialised()) 
 
267
  {
 
268
    mySummary += "Yes";
 
269
  }
 
270
  else
 
271
  {
 
272
    mySummary += "No" ;
 
273
  }
 
274
  mySummary += "\n\tShort Circuit?  : " ;
 
275
  if (r.isShortCircuited()) 
 
276
  {
 
277
    mySummary += "Yes";
 
278
  }
 
279
  else
 
280
  {
 
281
    mySummary += "No" ;
 
282
  }
 
283
 
 
284
  mySummary += "\n\tSource Spatial Ref Sys  : "; 
 
285
  if (r.sourceSRS()) 
 
286
  {
 
287
    mySummary << r.sourceSRS();
 
288
  }
 
289
  else
 
290
  {
 
291
    mySummary += "Undefined" ;
 
292
  }
 
293
 
 
294
  mySummary += "\n\tDest Spatial Ref Sys  : " ;
 
295
  if (r.destSRS()) 
 
296
  {
 
297
    mySummary << r.destSRS();
 
298
  }
 
299
  else
 
300
  {
 
301
    mySummary += "Undefined" ;
 
302
  }
 
303
*/
 
304
  mySummary+=("\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n");
 
305
  return os << mySummary.toLocal8Bit().data() << std::endl;
 
306
}
 
307
 
 
308
 
 
309
#endif // QGSCOORDINATETRANSFORM_H