QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 <QVector>
21 #include <QReadWriteLock>
22 #include "qgscoordinatetransform.h"
23 #include "qgsunittypes.h"
24 #include "qgsellipsoidutils.h"
25 
26 class QgsGeometry;
28 class QgsCurve;
29 class geod_geodesic;
30 
52 class CORE_EXPORT QgsDistanceArea
53 {
54  public:
55 
59 
61  QgsDistanceArea( const QgsDistanceArea &other );
62  QgsDistanceArea &operator=( const QgsDistanceArea &other );
63 
70  bool willUseEllipsoid() const;
71 
77  void setSourceCrs( const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context );
78 
84  QgsCoordinateReferenceSystem sourceCrs() const { return mCoordTransform.sourceCrs(); }
85 
92  QgsCoordinateReferenceSystem ellipsoidCrs() const { return mCoordTransform.destinationCrs(); }
93 
102  bool setEllipsoid( const QString &ellipsoid );
103 
111  bool setEllipsoid( double semiMajor, double semiMinor );
112 
120  QString ellipsoid() const { return mEllipsoid; }
121 
128  double ellipsoidSemiMajor() const { return mSemiMajor; }
129 
136  double ellipsoidSemiMinor() const { return mSemiMinor; }
137 
145  double ellipsoidInverseFlattening() const { return mInvFlattening; }
146 
157  double measureArea( const QgsGeometry &geometry ) const;
158 
169  double measureLength( const QgsGeometry &geometry ) const;
170 
181  double measurePerimeter( const QgsGeometry &geometry ) const;
182 
189  double measureLine( const QVector<QgsPointXY> &points ) const;
190 
198  double measureLine( const QgsPointXY &p1, const QgsPointXY &p2 ) const;
199 
214  double measureLineProjected( const QgsPointXY &p1, double distance = 1, double azimuth = M_PI_2, QgsPointXY *projectedPoint SIP_OUT = nullptr ) const;
215 
221  QgsUnitTypes::DistanceUnit lengthUnits() const;
222 
228  QgsUnitTypes::AreaUnit areaUnits() const;
229 
233  double measurePolygon( const QVector<QgsPointXY> &points ) const;
234 
238  double bearing( const QgsPointXY &p1, const QgsPointXY &p2 ) const;
239 
251  static QString formatDistance( double distance, int decimals, QgsUnitTypes::DistanceUnit unit, bool keepBaseUnit = false );
252 
264  static QString formatArea( double area, int decimals, QgsUnitTypes::AreaUnit unit, bool keepBaseUnit = false );
265 
276  double convertLengthMeasurement( double length, QgsUnitTypes::DistanceUnit toUnits ) const;
277 
288  double convertAreaMeasurement( double area, QgsUnitTypes::AreaUnit toUnits ) const;
289 
301  QgsPointXY computeSpheroidProject( const QgsPointXY &p1, double distance = 1, double azimuth = M_PI_2 ) const;
302 
322  QVector<QVector<QgsPointXY> > geodesicLine( const QgsPointXY &p1, const QgsPointXY &p2, double interval, bool breakLine = false ) const;
323 
342  double latitudeGeodesicCrossesAntimeridian( const QgsPointXY &p1, const QgsPointXY &p2, double &fractionAlongLine SIP_OUT ) const;
343 
365  QgsGeometry splitGeometryAtAntimeridian( const QgsGeometry &geometry ) const;
366 
367  private:
368 
373  double computePolygonArea( const QVector<QgsPointXY> &points ) const;
374 
375  double computePolygonFlatArea( const QVector<QgsPointXY> &points ) const;
376 
381  void computeAreaInit() const;
382 
383  void setFromParams( const QgsEllipsoidUtils::EllipsoidParameters &params );
384 
385  enum MeasureType
386  {
387  Default,
388  Area,
389  Length
390  };
391 
393  QgsCoordinateTransform mCoordTransform;
394 
396  QString mEllipsoid;
397 
399  double mSemiMajor, mSemiMinor, mInvFlattening;
400 
401  mutable std::unique_ptr< geod_geodesic > mGeod;
402 
403  // utility functions for polygon area measurement
404 
405  double measure( const QgsAbstractGeometry *geomV2, MeasureType type = Default ) const;
406  double measureLine( const QgsCurve *curve ) const;
407  double measurePolygon( const QgsCurve *curve ) const;
408 
409 };
410 
411 #endif
412 
Abstract base class for all geometries.
This class represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Class for doing transforms between two map coordinate systems.
Abstract base class for curved geometry type.
Definition: qgscurve.h:36
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
QgsCoordinateReferenceSystem ellipsoidCrs() const
Returns the ellipsoid (destination) spatial reference system.
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source spatial reference system.
double ellipsoidInverseFlattening() const
Returns ellipsoid's inverse flattening.
QString ellipsoid() const
Returns ellipsoid's acronym.
double ellipsoidSemiMajor() const
Returns the ellipsoid's semi major axis.
double ellipsoidSemiMinor() const
Returns ellipsoid's semi minor axis.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
A class to represent a 2D point.
Definition: qgspointxy.h:59
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:68
AreaUnit
Units of area.
Definition: qgsunittypes.h:94
#define SIP_OUT
Definition: qgis_sip.h:58
const QgsCoordinateReferenceSystem & crs
Contains parameters for an ellipsoid.