Skip to content

Commit f9e0093

Browse files
committedFeb 23, 2014
[fTools] Spatial join aggregates: handle NULL values
1 parent 30fff85 commit f9e0093

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed
 

‎python/plugins/fTools/tools/doSpatialJoin.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def myself(L):
5151
medianVal = L[ (nVal + 1) / 2 - 1]
5252
return medianVal
5353

54+
def filter_null(vals):
55+
"""Takes an iterator of values and returns a new iterator returning the same values but skipping any NULL values"""
56+
return (v for v in vals if v != NULL)
57+
5458
class Dialog(QDialog, Ui_Dialog):
5559

5660
def __init__(self, iface):
@@ -216,11 +220,27 @@ def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar
216220
atMap = atMap1
217221
for j in numFields.keys():
218222
for k in sumList:
219-
if k == "SUM": atMap.append(sum(numFields[j]))
220-
elif k == "MEAN": atMap.append(sum(numFields[j]) / count)
221-
elif k == "MIN": atMap.append(min(numFields[j]))
222-
elif k == "MED": atMap.append(myself(numFields[j]))
223-
else: atMap.append(max(numFields[j]))
223+
if k == "SUM":
224+
atMap.append(sum(filter_null(numFields[j])))
225+
elif k == "MEAN":
226+
try:
227+
nn_count = sum( 1 for _ in filter_null(numFields[j]) )
228+
atMap.append(sum(filter_null(numFields[j])) / nn_count)
229+
except ZeroDivisionError:
230+
atMap.append(NULL)
231+
elif k == "MIN":
232+
try:
233+
atMap.append(min(filter_null(numFields[j])))
234+
except ValueError:
235+
atMap.append(NULL)
236+
elif k == "MED":
237+
atMap.append(myself(numFields[j]))
238+
else:
239+
try:
240+
atMap.append(max(filter_null(numFields[j])))
241+
except ValueError:
242+
atMap.append(NULL)
243+
224244
numFields[j] = []
225245
atMap.append(count)
226246
atMap = dict(zip(seq, atMap))

0 commit comments

Comments
 (0)
Please sign in to comment.