QGIS API Documentation  2.99.0-Master (6a61179)
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 <QList>
20 #include "qgscoordinatetransform.h"
21 #include "qgsunittypes.h"
22 
23 class QgsGeometry;
25 class QgsCurve;
26 class QgsConstWkbPtr;
27 
38 class CORE_EXPORT QgsDistanceArea
39 {
40  public:
43 
45  ~QgsDistanceArea();
46 
48  QgsDistanceArea( const QgsDistanceArea &origDA );
49 
51  QgsDistanceArea & operator=( const QgsDistanceArea & origDA );
52 
59  void setEllipsoidalMode( bool flag );
60 
65  bool ellipsoidalEnabled() const { return mEllipsoidalMode; }
66 
73  bool willUseEllipsoid() const;
74 
76  void setSourceCrs( long srsid );
77 
83  void setSourceCrs( const QgsCoordinateReferenceSystem& srcCRS );
84 
86  void setSourceAuthId( const QString& authid );
87 
91  QgsCoordinateReferenceSystem sourceCrs() const { return mCoordTransform.sourceCrs(); }
92 
97  long sourceCrsId() const { return mCoordTransform.sourceCrs().srsid(); }
98 
100  bool geographic() const { return mCoordTransform.sourceCrs().isGeographic(); }
101 
109  bool setEllipsoid( const QString& ellipsoid );
110 
118  // Inverse flattening is calculated with invf = a/(a-b)
119  bool setEllipsoid( double semiMajor, double semiMinor );
120 
127  QString ellipsoid() const { return mEllipsoid; }
128 
130  double ellipsoidSemiMajor() const { return mSemiMajor; }
132  double ellipsoidSemiMinor() const { return mSemiMinor; }
134  double ellipsoidInverseFlattening() const { return mInvFlattening; }
135 
145  double measureArea( const QgsGeometry* geometry ) const;
146 
156  double measureArea( const QgsGeometry& geometry ) const;
157 
167  double measureLength( const QgsGeometry* geometry ) const;
168 
178  double measureLength( const QgsGeometry& geometry ) const;
179 
189  double measurePerimeter( const QgsGeometry *geometry ) const;
190 
200  double measurePerimeter( const QgsGeometry& geometry ) const;
201 
207  double measureLine( const QList<QgsPoint>& points ) const;
208 
215  double measureLine( const QgsPoint& p1, const QgsPoint& p2 ) const;
216 
224  double measureLine( const QgsPoint& p1, const QgsPoint& p2, QgsUnitTypes::DistanceUnit& units ) const;
225 
230  QgsUnitTypes::DistanceUnit lengthUnits() const;
231 
236  QgsUnitTypes::AreaUnit areaUnits() const;
237 
239  double measurePolygon( const QList<QgsPoint>& points ) const;
240 
242  double bearing( const QgsPoint& p1, const QgsPoint& p2 ) const;
243 
254  static QString formatDistance( double distance, int decimals, QgsUnitTypes::DistanceUnit unit, bool keepBaseUnit = false );
255 
266  static QString formatArea( double area, int decimals, QgsUnitTypes::AreaUnit unit, bool keepBaseUnit = false );
267 
277  double convertLengthMeasurement( double length, QgsUnitTypes::DistanceUnit toUnits ) const;
278 
288  double convertAreaMeasurement( double area, QgsUnitTypes::AreaUnit toUnits ) const;
289 
290  protected:
292  // @note not available in python bindings
293  QgsConstWkbPtr measurePolygon( QgsConstWkbPtr feature, double* area, double* perimeter, bool hasZptr = false ) const;
294 
305  double computeDistanceBearing( const QgsPoint& p1, const QgsPoint& p2,
306  double* course1 = nullptr, double* course2 = nullptr ) const;
307 
309  double computeDistanceFlat( const QgsPoint& p1, const QgsPoint& p2 ) const;
310 
312  double computeDistance( const QList<QgsPoint>& points ) const;
313 
318  double computePolygonArea( const QList<QgsPoint>& points ) const;
319 
320  double computePolygonFlatArea( const QList<QgsPoint>& points ) const;
321 
326  void computeAreaInit();
327 
328  private:
329 
330  enum MeasureType
331  {
332  Default,
333  Area,
334  Length
335  };
336 
338  void _copy( const QgsDistanceArea & origDA );
339 
341  QgsCoordinateTransform mCoordTransform;
342 
344  bool mEllipsoidalMode;
345 
347  QString mEllipsoid;
348 
350  double mSemiMajor, mSemiMinor, mInvFlattening;
351 
352  // utility functions for polygon area measurement
353 
354  double getQ( double x ) const;
355  double getQbar( double x ) const;
356 
357  double measure( const QgsAbstractGeometry* geomV2, MeasureType type = Default ) const;
358  double measureLine( const QgsCurve* curve ) const;
359  double measurePolygon( const QgsCurve* curve ) const;
360 
361  // temporary area measurement stuff
362 
363  double m_QA, m_QB, m_QC;
364  double m_QbarA, m_QbarB, m_QbarC, m_QbarD;
365  double m_AE; /* a^2(1-e^2) */
366  double m_Qp; /* Q at the north pole */
367  double m_E; /* area of the earth */
368  double m_TwoPI;
369 
370 };
371 
372 #endif
373 
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:78
bool geographic() const
What sort of coordinate system is being used?
Abstract base class for curved geometry type.
Definition: qgscurve.h:32
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:111
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:42
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:64