Skip to content

Commit a61e9b6

Browse files
committed
Fixed valuecount for cases where all pixels are nodata.
1 parent e8bbb01 commit a61e9b6

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

raster/valuecount.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,14 @@ def statistics(self, reset=False):
170170
for data in self.tiles():
171171
self._push_stats(data)
172172

173-
# Compute mean and std from totals sums
174-
mean = self._stats_t1 / self._stats_t0
175-
std = numpy.sqrt(self._stats_t0 * self._stats_t2 - self._stats_t1 * self._stats_t1) / self._stats_t0
173+
if self._stats_t0 == 0:
174+
# If totals sum is zero, no data was available to comput statistics
175+
mean = None
176+
std = None
177+
else:
178+
# Compute mean and std from totals sums.
179+
mean = self._stats_t1 / self._stats_t0
180+
std = numpy.sqrt(self._stats_t0 * self._stats_t2 - self._stats_t1 * self._stats_t1) / self._stats_t0
176181

177182
return (self._stats_min_value, self._stats_max_value, mean, std)
178183

tests/test_valuecount.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,17 @@ def test_valuecount_exception(self):
249249

250250
def test_valuecount_pixelsize(self):
251251
self.assertAlmostEqual(self.rasterlayer.pixelsize()[0], tile_scale(11))
252+
253+
def test_full_mask_data(self):
254+
# Override all tiles to be fully masked.
255+
for tile in self.rasterlayer.rastertile_set.all():
256+
tile.rast.bands[0].data([0], shape=(1, 1))
257+
tile.rast.bands[0].nodata_value = 0
258+
tile.save()
259+
260+
# Use a legend with simple int expression
261+
agg = Aggregator(
262+
layer_dict={'a': self.rasterlayer.id},
263+
formula='a',
264+
)
265+
self.assertEqual((None, None, None, None), agg.statistics())

0 commit comments

Comments
 (0)