|
26 | 26 | __revision__ = '$Format:%H$'
|
27 | 27 |
|
28 | 28 | 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 | +) |
30 | 40 | from qgis.utils import iface
|
31 | 41 | from processing.core.GeoAlgorithm import GeoAlgorithm
|
32 | 42 | from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
@@ -122,6 +132,14 @@ def processAlgorithm(self, progress):
|
122 | 132 | raise GeoAlgorithmExecutionException(
|
123 | 133 | self.tr('Evaluation error: %s' % exp.evalErrorString()))
|
124 | 134 |
|
| 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 | + |
125 | 143 | outFeature = QgsFeature()
|
126 | 144 | outFeature.initAttributes(len(fields))
|
127 | 145 | outFeature.setFields(fields)
|
@@ -152,6 +170,11 @@ def processAlgorithm(self, progress):
|
152 | 170 | progress.setPercentage(int(current * total))
|
153 | 171 | del writer
|
154 | 172 |
|
| 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 | + |
155 | 178 | if not calculationSuccess:
|
156 | 179 | raise GeoAlgorithmExecutionException(
|
157 | 180 | self.tr('An error occurred while evaluating the calculation '
|
|
0 commit comments