Skip to content

Commit

Permalink
implement is_tiled as a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
jkittner committed Oct 23, 2024
1 parent 4b2ad16 commit 31e2b5e
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions terracotta/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import rasterio
from rasterio.env import GDALVersion
from rasterio._base import DatasetBase

ValidationInfo = Tuple[List[str], List[str], Dict[str, Any]]

Expand All @@ -22,6 +23,21 @@ def validate(src_path: str, strict: bool = True) -> bool:
return not errors


def is_tiled(src: DatasetBase) -> bool:
"""
Check if a rasterio dataset is tiled.
Implementation copied from
https://github.com/rasterio/rasterio/blob/74ccaf126d08fc6eca3eacd7cb20ac8bb155ee3b/rasterio/_base.pyx#L1006-L1017
Since this was deprecated in rasterio
:param src: rasterio dataset
"""
# It's rare but possible that a dataset's bands have different block structure.
# Therefore we check them all against the width of the dataset.
return src.block_shapes and all(src.width != w for _, w in src.block_shapes)


def check_raster_file(src_path: str) -> ValidationInfo: # pragma: no cover
"""
Implementation from
Expand Down Expand Up @@ -51,10 +67,7 @@ def check_raster_file(src_path: str) -> ValidationInfo: # pragma: no cover

overviews = src.overviews(1)
if src.width > 512 and src.height > 512:
if not (
src.block_shapes
and all(src.width != w for _, w in src.block_shapes)
):
if not is_tiled(src):
errors.append(
"The file is greater than 512xH or 512xW, but is not tiled"
)
Expand Down Expand Up @@ -169,10 +182,7 @@ def check_raster_file(src_path: str) -> ValidationInfo: # pragma: no cover
for ix, dec in enumerate(overviews):
with rasterio.open(src_path, OVERVIEW_LEVEL=ix) as ovr_dst:
if ovr_dst.width > 512 and ovr_dst.height > 512:
if not (
ovr_dst.block_shapes
and all(ovr_dst.width != w for _, w in ovr_dst.block_shapes)
):
if not is_tiled(ovr_dst):
errors.append("Overview of index {} is not tiled".format(ix))

return errors, warnings, details

0 comments on commit 31e2b5e

Please sign in to comment.