Skip to content
Permalink
Browse files
More QgsDistanceArea API cleanups and simplification
- setEllipsoidalMode() was removed. Ellipsoidal calculations are
now enabled whenever a valid ellipsoid() has been set. Set the
ellipsoid to 'NONE' to disable ellipsoidal calculations.

- ellipsoidalEnabled() was removed. Ellipsoidal calculations
are now enabled whenever a valid ellipsoid() is set. Check
willUseEllipsoid() to determine whether ellipsoidal
calculations will be performed.

- sourceCrs() and setSourceCrs() now always take and return
QgsCoordinateReferenceSystem objects. All string/ID based CRS
methods were removed.

- geographic() was removed. This was only used internally
in one place, so was replaced with sourceCrs().isGeographic() instead.

- some largely overlapping measurement functions were consolidated
  • Loading branch information
nyalldawson committed Apr 17, 2017
1 parent ca2b199 commit cc5b9bfe17590ab59da536fa5f14db2991c11fa2
Showing with 172 additions and 317 deletions.
  1. +7 −0 doc/api_break.dox
  2. +1 −1 python/core/qgscoordinatereferencesystem.sip
  3. +39 −64 python/core/qgsdistancearea.sip
  4. +0 −1 python/plugins/processing/algs/qgis/FieldsCalculator.py
  5. +0 −1 python/plugins/processing/algs/qgis/FieldsMapper.py
  6. +3 −2 python/plugins/processing/algs/qgis/HubDistanceLines.py
  7. +3 −2 python/plugins/processing/algs/qgis/HubDistancePoints.py
  8. +0 −1 python/plugins/processing/tools/vector.py
  9. +0 −1 src/analysis/network/qgsgraphbuilderinterface.h
  10. +3 −2 src/analysis/vector/qgstransectsample.cpp
  11. +0 −1 src/app/qgisapp.cpp
  12. +0 −1 src/app/qgisappinterface.cpp
  13. +0 −1 src/app/qgsattributeactionpropertiesdialog.cpp
  14. +0 −3 src/app/qgsattributetabledialog.cpp
  15. +0 −2 src/app/qgsdiagramproperties.cpp
  16. +0 −1 src/app/qgsfeatureaction.cpp
  17. +0 −2 src/app/qgsfieldcalculator.cpp
  18. +0 −1 src/app/qgslabelinggui.cpp
  19. +0 −1 src/app/qgsmaptoolmeasureangle.cpp
  20. +0 −1 src/app/qgsmeasuredialog.cpp
  21. +2 −3 src/core/composer/qgscomposerhtml.cpp
  22. +2 −3 src/core/composer/qgscomposerlabel.cpp
  23. +2 −4 src/core/composer/qgscomposerscalebar.cpp
  24. +1 −1 src/core/qgscoordinatereferencesystem.h
  25. +3 −28 src/core/qgsdistancearea.cpp
  26. +63 −82 src/core/qgsdistancearea.h
  27. +0 −1 src/core/qgsexpression.cpp
  28. +2 −4 src/core/qgsvectorlayerfeatureiterator.cpp
  29. +2 −1 src/core/qgsvectorlayerfeatureiterator.h
  30. +0 −1 src/gui/qgsmaptoolidentify.cpp
  31. +16 −39 tests/src/core/testqgsdistancearea.cpp
  32. +1 −2 tests/src/core/testqgsexpression.cpp
  33. +4 −4 tests/src/core/testqgspointlocator.cpp
  34. +18 −55 tests/src/python/test_qgsdistancearea.py
