File: test_boundless_read.py

package info (click to toggle)
rasterio 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,732 kB
  • sloc: python: 23,119; sh: 947; makefile: 275; xml: 29
file content (204 lines) | stat: -rw-r--r-- 7,891 bytes parent folder | download
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
"""Test of boundless reads"""
import sys

from affine import Affine
import shutil
from hypothesis import example, given
import hypothesis.strategies as st
import numpy
from numpy.testing import assert_almost_equal
import pytest

import rasterio
from rasterio.io import MemoryFile
from rasterio.windows import Window


@given(col_start=st.integers(min_value=-700, max_value=0),
       row_start=st.integers(min_value=-700, max_value=0),
       col_stop=st.integers(min_value=0, max_value=700),
       row_stop=st.integers(min_value=0, max_value=700))
@pytest.mark.skipif(sys.platform != "linux", reason="https://github.com/rasterio/rasterio/issues/1696")
def test_outer_boundless_pixel_fidelity(
        path_rgb_byte_tif, col_start, row_start, col_stop, row_stop):
    """An outer boundless read doesn't change pixels"""
    with rasterio.open(path_rgb_byte_tif) as dataset:
        width = dataset.width + col_stop - col_start
        height = dataset.height + row_stop - row_start
        window = Window(col_start, row_start, width, height)
        rgb_padded = dataset.read(window=window, boundless=True)
        assert rgb_padded.shape == (dataset.count, height, width)
        rgb = dataset.read()
        assert numpy.all(
            rgb
            == rgb_padded[
                :, -row_start : height - row_stop, -col_start : width - col_stop
            ]
        )


@pytest.mark.xfail(reason="The bug reported in gh-2382")
@given(height=st.integers(min_value=500, max_value=20000))
@example(height=9508)
def test_issue2382(height):
    data_array = numpy.arange(height, dtype="f4").reshape((height, 1))

    with MemoryFile() as memfile:
        with memfile.open(
            driver='GTiff',
            count=1,
            height=height,
            width=1,
            dtype=data_array.dtype,
            transform=Affine(1.0, 0.0, 0, 0.0, -1.0, 0),
        ) as dataset:
            dataset.write(data_array[numpy.newaxis, ...])

        with memfile.open(driver='GTiff') as dataset:
            # read first column, rows 0-388
            a = dataset.read(
                1,
                window=Window(col_off=0, row_off=0, width=1, height=388),
                boundless=True,
                fill_value=-9999,
            )[:, 0]
            assert_almost_equal(a, numpy.arange(388))

            b = dataset.read(
                1,
                window=Window(col_off=0, row_off=-12, width=1, height=400),
                boundless=True,
                fill_value=-9999,
            )[:, 0]
            # the expected result is 12 * -9999 and then the same as above
            assert_almost_equal(b, numpy.concatenate([[-9999] * 12, a]))


@pytest.mark.xfail(reason="Likely the bug reported in gh-2382")
@given(
    col_start=st.integers(min_value=-700, max_value=0),
    row_start=st.integers(min_value=-700, max_value=0),
    col_stop=st.integers(min_value=0, max_value=700),
    row_stop=st.integers(min_value=0, max_value=700),
)
def test_outer_upper_left_boundless_pixel_fidelity(
    path_rgb_byte_tif, col_start, row_start, col_stop, row_stop
):
    """A partially outer boundless read doesn't change pixels"""
    with rasterio.open(path_rgb_byte_tif) as dataset:
        width = dataset.width - col_stop - col_start
        height = dataset.height - row_stop - row_start
        window = Window(col_start, row_start, width, height)
        rgb_boundless = dataset.read(window=window, boundless=True)
        assert rgb_boundless.shape == (dataset.count, height, width)
        rgb = dataset.read()
        assert numpy.all(
            rgb[:, 0 : height + row_start, 0 : width + col_start]
            == rgb_boundless[:, -row_start:height, -col_start:width]
        )


def test_image(red_green):
    """Read a red image with black background"""
    with rasterio.Env():
        with rasterio.open(str(red_green.join("red.tif"))) as src:
            data = src.read(boundless=True, window=Window(-src.width, -src.height, src.width * 3, src.height * 3))
            image = numpy.moveaxis(data, 0, -1)
            assert image[63, 63, 0] == 0
            assert image[64, 64, 0] == 204


