Skip to content

Instantly share code, notes, and snippets.

@perrygeo
Last active June 14, 2018 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perrygeo/31c58d154bed2e5907775566cc8abb49 to your computer and use it in GitHub Desktop.
Save perrygeo/31c58d154bed2e5907775566cc8abb49 to your computer and use it in GitHub Desktop.
Tiled=true fails silently
#!/usr/bin/env python
from pprint import pprint
from functools import reduce
import operator
import numpy as np
from affine import Affine
from rasterio.crs import CRS
import rasterio
image_shape = (4, 128, 128)
blocksizes = (128, 128)
size = reduce(operator.mul, image_shape, 1)
arr = (
np.random.randint(low=-0, high=255, size=size)
.astype("uint8")
.reshape(image_shape)
)
options = {
"driver": "GTiff",
"dtype": "uint8",
"nodata": None,
"count": image_shape[0],
"width": image_shape[1],
"height": image_shape[2],
"crs": CRS({"init": "epsg:3857"}),
"transform": Affine(600, 0.0, -20037508, 0.0, -600, -939258),
"tiled": True,
"compress": "JPEG",
"interleave": "pixel",
"blockxsize": blocksizes[0],
"blockysize": blocksizes[1],
}
with rasterio.open('/tmp/out.tif', 'w', **options) as dst:
dst.write(arr)
with rasterio.open('/tmp/out.tif', 'r') as test_src:
pprint(test_src.profile)
assert test_src.is_tiled
@perrygeo
Copy link
Author

Results when the image shape dimensions are less than blocksize (error)

Traceback (most recent call last):
  File "tiled_assert.py", line 40, in <module>
    with rasterio.open('/tmp/out.tif', 'w', **options) as dst:
  File "/Users/mperry/env/pxm/lib/python3.6/site-packages/rasterio/__init__.py", line 230, in open
    **kwargs)
  File "rasterio/_io.pyx", line 1010, in rasterio._io.DatasetWriterBase.__init__
ValueError: blockxsize exceeds raster width.

Results when the image dimensions are equal to blocksize (silently ignore geotiff tiling)

{'compress': 'jpeg',
 'count': 4,
 'crs': CRS({'init': 'epsg:3857'}),
 'driver': 'GTiff',
 'dtype': 'uint8',
 'height': 128,
 'interleave': 'pixel',
 'nodata': None,
 'tiled': False,
 'transform': Affine(600.0, 0.0, -20037508.0,
       0.0, -600.0, -939258.0),
 'width': 128}
Traceback (most recent call last):
  File "tiled_assert.py", line 45, in <module>
    assert test_src.is_tiled
AssertionError

Only when the image dimensions are greater than the blocksize does the assertion of internally tiled output hold.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment