QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 #include <QThreadStorage>
23 #include <QReadWriteLock>
24 
25 //qgis includes
26 #include "qgspoint.h"
27 #include "qgsrectangle.h"
28 #include "qgscsexception.h"
30 class QDomNode;
31 class QDomDocument;
32 class QPolygonF;
33 
34 //non qt includes
35 #include <iostream>
36 #include <vector>
37 
38 typedef void* projPJ;
39 typedef void* projCtx;
40 class QString;
41 
55 class CORE_EXPORT QgsCoordinateTransform : public QObject
56 {
57  Q_OBJECT
58  public:
61 
67  const QgsCoordinateReferenceSystem& theDest );
68 
70  QgsCoordinateTransform( long theSourceSrsId, long theDestSrsId );
71 
78  QgsCoordinateTransform( const QString& theSourceWkt, const QString& theDestWkt );
79 
87  QgsCoordinateTransform( long theSourceSrid,
88  const QString& theDestWkt,
90 
93 
94  QgsCoordinateTransform* clone() const;
95 
98  {
100  ReverseTransform
101  };
102 
107  void setSourceCrs( const QgsCoordinateReferenceSystem& theCRS );
108 
113  void setDestCRS( const QgsCoordinateReferenceSystem& theCRS );
114 
119  const QgsCoordinateReferenceSystem& sourceCrs() const { return mSourceCRS; }
120 
125  const QgsCoordinateReferenceSystem& destCRS() const { return mDestCRS; }
126 
134  QgsPoint transform( const QgsPoint &p, TransformDirection direction = ForwardTransform ) const;
135 
144  QgsPoint transform( const double x, const double y, TransformDirection direction = ForwardTransform ) const;
145 
158  QgsRectangle transformBoundingBox( const QgsRectangle &theRect, TransformDirection direction = ForwardTransform, const bool handle180Crossover = false ) const;
159 
160  // Same as for the other transform() functions, but alters the x
161  // and y variables in place. The second one works with good old-fashioned
162  // C style arrays.
163  void transformInPlace( double& x, double& y, double &z, TransformDirection direction = ForwardTransform ) const;
164 
166  void transformInPlace( float& x, float& y, double &z, TransformDirection direction = ForwardTransform ) const;
168  void transformInPlace( float& x, float& y, float& z, TransformDirection direction = ForwardTransform ) const;
170  void transformInPlace( QVector<float>& x, QVector<float>& y, QVector<float>& z,
171  TransformDirection direction = ForwardTransform ) const;
172 
174  void transformInPlace( QVector<double>& x, QVector<double>& y, QVector<double>& z,
175  TransformDirection direction = ForwardTransform ) const;
176 
177  void transformPolygon( QPolygonF& poly, TransformDirection direction = ForwardTransform ) const;
178 
186  QgsRectangle transform( const QgsRectangle &theRect, TransformDirection direction = ForwardTransform ) const;
187 
198  void transformCoords( int numPoint, double *x, double *y, double *z, TransformDirection direction = ForwardTransform ) const;
199 
204  bool isInitialised() const { return mInitialisedFlag; }
205 
209  bool isShortCircuited() const { return mShortCircuit; }
210 
220  void setDestCRSID( long theCRSID );
221 
225  static QList< QList< int > > datumTransformations( const QgsCoordinateReferenceSystem& srcCRS, const QgsCoordinateReferenceSystem& destCRS );
226  static QString datumTransformString( int datumTransform );
229  static bool datumTransformCrsInfo( int datumTransform, int& epsgNr, QString& srcProjection, QString& dstProjection, QString &remarks, QString &scope, bool &preferred, bool &deprecated );
230 
231  int sourceDatumTransform() const { return mSourceDatumTransform; }
232  void setSourceDatumTransform( int dt ) { mSourceDatumTransform = dt; }
233  int destinationDatumTransform() const { return mDestinationDatumTransform; }
234  void setDestinationDatumTransform( int dt ) { mDestinationDatumTransform = dt; }
235 
236  public slots:
238  void initialise();
239 
244  bool readXML( QDomNode & theNode );
245 
251  bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
252 
253  signals:
255  void invalidTransformInput() const;
256 
257  private:
258 
263  bool mShortCircuit;
264 
268  bool mInitialisedFlag;
269 
273  QgsCoordinateReferenceSystem mSourceCRS;
274 
279 
280  QString mSourceProjString;
281  QString mDestProjString;
282 
283  class QgsProjContextStore
284  {
285  public:
286 
287  QgsProjContextStore();
288  ~QgsProjContextStore();
289 
290  projCtx get() { return context; }
291 
292  private:
293 
294  projCtx context;
295  };
296 
297  static QThreadStorage< QgsProjContextStore* > mProjContext;
298  mutable QReadWriteLock mProjLock;
299  mutable QMap< uintptr_t, QPair< projPJ, projPJ > > mProjProjections;
300 
301  int mSourceDatumTransform;
302  int mDestinationDatumTransform;
303 
307  void setFinder();
308 
310  static QString stripDatumTransform( const QString& proj4 );
311  static void searchDatumTransform( const QString& sql, QList< int >& transforms );
313  void addNullGridShifts( QString& srcProjString, QString& destProjString );
314 
315  QPair< projPJ, projPJ > threadLocalProjData() const;
316  void freeProj();
317 };
318 
320 inline std::ostream& operator << ( std::ostream& os, const QgsCoordinateTransform &r )
321 {
322  QString mySummary( "\n%%%%%%%%%%%%%%%%%%%%%%%%\nCoordinate Transform def begins:" );
323  mySummary += "\n\tInitialised? : ";
324  //prevent warnings
325  if ( r.isInitialised() )
326  {
327  //do nothing this is a dummy
328  }
329 
330 #if 0
331  if ( r.isInitialised() )
332  {
333  mySummary += "Yes";
334  }
335  else
336  {
337  mySummary += "No";
338  }
339  mySummary += "\n\tShort Circuit? : ";
340  if ( r.isShortCircuited() )
341  {
342  mySummary += "Yes";
343  }
344  else
345  {
346  mySummary += "No";
347  }
348 
349  mySummary += "\n\tSource Spatial Ref Sys : ";
350  if ( r.sourceCrs() )
351  {
352  mySummary << r.sourceCrs();
353  }
354  else
355  {
356  mySummary += "Undefined";
357  }
358 
359  mySummary += "\n\tDest Spatial Ref Sys : ";
360  if ( r.destCRS() )
361  {
362  mySummary << r.destCRS();
363  }
364  else
365  {
366  mySummary += "Undefined";
367  }
368 #endif
369 
370  mySummary += ( "\nCoordinate Transform def ends \n%%%%%%%%%%%%%%%%%%%%%%%%\n" );
371  return os << mySummary.toLocal8Bit().data() << std::endl;
372 }
373 
374 
375 #endif // QGSCOORDINATETRANSFORM_H
A rectangle specified with double values.
Definition: qgsrectangle.h:35
TransformDirection
Enum used to indicate the direction (forward or inverse) of the transform.
const QgsCoordinateReferenceSystem & sourceCrs() const
bool isShortCircuited() const
See if the transform short circuits because src and dest are equivalent.
void * projCtx
A class to represent a point.
Definition: qgspoint.h:117
QByteArray toLocal8Bit() const
void * projPJ
Class for storing a coordinate reference system (CRS)
const QgsCoordinateReferenceSystem & destCRS() const
Class for doing transforms between two map coordinate systems.
char * data()
std::ostream & operator<<(std::ostream &os, const QgsCoordinateTransform &r)
Output stream operator.