@@ -951,7 +951,14 @@ QgsDial {#qgis_api_break_3_0_QgsDial}
QgsDistanceArea {#qgis_api_break_3_0_QgsDistanceArea}
---------------

- setEllipsoidalMode() was removed. Ellipsoidal calculations are now enabled whenever a valid ellipsoid() has been set. Set the
ellipsoid to 'NONE' to disable ellipsoidal calculations.
- ellipsoidalEnabled() was removed. Ellipsoidal calculations are now enabled whenever a valid ellipsoid() is set. Check
willUseEllipsoid() to determine whether ellipsoidal calculations will be performed.
- sourceCrs() now returns a QgsCoordinateReferenceSystem instead of the crs ID.
- setSourceCrs() now requires a QgsCoordinateReferenceSystem instead of crs ID.
- setSourceAuthId() was removed. Use setSourceCrs() instead.
- geographic() was removed. Check sourceCrs().isGeographic() instead.
- measure() has been removed. Use measureArea() or measureLength() instead.
- textUnit() was removed. Use formatDistance() or formatArea() instead.
- convertMeasurement was removed. Use QgsUnitTypes for conversion instead.
@@ -181,7 +181,7 @@ class QgsCoordinateReferenceSystem
* @param type One of the types described in CrsType
*/
// TODO QGIS 3: remove type and always use EPSG code
QgsCoordinateReferenceSystem( const long id, CrsType type = PostgisCrsId );
explicit QgsCoordinateReferenceSystem( const long id, CrsType type = PostgisCrsId );

operator QVariant() const;
static QList< long > validSrsIds();
@@ -12,14 +12,22 @@
class QgsDistanceArea
{
%Docstring
General purpose distance and area calculator.
- calculations are done on ellipsoid
- it's possible to pass points/features in any CRS, coordinates are transformed
- two options how to use it
+ use measure() takes QgsGeometry as a parameter and calculates distance or area
+ use directly measureLine(), measurePolygon() which take list of QgsPoints
(both cases transform the coordinates from source CRS to the ellipse coords)
- returned values are in meters resp. square meters
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.

Measurements can either be performed on existing QgsGeometry objects, or using
lists of points.

If a valid ellipsoid() has been set for the QgsDistanceArea, all calculations will be
performed using ellipsoidal algorithms (e.g. using Vincenty's formulas). If no
ellipsoid has been set, all calculations will be performed using cartesian
formulas only. The behavior can be determined by calling willUseEllipsoid().

In order to perform accurate calculations, the source coordinate reference system
of all measured geometries must first be specified using setSourceCrs().

Usually, the measurements returned by QgsDistanceArea are in meters. If no valid
ellipsoid is set, then the units may not be meters. The units can be retrieved
by calling lengthUnits() and areaUnits().
%End

%TypeHeaderCode
@@ -32,115 +40,83 @@ General purpose distance and area calculator.
Constructor
%End

void setEllipsoidalMode( bool flag );
%Docstring
Sets whether coordinates must be projected to ellipsoid before measuring
.. note::

for calculations to use the ellipsoid, both the ellipsoid mode must be true
and an ellipse must be set
\see setEllipsoid()
\see willUseEllipsoid()
%End

bool ellipsoidalEnabled() const;
%Docstring
Returns whether ellipsoidal calculations are enabled
\see willUseEllipsoid()
\see setEllipsoidalMode()
:rtype: bool
%End

bool willUseEllipsoid() const;
%Docstring
Returns whether calculations will use the ellipsoid. Calculations will only use the
ellipsoid if ellipsoidalEnabled() is true and an ellipsoid has been set.
ellipsoid if a valid ellipsoid() has been set.
.. versionadded:: 2.14
\see ellipsoidalEnabled()
\see ellipsoid()
:rtype: bool
%End

void setSourceCrs( long srsid );
%Docstring
Sets source spatial reference system (by QGIS CRS).
%End

void setSourceCrs( const QgsCoordinateReferenceSystem &srcCRS );
%Docstring
Sets source spatial reference system (by QGIS CRS).
Sets source spatial reference system.
.. versionadded:: 2.2
\see sourceCrs()
%End

void setSourceAuthId( const QString &authid );
%Docstring
Sets source spatial reference system by authid.
%End

QgsCoordinateReferenceSystem sourceCrs() const;
%Docstring
Returns the source spatial reference system.
\see setSourceCrs()
:rtype: QgsCoordinateReferenceSystem
%End

long sourceCrsId() const;
%Docstring
Returns the QgsCoordinateReferenceSystem.srsid() for the CRS used during calculations.
\see setSourceCrs()
.. versionadded:: 2.14
:rtype: long
%End

bool geographic() const;
%Docstring
Returns true if a geographic (latitude/longitude based) source CRS is used.
:rtype: bool
%End

bool setEllipsoid( const QString &ellipsoid );
%Docstring
Sets ellipsoid by its acronym. Calculations will only use the ellipsoid if
both the ellipsoid has been set and ellipsoidalEnabled() is true.
Sets the ``ellipsoid`` by its acronym.
Calculations will only use the ellipsoid if a valid ellipsoid has been set.
:return: true if ellipsoid was successfully set
\see ellipsoid()
\see setEllipsoidalMode()
\see willUseEllipsoid()
:rtype: bool
%End

bool setEllipsoid( double semiMajor, double semiMinor );
%Docstring
Inverse flattening is calculated with invf = a/(a-b)
Sets ellipsoid by supplied radii. Calculations will only use the ellipsoid if
a valid ellipsoid been set.
:return: true if ellipsoid was successfully set
\see ellipsoid()
\see willUseEllipsoid()
:rtype: bool
%End

QString ellipsoid() const;
%Docstring
Returns ellipsoid's acronym. Calculations will only use the
ellipsoid if ellipsoidalEnabled() is true and an ellipsoid has been set.
ellipsoid if a valid ellipsoid has been set.
\see setEllipsoid()
\see ellipsoidalEnabled()
\see willUseEllipsoid()
:rtype: str
%End

double ellipsoidSemiMajor() const;
%Docstring
Returns the ellipsoid's semi major axis.
\see ellipsoid()
\see ellipsoidSemiMinor()
\see ellipsoidInverseFlattening()
:rtype: float
%End

double ellipsoidSemiMinor() const;
%Docstring
Returns ellipsoid's semi minor axis.
\see ellipsoid()
\see ellipsoidSemiMajor()
\see ellipsoidInverseFlattening()
:rtype: float
%End

double ellipsoidInverseFlattening() const;
%Docstring
Returns ellipsoid's inverse flattening.
The inverse flattening is calculated with invf = a/(a-b).
\see ellipsoid()
\see ellipsoidSemiMajor()
\see ellipsoidSemiMinor()
:rtype: float
%End

@@ -194,18 +170,17 @@ Inverse flattening is calculated with invf = a/(a-b)

double measureLine( const QgsPoint &p1, const QgsPoint &p2 ) const;
%Docstring
Measures length of a line with one segment.
Measures the distance between two points.
\param p1 start of line
\param p2 end of line
:return: distance between points. The units for the returned distance can be retrieved by calling lengthUnits().
\see lengthUnits()
:rtype: float
%End


double measureLineProjected( const QgsPoint &p1, double distance = 1, double azimuth = M_PI / 2, QgsPoint *projectedPoint /Out/ = 0 ) const;
%Docstring
Calculates distance from one point with distance in meters and azimuth (direction)
Calculates the distance from one point with distance in meters and azimuth (direction)
When the sourceCrs() is geographic, computeSpheroidProject() will be called
otherwise QgsPoint.project() will be called after QgsUnitTypes.fromUnitToUnitFactor() has been applied to the distance
\note:
@@ -245,7 +220,7 @@ Inverse flattening is calculated with invf = a/(a-b)

double bearing( const QgsPoint &p1, const QgsPoint &p2 ) const;
%Docstring
Compute the bearing (in radians) between two points.
Computes the bearing (in radians) between two points.
:rtype: float
%End

@@ -119,7 +119,6 @@ def processAlgorithm(self, feedback):

da = QgsDistanceArea()
da.setSourceCrs(layer.crs())
da.setEllipsoidalMode(True)
da.setEllipsoid(QgsProject.instance().ellipsoid())
exp.setGeomCalculator(da)
exp.setDistanceUnits(QgsProject.instance().distanceUnits())
@@ -121,7 +121,6 @@ def processAlgorithm(self, feedback):

da = QgsDistanceArea()
da.setSourceCrs(layer.crs())
da.setEllipsoidalMode(True)
da.setEllipsoid(QgsProject.instance().ellipsoid())

exp_context = layer.createExpressionContext()
@@ -33,7 +33,8 @@
QgsFeature,
QgsFeatureRequest,
QgsWkbTypes,
QgsApplication)
QgsApplication,
QgsProject)
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
@@ -117,7 +118,7 @@ def processAlgorithm(self, feedback):

distance = QgsDistanceArea()
distance.setSourceCrs(layerPoints.crs())
distance.setEllipsoidalMode(True)
distance.setEllipsoid(QgsProject.instance().ellipsoid())

# Scan source points, find nearest hub, and write to output file
features = vector.features(layerPoints)
@@ -33,7 +33,8 @@
QgsFeature,
QgsFeatureRequest,
QgsWkbTypes,
QgsApplication)
QgsApplication,
QgsProject)
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
@@ -117,7 +118,7 @@ def processAlgorithm(self, feedback):

