Skip to content
Permalink
Browse files

Fix missing transform contexts for QgsDistanceArea

QgsDistanceArea.setSourceCrs() now requires a QgsTransformContext
argument.
  • Loading branch information
nyalldawson committed Dec 20, 2017
1 parent 6528f1c commit 4643712a1e574fe0ddb73c5d61f610c7620fdea3
Showing with 84 additions and 78 deletions.
  1. +2 −1 doc/api_break.dox
  2. +2 −2 python/core/qgsdistancearea.sip
  3. +0 −1 python/gui/qgsattributeeditorcontext.sip
  4. +1 −1 python/plugins/processing/algs/qgis/Aggregate.py
  5. +1 −1 python/plugins/processing/algs/qgis/ExportGeometryInfo.py
  6. +1 −1 python/plugins/processing/algs/qgis/FieldsCalculator.py
  7. +1 −1 python/plugins/processing/algs/qgis/FieldsMapper.py
  8. +1 −1 python/plugins/processing/algs/qgis/HubDistanceLines.py
  9. +1 −1 python/plugins/processing/algs/qgis/HubDistancePoints.py
  10. +1 −1 python/plugins/processing/algs/qgis/NearestNeighbourAnalysis.py
  11. +2 −2 python/plugins/processing/algs/qgis/PointDistance.py
  12. +1 −1 python/plugins/processing/algs/qgis/PointsToPaths.py
  13. +1 −1 python/plugins/processing/algs/qgis/RandomPointsAlongLines.py
  14. +1 −1 python/plugins/processing/algs/qgis/RandomPointsPolygons.py
  15. +1 −1 python/plugins/processing/algs/qgis/SumLines.py
  16. +2 −1 src/analysis/network/qgsgraphbuilderinterface.h
  17. +1 −1 src/app/qgisapp.cpp
  18. +1 −1 src/app/qgisappinterface.cpp
  19. +1 −1 src/app/qgsattributeactionpropertiesdialog.cpp
  20. +3 −3 src/app/qgsattributetabledialog.cpp
  21. +2 −2 src/app/qgsdiagramproperties.cpp
  22. +1 −1 src/app/qgsfeatureaction.cpp
  23. +2 −2 src/app/qgsfieldcalculator.cpp
  24. +1 −1 src/app/qgslabelinggui.cpp
  25. +1 −1 src/app/qgsmaptoolmeasureangle.cpp
  26. +1 −1 src/app/qgsmaptooloffsetpointsymbol.cpp
  27. +1 −1 src/app/qgsmeasuredialog.cpp
  28. +2 −2 src/core/composer/qgscomposerhtml.cpp
  29. +2 −2 src/core/composer/qgscomposerlabel.cpp
  30. +1 −1 src/core/composer/qgscomposerscalebar.cpp
  31. +1 −1 src/core/layout/qgslayoutitemhtml.cpp
  32. +1 −1 src/core/layout/qgslayoutitemlabel.cpp
  33. +1 −1 src/core/layout/qgslayoutitemscalebar.cpp
  34. +1 −0 src/core/processing/qgsprocessingcontext.h
  35. +4 −2 src/core/qgsdistancearea.cpp
  36. +2 −2 src/core/qgsdistancearea.h
  37. +1 −1 src/core/qgsrendercontext.cpp
  38. +1 −1 src/core/qgsvectorlayerfeatureiterator.cpp
  39. +2 −2 src/gui/qgsattributeeditorcontext.h
  40. +1 −1 src/gui/qgsmaptoolidentify.cpp
  41. +11 −10 tests/src/core/testqgsdistancearea.cpp
  42. +1 −1 tests/src/core/testqgsexpression.cpp
  43. +15 −15 tests/src/python/test_qgsdistancearea.py
  44. +3 −2 tests/src/python/test_qgsrendercontext.py
@@ -1128,7 +1128,8 @@ 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.
- setSourceCrs() now requires a QgsCoordinateReferenceSystem instead of crs ID, and requires a QgsCoordinateTransformContext object. PyQGIS code
can use QgsProject.instance().transformContext() for the QgsCoordinateTransformContext argument.
- setSourceAuthId() was removed. Use setSourceCrs() instead.
- geographic() was removed. Check sourceCrs().isGeographic() instead.
- measure() has been removed. Use measureArea() or measureLength() instead.
@@ -50,9 +50,9 @@ ellipsoid if a valid ellipsoid() has been set.
.. seealso:: :py:func:`ellipsoid()`
%End

void setSourceCrs( const QgsCoordinateReferenceSystem &srcCRS );
void setSourceCrs( const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context );
%Docstring
Sets source spatial reference system.
Sets source spatial reference system ``crs``.

.. versionadded:: 2.2

@@ -10,7 +10,6 @@




class QgsAttributeEditorContext
{
%Docstring
@@ -135,7 +135,7 @@ def prepareAlgorithm(self, parameters, context, feedback):
aggregates = self.parameterAsAggregates(parameters, self.AGGREGATES, context)

da = QgsDistanceArea()
da.setSourceCrs(source.sourceCrs())
da.setSourceCrs(source.sourceCrs(), context.transformContext())
da.setEllipsoid(context.project().ellipsoid())

self.source = source
@@ -123,7 +123,7 @@ def processAlgorithm(self, parameters, context, feedback):

self.distance_area = QgsDistanceArea()
if method == 2:
self.distance_area.setSourceCrs(source.sourceCrs())
self.distance_area.setSourceCrs(source.sourceCrs(), context.transformContext())
self.distance_area.setEllipsoid(context.project().ellipsoid())
elif method == 1:
coordTransform = QgsCoordinateTransform(source.sourceCrs(), context.project().crs(), context.project())
@@ -104,7 +104,7 @@ def processAlgorithm(self, parameters, context, feedback):

expression = QgsExpression(formula)
da = QgsDistanceArea()
da.setSourceCrs(source.sourceCrs())
da.setSourceCrs(source.sourceCrs(), context.transformContext())
da.setEllipsoid(context.project().ellipsoid())
expression.setGeomCalculator(da)

@@ -121,7 +121,7 @@ def prepareAlgorithm(self, parameters, context, feedback):
self.expressions = []

da = QgsDistanceArea()
da.setSourceCrs(source.sourceCrs())
da.setSourceCrs(source.sourceCrs(), context.transformContext())
da.setEllipsoid(context.project().ellipsoid())

# create an expression context using thread safe processing context
@@ -117,7 +117,7 @@ def processAlgorithm(self, parameters, context, feedback):
index = QgsSpatialIndex(hub_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(point_source.sourceCrs(), context.transformContext())))

distance = QgsDistanceArea()
distance.setSourceCrs(point_source.sourceCrs())
distance.setSourceCrs(point_source.sourceCrs(), context.transformContext())
distance.setEllipsoid(context.project().ellipsoid())

# Scan source points, find nearest hub, and write to output file
@@ -113,7 +113,7 @@ def processAlgorithm(self, parameters, context, feedback):
index = QgsSpatialIndex(hub_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(point_source.sourceCrs(), context.transformContext())))

distance = QgsDistanceArea()
distance.setSourceCrs(point_source.sourceCrs())
distance.setSourceCrs(point_source.sourceCrs(), context.transformContext())
distance.setEllipsoid(context.project().ellipsoid())

# Scan source points, find nearest hub, and write to output file
@@ -98,7 +98,7 @@ def processAlgorithm(self, parameters, context, feedback):
spatialIndex = QgsSpatialIndex(source, feedback)

distance = QgsDistanceArea()
distance.setSourceCrs(source.sourceCrs())
distance.setSourceCrs(source.sourceCrs(), context.transformContext())
distance.setEllipsoid(context.project().ellipsoid())

sumDist = 0.00
@@ -157,7 +157,7 @@ def linearMatrix(self, parameters, context, source, inField, target_source, targ
index = QgsSpatialIndex(target_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(source.sourceCrs(), context.transformContext())), feedback)

distArea = QgsDistanceArea()
distArea.setSourceCrs(source.sourceCrs())
distArea.setSourceCrs(source.sourceCrs(), context.transformContext())
distArea.setEllipsoid(context.project().ellipsoid())

