Skip to content

Commit d8a5e0f

Browse files
committed
[processing] correctly handle zero in Basic statistics alg (fix #14331)
1 parent a0da85f commit d8a5e0f

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

python/plugins/processing/algs/qgis/BasicStatisticsNumbers.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class BasicStatisticsNumbers(GeoAlgorithm):
5757
MAJORITY = 'MAJORITY'
5858
FIRSTQUARTILE = 'FIRSTQUARTILE'
5959
THIRDQUARTILE = 'THIRDQUARTILE'
60+
NULLVALUES = 'NULLVALUES'
6061
IQR = 'IQR'
6162

6263
def defineCharacteristics(self):
@@ -86,6 +87,7 @@ def defineCharacteristics(self):
8687
self.addOutput(OutputNumber(self.MAJORITY, self.tr('Majority (most frequently occurring value)')))
8788
self.addOutput(OutputNumber(self.FIRSTQUARTILE, self.tr('First quartile')))
8889
self.addOutput(OutputNumber(self.THIRDQUARTILE, self.tr('Third quartile')))
90+
self.addOutput(OutputNumber(self.NULLVALUES, self.tr('NULL (missed) values')))
8991
self.addOutput(OutputNumber(self.IQR, self.tr('Interquartile Range (IQR)')))
9092

9193
def processAlgorithm(self, progress):
@@ -95,8 +97,6 @@ def processAlgorithm(self, progress):
9597

9698
outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE)
9799

98-
index = layer.fieldNameIndex(fieldName)
99-
100100
cvValue = 0
101101
minValue = 0
102102
maxValue = 0
@@ -108,6 +108,7 @@ def processAlgorithm(self, progress):
108108
majority = 0
109109
firstQuartile = 0
110110
thirdQuartile = 0
111+
nullValues = 0
111112
iqr = 0
112113

113114
isFirst = True
@@ -117,8 +118,11 @@ def processAlgorithm(self, progress):
117118
count = len(features)
118119
total = 100.0 / float(count)
119120
for current, ft in enumerate(features):
120-
if ft.attributes()[index]:
121-
values.append(float(ft.attributes()[index]))
121+
value = ft[fieldName]
122+
if value or value == 0:
123+
values.append(float(value))
124+
else:
125+
nullValues += 1
122126

123127
progress.setPercentage(int(current * total))
124128

@@ -159,6 +163,7 @@ def processAlgorithm(self, progress):
159163
data.append('Majority (most frequently occurring value): ' + unicode(majority))
160164
data.append('First quartile: ' + unicode(firstQuartile))
161165
data.append('Third quartile: ' + unicode(thirdQuartile))
166+
data.append('NULL (missed) values: ' + unicode(nullValues))
162167
data.append('Interquartile Range (IQR): ' + unicode(iqr))
163168

164169
self.createHTML(outputFile, data)
@@ -176,6 +181,7 @@ def processAlgorithm(self, progress):
176181
self.setOutputValue(self.MAJORITY, majority)
177182
self.setOutputValue(self.FIRSTQUARTILE, firstQuartile)
178183
self.setOutputValue(self.THIRDQUARTILE, thirdQuartile)
184+
self.setOutputValue(self.NULLVALUES, nullValues)
179185
self.setOutputValue(self.IQR, iqr)
180186

181187
def createHTML(self, outputFile, algData):

0 commit comments

Comments
 (0)