Skip to content

Commit

Permalink
Add an OverviewCreationError
Browse files Browse the repository at this point in the history
Resolves #1333
  • Loading branch information
Sean C. Gillies committed Sep 24, 2018
1 parent 6af040e commit 5d274e1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Changes

Bug fixes:

- If the build_overviews method of a dataset is passed a list of factors that
specify more than one 1x1 pixel overview (#1333), Rasterio raises an
exception.
- Calling calculate_default_transform for large extents should no longer result
in the out of memory error reported in #1131. The rio-warp command should
also now run more quickly and with a smaller memory footprint.
Expand Down
8 changes: 7 additions & 1 deletion rasterio/_io.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ from __future__ import absolute_import
include "directives.pxi"
include "gdal.pxi"

from collections import Counter
import logging
import sys
import uuid
Expand All @@ -24,7 +25,7 @@ from rasterio.enums import ColorInterp, MaskFlags, Resampling
from rasterio.errors import (
CRSError, DriverRegistrationError, RasterioIOError,
NotGeoreferencedWarning, NodataShadowWarning, WindowError,
UnsupportedOperation
UnsupportedOperation, OverviewCreationError
)
from rasterio.sample import sample_gen
from rasterio.transform import Affine
Expand Down Expand Up @@ -1549,6 +1550,11 @@ cdef class DatasetWriterBase(DatasetReaderBase):
['Resampling.{0}'.format(Resampling(k).name) for k in
resampling_map.keys()])))

# Check factors
ovr_shapes = Counter([(int((self.height + f - 1) / f), int((self.width + f - 1) / f)) for f in factors])
if ovr_shapes[(1, 1)] > 1:
raise OverviewCreationError("Too many overviews levels of 1x1 dimension were requested")

# Allocate arrays.
if factors:
factors_c = <int *>CPLMalloc(len(factors)*sizeof(int))
Expand Down
4 changes: 4 additions & 0 deletions rasterio/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ class WarpOptionsError(RasterioError):

class UnsupportedOperation(RasterioError):
"""Raised when reading from a file opened in 'w' mode"""


class OverviewCreationError(RasterioError):
"""Raised when creation of an overview fails"""
11 changes: 11 additions & 0 deletions tests/test_overviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import rasterio
from rasterio.enums import Resampling
from rasterio.env import GDALVersion
from rasterio.errors import OverviewCreationError


gdal_version = GDALVersion()

Expand Down Expand Up @@ -79,3 +81,12 @@ def test_test_unsupported_algo(data):
with rasterio.open(inputfile, 'r+') as src:
overview_factors = [2, 4]
src.build_overviews(overview_factors, resampling=Resampling.q1)


def test_issue1333(data):
"""Fail if asked to create more than one 1x1 overview"""
inputfile = str(data.join('RGB.byte.tif'))
with pytest.raises(OverviewCreationError):
with rasterio.open(inputfile, 'r+') as src:
overview_factors = [1024, 2048]
src.build_overviews(overview_factors, resampling=Resampling.average)

0 comments on commit 5d274e1

Please sign in to comment.