QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 struct geod_geodesic;
30 
52 class CORE_EXPORT QgsDistanceArea
53 {
54  public:
55 
58  ~QgsDistanceArea();
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 
240  double bearing( const QgsPointXY &p1, const QgsPointXY &p2 ) const SIP_THROW( QgsCsException );
241 
253  static QString formatDistance( double distance, int decimals, QgsUnitTypes::DistanceUnit unit, bool keepBaseUnit = false );
254 
266  static QString formatArea( double area, int decimals, QgsUnitTypes::AreaUnit unit, bool keepBaseUnit = false );
267 
278  double convertLengthMeasurement( double length, QgsUnitTypes::DistanceUnit toUnits ) const;
279 
290  double convertAreaMeasurement( double area, QgsUnitTypes::AreaUnit toUnits ) const;
291 
303  QgsPointXY computeSpheroidProject( const QgsPointXY &p1, double distance = 1, double azimuth = M_PI_2 ) const;
304 
324  QVector<QVector<QgsPointXY> > geodesicLine( const QgsPointXY &p1, const QgsPointXY &p2, double interval, bool breakLine = false ) const;
325 
344  double latitudeGeodesicCrossesAntimeridian( const QgsPointXY &p1, const QgsPointXY &p2, double &fractionAlongLine SIP_OUT ) const;
345 
367  QgsGeometry splitGeometryAtAntimeridian( const QgsGeometry &geometry ) const;
368 
369  private:
370 
375  double computePolygonArea( const QVector<QgsPointXY> &points ) const;
376 
377  double computePolygonFlatArea( const QVector<QgsPointXY> &points ) const;
378 
383  void computeAreaInit() const;
384 
385  void setFromParams( const QgsEllipsoidUtils::EllipsoidParameters &params );
386 
387  enum MeasureType
388  {
389  Default,
390  Area,
391  Length
392  };
393 
395  QgsCoordinateTransform mCoordTransform;
396 
398  QString mEllipsoid;
399 
401  double mSemiMajor, mSemiMinor, mInvFlattening;
402 
403  mutable std::unique_ptr< geod_geodesic > mGeod;
404 
405  // utility functions for polygon area measurement
406 
407  double measure( const QgsAbstractGeometry *geomV2, MeasureType type = Default ) const;
408  double measureLine( const QgsCurve *curve ) const;
409  double measurePolygon( const QgsCurve *curve ) const;
410 
411 };
412 
413 #endif
414 
QgsCurve
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
QgsDistanceArea::ellipsoid
QString ellipsoid() const
Returns ellipsoid's acronym.
Definition: qgsdistancearea.h:120
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:57
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsDistanceArea::sourceCrs
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source spatial reference system.
Definition: qgsdistancearea.h:84
qgsunittypes.h
QgsUnitTypes::DistanceUnit
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:67
QgsDistanceArea::ellipsoidInverseFlattening
double ellipsoidInverseFlattening() const
Returns ellipsoid's inverse flattening.
Definition: qgsdistancearea.h:145
qgsellipsoidutils.h
QgsCsException
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:198
QgsDistanceArea::ellipsoidCrs
QgsCoordinateReferenceSystem ellipsoidCrs() const
Returns the ellipsoid (destination) spatial reference system.
Definition: qgsdistancearea.h:92
QgsDistanceArea::ellipsoidSemiMajor
double ellipsoidSemiMajor() const
Returns the ellipsoid's semi major axis.
Definition: qgsdistancearea.h:128
qgscoordinatetransform.h
QgsDistanceArea::ellipsoidSemiMinor
double ellipsoidSemiMinor() const
Returns ellipsoid's semi minor axis.
Definition: qgsdistancearea.h:136
QgsUnitTypes::AreaUnit
AreaUnit
Units of area.
Definition: qgsunittypes.h:93
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:79
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsEllipsoidUtils::EllipsoidParameters
Contains parameters for an ellipsoid.
Definition: qgsellipsoidutils.h:41
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsDistanceArea
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
Definition: qgsdistancearea.h:52
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:57