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

catch and expend image encoding error #767

Merged
merged 1 commit into from
Nov 18, 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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 7.2.2 (2024-11-17)

* Catch and expand error message when GDAL cannot encode data using specified image driver

# 7.2.1 (2024-11-14)

* add official support for floating point values in ColorMap
Expand Down
38 changes: 22 additions & 16 deletions rio_tiler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

from rio_tiler.colormap import apply_cmap
from rio_tiler.constants import WEB_MERCATOR_CRS, WGS84_CRS
from rio_tiler.errors import RioTilerError
from rio_tiler.errors import InvalidFormat, RioTilerError
from rio_tiler.types import BBox, ColorMapType, IntervalTuple, RIOResampling


Expand Down Expand Up @@ -588,24 +588,30 @@ def render(
}
output_profile.update(creation_options)

with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
category=NotGeoreferencedWarning,
module="rasterio",
)
with MemoryFile() as memfile:
with memfile.open(**output_profile) as dst:
dst.write(data, indexes=list(range(1, count + 1)))
try:
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
category=NotGeoreferencedWarning,
module="rasterio",
)
with MemoryFile() as memfile:
with memfile.open(**output_profile) as dst:
dst.write(data, indexes=list(range(1, count + 1)))

# Use Mask as an alpha band
if mask is not None:
if ColorInterp.alpha not in dst.colorinterp:
dst.colorinterp = *dst.colorinterp[:-1], ColorInterp.alpha

# Use Mask as an alpha band
if mask is not None:
if ColorInterp.alpha not in dst.colorinterp:
dst.colorinterp = *dst.colorinterp[:-1], ColorInterp.alpha
dst.write(mask.astype(data.dtype), indexes=count + 1)

dst.write(mask.astype(data.dtype), indexes=count + 1)
return memfile.read()

return memfile.read()
except Exception as e:
raise InvalidFormat(
f"Could not encode array of shape ({count},{height},{width}) and of datatype `{data.dtype}` using {img_format} driver"
) from e


def mapzen_elevation_rgb(data: numpy.ndarray) -> numpy.ndarray:
Expand Down
12 changes: 11 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
from rasterio.errors import NotGeoreferencedWarning
from rasterio.io import MemoryFile

from rio_tiler.errors import InvalidDatatypeWarning, InvalidPointDataError
from rio_tiler.errors import (
InvalidDatatypeWarning,
InvalidFormat,
InvalidPointDataError,
)
from rio_tiler.models import ImageData, PointData


Expand Down Expand Up @@ -456,3 +460,9 @@ def test_imagedata_coverage():

coverage = im.get_coverage_array(poly, cover_scale=1000)
assert numpy.round(numpy.unique(coverage), decimals=3).tolist() == [0, 0.125, 0.25]


def test_image_encoding_error():
"""Test ImageData error when using bad data array shape."""
with pytest.raises(InvalidFormat):
ImageData(numpy.zeros((5, 256, 256), dtype="uint8")).render(img_format="PNG")
Loading