Skip to content

Commit

Permalink
Merge pull request #5919 from nyalldawson/iter_datum
Browse files Browse the repository at this point in the history
Use transform context in feature requests with destination crs set
  • Loading branch information
nyalldawson authored Dec 20, 2017
2 parents bf85709 + 4643712 commit 3c7f745
Show file tree
Hide file tree
Showing 88 changed files with 242 additions and 187 deletions.
3 changes: 2 additions & 1 deletion doc/api_break.dox
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions python/core/processing/qgsprocessingcontext.sip
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ Returns the project in which the algorithm is being executed.
%Docstring
Sets the ``project`` in which the algorithm will be executed.

This also automatically sets the transformContext() to match
the project's transform context.

.. seealso:: :py:func:`project()`
%End

Expand All @@ -82,6 +85,23 @@ Returns the expression context.
void setExpressionContext( const QgsExpressionContext &context );
%Docstring
Sets the expression ``context``.
%End

QgsCoordinateTransformContext transformContext() const;
%Docstring
Returns the coordinate transform context.

.. seealso:: :py:func:`setTransformContext()`
%End

void setTransformContext( const QgsCoordinateTransformContext &context );
%Docstring
Sets the coordinate transform ``context``.

Note that setting a project for the context will automatically set the coordinate transform
context.

.. seealso:: :py:func:`transformContext()`
%End

QgsMapLayerStore *temporaryLayerStore();
Expand Down
4 changes: 2 additions & 2 deletions python/core/qgsdistancearea.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 15 additions & 1 deletion python/core/qgsfeaturerequest.sip
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,24 @@ and all features will be left with their original geometry.

.. seealso:: :py:func:`setDestinationCrs()`

.. seealso:: :py:func:`transformContext()`

.. versionadded:: 3.0
%End

QgsCoordinateTransformContext transformContext() const;
%Docstring
Returns the transform context, for use when a destinationCrs() has been set
and reprojection is required

.. seealso:: :py:func:`setDestinationCrs()`

.. seealso:: :py:func:`destinationCrs()`

.. versionadded:: 3.0
%End

QgsFeatureRequest &setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
QgsFeatureRequest &setDestinationCrs( const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context );
%Docstring
Sets the destination ``crs`` for feature's geometries. If set, all
geometries will be reprojected from their original coordinate reference
Expand Down
1 change: 0 additions & 1 deletion python/gui/qgsattributeeditorcontext.sip
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@




class QgsAttributeEditorContext
{
%Docstring
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/Aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/Difference.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def processAlgorithm(self, parameters, context, feedback):
featB = QgsFeature()
outFeat = QgsFeature()

indexB = QgsSpatialIndex(sourceB.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(sourceA.sourceCrs())), feedback)
indexB = QgsSpatialIndex(sourceB.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(sourceA.sourceCrs(), context.transformContext())), feedback)

total = 100.0 / (sourceA.featureCount() * sourceB.featureCount()) if sourceA.featureCount() and sourceB.featureCount() else 1
count = 0
Expand All @@ -101,7 +101,7 @@ def processAlgorithm(self, parameters, context, feedback):
attrs = featA.attributes()
intersects = indexB.intersects(geom.boundingBox())
request = QgsFeatureRequest().setFilterFids(intersects).setSubsetOfAttributes([])
request.setDestinationCrs(sourceA.sourceCrs())
request.setDestinationCrs(sourceA.sourceCrs(), context.transformContext())
for featB in sourceB.getFeatures(request):
if feedback.isCanceled():
break
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/ExportGeometryInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/FieldsCalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/FieldsMapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/qgis/HubDistanceLines.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ def processAlgorithm(self, parameters, context, feedback):
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, QgsWkbTypes.LineString, point_source.sourceCrs())

index = QgsSpatialIndex(hub_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(point_source.sourceCrs())))
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
Expand All @@ -133,7 +133,7 @@ def processAlgorithm(self, parameters, context, feedback):
src = f.geometry().boundingBox().center()

neighbors = index.nearestNeighbor(src, 1)
ft = next(hub_source.getFeatures(QgsFeatureRequest().setFilterFid(neighbors[0]).setSubsetOfAttributes([fieldName], hub_source.fields()).setDestinationCrs(point_source.sourceCrs())))
ft = next(hub_source.getFeatures(QgsFeatureRequest().setFilterFid(neighbors[0]).setSubsetOfAttributes([fieldName], hub_source.fields()).setDestinationCrs(point_source.sourceCrs(), context.transformContext())))
closest = ft.geometry().boundingBox().center()
hubDist = distance.measureLine(src, closest)

Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/qgis/HubDistancePoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ def processAlgorithm(self, parameters, context, feedback):
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, QgsWkbTypes.Point, point_source.sourceCrs())

index = QgsSpatialIndex(hub_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(point_source.sourceCrs())))
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
Expand All @@ -130,7 +130,7 @@ def processAlgorithm(self, parameters, context, feedback):
src = f.geometry().boundingBox().center()

neighbors = index.nearestNeighbor(src, 1)
ft = next(hub_source.getFeatures(QgsFeatureRequest().setFilterFid(neighbors[0]).setSubsetOfAttributes([fieldName], hub_source.fields()).setDestinationCrs(point_source.sourceCrs())))
ft = next(hub_source.getFeatures(QgsFeatureRequest().setFilterFid(neighbors[0]).setSubsetOfAttributes([fieldName], hub_source.fields()).setDestinationCrs(point_source.sourceCrs(), context.transformContext())))
closest = ft.geometry().boundingBox().center()
hubDist = distance.measureLine(src, closest)

Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/HypsometricCurves.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def processAlgorithm(self, parameters, context, feedback):
memVectorDriver = ogr.GetDriverByName('Memory')
memRasterDriver = gdal.GetDriverByName('MEM')

