QGIS API Documentation 3.27.0-Master (f261cc1f8b)
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>
23#include "qgsunittypes.h"
24#include "qgsellipsoidutils.h"
25
26class QgsGeometry;
28class QgsCurve;
29struct geod_geodesic;
30
52class 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
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
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.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
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:125
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_THROW(name)
Definition: qgis_sip.h:198
#define SIP_OUT
Definition: qgis_sip.h:58
const QgsCoordinateReferenceSystem & crs
Contains parameters for an ellipsoid.