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 overriding scale and offset values #177

Merged
merged 2 commits into from
Aug 15, 2024
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
9 changes: 9 additions & 0 deletions trollimage/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2676,6 +2676,15 @@ def test_save_scale_offset_custom_labels(self):
expected_tags,
scale_offset_tags=("gradient", "axis_intercept"))

@pytest.mark.skipif(sys.platform.startswith('win'), reason="'NamedTemporaryFile' not supported on Windows")
def test_save_scale_offset_custom_values(self):
"""Test saving GeoTIFF overriding the scale/offset values."""
expected_tags = {"gradient": 1, "axis_intercept": 0}
self.img.stretch()
self._save_and_check_tags(
expected_tags,
scale_offset_tags={"gradient": 1, "axis_intercept": 0})


def _get_tags_after_writing_to_geotiff(data):
import rasterio as rio
Expand Down
12 changes: 9 additions & 3 deletions trollimage/xrimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def rio_save(self, filename, fformat=None, fill_value=None,
provided. Common values include `nearest` (default),
`bilinear`, `average`, and many others. See the rasterio
documentation for more information.
scale_offset_tags (Tuple[str, str] or None):
scale_offset_tags (Tuple[str, str] or Dict[str, number] or None):
If set to a ``(str, str)`` tuple, scale and offset will be
stored in GDALMetaData tags. Those can then be used to
retrieve the original data values from pixel values.
Expand All @@ -314,7 +314,9 @@ def rio_save(self, filename, fformat=None, fill_value=None,
represented by a simple scale and offset. Scale and offset
are also saved as (NaN, NaN) for multi-band images (ex. RGB)
as storing multiple values in a single GDALMetaData tag is not
currently supported.
currently supported. If set to a dictionary, automatically
determined scale/offset values are overruled by the values
provided in the keys.
colormap_tag (str or None):
If set and the image was colorized or palettized, a tag will
be added with this name with the value of a comma-separated
Expand Down Expand Up @@ -471,7 +473,11 @@ def pil_save(self, filename, fformat=None, fill_value=None,

def _add_scale_offset_to_tags(self, scale_offset_tags, data_arr, tags):
scale_label, offset_label = scale_offset_tags
scale, offset = self.get_scaling_from_history(data_arr.attrs.get('enhancement_history', []))
try:
scale = scale_offset_tags[scale_label]
offset = scale_offset_tags[offset_label]
except TypeError:
scale, offset = self.get_scaling_from_history(data_arr.attrs.get('enhancement_history', []))
tags[scale_label], tags[offset_label] = invert_scale_offset(scale, offset)

def get_scaling_from_history(self, history=None):
Expand Down
Loading