File: test_rio_calc.py

package info (click to toggle)
rasterio 1.4.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,760 kB
  • sloc: python: 22,520; makefile: 275; sh: 164; xml: 29
file content (189 lines) | stat: -rw-r--r-- 6,670 bytes parent folder | download | duplicates (2)
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
import rasterio
from rasterio.rio.main import main_group


def test_err(tmpdir, runner):
    outfile = str(tmpdir.join("out.tif"))
    result = runner.invoke(
        main_group, ["calc"] + ["($ 0.1 (read 1))", "tests/data/shade.tif", outfile]
    )
    assert result.exit_code == 1


def test_multiband_calc(tmpdir, runner):
    outfile = str(tmpdir.join('out.tif'))
    result = runner.invoke(main_group, ['calc'] + [
        '(+ 125 (* 0.1 (read 1)))', 'tests/data/shade.tif', outfile],
        catch_exceptions=False)
    assert result.exit_code == 0
    with rasterio.open(outfile) as src:
        assert src.count == 1
        assert src.meta['dtype'] == 'uint8'
        data = src.read(masked=True)
        assert data.min() == 125
        assert data.data[0][0][0] == 255
        assert data.mask[0][0][0]


def test_singleband_calc_byindex(tmpdir, runner):
    outfile = str(tmpdir.join('out.tif'))
    result = runner.invoke(main_group, ['calc'] + [
        '(+ 125 (* 0.1 (read 1 1)))', 'tests/data/shade.tif', outfile],
        catch_exceptions=False)
    assert result.exit_code == 0
    with rasterio.open(outfile) as src:
        assert src.count == 1
        assert src.meta['dtype'] == 'uint8'
        data = src.read(masked=True)
        assert data.min() == 125


def test_singleband_calc_byname(tmpdir, runner):
    outfile = str(tmpdir.join('out.tif'))
    result = runner.invoke(main_group, ['calc'] + [
        '(+ 125 (* 0.1 (take shade 1)))', '--name', 'shade=tests/data/shade.tif',
        outfile], catch_exceptions=False)
    assert result.exit_code == 0
    with rasterio.open(outfile) as src:
        assert src.count == 1
        assert src.meta['dtype'] == 'uint8'
        data = src.read(masked=True)
        assert data.min() == 125


def test_parts_calc(tmpdir, runner):
    # Producing an RGB output from the hill shade.
    # Red band has bumped up values. Other bands are unchanged.
    outfile = str(tmpdir.join('out.tif'))
    result = runner.invoke(main_group, ['calc'] + [
        '(asarray (+ (read 1 1) 125) (read 1 1) (read 1 1))',
        'tests/data/shade.tif', outfile], catch_exceptions=False)
    assert result.exit_code == 0
    with rasterio.open(outfile) as src:
        assert src.count == 3
        assert src.meta['dtype'] == 'uint8'
        data = src.read(masked=True)
        assert data[0].min() == 125
        assert data[1].min() == 0
        assert data[2].min() == 0


def test_parts_calc_2(tmpdir, runner):
    # Produce greyscale output from the RGB file.
    outfile = str(tmpdir.join('out.tif'))
    result = runner.invoke(main_group, ['calc'] + [
        '(+ (+ (/ (read 1 1) 3.0) (/ (read 1 2) 3.0)) (/ (read 1 3) 3.0))',
        'tests/data/RGB.byte.tif', outfile], catch_exceptions=False)
    assert result.exit_code == 0
    with rasterio.open(outfile) as src:
        assert src.count == 1
        assert src.meta['dtype'] == 'uint8'
        data = src.read(masked=True)
        assert round(data.mean(), 1) == 60.3


def test_copy_rgb(tmpdir, runner):
    outfile = str(tmpdir.join('out.tif'))
    result = runner.invoke(main_group, ['calc'] + [
        '(read 1)', 'tests/data/RGB.byte.tif', outfile],
        catch_exceptions=False)
    assert result.exit_code == 0
    with rasterio.open(outfile) as src:
        assert src.count == 3
        assert src.meta['dtype'] == 'uint8'
        data = src.read(masked=True)
        assert round(data.mean(), 1) == 60.6


def test_fillnodata(tmpdir, runner):
    outfile = str(tmpdir.join('out.tif'))
    result = runner.invoke(main_group, ['calc'] + [
        '(asarray (fillnodata (read 1 1)) (fillnodata (read 1 2)) (fillnodata (read 1 3)))',
        'tests/data/RGB.byte.tif', outfile], catch_exceptions=False)
    assert result.exit_code == 0
    with rasterio.open(outfile) as src:
        assert src.count == 3
        assert src.meta['dtype'] == 'uint8'
        data = src.read(masked=True)
        assert round(data.mean(), 1) == 58.6


def test_fillnodata_map(tmpdir, runner):
    outfile = str(tmpdir.join('out.tif'))
    result = runner.invoke(main_group, ['calc'] + [
        '(asarray (map fillnodata (read 1)))',
        'tests/data/RGB.byte.tif', outfile], catch_exceptions=False)
    assert result.exit_code == 0
    with rasterio.open(outfile) as src:
        assert src.count == 3
        assert src.meta['dtype'] == 'uint8'
        data = src.read(masked=True)
        assert round(data.mean(), 1) == 58.6
        assert data[0][60][60] > 0


def test_sieve_band(tmpdir, runner):
    outfile = str(tmpdir.join('out.tif'))
    result = runner.invoke(main_group, ['calc'] + [
        '(sieve (band 1 1) 42)', 'tests/data/shade.tif', outfile],
        catch_exceptions=False)
    assert result.exit_code == 0
    with rasterio.open(outfile) as src:
        assert src.count == 1
        assert src.meta['dtype'] == 'uint8'


def test_sieve_read(tmpdir, runner):
    outfile = str(tmpdir.join('out.tif'))
    result = runner.invoke(main_group, ['calc'] + [
        "(sieve (read 1 1 'uint8') 42)",
        'tests/data/shade.tif', outfile], catch_exceptions=False)
    assert result.exit_code == 0
    with rasterio.open(outfile) as src:
        assert src.count == 1
        assert src.meta['dtype'] == 'uint8'


def test_positional_calculation_byindex(tmpdir, runner):
    # See Issue 947: https://github.com/rasterio/rasterio/issues/947
    # Prior to fix, 'shade.tif' reliably is read as 2nd input and
    # we should expect this to fail due to array shape error
    # ("operands could not be broadcast together")
    outfile = str(tmpdir.join('out.tif'))
    result = runner.invoke(main_group, ['calc'] + [
        '(- (read 1 1) (read 2 2))',
        'tests/data/RGB.byte.tif',
        'tests/data/RGB.byte.tif',
        'tests/data/shade.tif',
        outfile], catch_exceptions=False)
    assert result.exit_code == 0

    window = ((0, 1), (0, 1))
    with rasterio.open('tests/data/RGB.byte.tif') as rgb:
        answer = rgb.read(1, window=window) - rgb.read(1, window=window)

    with rasterio.open(outfile) as src:
        assert src.read(1, window=window) == answer


def test_bool(tmpdir, runner):
    """Check on issue #2401"""
    outfile = str(tmpdir.join("out.tif"))
    result = runner.invoke(
        main_group,
        ["calc"]
        + [
            "(>= (read 1 1) 127)",
            "-t",
            "uint8",
            "--masked",
            "--profile",
            "nodata=255",
            "tests/data/RGB.byte.tif",
            outfile,
        ],
        catch_exceptions=False,
    )
    assert result.exit_code == 0
    with rasterio.open(outfile) as src:
        assert (src.read() == 255).any()