distance = QgsDistanceArea()
distance.setSourceCrs(layerPoints.crs())
distance.setEllipsoidalMode(True)
distance.setEllipsoid(QgsProject.instance().ellipsoid())

# Scan source points, find nearest hub, and write to output file
features = vector.features(layerPoints)
@@ -343,7 +343,6 @@ def simpleMeasure(geom, method=0, ellips=None, crs=None):
if method == 2:
measure.setSourceCrs(crs)
measure.setEllipsoid(ellips)
measure.setEllipsoidalMode(True)

if geom.type() == QgsWkbTypes.PolygonGeometry:
attr1 = measure.measureArea(geom)
@@ -48,7 +48,6 @@ class ANALYSIS_EXPORT QgsGraphBuilderInterface
{
mDa.setSourceCrs( mCrs );
mDa.setEllipsoid( ellipsoidID );
mDa.setEllipsoidalMode( ctfEnabled );
}

virtual ~QgsGraphBuilderInterface()
@@ -19,6 +19,7 @@
#include "qgsspatialindex.h"
#include "qgsvectorfilewriter.h"
#include "qgsvectorlayer.h"
#include "qgsproject.h"
#include <QProgressDialog>
#include <QFileInfo>
#ifndef _MSC_VER
@@ -126,11 +127,11 @@ int QgsTransectSample::createSample( QProgressDialog *pd )
distanceArea.setSourceCrs( mStrataLayer->crs() );
if ( mMinDistanceUnits == Meters )
{
distanceArea.setEllipsoidalMode( true );
distanceArea.setEllipsoid( QgsProject::instance()->ellipsoid() );
}
else
{
distanceArea.setEllipsoidalMode( false );
distanceArea.setEllipsoid( GEO_NONE );
}

