Commit 2e45c6c1 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Added a validation that checks if the given nodata value is within the valid...


Added a validation that checks if the given nodata value is within the valid value range of the array data type.
Signed-off-by: Daniel Scheffler's avatarDaniel Scheffler <danschef@gfz-potsdam.de>
parent 81f049fc
Pipeline #22764 failed with stage
in 1 minute and 52 seconds
......@@ -141,6 +141,18 @@ class GeoArray(object):
if self.filePath:
self.set_gdalDataset_meta()
if 'nodata' in self._initParams and self._initParams['nodata'] is not None:
self._validate_nodataVal()
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))
@property
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
gA = GeoArray(np.array([1, 2]).astype(np.uint8), nodata=255)
# test nodata value out of range in range
with self.assertRaises(ValueError):
gA = GeoArray(np.array([1, 2]).astype(np.uint8), nodata=256)
@parameterized.expand(_get_gA_inMem_notInMem())
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