def test_hit_ovr(red_green):
    """Zoomed out read hits the overviews"""
    # GDAL doesn't log overview hits for local files , so we copy the
    # overviews of green.tif over the red overviews and expect to find
    # green pixels below.
    green_ovr = red_green.join("green.tif.ovr")
    shutil.move(green_ovr, red_green.join("red.tif.ovr"))
    assert not green_ovr.exists()
    with rasterio.open(str(red_green.join("red.tif.ovr"))) as ovr:
        data = ovr.read()
        assert (data[1] == 204).all()

    with rasterio.Env():
        with rasterio.open(str(red_green.join("red.tif"))) as src:
            data = src.read(out_shape=(3, 32, 32))
            image = numpy.moveaxis(data, 0, -1)
            assert image[0, 0, 0] == 17
            assert image[0, 0, 1] == 204


def test_boundless_mask_not_all_valid():
    """Confirm resolution of issue #1449"""
    with rasterio.open("tests/data/red.tif") as src:
        masked = src.read(1, boundless=True, masked=True, window=Window(-1, -1, 66, 66))
    assert not masked.mask.all()
    assert masked.mask[:, 0].all()
    assert masked.mask[:, -1].all()
    assert masked.mask[0, :].all()
    assert masked.mask[-1, :].all()


def test_boundless_fill_value():
    """Confirm resolution of issue #1471"""
    with rasterio.open("tests/data/red.tif") as src:
        filled = src.read(1, boundless=True, fill_value=5, window=Window(-1, -1, 66, 66))
    assert (filled[:, 0] == 5).all()
    assert (filled[:, -1] == 5).all()
    assert (filled[0, :] == 5).all()
    assert (filled[-1, :] == 5).all()


def test_boundless_masked_special():
    """Confirm resolution of special case in issue #1471"""
    with rasterio.open("tests/data/green.tif") as src:
        masked = src.read(2, boundless=True, masked=True, window=Window(-1, -1, 66, 66))
    assert not masked[:, 0].any()
    assert not masked[:, -1].any()
    assert not masked[0, :].any()
    assert not masked[-1, :].any()


def test_boundless_mask_special():
    """Confirm resolution of special case in issue #1471"""
    with rasterio.open("tests/data/green.tif") as src:
        mask = src.read_masks(2, boundless=True, window=Window(-1, -1, 66, 66))
    assert not mask[:, 0].any()
    assert not mask[:, -1].any()
    assert not mask[0, :].any()
    assert not mask[-1, :].any()


def test_boundless_fill_value_overview_masks():
    """Confirm a more general resolution to issue #1471"""
    with rasterio.open("tests/data/cogeo.tif") as src:
        data = src.read(1, boundless=True, window=Window(-300, -335, 1000, 1000), fill_value=5, out_shape=(512, 512))
    assert (data[:, 0] == 5).all()


def test_boundless_masked_fill_value_overview_masks():
    """Confirm a more general resolution to issue #1471"""
    with rasterio.open("tests/data/cogeo.tif") as src:
        data = src.read(1, masked=True, boundless=True, window=Window(-300, -335, 1000, 1000), fill_value=5, out_shape=(512, 512))
    assert data.fill_value == 5
    assert data.mask[:, 0].all()


def test_boundless_open_options():
    """Open options are taken into account"""
    with rasterio.open("tests/data/cogeo.tif", overview_level=1) as src:
        data1 = src.read(1, boundless=True)
    with rasterio.open("tests/data/cogeo.tif", overview_level=2) as src:
        data2 = src.read(1, boundless=True)
    assert not numpy.array_equal(data1, data2)


def test_issue3245(data):
    """Boundless read of a dataset without a mask should have no masking."""
    with rasterio.open(data / "RGB.byte.tif", "r+") as dst:
        dst.nodata = None
        assert not dst.read(masked=True).mask.any()
        data = dst.read(boundless=True, masked=True, window=Window(0, 0, dst.width, dst.height))
        assert not data.mask.any()
        assert (data >= 0).all()