|
8 | 8 | handle_nodata_as_nan,
|
9 | 9 | nan_to_nodata,
|
10 | 10 | nodata_to_nan,
|
| 11 | + replace_with_nodata, |
11 | 12 | set_raster_nodata,
|
12 | 13 | unify_raster_nodata,
|
13 | 14 | )
|
@@ -38,6 +39,76 @@ def test_convert_raster_nodata():
|
38 | 39 | assert out_meta["nodata"] == -999
|
39 | 40 |
|
40 | 41 |
|
| 42 | +def test_replace_with_nodata(): |
| 43 | + """Test that replacing raster pixel values with nodata works as expected.""" |
| 44 | + target_value = 2.705 |
| 45 | + nodata_value = -999 |
| 46 | + |
| 47 | + with rasterio.open(SMALL_RASTER_PATH) as raster: |
| 48 | + raster_data = raster.read() |
| 49 | + nr_of_pixels = np.count_nonzero(raster_data == target_value) |
| 50 | + assert nr_of_pixels > 0 |
| 51 | + assert np.count_nonzero(raster_data == nodata_value) == 0 |
| 52 | + |
| 53 | + replace_condition = "equal" |
| 54 | + out_image, out_meta = replace_with_nodata(raster, target_value, nodata_value, replace_condition) |
| 55 | + assert np.count_nonzero(out_image == nodata_value) > 0 |
| 56 | + assert np.count_nonzero(out_image == target_value) == 0 |
| 57 | + assert out_meta["nodata"] == -999 |
| 58 | + |
| 59 | + with rasterio.open(SMALL_RASTER_PATH) as raster: |
| 60 | + raster_data = raster.read() |
| 61 | + # Ensure some pixels exist that are less than the target value |
| 62 | + nr_of_pixels_less_than_target = np.count_nonzero(raster_data < target_value) |
| 63 | + assert nr_of_pixels_less_than_target > 0 |
| 64 | + |
| 65 | + replace_condition = "less_than" |
| 66 | + out_image, out_meta = replace_with_nodata(raster, target_value, nodata_value, replace_condition) |
| 67 | + |
| 68 | + assert np.count_nonzero(out_image == nodata_value) == nr_of_pixels_less_than_target |
| 69 | + assert np.count_nonzero((out_image < target_value) & (out_image != nodata_value)) == 0 |
| 70 | + assert out_meta["nodata"] == -999 |
| 71 | + |
| 72 | + with rasterio.open(SMALL_RASTER_PATH) as raster: |
| 73 | + raster_data = raster.read() |
| 74 | + # Ensure some pixels exist that are greater than the target value |
| 75 | + nr_of_pixels_greater_than_target = np.count_nonzero(raster_data > target_value) |
| 76 | + assert nr_of_pixels_greater_than_target > 0 |
| 77 | + |
| 78 | + replace_condition = "greater_than" |
| 79 | + out_image, out_meta = replace_with_nodata(raster, target_value, nodata_value, replace_condition) |
| 80 | + |
| 81 | + assert np.count_nonzero(out_image == nodata_value) == nr_of_pixels_greater_than_target |
| 82 | + assert np.count_nonzero(out_image > target_value) == 0 |
| 83 | + assert out_meta["nodata"] == -999 |
| 84 | + |
| 85 | + with rasterio.open(SMALL_RASTER_PATH) as raster: |
| 86 | + raster_data = raster.read() |
| 87 | + # Ensure some pixels exist that are less than or equal to the target value |
| 88 | + nr_of_pixels_less_than_or_equal = np.count_nonzero(raster_data <= target_value) |
| 89 | + assert nr_of_pixels_less_than_or_equal > 0 |
| 90 | + |
| 91 | + replace_condition = "less_than_or_equal" |
| 92 | + out_image, out_meta = replace_with_nodata(raster, target_value, nodata_value, replace_condition) |
| 93 | + |
| 94 | + assert np.count_nonzero(out_image == nodata_value) == nr_of_pixels_less_than_or_equal |
| 95 | + assert np.count_nonzero((out_image <= target_value) & (out_image != nodata_value)) == 0 |
| 96 | + assert out_meta["nodata"] == -999 |
| 97 | + |
| 98 | + with rasterio.open(SMALL_RASTER_PATH) as raster: |
| 99 | + raster_data = raster.read() |
| 100 | + # Ensure some pixels exist that are greater than or equal to the target value |
| 101 | + nr_of_pixels_greater_than_or_equal = np.count_nonzero(raster_data >= target_value) |
| 102 | + assert nr_of_pixels_greater_than_or_equal > 0 |
| 103 | + |
| 104 | + replace_condition = "greater_than_or_equal" |
| 105 | + out_image, out_meta = replace_with_nodata(raster, target_value, nodata_value, replace_condition) |
| 106 | + |
| 107 | + assert np.count_nonzero(out_image == nodata_value) == nr_of_pixels_greater_than_or_equal |
| 108 | + assert np.count_nonzero(out_image >= target_value) == 0 |
| 109 | + assert out_meta["nodata"] == -999 |
| 110 | + |
| 111 | + |
41 | 112 | def test_unify_raster_nodata():
|
42 | 113 | """Test that unifying raster nodata for multiple rasters works as expected."""
|
43 | 114 | with rasterio.open(SMALL_RASTER_PATH) as raster:
|
|
0 commit comments