features = source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([inIdx]))
@@ -208,7 +208,7 @@ def linearMatrix(self, parameters, context, source, inField, target_source, targ
def regularMatrix(self, parameters, context, source, inField, target_source, targetField,
nPoints, feedback):
distArea = QgsDistanceArea()
distArea.setSourceCrs(source.sourceCrs())
distArea.setSourceCrs(source.sourceCrs(), context.transformContext())
distArea.setEllipsoid(context.project().ellipsoid())

inIdx = source.fields().lookupField(inField)
@@ -153,7 +153,7 @@ def processAlgorithm(self, parameters, context, feedback):
feedback.setProgress(0)

da = QgsDistanceArea()
da.setSourceCrs(source.sourceCrs())
da.setSourceCrs(source.sourceCrs(), context.transformContext())
da.setEllipsoid(context.project().ellipsoid())

current = 0
@@ -109,7 +109,7 @@ def processAlgorithm(self, parameters, context, feedback):
points = dict()

da = QgsDistanceArea()
da.setSourceCrs(source.sourceCrs())
da.setSourceCrs(source.sourceCrs(), context.transformContext())
da.setEllipsoid(context.project().ellipsoid())

request = QgsFeatureRequest()
@@ -127,7 +127,7 @@ def processAlgorithm(self, parameters, context, feedback):
fields, QgsWkbTypes.Point, source.sourceCrs())

da = QgsDistanceArea()
da.setSourceCrs(source.sourceCrs())
da.setSourceCrs(source.sourceCrs(), context.transformContext())
da.setEllipsoid(context.project().ellipsoid())

total = 100.0 / source.featureCount() if source.featureCount() else 0
@@ -107,7 +107,7 @@ def processAlgorithm(self, parameters, context, feedback):
QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(poly_source.sourceCrs(), context.transformContext())), feedback)

distArea = QgsDistanceArea()
distArea.setSourceCrs(poly_source.sourceCrs())
distArea.setSourceCrs(poly_source.sourceCrs(), context.transformContext())
distArea.setEllipsoid(context.project().ellipsoid())

features = poly_source.getFeatures()
@@ -20,6 +20,7 @@
#include <QVariant>

#include "qgspoint.h"
#include "qgsproject.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsdistancearea.h"
#include "qgis_analysis.h"
@@ -62,7 +63,7 @@ class ANALYSIS_EXPORT QgsGraphBuilderInterface
, mCtfEnabled( ctfEnabled )
, mTopologyTolerance( topologyTolerance )
{
mDa.setSourceCrs( mCrs );
mDa.setSourceCrs( mCrs, QgsProject::instance()->transformContext() );
mDa.setEllipsoid( ellipsoidID );
}

@@ -8196,7 +8196,7 @@ void QgisApp::selectByForm()
}
QgsDistanceArea myDa;

myDa.setSourceCrs( vlayer->crs() );
myDa.setSourceCrs( vlayer->crs(), QgsProject::instance()->transformContext() );
myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );

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

myDa.setSourceCrs( l->crs() );
myDa.setSourceCrs( l->crs(), QgsProject::instance()->transformContext() );
myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );

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

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

mFieldExpression->setLayer( mLayer );
@@ -142,7 +142,7 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *layer, QgsAttr

myDa = new QgsDistanceArea();

myDa->setSourceCrs( mLayer->crs() );
myDa->setSourceCrs( mLayer->crs(), QgsProject::instance()->transformContext() );
myDa->setEllipsoid( QgsProject::instance()->ellipsoid() );

mEditorContext.setDistanceArea( *myDa );
@@ -621,7 +621,7 @@ void QgsAttributeTableDialog::filterExpressionBuilder()
dlg.setWindowTitle( tr( "Expression Based Filter" ) );

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

@@ -981,7 +981,7 @@ void QgsAttributeTableDialog::setFilterExpression( const QString &filterString,
QgsFeatureIds filteredFeatures;
QgsDistanceArea myDa;

myDa.setSourceCrs( mLayer->crs() );
myDa.setSourceCrs( mLayer->crs(), QgsProject::instance()->transformContext() );
myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );

// parse search string and build parsed tree
@@ -207,7 +207,7 @@ QgsDiagramProperties::QgsDiagramProperties( QgsVectorLayer *layer, QWidget *pare
// field combo and expression button
mSizeFieldExpressionWidget->setLayer( mLayer );
QgsDistanceArea myDa;
myDa.setSourceCrs( mLayer->crs() );
myDa.setSourceCrs( mLayer->crs(), QgsProject::instance()->transformContext() );
myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );
mSizeFieldExpressionWidget->setGeomCalculator( myDa );

