Commit 876e03ba authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Merge branch 'enhancement/validate_nodataVal' into 'master'

Resolve #32 validation of nodata value.

Closes #32

See merge request !26
parents 81f049fc 50b03960
Pipeline #22767 passed with stages
in 19 minutes and 19 seconds
......@@ -2,6 +2,11 @@
0.11.1 (2021-05-07)
* Added a validation that checks if the given nodata value is within the valid value range of the array data type.
0.11.0 (2021-04-22)
......@@ -141,6 +141,18 @@ class GeoArray(object):
if self.filePath:
if 'nodata' in self._initParams and self._initParams['nodata'] is not None:
def _validate_nodataVal(self):
"""Check if a given nodata value is within the valid value range of the data type."""
if (np.issubdtype(self.dtype, np.integer) and
not np.can_cast(self._initParams['nodata'], self.dtype)) or \
(np.issubdtype(self.dtype, np.floating) and
not np.can_cast(float(self._initParams['nodata']), self.dtype)):
raise ValueError("The given no-data value (%s) is out range for data type %s."
% (self._initParams['nodata'], self.dtype))
def arr(self):
return self._arr
......@@ -182,7 +194,7 @@ class GeoArray(object):
bN_dict = OrderedDict((band, i) for i, band in enumerate(list_bandnames))
if len(bN_dict) != self.bands:
raise ValueError('Bands must different names. Received band list: %s' % list_bandnames)
raise ValueError('Bands must have unique names. Received band list: %s' % list_bandnames)
self._bandnames = bN_dict
......@@ -137,6 +137,15 @@ class Test_GeoArray(TestCase):
gA = GeoArray(np.array([1, 2]), nodata=-9999)
self.assertEqual(gA.nodata, -9999.0)
def test_nodata_out_of_range(self):
"""Test if given nodata value is valid with respect to the array data type."""
# test nodata value in range
GeoArray(np.array([1, 2]).astype(np.uint8), nodata=255)
# test nodata value out of range in range
with self.assertRaises(ValueError):
GeoArray(np.array([1, 2]).astype(np.uint8), nodata=256)
def test_calc_mask_nodata(self, _, gA):
def test_with_flag_all(_gA):
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment