Skip to content

Commit d3852e4

Browse files
committed
[processing] fix handling of NULLs is Basic statistics for text fields
1 parent 847f1c0 commit d3852e4

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

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

+14-12
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ def processAlgorithm(self, progress):
8484
minValue = 0
8585
maxValue = 0
8686
meanValue = 0
87-
countEmpty = 0
88-
countFilled = 0
87+
nullValues = 0
88+
filledValues = 0
8989

9090
isFirst = True
9191
values = []
@@ -94,7 +94,14 @@ def processAlgorithm(self, progress):
9494
count = len(features)
9595
total = 100.0 / count
9696
for current, ft in enumerate(features):
97-
length = float(len(ft.attributes()[index]))
97+
value = ft[fieldName]
98+
if value:
99+
length = float(len(value))
100+
filledValues += 1
101+
else:
102+
nullValues += 1
103+
progress.setPercentage(int(current * total))
104+
continue
98105

99106
if isFirst:
100107
minValue = length
@@ -106,11 +113,6 @@ def processAlgorithm(self, progress):
106113
if length > maxValue:
107114
maxValue = length
108115

109-
if length != 0.00:
110-
countFilled += 1
111-
else:
112-
countEmpty += 1
113-
114116
values.append(length)
115117
sumValue += length
116118

@@ -128,8 +130,8 @@ def processAlgorithm(self, progress):
128130
data.append('Minimum length: ' + unicode(minValue))
129131
data.append('Maximum length: ' + unicode(maxValue))
130132
data.append('Mean length: ' + unicode(meanValue))
131-
data.append('Filled: ' + unicode(countFilled))
132-
data.append('Empty: ' + unicode(countEmpty))
133+
data.append('Filled values: ' + unicode(filledValues))
134+
data.append('NULL (missed) values: ' + unicode(nullValues))
133135
data.append('Count: ' + unicode(count))
134136
data.append('Unique: ' + unicode(uniqueValues))
135137

@@ -138,8 +140,8 @@ def processAlgorithm(self, progress):
138140
self.setOutputValue(self.MIN_LEN, minValue)
139141
self.setOutputValue(self.MAX_LEN, maxValue)
140142
self.setOutputValue(self.MEAN_LEN, meanValue)
141-
self.setOutputValue(self.FILLED, countFilled)
142-
self.setOutputValue(self.EMPTY, countEmpty)
143+
self.setOutputValue(self.FILLED, filledValues)
144+
self.setOutputValue(self.EMPTY, nullValues)
143145
self.setOutputValue(self.COUNT, count)
144146
self.setOutputValue(self.UNIQUE, uniqueValues)
145147

python/plugins/processing/tests/testdata/algorithm_tests.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ tests:
6363
OUTPUT:
6464
name: expected/polys_to_lines.gml
6565
type: vector
66+
6667
- algorithm: qgis:basicstatisticsfornumericfields
6768
name: Test (qgis:basicstatisticsfornumericfields)
6869
params:
@@ -73,3 +74,14 @@ tests:
7374
OUTPUT_HTML_FILE:
7475
name: expected/basic_statistics_numeric_float.html
7576
type: file
77+
78+
- algorithm: qgis:basicstatisticsfortextfields
79+
name: Test (qgis:basicstatisticsfortextfields)
80+
params:
81+
- name: multipolys.gml
82+
type: vector
83+
- 'Bname'
84+
results:
85+
OUTPUT_HTML_FILE:
86+
name: expected/basic_statistics_string.html
87+
type: file
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html><head>
2+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>
3+
<p>Analyzed layer: multipolys.gml</p>
4+
<p>Analyzed field: Bname</p>
5+
<p>Minimum length: 4.0</p>
6+
<p>Maximum length: 4.0</p>
7+
<p>Mean length: 4.0</p>
8+
<p>Filled values: 3</p>
9+
<p>NULL (missed) values: 1</p>
10+
<p>Count: 4</p>
11+
<p>Unique: 2</p>
12+
</body></html>

0 commit comments

Comments
 (0)