|
| 1 | +import tempfile |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import pytest |
| 6 | +import tifffile |
| 7 | +import yirgacheffe as yg |
| 8 | + |
| 9 | +def test_sprase_needs_nodata() -> None: |
| 10 | + data = np.zeros((5000, 5000)) |
| 11 | + with tempfile.TemporaryDirectory() as tempdir: |
| 12 | + filename = Path(tempdir) / "test.tif" |
| 13 | + with yg.from_array(data, (0, 0), yg.MapProjection("EPSG:4326", 10.0, -10.0)) as layer: |
| 14 | + with pytest.raises(ValueError): |
| 15 | + layer.to_geotiff(filename, sparse=True) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.parametrize("dtype,zero", [ |
| 19 | + (np.int8, 0), |
| 20 | + (np.float32, 0.0), |
| 21 | +]) |
| 22 | +def test_all_sparse(dtype: type, zero: int | float) -> None: |
| 23 | + data = np.zeros((5000, 5000)).astype(dtype) # type: ignore |
| 24 | + with tempfile.TemporaryDirectory() as tempdir: |
| 25 | + filename = Path(tempdir) / "test.tif" |
| 26 | + with yg.from_array(data, (0, 0), yg.MapProjection("EPSG:4326", 10.0, -10.0)) as layer: |
| 27 | + layer.to_geotiff(filename, nodata=zero, sparse=True) |
| 28 | + with yg.read_raster(filename) as layer: |
| 29 | + # no data is nan, so convert that to 0, as otherwise |
| 30 | + # this test will miss things |
| 31 | + assert layer.nan_to_num().sum() == 0 |
| 32 | + |
| 33 | + # now look into the tiff structure to see that all blocks are tiff |
| 34 | + with tifffile.TiffFile(filename) as tif: |
| 35 | + page = tif.pages[0] # type: ignore |
| 36 | + offsets = page.tags.get('StripOffsets').value # type: ignore |
| 37 | + byte_counts = page.tags.get('StripByteCounts').value # type: ignore |
| 38 | + |
| 39 | + for offset, count in zip(offsets, byte_counts): |
| 40 | + assert offset == 0 |
| 41 | + assert count == 0 |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize("dtype,zero", [ |
| 45 | + (np.int8, 0), |
| 46 | + (np.float32, 0.0), |
| 47 | +]) |
| 48 | +def test_none_sparse(dtype: type, zero: int | float) -> None: |
| 49 | + data = np.ones((5000, 5000)).astype(dtype) # type: ignore |
| 50 | + with tempfile.TemporaryDirectory() as tempdir: |
| 51 | + filename = Path(tempdir) / "test.tif" |
| 52 | + with yg.from_array(data, (0, 0), yg.MapProjection("EPSG:4326", 10.0, -10.0)) as layer: |
| 53 | + layer.to_geotiff(filename, nodata=zero, sparse=True) |
| 54 | + with yg.read_raster(filename) as layer: |
| 55 | + assert layer.sum() == 5000 * 5000 |
| 56 | + |
| 57 | + # now look into the tiff structure to see that all blocks are tiff |
| 58 | + with tifffile.TiffFile(filename) as tif: |
| 59 | + page = tif.pages[0] # type: ignore |
| 60 | + offsets = page.tags.get('StripOffsets').value # type: ignore |
| 61 | + byte_counts = page.tags.get('StripByteCounts').value # type: ignore |
| 62 | + |
| 63 | + for offset, count in zip(offsets, byte_counts): |
| 64 | + assert offset != 0 |
| 65 | + assert count != 0 |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize("dtype,zero", [ |
| 69 | + (np.int8, 0), |
| 70 | + (np.float32, 0.0), |
| 71 | +]) |
| 72 | +def test_mixed_sparse(dtype: type, zero: int | float) -> None: |
| 73 | + data = np.array([[i % 2] * 5000 for i in range(5000)]).astype(dtype) # type: ignore |
| 74 | + with tempfile.TemporaryDirectory() as tempdir: |
| 75 | + filename = Path(tempdir) / "test.tif" |
| 76 | + with yg.from_array(data, (0, 0), yg.MapProjection("EPSG:4326", 10.0, -10.0)) as layer: |
| 77 | + layer.to_geotiff(filename, nodata=zero, sparse=True) |
| 78 | + with yg.read_raster(filename) as layer: |
| 79 | + # no data is nan, so convert that to 0, as otherwise |
| 80 | + # this test will miss things |
| 81 | + assert layer.nan_to_num().sum() == 5000 * 5000 / 2 |
| 82 | + |
| 83 | + # now look into the tiff structure to see that all blocks are tiff |
| 84 | + with tifffile.TiffFile(filename) as tif: |
| 85 | + page = tif.pages[0] # type: ignore |
| 86 | + offsets = page.tags.get('StripOffsets').value # type: ignore |
| 87 | + byte_counts = page.tags.get('StripByteCounts').value # type: ignore |
| 88 | + |
| 89 | + for i, (offset, count) in enumerate(zip(offsets, byte_counts)): |
| 90 | + if i % 2: |
| 91 | + assert offset != 0 |
| 92 | + assert count != 0 |
| 93 | + else: |
| 94 | + assert offset == 0 |
| 95 | + assert count == 0 |
0 commit comments