1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
"""Dataset mask test."""
from affine import Affine
import numpy as np
import pytest
from affine import Affine
import rasterio
from rasterio.enums import Resampling
from rasterio.errors import NodataShadowWarning
from rasterio.crs import CRS
# Setup test arrays
red = np.array([[0, 0, 0],
[0, 1, 1],
[1, 0, 1]]).astype('uint8') * 255
grn = np.array([[0, 0, 0],
[1, 0, 1],
[1, 0, 1]]).astype('uint8') * 255
blu = np.array([[0, 0, 0],
[1, 1, 0],
[1, 0, 1]]).astype('uint8') * 255
# equivalent to alp = red | grn | blu
# valid data anywhere there is at least one R, G or B value
alp = np.array([[0, 0, 0],
[1, 1, 1],
[1, 0, 1]]).astype('uint8') * 255
# mask might be constructed using different tools
# and differ from a strict interpretation of rgb values
msk = np.array([[0, 0, 0],
[1, 1, 1],
[1, 1, 1]]).astype('uint8') * 255
alldata = np.array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]).astype('uint8') * 255
# boundless window ((1, 4, (1, 4))
alp_shift_lr = np.array([[1, 1, 0],
[0, 1, 0],
[0, 0, 0]]).astype('uint8') * 255
# whole mask resampled to (1, 5, 5) array
resampmask = np.array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[1, 1, 0, 1, 1],
[1, 1, 0, 1, 1]]).astype('uint8') * 255
# whole mask resampled to (1, 5, 5) array
resampave = np.array([[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 0, 1, 1]]).astype('uint8') * 255
@pytest.fixture(scope='function')
def tiffs(tmpdir):
_profile = {
'transform': Affine(5.0, 0.0, 0.0, 0.0, -5.0, 0.0),
'crs': CRS({'init': 'epsg:4326'}),
'driver': 'GTiff',
'dtype': 'uint8',
'height': 3,
'width': 3}
# 1. RGB without nodata value
prof = _profile.copy()
prof['count'] = 3
prof['nodata'] = None
with rasterio.open(str(tmpdir.join('rgb_no_ndv.tif')), 'w', **prof) as dst:
dst.write(red, 1)
dst.write(grn, 2)
dst.write(blu, 3)
# 2. RGB with nodata value
prof = _profile.copy()
prof['count'] = 3
prof['nodata'] = 0
with rasterio.open(str(tmpdir.join('rgb_ndv.tif')), 'w', **prof) as dst:
dst.write(red, 1)
dst.write(grn, 2)
dst.write(blu, 3)
# 3. RGBA without nodata value
prof = _profile.copy()
prof['count'] = 4
prof['nodata'] = None
with rasterio.open(str(tmpdir.join('rgba_no_ndv.tif')), 'w', **prof) as dst:
dst.write(red, 1)
dst.write(grn, 2)
dst.write(blu, 3)
dst.write(alp, 4)
# 4. RGBA with nodata value
prof = _profile.copy()
prof['count'] = 4
prof['nodata'] = 0
with rasterio.open(str(tmpdir.join('rgba_ndv.tif')), 'w', **prof) as dst:
dst.write(red, 1)
dst.write(grn, 2)
dst.write(blu, 3)
dst.write(alp, 4)
# 5. RGB with msk
prof = _profile.copy()
prof['count'] = 3
with rasterio.open(str(tmpdir.join('rgb_msk.tif')), 'w', **prof) as dst:
dst.write(red, 1)
dst.write(grn, 2)
dst.write(blu, 3)
dst.write_mask(msk)
# 6. RGB with msk (internal)
prof = _profile.copy()
prof['count'] = 3
with rasterio.Env(GDAL_TIFF_INTERNAL_MASK=True):
with rasterio.open(str(tmpdir.join('rgb_msk_internal.tif')),
'w', **prof) as dst:
dst.write(red, 1)
dst.write(grn, 2)
dst.write(blu, 3)
dst.write_mask(msk)
# 7. RGBA with msk
prof = _profile.copy()
prof['count'] = 4
with rasterio.open(str(tmpdir.join('rgba_msk.tif')), 'w', **prof) as dst:
dst.write(red, 1)
dst.write(grn, 2)
dst.write(blu, 3)
dst.write(alp, 4)
dst.write_mask(msk)
return tmpdir
def test_no_ndv(tiffs):
with rasterio.open(str(tiffs.join('rgb_no_ndv.tif'))) as src:
assert np.array_equal(src.dataset_mask(), alldata)
def test_rgb_ndv(tiffs):
with rasterio.open(str(tiffs.join('rgb_ndv.tif'))) as src:
res = src.dataset_mask()
assert res.dtype.name == "uint8"
assert np.array_equal(src.dataset_mask(), alp)
def test_rgba_no_ndv(tiffs):
with rasterio.open(str(tiffs.join('rgba_no_ndv.tif'))) as src:
assert np.array_equal(src.dataset_mask(), alp)
def test_rgba_ndv(tiffs):
with rasterio.open(str(tiffs.join('rgba_ndv.tif'))) as src:
with pytest.warns(NodataShadowWarning):
res = src.dataset_mask()
assert np.array_equal(res, alp)
def test_rgb_msk(tiffs):
with rasterio.open(str(tiffs.join('rgb_msk.tif'))) as src:
assert np.array_equal(src.dataset_mask(), msk)
# each band's mask is also equal
for bmask in src.read_masks():
assert np.array_equal(bmask, msk)
def test_rgb_msk_int(tiffs):
with rasterio.open(str(tiffs.join('rgb_msk_internal.tif'))) as src:
assert np.array_equal(src.dataset_mask(), msk)
def test_rgba_msk(tiffs):
with rasterio.open(str(tiffs.join('rgba_msk.tif'))) as src:
# mask takes precedent over alpha
assert np.array_equal(src.dataset_mask(), msk)
@pytest.mark.parametrize("kwds,expected", [(dict(window=((1, 4), (1, 4)), boundless=True), alp_shift_lr), (dict(out_shape=(1, 5, 5)), resampmask), (dict(out=np.zeros((1, 5, 5), dtype=np.uint8)), resampmask)])
def test_kwargs(tiffs, kwds, expected):
with rasterio.open(str(tiffs.join('rgb_ndv.tif'))) as src:
result = src.dataset_mask(**kwds)
assert np.array_equal(expected, result)
def test_indexes_not_supported(tiffs):
with rasterio.open(str(tiffs.join('rgb_ndv.tif'))) as src:
with pytest.raises(TypeError):
src.dataset_mask(indexes=1)
def test_kwargs_resampling(tiffs):
with rasterio.open(str(tiffs.join('rgb_ndv.tif'))) as src:
other = src.dataset_mask(out_shape=(1, 5, 5), resampling=Resampling.bilinear) != 0
other = other.astype(np.uint8) * 255
assert np.array_equal(resampave, other)
|