From 3304f8be45c377f912c8beb19d02dc83aee12d86 Mon Sep 17 00:00:00 2001 From: Sean Gillies Date: Thu, 5 Nov 2015 16:59:37 -0700 Subject: [PATCH] Closes #520 --- rasterio/_io.pyx | 8 ++++++++ tests/test_blocks.py | 4 ++-- tests/test_profile.py | 31 +++++++++++++++++++++++-------- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/rasterio/_io.pyx b/rasterio/_io.pyx index 44afff77..9af954f0 100644 --- a/rasterio/_io.pyx +++ b/rasterio/_io.pyx @@ -1298,6 +1298,13 @@ cdef class RasterUpdater(RasterReader): continue kwds.append((k.lower(), v)) k, v = k.upper(), str(v).upper() + + # Guard against block size that exceed image size. + if k == 'BLOCKXSIZE' and int(v) > self.width: + raise ValueError("blockxsize exceeds raster width.") + if k == 'BLOCKYSIZE' and int(v) > self.height: + raise ValueError("blockysize exceeds raster height.") + key_b = k.encode('utf-8') val_b = v.encode('utf-8') key_c = key_b @@ -1310,6 +1317,7 @@ cdef class RasterUpdater(RasterReader): self._hds = _gdal.GDALCreate( drv, fname, self.width, self.height, self._count, gdal_dtype, options) + if self._hds == NULL: raise ValueError("NULL dataset") diff --git a/tests/test_blocks.py b/tests/test_blocks.py index 912baf5a..4ca99bec 100644 --- a/tests/test_blocks.py +++ b/tests/test_blocks.py @@ -1,15 +1,16 @@ import logging import os.path -import unittest import shutil import subprocess import sys import tempfile +import unittest import numpy import rasterio + logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) class WindowTest(unittest.TestCase): @@ -120,4 +121,3 @@ class WindowWriteTest(unittest.TestCase): "Minimum=0.000, Maximum=127.000, " "Mean=31.750, StdDev=54.993" in info.decode('utf-8'), info) - diff --git a/tests/test_profile.py b/tests/test_profile.py index 07cc2cf3..b85b885f 100644 --- a/tests/test_profile.py +++ b/tests/test_profile.py @@ -60,14 +60,29 @@ def test_gtiff_profile_protected_driver(): def test_open_with_profile(tmpdir): - tiffname = str(tmpdir.join('foo.tif')) - with rasterio.open( - tiffname, - 'w', - **default_gtiff_profile( - count=1, width=1, height=1)) as dst: - data = dst.read() - assert data.flatten().tolist() == [0] + tiffname = str(tmpdir.join('foo.tif')) + with rasterio.open( + tiffname, + 'w', + **default_gtiff_profile( + count=1, width=256, height=256)) as dst: + data = dst.read() + + +def test_blockxsize_guard(tmpdir): + """blockxsize can't be greater than image width.""" + tiffname = str(tmpdir.join('foo.tif')) + with pytest.raises(ValueError): + _ = rasterio.open(tiffname, 'w', **default_gtiff_profile( + count=1, width=128, height=256)) + + +def test_blockysize_guard(tmpdir): + """blockysize can't be greater than image height.""" + tiffname = str(tmpdir.join('foo.tif')) + with pytest.raises(ValueError): + _ = rasterio.open(tiffname, 'w', **default_gtiff_profile( + count=1, width=256, height=128)) def test_profile_overlay():