Skip to content

Commit

Permalink
Add out layer to registry before its update.
Browse files Browse the repository at this point in the history
Useful only in case of use waterfall aggregate functions because they get
layer from registry basing on layer scope.

Fixes #17300
(#5950)
  • Loading branch information
luipir authored and strk committed Jan 24, 2018
1 parent 845bf29 commit 1c34b25
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion python/plugins/processing/algs/qgis/FieldsCalculator.py
Expand Up @@ -26,7 +26,17 @@
__revision__ = '$Format:%H$'

from qgis.PyQt.QtCore import QVariant
from qgis.core import QgsExpression, QgsExpressionContext, QgsExpressionContextUtils, QgsFeature, QgsField, QgsDistanceArea, QgsProject, GEO_NONE
from qgis.core import (
QgsExpression,
QgsExpressionContext,
QgsExpressionContextUtils,
QgsFeature,
QgsField,
QgsDistanceArea,
QgsProject,
QgsMapLayerRegistry,
GEO_NONE
)
from qgis.utils import iface
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
Expand Down Expand Up @@ -122,6 +132,14 @@ def processAlgorithm(self, progress):
raise GeoAlgorithmExecutionException(
self.tr('Evaluation error: %s' % exp.evalErrorString()))

# add layer to registry to fix https://issues.qgis.org/issues/17300
# it is necessary only for aggregate expressions that verify that layer
# is registered
removeRegistryAfterEvaluation = False
if not QgsMapLayerRegistry.instance().mapLayer(layer.id()):
removeRegistryAfterEvaluation = True
QgsMapLayerRegistry.instance().addMapLayer(layer, addToLegend=False)

outFeature = QgsFeature()
outFeature.initAttributes(len(fields))
outFeature.setFields(fields)
Expand Down Expand Up @@ -152,6 +170,11 @@ def processAlgorithm(self, progress):
progress.setPercentage(int(current * total))
del writer

# remove from registry if added for expression requirement
# see above comment about fix #17300
if removeRegistryAfterEvaluation:
QgsMapLayerRegistry.instance().removeMapLayer(layer)

if not calculationSuccess:
raise GeoAlgorithmExecutionException(
self.tr('An error occurred while evaluating the calculation '
Expand Down

0 comments on commit 1c34b25

Please sign in to comment.