File: test_sampling.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 (84 lines) | stat: -rw-r--r-- 2,925 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
import numpy

import rasterio


def test_sampling():
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        data = next(src.sample([(220650.0, 2719200.0)]))
        assert list(data) == [18, 25, 14]


def test_sampling_beyond_bounds():
    """Unmasked sampling beyond bounds yields unmasked array of zeros."""
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        data = next(src.sample([(0.0, 0.0)]))
        assert not numpy.ma.is_masked(data)
        assert list(data) == [0, 0, 0]


def test_sampling_masked_beyond_bounds():
    """Masked sampling beyond bounds yields an entirely masked array."""
    with rasterio.open("tests/data/RGB.byte.tif") as src:
        data = next(src.sample([(0.0, 0.0)], masked=True))
        assert numpy.ma.is_masked(data)
        assert all(data.mask == True)


def test_sampling_no_nodata_masked_beyond_bounds(data):
    """Masked sampling beyond bounds yields an entirely masked array."""
    filename = str(data.join("RGB.byte.tif"))

    with rasterio.open(filename, "r+") as src:
        src.nodata = None

    with rasterio.open(filename) as src:
        data = next(src.sample([(0.0, 0.0)], masked=True))
        assert numpy.ma.is_masked(data)
        assert all(data.mask == True)


def test_sampling_beyond_bounds_no_nodata_masked():
    """Masked sampling beyond bounds yields an entirely masked array."""
    with rasterio.open('tests/data/RGB2.byte.tif') as src:
        data = next(src.sample([(0.0, 0.0)], masked=True))
        assert all(data.mask == True)


def test_sampling_beyond_bounds_masked():
    """Masked sampling beyond bounds yields a masked array with last element being False."""
    with rasterio.open('tests/data/RGBA.byte.tif') as src:
        data = next(src.sample([(0.0, 0.0)], masked=True))
        assert list(data.mask) == [True, True, True, False]


def test_sampling_beyond_bounds_nan():
    with rasterio.open('tests/data/float_nan.tif') as src:
        data = next(src.sample([(-10.0, 0.0)]))
        assert numpy.isnan(data)


def test_sampling_indexes():
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        data = next(src.sample([(220650.0, 2719200.0)], indexes=[2]))
        assert list(data) == [25]


def test_sampling_single_index():
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        data = next(src.sample([(220650.0, 2719200.0)], indexes=2))
        assert list(data) == [25]


def test_sampling_type():
    """See https://github.com/rasterio/rasterio/issues/378."""
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        sampler = src.sample([(220650.0, 2719200.0)], indexes=[2])
        assert type(sampler)


def test_sampling_ndarray():
    """sample and sample_gen can take ndarrays of coords."""
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        data = next(src.sample(numpy.array([[220650.0, 2719200.0]])))
        assert list(data) == [18, 25, 14]