From 9388c61df43e487056221a39c38a6f5f715ac2ae Mon Sep 17 00:00:00 2001 From: janf Date: Fri, 22 Mar 2024 13:45:13 +0100 Subject: [PATCH 1/2] add optional profile attr to imagedata when unscaling is requested --- rio_tiler/models.py | 5 +++-- rio_tiler/reader.py | 10 +++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/rio_tiler/models.py b/rio_tiler/models.py index dcf38363..e01c5e50 100644 --- a/rio_tiler/models.py +++ b/rio_tiler/models.py @@ -321,6 +321,7 @@ class ImageData: metadata (dict, optional): Additional metadata. Defaults to `{}`. band_names (list, optional): name of each band. Defaults to `["1", "2", "3"]` for 3 bands image. dataset_statistics (list, optional): dataset statistics `[(min, max), (min, max)]` + profile (dict, optional): dataset profile Note: `mask` should be considered as `PER_BAND` so shape should be similar as the data @@ -337,8 +338,8 @@ class ImageData: dataset_statistics: Optional[Sequence[Tuple[float, float]]] = attr.ib( default=None, kw_only=True ) - cutline_mask: Optional[numpy.ndarray] = attr.ib(default=None) - + cutline_mask: Optional[numpy.ndarray] = attr.ib(default=None), + profile: Optional[Dict] = attr.field(factory=dict, kw_only=True) @band_names.default def _default_names(self): return [f"b{ix + 1}" for ix in range(self.count)] diff --git a/rio_tiler/reader.py b/rio_tiler/reader.py index 6371763e..4bc34e26 100644 --- a/rio_tiler/reader.py +++ b/rio_tiler/reader.py @@ -256,11 +256,18 @@ def read( # TODO: DEPRECATED, masked array are already using bool if force_binary_mask: pass - + # profile is added only if unscaling was requested. This would ensure/signalized + # an advanced usage specifically applied to COGS + dataset_profile = None if unscale: data = data.astype("float32", casting="unsafe") numpy.multiply(data, dataset.scales[0], out=data, casting="unsafe") numpy.add(data, dataset.offsets[0], out=data, casting="unsafe") + dataset_profile = dataset.profile + if not 'scales' in dataset_profile: + dataset_profile['scales'] = dataset.scales + if not 'offsets' in dataset_profile: + dataset_profile['offsets'] = dataset.offsets if post_process: data = post_process(data) @@ -276,6 +283,7 @@ def read( band_names=[f"b{idx}" for idx in indexes], dataset_statistics=dataset_statistics, metadata=dataset.tags(), + profile=dataset.profile ) From 4c7e1e87b61f36497f2f1c34ace179992e9f16ca Mon Sep 17 00:00:00 2001 From: janf Date: Fri, 22 Mar 2024 14:27:51 +0100 Subject: [PATCH 2/2] fix dataset_profile --- rio_tiler/reader.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rio_tiler/reader.py b/rio_tiler/reader.py index 4bc34e26..b4536819 100644 --- a/rio_tiler/reader.py +++ b/rio_tiler/reader.py @@ -283,7 +283,7 @@ def read( band_names=[f"b{idx}" for idx in indexes], dataset_statistics=dataset_statistics, metadata=dataset.tags(), - profile=dataset.profile + profile=dataset_profile )