//possibility to transform output points to lat/long
@@ -7734,7 +7734,6 @@ void QgisApp::selectByForm()
QgsDistanceArea myDa;

myDa.setSourceCrs( vlayer->crs() );
myDa.setEllipsoidalMode( true );
myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );

QgsAttributeEditorContext context;
@@ -705,7 +705,6 @@ QgsAttributeDialog *QgisAppInterface::getFeatureForm( QgsVectorLayer *l, QgsFeat
QgsDistanceArea myDa;

myDa.setSourceCrs( l->crs() );
myDa.setEllipsoidalMode( true );
myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );

QgsAttributeEditorContext context;
@@ -200,7 +200,6 @@ void QgsAttributeActionPropertiesDialog::init( const QSet<QString> &actionScopes

QgsDistanceArea myDa;
myDa.setSourceCrs( mLayer->crs() );
myDa.setEllipsoidalMode( true );
myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );

mFieldExpression->setLayer( mLayer );
@@ -125,7 +125,6 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *layer, QWidget
myDa = new QgsDistanceArea();

myDa->setSourceCrs( mLayer->crs() );
myDa->setEllipsoidalMode( true );
myDa->setEllipsoid( QgsProject::instance()->ellipsoid() );

mEditorContext.setDistanceArea( *myDa );
@@ -588,7 +587,6 @@ void QgsAttributeTableDialog::filterExpressionBuilder()

QgsDistanceArea myDa;
myDa.setSourceCrs( mLayer->crs() );
myDa.setEllipsoidalMode( true );
myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );
dlg.setGeomCalculator( myDa );

@@ -949,7 +947,6 @@ void QgsAttributeTableDialog::setFilterExpression( const QString &filterString,
QgsDistanceArea myDa;

myDa.setSourceCrs( mLayer->crs() );
myDa.setEllipsoidalMode( true );
myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );

// parse search string and build parsed tree
@@ -174,7 +174,6 @@ QgsDiagramProperties::QgsDiagramProperties( QgsVectorLayer *layer, QWidget *pare
mSizeFieldExpressionWidget->setLayer( mLayer );
QgsDistanceArea myDa;
myDa.setSourceCrs( mLayer->crs() );
myDa.setEllipsoidalMode( true );
myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );
mSizeFieldExpressionWidget->setGeomCalculator( myDa );

@@ -866,7 +865,6 @@ QString QgsDiagramProperties::showExpressionBuilder( const QString &initialExpre

QgsDistanceArea myDa;
myDa.setSourceCrs( mLayer->crs() );
myDa.setEllipsoidalMode( true );
myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );
dlg.setGeomCalculator( myDa );

@@ -57,7 +57,6 @@ QgsAttributeDialog *QgsFeatureAction::newDialog( bool cloneFeature )
QgsDistanceArea myDa;

myDa.setSourceCrs( mLayer->crs() );
myDa.setEllipsoidalMode( true );
myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );

context.setDistanceArea( myDa );
Loading

0 comments on commit cc5b9bf

Please sign in to comment.