from __future__ import annotations
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any, Literal
import numpy as np
import numpy.typing as npt
from .batch_table import BatchTable
from .feature_table import FeatureTable
[docs]
class TileContent(ABC):
header: TileContentHeader
body: TileContentBody
[docs]
def to_array(self) -> npt.NDArray[np.uint8]:
self.sync()
header_arr = self.header.to_array()
body_arr = self.body.to_array()
return np.concatenate((header_arr, body_arr))
[docs]
def to_hex_str(self) -> str:
arr = self.to_array()
return " ".join(f"{x:02X}" for x in arr)
[docs]
def save_as(self, path: Path) -> None:
tile_arr = self.to_array()
with path.open("bw") as f:
f.write(bytes(tile_arr))
def __str__(self) -> str:
return (
"------ Tile header ------\n"
+ (str(self.header) if self.header is not None else "")
+ "\n"
+ "------ Tile body ------\n"
+ (str(self.body) if self.body is not None else "")
)
[docs]
@abstractmethod
def sync(self) -> None:
"""
Allow to synchronize headers with contents.
"""
[docs]
@staticmethod
@abstractmethod
def from_array(array: npt.NDArray[np.uint8]) -> TileContent:
...
[docs]
class TileContentBody(ABC):
batch_table: BatchTable
feature_table: FeatureTable[Any, Any]
@abstractmethod
def __str__(self) -> str:
...
[docs]
@abstractmethod
def to_array(self) -> npt.NDArray[np.uint8]:
...