@@ -939,7 +939,7 @@ QString QgsDiagramProperties::showExpressionBuilder( const QString &initialExpre
dlg.setWindowTitle( tr( "Expression Based Attribute" ) );

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

@@ -57,7 +57,7 @@ QgsAttributeDialog *QgsFeatureAction::newDialog( bool cloneFeature )

QgsDistanceArea myDa;

myDa.setSourceCrs( mLayer->crs() );
myDa.setSourceCrs( mLayer->crs(), QgsProject::instance()->transformContext() );
myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );

context.setDistanceArea( myDa );
@@ -61,7 +61,7 @@ QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer *vl, QWidget *parent )
connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsFieldCalculator::showHelp );

QgsDistanceArea myDa;
myDa.setSourceCrs( vl->crs() );
myDa.setSourceCrs( vl->crs(), QgsProject::instance()->transformContext() );
myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );
builder->setGeomCalculator( myDa );

@@ -160,7 +160,7 @@ void QgsFieldCalculator::accept()
// Set up QgsDistanceArea each time we (re-)calculate
QgsDistanceArea myDa;

myDa.setSourceCrs( mVectorLayer->crs() );
myDa.setSourceCrs( mVectorLayer->crs(), QgsProject::instance()->transformContext() );
myDa.setEllipsoid( QgsProject::instance()->ellipsoid() );

QString calcString = builder->expressionText();
@@ -113,7 +113,7 @@ void QgsLabelingGui::setLayer( QgsMapLayer *mapLayer )

mFieldExpressionWidget->setLayer( mLayer );
QgsDistanceArea da;
da.setSourceCrs( mLayer->crs() );
da.setSourceCrs( mLayer->crs(), QgsProject::instance()->transformContext() );
da.setEllipsoid( QgsProject::instance()->ellipsoid() );
mFieldExpressionWidget->setGeomCalculator( da );

