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

allow the passing of options to RasterBand #36

Merged
merged 7 commits into from
Sep 27, 2023
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
12 changes: 10 additions & 2 deletions src/tophu/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import textwrap
from dataclasses import dataclass
from pathlib import Path
from typing import Protocol, overload, runtime_checkable
from typing import Any, Protocol, overload, runtime_checkable

import h5py
import numpy as np
Expand Down Expand Up @@ -480,6 +480,7 @@ def __init__(
driver: str | None = None,
crs: str | dict | rasterio.crs.CRS | None = None,
transform: rasterio.transform.Affine | None = None,
**options: dict[str, Any],
gmgunter marked this conversation as resolved.
Show resolved Hide resolved
): # noqa: D418
"""
Construct a new `RasterBand` object.
Expand All @@ -501,6 +502,8 @@ def __init__(
transform : Affine instance, optional
Affine transformation mapping the pixel space to geographic space.
Defaults to None.
**options : dict, optional
Additional driver-specific creation options passed to `rasterio.open`.
"""
...

Expand All @@ -515,13 +518,16 @@ def __init__(
driver=None,
crs=None,
transform=None,
**options,
): # noqa: D107
filepath = Path(filepath)

# If any of `width`, `height`, or `dtype` are provided, all three must be
# provided. If any were not provided, the dataset must already exist. Otherwise,
# create the dataset if it did not exist.
if (width is None) and (height is None) and (dtype is None):
# In addition, `options` may only be provided when creating a new dataset. It
# must be an empty dict when opening an existing dataset.
if (width is None) and (height is None) and (dtype is None) and (not options):
mode = "r"
count = None
elif (width is not None) and (height is not None) and (dtype is not None):
Expand Down Expand Up @@ -549,6 +555,7 @@ def __init__(
driver: str | None = None,
crs: str | dict | rasterio.crs.CRS | None = None,
transform: rasterio.transform.Affine | None = None,
**options: dict[str, Any],
)
""").strip()
raise TypeError(errmsg)
Expand All @@ -564,6 +571,7 @@ def __init__(
crs=crs,
transform=transform,
dtype=dtype,
**options,
) as dataset:
# Band index must not be None if the dataset contains more than one band.
# If a band index was supplied, check that it's within the range of valid
Expand Down
2 changes: 1 addition & 1 deletion src/tophu/_multiscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def lowpass_filter_and_multilook(
def get_filter_coeffs(n: int) -> NDArray:
# If `n` is 1, then the subsequent multilooking step will be a no-op, so there's
# no need to apply a low-pass filter. In that case, we choose the filter
# coefficents to have a frequency response of unity.
# coefficients to have a frequency response of unity.
if n == 1:
return 1.0

Expand Down
46 changes: 46 additions & 0 deletions test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tempfile
from collections.abc import Callable, Iterator
from pathlib import Path
from typing import Any

import h5py
import numpy as np
Expand Down Expand Up @@ -42,6 +43,7 @@ def create_raster_dataset(
shape: tuple[int, int],
count: int,
dtype: DTypeLike,
**options: dict[str, Any],
) -> None:
"""Create a new raster dataset."""
with rasterio.open(
Expand All @@ -52,6 +54,7 @@ def create_raster_dataset(
width=shape[1],
count=count,
dtype=dtype,
**options,
):
pass

Expand Down Expand Up @@ -418,3 +421,46 @@ def test_bad_init_overload(self):
with pytest.raises(TypeError, match=errmsg):
# Required parameter `dtype` is missing.
tophu.RasterBand(filepath="asdf.tif", width=128, height=128)

def test_creation_options(self):
shape = (1024, 512)
dtype = np.float32
block_shape = (128, 128)
gtiff_options = {
"compress": "deflate",
"tiled": "yes",
"blockysize": str(block_shape[0]),
"blockxsize": str(block_shape[1]),
}
with tempfile.NamedTemporaryFile() as f:
filepath = f.name
driver = "GTiff"
create_raster_dataset(
filepath=filepath,
driver=driver,
shape=shape,
count=1,
dtype=dtype,
**gtiff_options,
)

with rasterio.open(filepath) as src:
assert src.block_shapes == [block_shape]
assert src.compression.name == gtiff_options["compress"]

envi_options = {"suffix": "add"}
suffix = ".bin"
with tempfile.NamedTemporaryFile(suffix=suffix) as f:
filepath = f.name
driver = "ENVI"
create_raster_dataset(
filepath=filepath,
driver=driver,
shape=shape,
count=1,
dtype=dtype,
**envi_options,
)
# Check that we created a header file with .hdr appended,
# not replacing the original suffix
assert Path(filepath).with_suffix(suffix + ".hdr").exists()