-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement profile as a callable class.
Add more tests.
- Loading branch information
Sean Gillies
committed
Apr 23, 2015
1 parent
37d56d1
commit 9c94977
Showing
3 changed files
with
91 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Raster dataset profiles.""" | ||
|
||
from rasterio.dtypes import uint8 | ||
|
||
|
||
class Profile: | ||
"""Base class for Rasterio dataset profiles. | ||
Subclasses will declare a format driver and driver-specific | ||
creation options. | ||
""" | ||
driver = None | ||
defaults = {} | ||
|
||
def __call__(self, **kwargs): | ||
"""Return a mapping suitable for writing new GeoTIFF datasets.""" | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
if kwargs.get('driver', self.driver) != self.driver: | ||
raise ValueError( | ||
"Overriding this profile's driver is not allowed.") | ||
profile = self.defaults.copy() | ||
profile.update(**kwargs) | ||
profile['driver'] = self.driver | ||
return profile | ||
|
||
|
||
class DefaultGTiffProfile(Profile): | ||
"""A tiled, band-interleaved, LZW-compressed, 8-bit GTiff profile.""" | ||
driver = 'GTiff' | ||
defaults = { | ||
'interleave': 'band', | ||
'tiled': True, | ||
'blockxsize': 256, | ||
'blockysize': 256, | ||
'compress': 'lzw', | ||
'nodata': 0, | ||
'dtype': uint8 | ||
} | ||
|
||
|
||
default_gtiff_profile = DefaultGTiffProfile() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,79 @@ | ||
import pytest | ||
|
||
import rasterio | ||
from rasterio.profiles import Profile, DefaultGTiffProfile | ||
from rasterio.profiles import default_gtiff_profile | ||
|
||
|
||
def test_base_profile(): | ||
assert Profile()()['driver'] is None | ||
|
||
|
||
def test_base_profile_kwarg(): | ||
assert Profile()(foo='bar')['foo'] == 'bar' | ||
|
||
|
||
def test_gtiff_profile_format(): | ||
assert DefaultGTiffProfile()()['driver'] == 'GTiff' | ||
|
||
def test_profile_format(): | ||
assert rasterio.default_gtiff_profile()['driver'] == 'GTiff' | ||
|
||
def test_gtiff_profile_interleave(): | ||
assert DefaultGTiffProfile()()['interleave'] == 'band' | ||
|
||
def test_profile_interleave(): | ||
assert rasterio.default_gtiff_profile()['interleave'] == 'band' | ||
|
||
def test_gtiff_profile_tiled(): | ||
assert DefaultGTiffProfile()()['tiled'] == True | ||
|
||
def test_profile_tiled(): | ||
assert rasterio.default_gtiff_profile()['tiled'] == True | ||
|
||
def test_gtiff_profile_blockxsize(): | ||
assert DefaultGTiffProfile()()['blockxsize'] == 256 | ||
|
||
def test_profile_blockxsize(): | ||
assert rasterio.default_gtiff_profile()['blockxsize'] == 256 | ||
|
||
def test_gtiff_profile_blockysize(): | ||
assert DefaultGTiffProfile()()['blockysize'] == 256 | ||
|
||
def test_profile_blockysize(): | ||
assert rasterio.default_gtiff_profile()['blockysize'] == 256 | ||
|
||
def test_gtiff_profile_compress(): | ||
assert DefaultGTiffProfile()()['compress'] == 'lzw' | ||
|
||
def test_profile_compress(): | ||
assert rasterio.default_gtiff_profile()['compress'] == 'lzw' | ||
|
||
def test_gtiff_profile_nodata(): | ||
assert DefaultGTiffProfile()()['nodata'] == 0 | ||
|
||
def test_profile_nodata(): | ||
assert rasterio.default_gtiff_profile()['nodata'] == 0 | ||
|
||
def test_gtiff_profile_dtype(): | ||
assert DefaultGTiffProfile()()['dtype'] == rasterio.uint8 | ||
|
||
def test_profile_dtype(): | ||
assert rasterio.default_gtiff_profile()['dtype'] == rasterio.uint8 | ||
|
||
def test_gtiff_profile_other(): | ||
assert DefaultGTiffProfile()(count=3)['count'] == 3 | ||
|
||
def test_profile_other(): | ||
assert rasterio.default_gtiff_profile(count=3)['count'] == 3 | ||
|
||
def test_gtiff_profile_dtype_override(): | ||
assert DefaultGTiffProfile()(dtype='uint16')['dtype'] == rasterio.uint16 | ||
|
||
|
||
def test_gtiff_profile_protected_driver(): | ||
"""Overriding the driver is not allowed.""" | ||
with pytest.raises(ValueError): | ||
DefaultGTiffProfile()(driver='PNG') | ||
|
||
|
||
def test_open_with_profile(tmpdir): | ||
tiffname = str(tmpdir.join('foo.tif')) | ||
with rasterio.open( | ||
tiffname, | ||
'w', | ||
**rasterio.default_gtiff_profile( | ||
**default_gtiff_profile( | ||
count=1, width=1, height=1)) as dst: | ||
data = dst.read() | ||
assert data.flatten().tolist() == [0] | ||
|
||
|
||
def test_profile_overlay(): | ||
with rasterio.open('tests/data/RGB.byte.tif') as src: | ||
kwds = src.meta | ||
kwds.update(**default_gtiff_profile()) | ||
assert kwds['tiled'] | ||
assert kwds['compress'] == 'lzw' | ||
assert kwds['count'] == 3 |
@sgillies Isn't
GeoTIFF
out of place here, since this is the base class?