Skip to content

Commit 1c34b25

Browse files
luipirstrk
authored andcommitted
Add out layer to registry before its update.
Useful only in case of use waterfall aggregate functions because they get layer from registry basing on layer scope. Fixes #17300 (#5950)
1 parent 845bf29 commit 1c34b25

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

python/plugins/processing/algs/qgis/FieldsCalculator.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,17 @@
2626
__revision__ = '$Format:%H$'
2727

2828
from qgis.PyQt.QtCore import QVariant
29-
from qgis.core import QgsExpression, QgsExpressionContext, QgsExpressionContextUtils, QgsFeature, QgsField, QgsDistanceArea, QgsProject, GEO_NONE
29+
from qgis.core import (
30+
QgsExpression,
31+
QgsExpressionContext,
32+
QgsExpressionContextUtils,
33+
QgsFeature,
34+
QgsField,
35+
QgsDistanceArea,
36+
QgsProject,
37+
QgsMapLayerRegistry,
38+
GEO_NONE
39+
)
3040
from qgis.utils import iface
3141
from processing.core.GeoAlgorithm import GeoAlgorithm
3242
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
@@ -122,6 +132,14 @@ def processAlgorithm(self, progress):
122132
raise GeoAlgorithmExecutionException(
123133
self.tr('Evaluation error: %s' % exp.evalErrorString()))
124134

135+
# add layer to registry to fix https://issues.qgis.org/issues/17300
136+
# it is necessary only for aggregate expressions that verify that layer
137+
# is registered
138+
removeRegistryAfterEvaluation = False
139+
if not QgsMapLayerRegistry.instance().mapLayer(layer.id()):
140+
removeRegistryAfterEvaluation = True
141+
QgsMapLayerRegistry.instance().addMapLayer(layer, addToLegend=False)
142+
125143
outFeature = QgsFeature()
126144
outFeature.initAttributes(len(fields))
127145
outFeature.setFields(fields)
@@ -152,6 +170,11 @@ def processAlgorithm(self, progress):
152170
progress.setPercentage(int(current * total))
153171
del writer
154172

173+
# remove from registry if added for expression requirement
174+
# see above comment about fix #17300
175+
if removeRegistryAfterEvaluation:
176+
QgsMapLayerRegistry.instance().removeMapLayer(layer)
177+
155178
if not calculationSuccess:
156179
raise GeoAlgorithmExecutionException(
157180
self.tr('An error occurred while evaluating the calculation '

0 commit comments

Comments
 (0)