features = source.getFeatures(QgsFeatureRequest().setDestinationCrs(target_crs))
features = source.getFeatures(QgsFeatureRequest().setDestinationCrs(target_crs, context.transformContext()))
total = 100.0 / source.featureCount() if source.featureCount() else 0

for current, f in enumerate(features):
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/Intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def processAlgorithm(self, parameters, context, feedback):
output_fields, geomType, sourceA.sourceCrs())

outFeat = QgsFeature()
indexB = QgsSpatialIndex(sourceB.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(sourceA.sourceCrs())), feedback)
indexB = QgsSpatialIndex(sourceB.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(sourceA.sourceCrs(), context.transformContext())), feedback)

total = 100.0 / sourceA.featureCount() if sourceA.featureCount() else 1
count = 0
Expand All @@ -149,7 +149,7 @@ def processAlgorithm(self, parameters, context, feedback):
intersects = indexB.intersects(geom.boundingBox())

request = QgsFeatureRequest().setFilterFids(intersects)
request.setDestinationCrs(sourceA.sourceCrs())
request.setDestinationCrs(sourceA.sourceCrs(), context.transformContext())
request.setSubsetOfAttributes(field_indices_b)

engine = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions python/plugins/processing/algs/qgis/PointDistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ def linearMatrix(self, parameters, context, source, inField, target_source, targ
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, out_wkb, source.sourceCrs())

index = QgsSpatialIndex(target_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(source.sourceCrs())), feedback)
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]))
Expand All @@ -171,7 +171,7 @@ def linearMatrix(self, parameters, context, source, inField, target_source, targ
featList = index.nearestNeighbor(inGeom.asPoint(), nPoints)
distList = []
vari = 0.0
request = QgsFeatureRequest().setFilterFids(featList).setSubsetOfAttributes([outIdx]).setDestinationCrs(source.sourceCrs())
request = QgsFeatureRequest().setFilterFids(featList).setSubsetOfAttributes([outIdx]).setDestinationCrs(source.sourceCrs(), context.transformContext())
for outFeat in target_source.getFeatures(request):
if feedback.isCanceled():
break
Expand Down Expand Up @@ -208,13 +208,13 @@ 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)
targetIdx = target_source.fields().lookupField(targetField)

index = QgsSpatialIndex(target_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(source.sourceCrs())), feedback)
index = QgsSpatialIndex(target_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(source.sourceCrs(), context.transformContext())), feedback)

first = True
sink = None
Expand All @@ -233,14 +233,14 @@ def regularMatrix(self, parameters, context, source, inField, target_source, tar
input_id_field = source.fields()[inIdx]
input_id_field.setName('ID')
fields.append(input_id_field)
for f in target_source.getFeatures(QgsFeatureRequest().setFilterFids(featList).setSubsetOfAttributes([targetIdx]).setDestinationCrs(source.sourceCrs())):
for f in target_source.getFeatures(QgsFeatureRequest().setFilterFids(featList).setSubsetOfAttributes([targetIdx]).setDestinationCrs(source.sourceCrs(), context.transformContext())):
fields.append(QgsField(str(f[targetField]), QVariant.Double))

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, source.wkbType(), source.sourceCrs())

data = [inFeat[inField]]
for target in target_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setFilterFids(featList).setDestinationCrs(source.sourceCrs())):
for target in target_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setFilterFids(featList).setDestinationCrs(source.sourceCrs(), context.transformContext())):
if feedback.isCanceled():
break
outGeom = target.geometry()
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/PointsFromLines.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def processAlgorithm(self, parameters, context, feedback):
self.lineId = 0
self.pointId = 0

features = source.getFeatures(QgsFeatureRequest().setDestinationCrs(raster_layer.crs()))
features = source.getFeatures(QgsFeatureRequest().setDestinationCrs(raster_layer.crs(), context.transformContext()))
total = 100.0 / source.featureCount() if source.featureCount() else 0
for current, f in enumerate(features):
if feedback.isCanceled():
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/PointsFromPolygons.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def processAlgorithm(self, parameters, context, feedback):
polyId = 0
pointId = 0

features = source.getFeatures(QgsFeatureRequest().setDestinationCrs(raster_layer.crs()))
features = source.getFeatures(QgsFeatureRequest().setDestinationCrs(raster_layer.crs(), context.transformContext()))
total = 100.0 / source.featureCount() if source.featureCount() else 0
for current, f in enumerate(features):
if feedback.isCanceled():
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/PointsInPolygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def processAlgorithm(self, parameters, context, feedback):
fields, poly_source.wkbType(), poly_source.sourceCrs())

spatialIndex = QgsSpatialIndex(point_source.getFeatures(
QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(poly_source.sourceCrs())), feedback)
QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(poly_source.sourceCrs(), context.transformContext())), feedback)

point_attribute_indices = []
if weight_field_index >= 0:
Expand All @@ -140,7 +140,7 @@ def processAlgorithm(self, parameters, context, feedback):

points = spatialIndex.intersects(geom.boundingBox())
if len(points) > 0:
request = QgsFeatureRequest().setFilterFids(points).setDestinationCrs(poly_source.sourceCrs())
request = QgsFeatureRequest().setFilterFids(points).setDestinationCrs(poly_source.sourceCrs(), context.transformContext())
request.setSubsetOfAttributes(point_attribute_indices)
for point_feature in point_source.getFeatures(request):
if feedback.isCanceled():
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/PointsToPaths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down