Skip to content

Commit

Permalink
Merge pull request natcap#1530 from davemfish/bugfix/CV-1528-nan-nodata
Browse files Browse the repository at this point in the history
Bugfix CV: mask out nan nodata
  • Loading branch information
phargogh authored Feb 29, 2024
2 parents 9a70648 + beca6d7 commit 330b9c7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ Unreleased Changes
* Annual Water Yield
* Added the results_suffix to a few intermediate files where it was
missing. https://github.com/natcap/invest/issues/1517
* Coastal Vulnerability
* Fixed a bug in handling ``nan`` as the nodata value of the bathymetry
raster. ``nan`` pixels will now be propertly ignored before calculating
mean depths along fetch rays.
https://github.com/natcap/invest/issues/1528
* Urban Nature Access
* Fixed a ``NameError`` that occurred when running the model using
search radii defined per population group with an exponential search
Expand Down
5 changes: 3 additions & 2 deletions src/natcap/invest/coastal_vulnerability.py
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,8 @@ def extract_bathymetry_along_ray(
raise ValueError(
f'got a {value} when trying to read bathymetry at {location}. '
'Does the bathymetry input fully cover the fetch ray area?')
if bathy_nodata is None or not math.isclose(value[0][0], bathy_nodata):
if bathy_nodata is None or not numpy.isclose(
value[0][0], bathy_nodata, equal_nan=True):
bathy_values.append(value)

# Gaps between shoreline and bathymetry input datasets could result in no
Expand Down Expand Up @@ -3160,7 +3161,7 @@ def _aggregate_raster_values_in_radius(
max_distance=pixel_dist,
normalize=False)
radial_kernel_mask = pygeoprocessing.raster_to_numpy_array(
kernel_path).astype(bool)
kernel_path).astype(bool)
shutil.rmtree(temp_dir, ignore_errors=True)

result = {}
Expand Down
32 changes: 17 additions & 15 deletions tests/test_coastal_vulnerability.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,27 +224,29 @@ def test_extract_bathymetry(self):
projection_wkt = srs.ExportToWkt()
geotransform = [0, 1.0, 0.0, 0, 0.0, -1.0]
n = 5
nodata_val = 9999
band1_nodata = numpy.nan
gtiff_driver = gdal.GetDriverByName('GTiff')
new_raster = gtiff_driver.Create(
raster_path, n, n, 2, gdal.GDT_Int32, options=[
raster_path, n, n, 2, gdal.GDT_Float32, options=[
'TILED=YES', 'BIGTIFF=YES', 'COMPRESS=LZW',
'BLOCKXSIZE=16', 'BLOCKYSIZE=16'])
new_raster.SetProjection(projection_wkt)
new_raster.SetGeoTransform(geotransform)
array = numpy.array([-1]*n*n).reshape((n, n))
valid_value = -10.0
array = numpy.array([valid_value]*n*n).reshape((n, n))

# nodata across the top row for Band 1
new_band = new_raster.GetRasterBand(1)
array[:1] = nodata_val
array[:1] = band1_nodata
new_band.WriteArray(array)
new_band.SetNoDataValue(nodata_val)
new_band.SetNoDataValue(band1_nodata)

# all nodata for Band 2
band2_nodata = 999.99
nodata_band = new_raster.GetRasterBand(2)
array[:] = nodata_val
array[:] = band2_nodata
nodata_band.WriteArray(array)
nodata_band.SetNoDataValue(nodata_val)
nodata_band.SetNoDataValue(band2_nodata)

new_raster.FlushCache()
new_band = None
Expand Down Expand Up @@ -276,25 +278,25 @@ def test_extract_bathymetry(self):
band = raster.GetRasterBand(1) # nodata across top row

values = coastal_vulnerability.extract_bathymetry_along_ray(
all_valid_ray, geotransform, nodata_val, band)
self.assertTrue(numpy.mean(values) == -1)
all_valid_ray, geotransform, band1_nodata, band)
self.assertEqual(numpy.mean(values), valid_value)

values = coastal_vulnerability.extract_bathymetry_along_ray(
some_nodata_ray, geotransform, nodata_val, band)
self.assertTrue(numpy.mean(values) == -1)
some_nodata_ray, geotransform, band1_nodata, band)
self.assertEqual(numpy.mean(values), valid_value)

values = coastal_vulnerability.extract_bathymetry_along_ray(
all_nodata_ray, geotransform, nodata_val, band)
self.assertTrue(numpy.mean(values) == -1)
all_nodata_ray, geotransform, band1_nodata, band)
self.assertEqual(numpy.mean(values), valid_value)

with self.assertRaises(ValueError):
values = coastal_vulnerability.extract_bathymetry_along_ray(
out_of_bounds_ray, geotransform, nodata_val, band)
out_of_bounds_ray, geotransform, band1_nodata, band)

nodata_band = raster.GetRasterBand(2) # all nodata band
with self.assertRaises(ValueError):
values = coastal_vulnerability.extract_bathymetry_along_ray(
all_valid_ray, geotransform, nodata_val, nodata_band)
all_valid_ray, geotransform, band2_nodata, nodata_band)

raster = None
band = None
Expand Down

0 comments on commit 330b9c7

Please sign in to comment.