Skip to content

Commit

Permalink
add tests to cover nan nodata in bathymetry. natcap#1528
Browse files Browse the repository at this point in the history
  • Loading branch information
davemfish committed Feb 20, 2024
1 parent 886bb2c commit 69fabb1
Showing 1 changed file with 17 additions and 15 deletions.
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 69fabb1

Please sign in to comment.