Skip to content

Commit

Permalink
Fix for issue #281 COG validation fails for files that are SPARSE and…
Browse files Browse the repository at this point in the history
… have sparse edge tiles in the overviews
  • Loading branch information
Marta Padilla Ruiz committed Feb 2, 2024
1 parent 8d7969c commit 463025c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
47 changes: 43 additions & 4 deletions rio_cogeo/cogeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,17 +564,56 @@ def cog_validate( # noqa: C901
)
)

block_offset = src.get_tag_item("BLOCK_OFFSET_0_0", "TIFF", bidx=1)
# Get blocks size
block_size = src.block_shapes[0]

# Extract number of blocks per row and column
blocks_per_row = src.width // block_size[1]
blocks_per_column = src.height // block_size[0]

# Initialize loop variables
y = 0
block_offset = None

# Find the first block with a valid block_offset
while y < blocks_per_column and block_offset is None:
x = 0
while x < blocks_per_row and block_offset is None:
block_offset = src.get_tag_item(
"BLOCK_OFFSET_%d_%d" % (x, y), "TIFF", bidx=1
)
x += 1
y += 1

data_offset = int(block_offset) if block_offset else 0
data_offsets = [data_offset]
details["data_offsets"] = {}
details["data_offsets"]["main"] = data_offset

for ix, _dec in enumerate(overviews):
block_offset = src.get_tag_item(
"BLOCK_OFFSET_0_0", "TIFF", bidx=1, ovr=ix
)

# Get the width and height of the overview
overview_width = src.width // (_dec)
overview_height = src.height // (_dec)

# Extract number of blocks per row and column
blocks_per_row = overview_width // block_size[1]
blocks_per_column = overview_height // block_size[0]

# Initialize loop variables
y = 0
block_offset = None

# Find the first block with a valid block_offset
while y < blocks_per_column and block_offset is None:
x = 0
while x < blocks_per_row and block_offset is None:
block_offset = src.get_tag_item(
"BLOCK_OFFSET_%d_%d" % (x, y), "TIFF", bidx=1, ovr=ix
)
x += 1
y += 1

data_offset = int(block_offset) if block_offset else 0
data_offsets.append(data_offset)
details["data_offsets"]["overview_{}".format(ix)] = data_offset
Expand Down
Binary file added tests/fixtures/validate/sparse.tif
Binary file not shown.
4 changes: 4 additions & 0 deletions tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
raster_jpeg = os.path.join(fixture_dir, "validate", "nontiff.jpg")
raster_big = os.path.join(fixture_dir, "image_2000px.tif")
raster_zero_offset = os.path.join(fixture_dir, "validate", "cog_no_offest.tif")
raster_sparse = os.path.join(fixture_dir, "validate", "sparse.tif")

# COG created with rio-cogeo but using gdal 3.1
raster_rioCOGgdal31 = os.path.join(fixture_dir, "validate", "image_rioCOG_gdal3.1.tif")
Expand Down Expand Up @@ -71,6 +72,9 @@ def test_cog_validate_valid(monkeypatch):
with pytest.warns(NotGeoreferencedWarning):
assert cog_validate(raster_zero_offset, config=config)

# Sparse COG with some overviews that contain zeros
assert cog_validate(raster_sparse, config=config)[0]


def test_cog_validate_return():
"""Checkout returned values."""
Expand Down

0 comments on commit 463025c

Please # to comment.