Skip to content

Rasterio Library

siapy.entities.images.rasterio_lib

RasterioLibImage dataclass

RasterioLibImage(file: XarrayType)

Bases: ImageBase

Source code in siapy/entities/images/rasterio_lib.py
23
24
def __init__(self, file: "XarrayType"):
    self._file = file

file property

file: XarrayType

filepath property

filepath: Path

metadata property

metadata: dict[str, Any]

shape property

shape: tuple[int, int, int]

bands property

bands: int

default_bands property

default_bands: list[int]

wavelengths property

wavelengths: list[float]

camera_id property

camera_id: str

open classmethod

open(filepath: str | Path) -> RasterioLibImage
Source code in siapy/entities/images/rasterio_lib.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@classmethod
def open(cls, filepath: str | Path) -> "RasterioLibImage":
    filepath = Path(filepath)
    if not filepath.exists():
        raise InvalidFilepathError(filepath)

    try:
        raster = rioxarray.open_rasterio(filepath)
    except Exception as e:
        raise InvalidInputError({"filepath": str(filepath)}, f"Failed to open raster file: {e}") from e

    if isinstance(raster, list):
        raise InvalidInputError({"file_type": type(raster).__name__}, "Expected DataArray, got Dataset")

    return cls(raster)

to_display

to_display(equalize: bool = True) -> Image
Source code in siapy/entities/images/rasterio_lib.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def to_display(self, equalize: bool = True) -> Image.Image:
    selected_bands = [i + 1 for i in self.default_bands]  # Adjust for 1-indexed bands
    bands_data = self.file.sel(band=selected_bands)
    image_3ch = bands_data.transpose("y", "x", "band").values
    image_3ch_clean = np.nan_to_num(np.asarray(image_3ch))
    min_val = np.nanmin(image_3ch_clean)
    max_val = np.nanmax(image_3ch_clean)

    image_scaled = ((image_3ch_clean - min_val) * (255.0 / (max_val - min_val))).astype(np.uint8)

    image = Image.fromarray(image_scaled)
    if equalize:
        image = ImageOps.equalize(image)
    return image

to_numpy

to_numpy(nan_value: float | None = None) -> ndarray
Source code in siapy/entities/images/rasterio_lib.py
102
103
104
105
106
def to_numpy(self, nan_value: float | None = None) -> np.ndarray:
    image = np.moveaxis(np.asarray(self.file.values), 0, -1)
    if nan_value is not None:
        image = np.nan_to_num(image, nan=nan_value)
    return image