QGIS API Documentation  2.99.0-Master (9ed189e)
qgsdistancearea.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdistancearea.h - Distance and area calculations on the ellipsoid
3  ---------------------------------------------------------------------------
4  Date : September 2005
5  Copyright : (C) 2005 by Martin Dobias
6  email : won.der at centrum.sk
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSDISTANCEAREA_H
17 #define QGSDISTANCEAREA_H
18 
19 #include "qgis_core.h"
20 #include <QList>
21 #include "qgscoordinatetransform.h"
22 #include "qgsunittypes.h"
23 
24 class QgsGeometry;
26 class QgsCurve;
27 class QgsConstWkbPtr;
28 
39 class CORE_EXPORT QgsDistanceArea
40 {
41  public:
44 
46  QgsDistanceArea( const QgsDistanceArea &origDA );
47 
49  QgsDistanceArea & operator=( const QgsDistanceArea & origDA );
50 
57  void setEllipsoidalMode( bool flag );
58 
63  bool ellipsoidalEnabled() const { return mEllipsoidalMode; }
64 
71  bool willUseEllipsoid() const;
72 
74  void setSourceCrs( long srsid );
75 
81  void setSourceCrs( const QgsCoordinateReferenceSystem& srcCRS );
82 
84  void setSourceAuthId( const QString& authid );
85 
89  QgsCoordinateReferenceSystem sourceCrs() const { return mCoordTransform.sourceCrs(); }
90 
95  long sourceCrsId() const { return mCoordTransform.sourceCrs().srsid(); }
96 
98  bool geographic() const { return mCoordTransform.sourceCrs().isGeographic(); }
99 
107  bool setEllipsoid( const QString& ellipsoid );
108 
116  // Inverse flattening is calculated with invf = a/(a-b)
117  bool setEllipsoid( double semiMajor, double semiMinor );
118 
125  QString ellipsoid() const { return mEllipsoid; }
126 
128  double ellipsoidSemiMajor() const { return mSemiMajor; }
130  double ellipsoidSemiMinor() const { return mSemiMinor; }
132  double ellipsoidInverseFlattening() const { return mInvFlattening; }
133 
143  double measureArea( const QgsGeometry* geometry ) const;
144 
154  double measureArea( const QgsGeometry& geometry ) const;
155 
165  double measureLength( const QgsGeometry* geometry ) const;
166 
176  double measureLength( const QgsGeometry& geometry ) const;
177 
187  double measurePerimeter( const QgsGeometry *geometry ) const;
188 
198  double measurePerimeter( const QgsGeometry& geometry ) const;
199 
205  double measureLine( const QList<QgsPoint>& points ) const;
206 
213  double measureLine( const QgsPoint& p1, const QgsPoint& p2 ) const;
214 
222  double measureLine( const QgsPoint& p1, const QgsPoint& p2, QgsUnitTypes::DistanceUnit& units ) const;
223 
228  QgsUnitTypes::DistanceUnit lengthUnits() const;
229 
234  QgsUnitTypes::AreaUnit areaUnits() const;
235 
237  double measurePolygon( const QList<QgsPoint>& points ) const;
238 
240  double bearing( const QgsPoint& p1, const QgsPoint& p2 ) const;
241 
252  static QString formatDistance( double distance, int decimals, QgsUnitTypes::DistanceUnit unit, bool keepBaseUnit = false );
253 
264  static QString formatArea( double area, int decimals, QgsUnitTypes::AreaUnit unit, bool keepBaseUnit = false );
265 
275  double convertLengthMeasurement( double length, QgsUnitTypes::DistanceUnit toUnits ) const;
276 
286  double convertAreaMeasurement( double area, QgsUnitTypes::AreaUnit toUnits ) const;
287 
288  protected:
290  // @note not available in python bindings
291  QgsConstWkbPtr measurePolygon( QgsConstWkbPtr feature, double* area, double* perimeter, bool hasZptr = false ) const;
292 
303  double computeDistanceBearing( const QgsPoint& p1, const QgsPoint& p2,
304  double* course1 = nullptr, double* course2 = nullptr ) const;
305 
307  double computeDistanceFlat( const QgsPoint& p1, const QgsPoint& p2 ) const;
308 
310  double computeDistance( const QList<QgsPoint>& points ) const;
311 
316  double computePolygonArea( const QList<QgsPoint>& points ) const;
317 
318  double computePolygonFlatArea( const QList<QgsPoint>& points ) const;
319 
324  void computeAreaInit();
325 
326  private:
327 
328  enum MeasureType
329  {
330  Default,
331  Area,
332  Length
333  };
334 
336  void _copy( const QgsDistanceArea & origDA );
337 
339  QgsCoordinateTransform mCoordTransform;
340 
342  bool mEllipsoidalMode;
343 
345  QString mEllipsoid;
346 
348  double mSemiMajor, mSemiMinor, mInvFlattening;
349 
350  // utility functions for polygon area measurement
351 
352  double getQ( double x ) const;
353  double getQbar( double x ) const;
354 
355  double measure( const QgsAbstractGeometry* geomV2, MeasureType type = Default ) const;
356  double measureLine( const QgsCurve* curve ) const;
357  double measurePolygon( const QgsCurve* curve ) const;
358 
359  // temporary area measurement stuff
360 
361  double m_QA, m_QB, m_QC;
362  double m_QbarA, m_QbarB, m_QbarC, m_QbarD;
363  double m_AE; /* a^2(1-e^2) */
364  double m_Qp; /* Q at the north pole */
365  double m_E; /* area of the earth */
366  double m_TwoPI;
367 
368 };
369 
370 #endif
371 
double ellipsoidSemiMajor() const
returns ellipsoid&#39;s semi major axis
double ellipsoidSemiMinor() const
returns ellipsoid&#39;s semi minor axis
double ellipsoidInverseFlattening() const
returns ellipsoid&#39;s inverse flattening
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:79
bool geographic() const
What sort of coordinate system is being used?
Abstract base class for curved geometry type.
Definition: qgscurve.h:33
long sourceCrsId() const
Returns the QgsCoordinateReferenceSystem::srsid() for the CRS used during calculations.
Abstract base class for all geometries.
A class to represent a point.
Definition: qgspoint.h:143
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:43
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source spatial reference system.
General purpose distance and area calculator.
QString ellipsoid() const
Returns ellipsoid&#39;s acronym.
bool ellipsoidalEnabled() const
Returns whether ellipsoidal calculations are enabled.
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
AreaUnit
Units of area.
Definition: qgsunittypes.h:65