|
26 | 26 | __revision__ = '$Format:%H$' |
27 | 27 |
|
28 | 28 |
|
29 | | -from qgis.core import QgsField, QgsExpression, QgsFeature |
| 29 | +from qgis.core import QgsField, QgsExpression, QgsExpressionContext, QgsExpressionContextUtils, QgsDistanceArea, QgsProject, QgsFeature, GEO_NONE |
| 30 | +from qgis.utils import iface |
30 | 31 | from processing.core.GeoAlgorithm import GeoAlgorithm |
31 | 32 | from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException |
32 | 33 | from processing.core.parameters import ParameterVector |
@@ -68,19 +69,36 @@ def processAlgorithm(self, progress): |
68 | 69 | provider = layer.dataProvider() |
69 | 70 | fields = [] |
70 | 71 | expressions = [] |
| 72 | + |
| 73 | + da = QgsDistanceArea() |
| 74 | + da.setSourceCrs(layer.crs().srsid()) |
| 75 | + da.setEllipsoidalMode( |
| 76 | + iface.mapCanvas().mapSettings().hasCrsTransformEnabled()) |
| 77 | + da.setEllipsoid(QgsProject.instance().readEntry( |
| 78 | + 'Measure', '/Ellipsoid', GEO_NONE)[0]) |
| 79 | + |
| 80 | + exp_context = QgsExpressionContext() |
| 81 | + exp_context.appendScope(QgsExpressionContextUtils.globalScope()) |
| 82 | + exp_context.appendScope(QgsExpressionContextUtils.projectScope()) |
| 83 | + exp_context.appendScope(QgsExpressionContextUtils.layerScope(layer)) |
| 84 | + |
71 | 85 | for field_def in mapping: |
72 | 86 | fields.append(QgsField(name=field_def['name'], |
73 | 87 | type=field_def['type'], |
74 | 88 | len=field_def['length'], |
75 | 89 | prec=field_def['precision'])) |
76 | 90 |
|
77 | 91 | expression = QgsExpression(field_def['expression']) |
| 92 | + expression.setGeomCalculator(da) |
| 93 | + expression.setDistanceUnits(QgsProject.instance().distanceUnits()) |
| 94 | + expression.setAreaUnits(QgsProject.instance().areaUnits()) |
| 95 | + |
78 | 96 | if expression.hasParserError(): |
79 | 97 | raise GeoAlgorithmExecutionException( |
80 | 98 | self.tr(u'Parser error in expression "{}": {}') |
81 | 99 | .format(unicode(field_def['expression']), |
82 | 100 | unicode(expression.parserErrorString()))) |
83 | | - expression.prepare(provider.fields()) |
| 101 | + expression.prepare(exp_context) |
84 | 102 | if expression.hasEvalError(): |
85 | 103 | raise GeoAlgorithmExecutionException( |
86 | 104 | self.tr(u'Evaluation error in expression "{}": {}') |
@@ -108,8 +126,9 @@ def processAlgorithm(self, progress): |
108 | 126 | for i in xrange(0, len(mapping)): |
109 | 127 | field_def = mapping[i] |
110 | 128 | expression = expressions[i] |
111 | | - expression.setCurrentRowNumber(rownum) |
112 | | - value = expression.evaluate(inFeat) |
| 129 | + exp_context.setFeature(inFeat) |
| 130 | + exp_context.lastScope().setVariable("row_number", rownum) |
| 131 | + value = expression.evaluate(exp_context) |
113 | 132 | if expression.hasEvalError(): |
114 | 133 | calculationSuccess = False |
115 | 134 | error = expression.evalErrorString() |
|
0 commit comments