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

Feat/set profile in imagedata #688

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 3 additions & 2 deletions rio_tiler/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)]
Expand Down
10 changes: 9 additions & 1 deletion rio_tiler/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if we want the scales and offsets info even if we don't apply them?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alos I think if we add profile we should always populate the field, not just when we unscale=True

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, i made it conditional on unscale request as to avoid issues with STAC and Mosaic readers.
I believe the profile probably is always available but i did this per your suggestion.

It would be easy to remove the condition and update the profile with "scales" and "offsets"


if post_process:
data = post_process(data)
Expand All @@ -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
)


Expand Down
Loading