Skip to content
Permalink
Browse files

Require a transform context when setting the destination crs for

a feature request

Forces correct datum handling for these requests, and removes
a few more uses of the deprecated/datum unaware transforms.
  • Loading branch information
nyalldawson committed Dec 20, 2017
1 parent b24a1bf commit 6528f1c30730cd4f0e5fb24d0755c9e5431733ae
Showing with 111 additions and 108 deletions.
  1. +15 −1 python/core/qgsfeaturerequest.sip
  2. +2 −2 python/plugins/processing/algs/qgis/Difference.py
  3. +2 −2 python/plugins/processing/algs/qgis/HubDistanceLines.py
  4. +2 −2 python/plugins/processing/algs/qgis/HubDistancePoints.py
  5. +1 −1 python/plugins/processing/algs/qgis/HypsometricCurves.py
  6. +2 −2 python/plugins/processing/algs/qgis/Intersection.py
  7. +5 −5 python/plugins/processing/algs/qgis/PointDistance.py
  8. +1 −1 python/plugins/processing/algs/qgis/PointsFromLines.py
  9. +1 −1 python/plugins/processing/algs/qgis/PointsFromPolygons.py
  10. +2 −2 python/plugins/processing/algs/qgis/PointsInPolygon.py
  11. +1 −1 python/plugins/processing/algs/qgis/ServiceAreaFromLayer.py
  12. +1 −1 python/plugins/processing/algs/qgis/ShortestPathLayerToPoint.py
  13. +1 −1 python/plugins/processing/algs/qgis/ShortestPathPointToLayer.py
  14. +1 −1 python/plugins/processing/algs/qgis/SpatialJoin.py
  15. +1 −1 python/plugins/processing/algs/qgis/SpatialJoinSummary.py
  16. +2 −2 python/plugins/processing/algs/qgis/SumLines.py
  17. +3 −3 python/plugins/processing/algs/qgis/SymmetricalDifference.py
  18. +4 −4 python/plugins/processing/algs/qgis/Union.py
  19. +2 −2 src/3d/symbols/qgsline3dsymbol_p.cpp
  20. +4 −4 src/3d/symbols/qgspoint3dsymbol_p.cpp
  21. +2 −2 src/3d/symbols/qgspolygon3dsymbol_p.cpp
  22. +1 −1 src/analysis/processing/qgsalgorithmclip.cpp
  23. +4 −4 src/analysis/processing/qgsalgorithmextractbylocation.cpp
  24. +1 −1 src/analysis/processing/qgsalgorithmextractbylocation.h
  25. +1 −1 src/analysis/processing/qgsalgorithmjoinwithlines.cpp
  26. +2 −2 src/analysis/processing/qgsalgorithmlineintersection.cpp
  27. +1 −1 src/analysis/processing/qgsalgorithmmergevector.cpp
  28. +1 −1 src/analysis/processing/qgsalgorithmsplitwithlines.cpp
  29. +1 −3 src/core/providers/memory/qgsmemoryfeatureiterator.cpp
  30. +2 −6 src/core/qgscachedfeatureiterator.cpp
  31. +7 −1 src/core/qgsfeaturerequest.cpp
  32. +12 −1 src/core/qgsfeaturerequest.h
  33. +2 −4 src/core/qgsvectorlayerfeatureiterator.cpp
  34. +3 −5 src/core/qgsvectorlayerlabelprovider.cpp
  35. +1 −3 src/providers/arcgisrest/qgsafsfeatureiterator.cpp
  36. +1 −3 src/providers/db2/qgsdb2featureiterator.cpp
  37. +1 −3 src/providers/delimitedtext/qgsdelimitedtextfeatureiterator.cpp
  38. +1 −3 src/providers/gpx/qgsgpxfeatureiterator.cpp
  39. +1 −3 src/providers/mssql/qgsmssqlfeatureiterator.cpp
  40. +1 −3 src/providers/ogr/qgsogrfeatureiterator.cpp
  41. +1 −1 src/providers/oracle/qgsoraclefeatureiterator.cpp
  42. +1 −3 src/providers/postgres/qgspostgresfeatureiterator.cpp
  43. +1 −3 src/providers/spatialite/qgsspatialitefeatureiterator.cpp
  44. +1 −3 src/providers/virtual/qgsvirtuallayerfeatureiterator.cpp
  45. +1 −3 src/providers/wfs/qgswfsfeatureiterator.cpp
  46. +4 −3 tests/src/python/featuresourcetestbase.py
  47. +2 −1 tests/src/python/test_qgsfeaturesource.py
  48. +1 −1 tests/src/python/test_qgsvectorlayer.py
@@ -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
@@ -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
@@ -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
@@ -114,7 +114,7 @@ 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())
@@ -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)

@@ -110,7 +110,7 @@ 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())
@@ -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)

