QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgscoordinatetransform.h
Go to the documentation of this file.
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 #ifndef QGSCOORDINATETRANSFORM_H
18 #define QGSCOORDINATETRANSFORM_H
19 
20 //qt includes
21 #include <QObject>
22 
23 //qgis includes
24 #include "qgspoint.h"
25 #include "qgsrectangle.h"
26 #include "qgscsexception.h"
28 class QDomNode;
29 class QDomDocument;
30 class QPolygonF;
31 
32 //non qt includes
33 #include <iostream>
34 #include <vector>
35 
36 typedef void* projPJ;
37 class QString;
38 
52 class CORE_EXPORT QgsCoordinateTransform : public QObject
53 {
54  Q_OBJECT
55  public:
58 
64  const QgsCoordinateReferenceSystem& theDest );
65 
67  QgsCoordinateTransform( long theSourceSrsId, long theDestSrsId );
68 
75  QgsCoordinateTransform( QString theSourceWkt, QString theDestWkt );
76 
84  QgsCoordinateTransform( long theSourceSrid,
85  QString theDestWkt,
87 
90 
91  QgsCoordinateTransform* clone() const;
92 
95  {
97  ReverseTransform
98  };
99 
104  void setSourceCrs( const QgsCoordinateReferenceSystem& theCRS );
105 
110  void setDestCRS( const QgsCoordinateReferenceSystem& theCRS );
111 
116  const QgsCoordinateReferenceSystem& sourceCrs() const { return mSourceCRS; }
117 
122  const QgsCoordinateReferenceSystem& destCRS() const { return mDestCRS; }
123 
131  QgsPoint transform( const QgsPoint &p, TransformDirection direction = ForwardTransform ) const;
132 
141  QgsPoint transform( const double x, const double y, TransformDirection direction = ForwardTransform ) const;
142 
155  QgsRectangle transformBoundingBox( const QgsRectangle &theRect, TransformDirection direction = ForwardTransform, const bool handle180Crossover = false ) const;
156 
157  // Same as for the other transform() functions, but alters the x
158  // and y variables in place. The second one works with good old-fashioned
159  // C style arrays.
160  void transformInPlace( double& x, double& y, double &z, TransformDirection direction = ForwardTransform ) const;
161  void transformInPlace( float& x, float& y, double &z, TransformDirection direction = ForwardTransform ) const;
162  void transformInPlace( float& x, float& y, float& z, TransformDirection direction = ForwardTransform ) const;
163 
164  void transformInPlace( QVector<float>& x, QVector<float>& y, QVector<float>& z,
165  TransformDirection direction = ForwardTransform ) const;
166 
167 
169  void transformInPlace( QVector<double>& x, QVector<double>& y, QVector<double>& z,
170  TransformDirection direction = ForwardTransform ) const;
171 
172  void transformPolygon( QPolygonF& poly, TransformDirection direction = ForwardTransform ) const;
173 
181  QgsRectangle transform( const QgsRectangle &theRect, TransformDirection direction = ForwardTransform ) const;
182 
193  void transformCoords( const int &numPoint, double *x, double *y, double *z, TransformDirection direction = ForwardTransform ) const;
194 
199  bool isInitialised() const {return mInitialisedFlag;}
200 
204  bool isShortCircuited() {return mShortCircuit;}
205 
215  void setDestCRSID( long theCRSID );
216 
220  static QList< QList< int > > datumTransformations( const QgsCoordinateReferenceSystem& srcCRS, const QgsCoordinateReferenceSystem& destCRS );
221  static QString datumTransformString( int datumTransform );
224  static bool datumTransformCrsInfo( int datumTransform, int& epsgNr, QString& srcProjection, QString& dstProjection, QString &remarks, QString &scope, bool &preferred, bool &deprecated );
225 
226  int sourceDatumTransform() const { return mSourceDatumTransform; }
227  void setSourceDatumTransform( int dt ) { mSourceDatumTransform = dt; }
228  int destinationDatumTransform() const { return mDestinationDatumTransform; }
229  void setDestinationDatumTransform( int dt ) { mDestinationDatumTransform = dt; }
230 
231  public slots:
233  void initialise();
234 
239  bool readXML( QDomNode & theNode );
240 
246  bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
247 
248  signals:
250  void invalidTransformInput() const;
251 
252  private:
253 
258  bool mShortCircuit;
259 
263  bool mInitialisedFlag;
264 
268  QgsCoordinateReferenceSystem mSourceCRS;
269 
274 
278  projPJ mSourceProjection;
279 
283  projPJ mDestinationProjection;
284 
285  int mSourceDatumTransform;
286  int mDestinationDatumTransform;
287 
291  void setFinder();
292 
294  static QString stripDatumTransform( const QString& proj4 );
295  static void searchDatumTransform( const QString& sql, QList< int >& transforms );
297  void addNullGridShifts( QString& srcProjString, QString& destProjString );
298 };
299 
301 inline std::ostream& operator << ( std::ostream& os, const QgsCoordinateTransform &r )
302 {
303  QString mySummary( "\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:" );
304  mySummary += "\n\tInitialised? : ";
305  //prevent warnings
306  if ( r.isInitialised() )
307  {
308  //do nothing this is a dummy
309  }
310 
311 #if 0
312  if ( r.isInitialised() )
313  {
314  mySummary += "Yes";
315  }
316  else
317  {
318  mySummary += "No";
319  }
320  mySummary += "\n\tShort Circuit? : ";
321  if ( r.isShortCircuited() )
322  {
323  mySummary += "Yes";
324  }
325  else
326  {
327  mySummary += "No";
328  }
329 
330  mySummary += "\n\tSource Spatial Ref Sys : ";
331  if ( r.sourceCrs() )
332  {
333  mySummary << r.sourceCrs();
334  }
335  else
336  {
337  mySummary += "Undefined";
338  }
339 
340  mySummary += "\n\tDest Spatial Ref Sys : ";
341  if ( r.destCRS() )
342  {
343  mySummary << r.destCRS();
344  }
345  else
346  {
347  mySummary += "Undefined";
348  }
349 #endif
350 
351  mySummary += ( "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n" );
352  return os << mySummary.toLocal8Bit().data() << std::endl;
353 }
354 
355 
356 #endif // QGSCOORDINATETRANSFORM_H
const QgsCoordinateReferenceSystem & sourceCrs() const
A rectangle specified with double values.
Definition: qgsrectangle.h:35
TransformDirection
Enum used to indicate the direction (forward or inverse) of the transform.
bool isShortCircuited()
See if the transform short circuits because src and dest are equivalent.
A class to represent a point.
Definition: qgspoint.h:63
QByteArray toLocal8Bit() const
void * projPJ
Class for storing a coordinate reference system (CRS)
Class for doing transforms between two map coordinate systems.
char * data()
const QgsCoordinateReferenceSystem & destCRS() const
std::ostream & operator<<(std::ostream &os, const QgsCoordinateTransform &r)
Output stream operator.