Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nodata attribute to Raster #20

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/snaphu/io/_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def create(
driver: str | None = None,
crs: str | Mapping[str, str] | rasterio.crs.CRS | None = None,
transform: rasterio.transform.Affine | None = None,
nodata: float | None = None,
*,
like: Raster | None = None,
**kwargs: Any,
Expand Down Expand Up @@ -135,6 +136,11 @@ def create(
Affine transformation mapping the pixel space to geographic space. If None,
the geotransform of `like` will be used, if available, otherwise the default
transform will be used. Defaults to None.
nodata : float or None, optional
Defines the pixel value to be interpreted as not valid data. If None, the
nodata value of `like` will be used, if available, otherwise no nodata value
will be populated. Only real-valued nodata values are supported, even if the
raster data is complex-valued. Defaults to None.
like : Raster or None, optional
An optional reference raster. If not None, the new raster will be created
with the same metadata (shape, data-type, driver, CRS/geotransform, etc) as
Expand All @@ -158,6 +164,8 @@ def create(
kwargs["crs"] = crs
if transform is not None:
kwargs["transform"] = transform
if nodata is not None:
kwargs["nodata"] = nodata

# Always create a single-band dataset, even if `like` was part of a multi-band
# dataset.
Expand Down Expand Up @@ -233,6 +241,15 @@ def transform(self) -> rasterio.transform.Affine:
"""
return self.dataset.transform

@property
def nodata(self) -> float | None:
"""
float or None : The raster's nodata value (may be unset).

The raster's nodata value, or None if no nodata value was set.
"""
return self.dataset.nodatavals[self.band - 1] # type: ignore[no-any-return]

@property
def closed(self) -> bool:
"""bool : True if the dataset is closed.""" # noqa: D403
Expand Down
24 changes: 24 additions & 0 deletions test/io/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

import snaphu

# Maximum value representable by 32-bit unsigned integer type.
UINT32_MAX = np.iinfo(np.uint32).max


def has_rasterio() -> bool:
"""Check if `rasterio` can be imported."""
Expand All @@ -27,6 +30,7 @@ def make_geotiff_raster(
dtype: DTypeLike = np.int32,
epsg: int = 4326,
extents: tuple[float, float, float, float] = (0.0, 0.0, 1.0, 1.0),
nodata: float | None = None,
) -> Generator[snaphu.io.Raster, None, None]:
"""
Make a dummy GeoTiff raster for testing.
Expand All @@ -47,6 +51,8 @@ def make_geotiff_raster(
The geospatial extents of the raster image, in coordinates defined by the CRS
represented by the `epsg` code, in the following order: West, South, East,
North.
nodata : float or None, optional
Defines the pixel value to be interpreted as not valid data.

Yields
------
Expand All @@ -65,6 +71,7 @@ def make_geotiff_raster(
dtype=dtype,
crs=crs,
transform=transform,
nodata=nodata,
driver="GTiff",
) as raster:
yield raster
Expand Down Expand Up @@ -179,6 +186,23 @@ def test_transform(self, geotiff_raster: snaphu.io.Raster):
transform = rasterio.transform.from_bounds(0.0, 0.0, 1.0, 1.0, width, height)
assert geotiff_raster.transform == transform

@pytest.mark.parametrize(
("dtype", "nodata"),
[
(np.int32, -999.0),
(np.uint32, UINT32_MAX),
(np.float64, 123.456),
(np.complex64, 0.0),
(np.float32, None),
],
)
def test_nodata(self, dtype: DTypeLike, nodata: float | None):
with (
tempfile.NamedTemporaryFile(suffix=".tif") as file_,
make_geotiff_raster(file_.name, dtype=dtype, nodata=nodata) as raster,
):
assert raster.nodata == nodata

def test_open_closed(self):
with make_temp_geotiff_raster() as raster:
assert not raster.closed
Expand Down