@@ -78,10 +78,7 @@ def processAlgorithm(self, progress):
7878 da .setEllipsoid (QgsProject .instance ().readEntry (
7979 'Measure' , '/Ellipsoid' , GEO_NONE )[0 ])
8080
81- exp_context = QgsExpressionContext ()
82- exp_context .appendScope (QgsExpressionContextUtils .globalScope ())
83- exp_context .appendScope (QgsExpressionContextUtils .projectScope ())
84- exp_context .appendScope (QgsExpressionContextUtils .layerScope (layer ))
81+ exp_context = layer .createExpressionContext ()
8582
8683 for field_def in mapping :
8784 fields .append (QgsField (name = field_def ['name' ],
@@ -93,27 +90,20 @@ def processAlgorithm(self, progress):
9390 expression .setGeomCalculator (da )
9491 expression .setDistanceUnits (QgsProject .instance ().distanceUnits ())
9592 expression .setAreaUnits (QgsProject .instance ().areaUnits ())
96-
93+ expression . prepare ( exp_context )
9794 if expression .hasParserError ():
9895 raise GeoAlgorithmExecutionException (
9996 self .tr (u'Parser error in expression "{}": {}' )
100- .format (str (field_def ['expression' ]),
101- str (expression .parserErrorString ())))
102- expression .prepare (exp_context )
103- if expression .hasEvalError ():
104- raise GeoAlgorithmExecutionException (
105- self .tr (u'Evaluation error in expression "{}": {}' )
106- .format (str (field_def ['expression' ]),
107- str (expression .evalErrorString ())))
97+ .format (unicode (expression .expression ()),
98+ unicode (expression .parserErrorString ())))
10899 expressions .append (expression )
109100
110101 writer = output .getVectorWriter (fields ,
111102 layer .wkbType (),
112103 layer .crs ())
113104
114105 # Create output vector layer with new attributes
115- error = ''
116- calculationSuccess = True
106+ error_exp = None
117107 inFeat = QgsFeature ()
118108 outFeat = QgsFeature ()
119109 features = vector .features (layer )
@@ -132,8 +122,7 @@ def processAlgorithm(self, progress):
132122 exp_context .lastScope ().setVariable ("row_number" , rownum )
133123 value = expression .evaluate (exp_context )
134124 if expression .hasEvalError ():
135- calculationSuccess = False
136- error = expression .evalErrorString ()
125+ error_exp = expression
137126 break
138127
139128 attrs .append (value )
@@ -145,7 +134,8 @@ def processAlgorithm(self, progress):
145134
146135 del writer
147136
148- if not calculationSuccess :
137+ if error_exp is not None :
149138 raise GeoAlgorithmExecutionException (
150- self .tr ('An error occurred while evaluating the calculation'
151- ' string:\n ' ) + error )
139+ self .tr (u'Evaluation error in expression "{}": {}' )
140+ .format (unicode (error_exp .expression ()),
141+ unicode (error_exp .parserErrorString ())))
0 commit comments