@@ -47,13 +47,9 @@ class FieldsCalculator(GeoAlgorithm):
4747 FORMULA = "FORMULA"
4848 OUTPUT_LAYER = "OUTPUT_LAYER"
4949
50- TYPE_NAMES = ["Integer " , "Float " , "String" ]
51- TYPES = [QVariant .Int , QVariant .Double , QVariant .String ]
50+ TYPE_NAMES = ["Float " , "Integer " , "String" , "Boolean " ]
51+ TYPES = [QVariant .Double , QVariant .Int , QVariant .String . QVariant . Bool ]
5252
53- #===========================================================================
54- # def getIcon(self):
55- # return QtGui.QIcon(os.path.dirname(__file__) + "/../images/qgis.png")
56- #===========================================================================
5753
5854 def defineCharacteristics (self ):
5955 self .name = "Field calculator"
@@ -62,7 +58,7 @@ def defineCharacteristics(self):
6258 self .addParameter (ParameterString (self .FIELD_NAME , "Result field name" ))
6359 self .addParameter (ParameterSelection (self .FIELD_TYPE , "Field type" , self .TYPE_NAMES ))
6460 self .addParameter (ParameterNumber (self .FIELD_LENGTH , "Field length" , 1 , 255 , 10 ))
65- self .addParameter (ParameterNumber (self .FIELD_PRECISION , "Field precision" , 0 , 10 , 0 ))
61+ self .addParameter (ParameterNumber (self .FIELD_PRECISION , "Field precision" , 0 , 10 , 5 ))
6662 self .addParameter (ParameterString (self .FORMULA , "Formula" ))
6763 self .addOutput (OutputVector (self .OUTPUT_LAYER , "Output layer" ))
6864
@@ -85,17 +81,20 @@ def processAlgorithm(self, progress):
8581 nFeat = provider .featureCount ()
8682 nElement = 0
8783 features = QGisLayers .features (layer )
84+
85+ fieldnames = [field .name () for field in provider .fields ()]
86+ fieldnames .sort (key = len , reverse = False )
87+ fieldidx = [fieldnames .index (field .name ()) for field in provider .fields ()]
88+ print fieldidx
8889 for inFeat in features :
8990 progress .setPercentage (int ((100 * nElement ) / nFeat ))
9091 attrs = inFeat .attributes ()
91- expression = formula
92- k = 0
93- for attr in attrs :
94- expression = expression .replace (unicode (fields [k ].name ()), unicode (attr ))
95- k += 1
96- try :
92+ expression = formula
93+ for idx in fieldidx :
94+ expression = expression .replace (unicode (fields [idx ].name ()), unicode (attrs [idx ]))
95+ try :
9796 result = eval (expression )
98- except Exception :
97+ except Exception :
9998 result = None
10099 #raise GeoAlgorithmExecutionException("Problem evaluation formula: Wrong field values or formula")
101100 nElement += 1
0 commit comments