@@ -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):
@@ -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
@@ -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
@@ -154,7 +154,7 @@ 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())
@@ -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
@@ -214,7 +214,7 @@ def regularMatrix(self, parameters, context, source, inField, target_source, tar
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
@@ -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()
@@ -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():
@@ -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():
@@ -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:
@@ -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():
@@ -213,7 +213,7 @@ def processAlgorithm(self, parameters, context, feedback):

feedback.pushInfo(self.tr('Loading start points...'))
request = QgsFeatureRequest()
request.setDestinationCrs(network.sourceCrs())
request.setDestinationCrs(network.sourceCrs(), context.transformContext())
features = startPoints.getFeatures(request)
total = 100.0 / startPoints.featureCount() if startPoints.featureCount() else 0

@@ -215,7 +215,7 @@ def processAlgorithm(self, parameters, context, feedback):

feedback.pushInfo(self.tr('Loading start points...'))
request = QgsFeatureRequest()
request.setDestinationCrs(network.sourceCrs())
request.setDestinationCrs(network.sourceCrs(), context.transformContext())
features = startPoints.getFeatures(request)
total = 100.0 / startPoints.featureCount() if startPoints.featureCount() else 0

@@ -216,7 +216,7 @@ def processAlgorithm(self, parameters, context, feedback):

feedback.pushInfo(self.tr('Loading end points...'))
request = QgsFeatureRequest()
request.setDestinationCrs(network.sourceCrs())
request.setDestinationCrs(network.sourceCrs(), context.transformContext())
features = endPoints.getFeatures(request)
total = 100.0 / endPoints.featureCount() if endPoints.featureCount() else 0

@@ -168,7 +168,7 @@ def processAlgorithm(self, parameters, context, feedback):

added_set = set()

request = QgsFeatureRequest().setSubsetOfAttributes(join_field_indexes).setDestinationCrs(source.sourceCrs())
request = QgsFeatureRequest().setSubsetOfAttributes(join_field_indexes).setDestinationCrs(source.sourceCrs(), context.transformContext())
features = join_source.getFeatures(request)
total = 100.0 / join_source.featureCount() if join_source.featureCount() else 0

@@ -278,7 +278,7 @@ def addField(original, stat, type):

values = []

request = QgsFeatureRequest().setFilterRect(bbox).setSubsetOfAttributes(join_field_indexes).setDestinationCrs(source.sourceCrs())
request = QgsFeatureRequest().setFilterRect(bbox).setSubsetOfAttributes(join_field_indexes).setDestinationCrs(source.sourceCrs(), context.transformContext())
for test_feat in join_source.getFeatures(request):
if feedback.isCanceled():
break
@@ -104,7 +104,7 @@ def processAlgorithm(self, parameters, context, feedback):
fields, poly_source.wkbType(), poly_source.sourceCrs())

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

distArea = QgsDistanceArea()
distArea.setSourceCrs(poly_source.sourceCrs())
@@ -131,7 +131,7 @@ def processAlgorithm(self, parameters, context, feedback):
engine.prepareGeometry()

if has_intersections:
request = QgsFeatureRequest().setFilterFids(lines).setSubsetOfAttributes([]).setDestinationCrs(poly_source.sourceCrs())
request = QgsFeatureRequest().setFilterFids(lines).setSubsetOfAttributes([]).setDestinationCrs(poly_source.sourceCrs(), context.transformContext())
for line_feature in line_source.getFeatures(request):
if feedback.isCanceled():
break
@@ -92,7 +92,7 @@ def processAlgorithm(self, parameters, context, feedback):
outFeat = QgsFeature()

indexA = QgsSpatialIndex(sourceA, feedback)
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
@@ -106,7 +106,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
@@ -128,7 +128,7 @@ def processAlgorithm(self, parameters, context, feedback):

length = len(sourceA.fields())

for featA in sourceB.getFeatures(QgsFeatureRequest().setDestinationCrs(sourceA.sourceCrs())):
for featA in sourceB.getFeatures(QgsFeatureRequest().setDestinationCrs(sourceA.sourceCrs(), context.transformContext())):
if feedback.isCanceled():
break

@@ -92,7 +92,7 @@ def processAlgorithm(self, parameters, context, feedback):
outFeat = QgsFeature()

indexA = QgsSpatialIndex(sourceA, feedback)
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
@@ -117,7 +117,7 @@ def processAlgorithm(self, parameters, context, feedback):
feedback.pushInfo(self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'))
else:
request = QgsFeatureRequest().setFilterFids(intersects).setSubsetOfAttributes([])
request.setDestinationCrs(sourceA.sourceCrs())
request.setDestinationCrs(sourceA.sourceCrs(), context.transformContext())

engine = QgsGeometry.createGeometryEngine(geom.constGet())
engine.prepareGeometry()
@@ -190,7 +190,7 @@ def processAlgorithm(self, parameters, context, feedback):
length = len(sourceA.fields())
atMapA = [None] * length

for featA in sourceB.getFeatures(QgsFeatureRequest().setDestinationCrs(sourceA.sourceCrs())):
for featA in sourceB.getFeatures(QgsFeatureRequest().setDestinationCrs(sourceA.sourceCrs(), context.transformContext())):
if feedback.isCanceled():
break

@@ -211,7 +211,7 @@ def processAlgorithm(self, parameters, context, feedback):
feedback.pushInfo(self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'))
else:
request = QgsFeatureRequest().setFilterFids(intersects).setSubsetOfAttributes([])
request.setDestinationCrs(sourceA.sourceCrs())
request.setDestinationCrs(sourceA.sourceCrs(), context.transformContext())

# use prepared geometries for faster intersection tests
engine = QgsGeometry.createGeometryEngine(diff_geom.constGet())

0 comments on commit 6528f1c

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