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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
"""Tests of the Python opener VSI plugin."""
import io
import os
import warnings
from pathlib import Path
from threading import Thread
import zipfile
from affine import Affine
import fsspec
import numpy as np
import pytest
import rasterio
from rasterio.enums import MaskFlags
from rasterio.errors import OpenerRegistrationError
from rasterio.warp import reproject
def test_opener_failure():
"""Use int as an opener :)"""
with pytest.raises(OpenerRegistrationError) as exc_info:
rasterio.open("tests/data/RGB.byte.tif", opener=int)
def test_opener_io_open():
"""Use io.open as opener."""
with rasterio.open("tests/data/RGB.byte.tif", opener=io.open) as src:
profile = src.profile
assert profile["driver"] == "GTiff"
assert profile["count"] == 3
def test_opener_zipfile_open():
"""Use zipfile as opener."""
with zipfile.ZipFile("tests/data/files.zip") as zf:
with rasterio.open("RGB.byte.tif", opener=zf.open) as src:
profile = src.profile
assert profile["driver"] == "GTiff"
assert profile["count"] == 3
@pytest.mark.parametrize(
"urlpath", ["file://tests/data/RGB.byte.tif", "zip://RGB.byte.tif::tests/data/files.zip"]
)
def test_opener_fsspec_open(urlpath):
"""Use fsspec.open as opener."""
with rasterio.open(urlpath, opener=fsspec.open) as src:
profile = src.profile
assert profile["driver"] == "GTiff"
assert profile["count"] == 3
def test_opener_fsspec_fs_open():
"""Use fsspec filesystem open() as opener."""
fs = fsspec.filesystem("file")
with rasterio.open("tests/data/RGB.byte.tif", opener=fs.open) as src:
profile = src.profile
assert profile["driver"] == "GTiff"
assert profile["count"] == 3
def test_opener_fsspec_fs():
"""Use fsspec filesystem as opener."""
fs = fsspec.filesystem("file")
with rasterio.open("tests/data/RGB.byte.tif", opener=fs) as src:
profile = src.profile
assert profile["driver"] == "GTiff"
assert profile["count"] == 3
@pytest.mark.network
def test_opener_fsspec_http_fs():
"""Use fsspec http filesystem as opener."""
fs = fsspec.filesystem("http")
with rasterio.open(
"https://raw.githubusercontent.com/rasterio/rasterio/main/tests/data/float32.tif",
opener=fs,
) as src:
profile = src.profile
assert profile["driver"] == "GTiff"
assert profile["count"] == 1
def test_opener_fsspec_fs_open_write(tmp_path):
"""Use fsspec filesystem open() as opener for writing."""
data = np.ma.masked_less_equal(np.array([[0, 1, 2]], dtype="uint8"), 1)
fs = fsspec.filesystem("file")
with rasterio.open(
os.fspath(tmp_path / "test.tif"),
"w",
driver="GTiff",
count=1,
width=3,
height=1,
dtype="uint8",
nodata=0,
opener=fs.open,
) as dst:
dst.write(data, indexes=1)
# Expect the dataset's nodata value in the first two pixels.
with rasterio.open(tmp_path / "test.tif") as src:
assert src.mask_flag_enums == ([MaskFlags.nodata],)
arr = src.read()
assert list(arr.flatten()) == [0, 0, 2]
def test_opener_fsspec_fs_write(tmp_path):
"""Use fsspec filesystem open() as opener for writing."""
data = np.ma.masked_less_equal(np.array([[0, 1, 2]], dtype="uint8"), 1)
fs = fsspec.filesystem("file")
with rasterio.open(
os.fspath(tmp_path / "test.tif"),
"w",
driver="GTiff",
count=1,
width=3,
height=1,
dtype="uint8",
nodata=0,
opener=fs,
) as dst:
dst.write(data, indexes=1)
# Expect the dataset's nodata value in the first two pixels.
with rasterio.open(tmp_path / "test.tif") as src:
assert src.mask_flag_enums == ([MaskFlags.nodata],)
arr = src.read()
assert list(arr.flatten()) == [0, 0, 2]
def test_fp_fsspec_openfile_write(tmp_path):
"""Use an fsspec OpenFile for writing."""
data = np.ma.masked_less_equal(np.array([[0, 1, 2]], dtype="uint8"), 1)
of = fsspec.open(os.fspath(tmp_path / "test.tif"), "wb")
with rasterio.open(
of,
"w",
driver="GTiff",
count=1,
width=3,
height=1,
dtype="uint8",
nodata=0,
) as dst:
dst.write(data, indexes=1)
# Expect the dataset's nodata value in the first two pixels.
with rasterio.open(tmp_path / "test.tif") as src:
assert src.mask_flag_enums == ([MaskFlags.nodata],)
arr = src.read()
assert list(arr.flatten()) == [0, 0, 2]
def test_opener_msk_sidecar():
"""Access .msk sidecar file via opener."""
# This test fails before issue 3027 is resolved because
# RGB2.byte.tif.msk is not found.
with rasterio.open("tests/data/RGB2.byte.tif", opener=io.open) as src:
for val in src.mask_flag_enums:
assert val == [MaskFlags.per_dataset]
def test_fsspec_msk_sidecar():
"""Access .msk sidecar file via fsspec."""
fs = fsspec.filesystem("file")
with rasterio.open("tests/data/RGB2.byte.tif", opener=fs) as src:
for val in src.mask_flag_enums:
assert val == [MaskFlags.per_dataset]
@pytest.mark.network
def test_fsspec_http_msk_sidecar():
"""Use fsspec http filesystem as opener."""
fs = fsspec.filesystem("http")
with rasterio.open(
"https://raw.githubusercontent.com/rasterio/rasterio/main/tests/data/RGB2.byte.tif",
opener=fs,
) as src:
for val in src.mask_flag_enums:
assert val == [MaskFlags.per_dataset]
def test_opener_tiledb_vfs():
"""Use tiledb virtual filesystem as opener."""
tiledb = pytest.importorskip("tiledb")
fs = tiledb.VFS()
with rasterio.open("tests/data/RGB.byte.tif", opener=fs) as src:
profile = src.profile
assert profile["driver"] == "GTiff"
assert profile["count"] == 3
def test_delete_on_overwrite(data):
"""Opener can delete dataset when overwriting."""
fs = fsspec.filesystem("file")
outputfile = os.path.join(str(data), "RGB.byte.tif")
with rasterio.open(outputfile, opener=fs) as dst:
profile = dst.profile
# No need to write any data, as open() will error if VSI unlinking
# isn't implemented.
with rasterio.open(outputfile, "w", opener=fs, **profile) as dst:
pass
@pytest.mark.parametrize("opener", [io.open, fsspec.filesystem("file")])
def test_opener_registration(opener):
"""Opener is correctly registered."""
from rasterio._vsiopener import _OPENER_REGISTRY, _opener_registration
with _opener_registration("tests/data/RGB.byte.tif", opener) as registered_vsi_path:
assert registered_vsi_path.startswith("/vsiriopener_")
key = (Path("tests/data"), registered_vsi_path.split("/")[1].split("_")[1])
val = _OPENER_REGISTRY.get()[key]
assert val.isfile
assert val._obj == opener
def test_threads_context():
"""Threads have opener registries."""
def target():
with rasterio.open("tests/data/RGB.byte.tif", opener=io.open) as dst:
assert dst.count == 3
thread = Thread(target=target)
thread.start()
thread.join()
def test_threads_overviews(data):
"""."""
fs = fsspec.filesystem("file")
outputfile = os.path.join(str(data), "RGB.byte.tif")
with rasterio.Env(GDAL_NUM_THREADS=1):
with rasterio.open(outputfile, "r+", opener=fs) as dst:
dst.build_overviews([2])
def test_warp(tmp_path):
"""File to file."""
fs = fsspec.filesystem("file")
tiffname = str(tmp_path.joinpath("foo.tif"))
with rasterio.open("tests/data/RGB.byte.tif", opener=fs) as src:
kwargs = src.profile
kwargs.update(
transform=Affine(300.0, 0.0, -8789636.708, 0.0, -300.0, 2943560.235),
crs="EPSG:3857",
)
with rasterio.open(tiffname, "w", opener=fs, **kwargs) as dst:
for i in (1, 2, 3):
reproject(rasterio.band(src, i), rasterio.band(dst, i), num_threads=2)
def test_opener_fsspec_fs_tiff_threads():
"""Fsspec filesystem opener is compatible with multithreaded tiff decoding."""
fs = fsspec.filesystem("file")
with rasterio.open("tests/data/rgb_lzw.tif", opener=fs, num_threads=2) as src:
profile = src.profile
assert profile["driver"] == "GTiff"
assert profile["count"] == 3
assert src.read().shape == (3, 718, 791)
def test_opener_fsspec_fs_tiff_threads_2():
"""Fsspec filesystem opener is compatible with multithreaded tiff decoding."""
fs = fsspec.filesystem("file")
with rasterio.Env(GDAL_NUM_THREADS=2):
with rasterio.open("tests/data/rgb_lzw.tif", opener=fs) as src:
profile = src.profile
assert profile["driver"] == "GTiff"
assert profile["count"] == 3
assert src.read().shape == (3, 718, 791)
def test_opener_multi_range_read():
"""Test with Opener with multi-range-read method."""
from rasterio.abc import MultiByteRangeResourceContainer
class CustomResource(io.FileIO):
"""Custom FileIO FS with `read_multi_range` method."""
def get_byte_ranges(
self,
offsets,
sizes,
):
warnings.warn("Using MultiRange Reads", UserWarning, stacklevel=2)
return [
self._read_range(offset, size) for (offset, size) in zip(offsets, sizes)
]
def _read_range(self, offset, size):
_ = self.seek(offset)
return self.read(size)
class CustomResourceContainer:
def open(self, path, mode="r", **kwds):
return CustomResource(path, mode=mode, **kwds)
def isfile(self, path):
return True
def isdir(self, path):
return False
def ls(self, path):
return []
def mtime(self, path):
return 0
def size(self, path):
with CustomResource(path) as f:
return f.size()
MultiByteRangeResourceContainer.register(CustomResourceContainer)
with rasterio.open(
"tests/data/RGB.byte.tif", opener=CustomResourceContainer()
) as src:
profile = src.profile
assert profile["driver"] == "GTiff"
assert profile["count"] == 3
# Should emit a multi-range read
with pytest.warns(UserWarning, match="Using MultiRange Reads"):
_ = src.read()
|