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

Fix for issue #281 COG validation fails for sparse edge tiles #282

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
Loading