File: test_int8.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 (58 lines) | stat: -rw-r--r-- 1,576 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
import affine
import numpy
import pytest

import rasterio
from rasterio.env import GDALVersion

from .conftest import gdal_version


@pytest.mark.xfail(gdal_version < GDALVersion(3, 7), reason="GDAL < 3.7 doesn't handle signed int8 correctly")
@pytest.mark.parametrize("nodata", [-1, -128])
def test_write_int8_mem(nodata):
    profile = {
        "driver": "GTiff",
        "width": 2,
        "height": 1,
        "count": 1,
        "dtype": "int8",
        "crs": "EPSG:3857",
        "transform": affine.Affine(10, 0, 0, 0, -10, 0),
        "nodata": nodata,
    }

    values = numpy.array([[nodata, nodata]], dtype="int8")

    with rasterio.open("/vsimem/test.tif", "w", **profile) as src:
        src.write(values, indexes=1)

    with rasterio.open("/vsimem/test.tif") as src:
        read = src.read(indexes=1)
        assert read[0][0] == nodata
        assert read[0][1] == nodata


@pytest.mark.parametrize("nodata", [None, -1, -128])
def test_write_int8_fs(tmp_path, nodata):
    filename = tmp_path.joinpath("test.tif")
    profile = {
        "driver": "GTiff",
        "width": 2,
        "height": 1,
        "count": 1,
        "dtype": "int8",
        "crs": "EPSG:3857",
        "transform": affine.Affine(10, 0, 0, 0, -10, 0),
        "nodata": nodata,
    }

    values = numpy.array([[127, -128]], dtype="int8")

    with rasterio.open(filename, "w", **profile) as src:
        src.write(values, indexes=1)

    with rasterio.open(filename) as src:
        read = src.read(indexes=1)
        assert read[0][0] == 127
        assert read[0][1] == -128