@@ -182,6 +182,6 @@ void QgsMapToolMeasureAngle::updateSettings()
void QgsMapToolMeasureAngle::configureDistanceArea()
{
QString ellipsoidId = QgsProject::instance()->ellipsoid();
mDa.setSourceCrs( mCanvas->mapSettings().destinationCrs() );
mDa.setSourceCrs( mCanvas->mapSettings().destinationCrs(), QgsProject::instance()->transformContext() );
mDa.setEllipsoid( ellipsoidId );
}
@@ -250,7 +250,7 @@ QPointF QgsMapToolOffsetPointSymbol::calculateOffset( const QgsPointXY &startPoi
case QgsUnitTypes::RenderMetersInMapUnits:
{
QgsDistanceArea distanceArea;
distanceArea.setSourceCrs( mCanvas->mapSettings().destinationCrs() );
distanceArea.setSourceCrs( mCanvas->mapSettings().destinationCrs(), QgsProject::instance()->transformContext() );
distanceArea.setEllipsoid( mCanvas->mapSettings().ellipsoid() );
// factor=1.0 / 1 meter in MapUnits
factor = 1.0 / distanceArea.measureLineProjected( startPoint );
@@ -105,7 +105,7 @@ void QgsMeasureDialog::updateSettings()
// Configure QgsDistanceArea
mDistanceUnits = QgsProject::instance()->distanceUnits();
mAreaUnits = QgsProject::instance()->areaUnits();
mDa.setSourceCrs( mTool->canvas()->mapSettings().destinationCrs() );
mDa.setSourceCrs( mTool->canvas()->mapSettings().destinationCrs(), QgsProject::instance()->transformContext() );
mDa.setEllipsoid( QgsProject::instance()->ellipsoid() );

mTable->clear();
@@ -530,14 +530,14 @@ void QgsComposerHtml::setExpressionContext( const QgsFeature &feature, QgsVector
//setup distance area conversion
if ( layer )
{
mDistanceArea->setSourceCrs( layer->crs() );
mDistanceArea->setSourceCrs( layer->crs(), mComposition->project()->transformContext() );
}
else if ( mComposition )
{
//set to composition's mapsettings' crs
QgsComposerMap *referenceMap = mComposition->referenceMap();
if ( referenceMap )
mDistanceArea->setSourceCrs( referenceMap->crs() );
mDistanceArea->setSourceCrs( referenceMap->crs(), mComposition->project()->transformContext() );
}
if ( mComposition )
{
@@ -260,14 +260,14 @@ void QgsComposerLabel::refreshExpressionContext()
//setup distance area conversion
if ( layer )
{
mDistanceArea->setSourceCrs( layer->crs() );
mDistanceArea->setSourceCrs( layer->crs(), mComposition->project()->transformContext() );
}
else
{
//set to composition's reference map's crs
QgsComposerMap *referenceMap = mComposition->referenceMap();
if ( referenceMap )
mDistanceArea->setSourceCrs( referenceMap->crs() );
mDistanceArea->setSourceCrs( referenceMap->crs(), mComposition->project()->transformContext() );
}
mDistanceArea->setEllipsoid( mComposition->project()->ellipsoid() );
contentChanged();
@@ -337,7 +337,7 @@ double QgsComposerScaleBar::mapWidth() const
else
{
QgsDistanceArea da;
da.setSourceCrs( mComposerMap->crs() );
da.setSourceCrs( mComposerMap->crs(), mComposition->project()->transformContext() );
da.setEllipsoid( mComposition->project()->ellipsoid() );

QgsUnitTypes::DistanceUnit units = da.lengthUnits();
@@ -487,7 +487,7 @@ void QgsLayoutItemHtml::setExpressionContext( const QgsFeature &feature, QgsVect
//setup distance area conversion
if ( layer )
{
mDistanceArea.setSourceCrs( layer->crs() );
mDistanceArea.setSourceCrs( layer->crs(), mLayout->project()->transformContext() );
}
else if ( mLayout )
{
@@ -250,7 +250,7 @@ void QgsLayoutItemLabel::refreshExpressionContext()
//setup distance area conversion
if ( layer )
{
mDistanceArea->setSourceCrs( layer->crs() );
mDistanceArea->setSourceCrs( layer->crs(), mLayout->project()->transformContext() );
}
else
{
@@ -301,7 +301,7 @@ double QgsLayoutItemScaleBar::mapWidth() const
else
{
QgsDistanceArea da;
da.setSourceCrs( mMap->crs() );
da.setSourceCrs( mMap->crs(), mLayout->project()->transformContext() );
da.setEllipsoid( mLayout->project()->ellipsoid() );

QgsUnitTypes::DistanceUnit units = da.lengthUnits();
@@ -74,6 +74,7 @@ class CORE_EXPORT QgsProcessingContext
{
mFlags = other.mFlags;
mProject = other.mProject;
mTransformContext = other.mTransformContext;
mExpressionContext = other.mExpressionContext;
mInvalidGeometryCallback = other.mInvalidGeometryCallback;
mInvalidGeometryCheck = other.mInvalidGeometryCheck;
@@ -44,7 +44,8 @@ QgsDistanceArea::QgsDistanceArea()
mSemiMajor = -1.0;
mSemiMinor = -1.0;
mInvFlattening = -1.0;
setSourceCrs( QgsCoordinateReferenceSystem::fromSrsId( GEOCRS_ID ) ); // WGS 84
QgsCoordinateTransformContext context; // this is ok - by default we have a source/dest of WGS84, so no reprojection takes place
setSourceCrs( QgsCoordinateReferenceSystem::fromSrsId( GEOCRS_ID ), context ); // WGS 84
setEllipsoid( GEO_NONE );
}

@@ -53,8 +54,9 @@ bool QgsDistanceArea::willUseEllipsoid() const
return mEllipsoid != GEO_NONE;
}

void QgsDistanceArea::setSourceCrs( const QgsCoordinateReferenceSystem &srcCRS )
void QgsDistanceArea::setSourceCrs( const QgsCoordinateReferenceSystem &srcCRS, const QgsCoordinateTransformContext &context )
{
mCoordTransform.setContext( context );
mCoordTransform.setSourceCrs( srcCRS );
}

0 comments on commit 4643712

Please sign in to comment.
You can’t perform that action at this time.