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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
|
import logging
import re
from pathlib import Path
import subprocess
import affine
import numpy as np
import pytest
import rasterio
from rasterio.drivers import blacklist
from rasterio.enums import MaskFlags, Resampling
from rasterio.env import Env
from rasterio.errors import RasterioIOError
def test_validate_dtype_None(tmpdir):
"""Raise TypeError if there is no dtype"""
name = str(tmpdir.join("lol.tif"))
with pytest.raises(TypeError):
rasterio.open(
name, 'w', driver='GTiff', width=100, height=100, count=1)
def test_validate_dtype_str(tmpdir):
name = str(tmpdir.join("lol.tif"))
with pytest.raises(TypeError):
rasterio.open(
name, 'w', driver='GTiff', width=100, height=100, count=1,
dtype='Int16')
def test_validate_dtype_float128(tmpdir, basic_image):
"""Raise TypeError if dtype is unsupported by GDAL."""
name = str(tmpdir.join('float128.tif'))
try:
basic_image_f128 = basic_image.astype('float128')
except TypeError:
pytest.skip("Unsupported data type")
height, width = basic_image_f128.shape
with pytest.raises(TypeError):
rasterio.open(name, 'w', driver='GTiff', width=width, height=height,
count=1, dtype=basic_image_f128.dtype)
def test_validate_count_None(tmpdir):
name = str(tmpdir.join("lol.tif"))
with pytest.raises(TypeError):
rasterio.open(
name, 'w', driver='GTiff', width=100, height=100, # count=None
dtype=rasterio.uint8)
def test_no_crs(tmpdir):
# A dataset without crs is okay.
name = str(tmpdir.join("lol.tif"))
with rasterio.open(
name, 'w', driver='GTiff', width=100, height=100, count=1,
dtype=rasterio.uint8) as dst:
dst.write(np.ones((100, 100), dtype=rasterio.uint8), indexes=1)
@pytest.mark.gdalbin
def test_context(tmpdir):
name = Path(str(tmpdir.join("test_context.tif"))).as_posix()
with rasterio.open(
name, 'w',
driver='GTiff', width=100, height=100, count=1,
dtype=rasterio.ubyte) as s:
assert s.name == name
assert s.driver == 'GTiff'
assert not s.closed
assert s.count == 1
assert s.width == 100
assert s.height == 100
assert s.shape == (100, 100)
assert s.indexes == (1,)
assert repr(s) == "<open DatasetWriter name='%s' mode='w'>" % name
assert s.closed
assert s.count == 1
assert s.width == 100
assert s.height == 100
assert s.shape == (100, 100)
assert repr(s) == "<closed DatasetWriter name='%s' mode='w'>" % name
info = subprocess.check_output(["gdalinfo", name]).decode('utf-8')
assert "GTiff" in info
assert "Size is 100, 100" in info
assert "Band 1 Block=100x81 Type=Byte, ColorInterp=Gray" in info
@pytest.mark.gdalbin
def test_write_ubyte(tmpdir):
name = str(tmpdir.mkdir("sub").join("test_write_ubyte.tif"))
a = np.ones((100, 100), dtype=rasterio.ubyte) * 127
with rasterio.open(
name, 'w',
driver='GTiff', width=100, height=100, count=1,
dtype=a.dtype) as s:
s.write(a, indexes=1)
info = subprocess.check_output(["gdalinfo", "-stats", name]).decode('utf-8')
assert "Minimum=127.000, Maximum=127.000, Mean=127.000, StdDev=0.000" in info
@pytest.mark.gdalbin
def test_write_sbyte(tmpdir):
name = str(tmpdir.mkdir("sub").join("test_write_sbyte.tif"))
a = np.ones((100, 100), dtype=rasterio.sbyte) * -33
with rasterio.open(
name, 'w',
driver='GTiff', width=100, height=100, count=1,
dtype=a.dtype) as dst:
dst.write(a, indexes=1)
with rasterio.open(name) as dst:
assert (dst.read() == -33).all()
info = subprocess.check_output(["gdalinfo", "-stats", name]).decode('utf-8')
assert "Minimum=-33.000, Maximum=-33.000, Mean=-33.000, StdDev=0.000" in info
assert 'Int8' in info
@pytest.mark.gdalbin
def test_write_ubyte_multi(tmpdir):
name = str(tmpdir.mkdir("sub").join("test_write_ubyte_multi.tif"))
a = np.ones((100, 100), dtype=rasterio.ubyte) * 127
with rasterio.open(
name, 'w',
driver='GTiff', width=100, height=100, count=1,
dtype=a.dtype) as s:
s.write(a, 1)
info = subprocess.check_output(["gdalinfo", "-stats", name]).decode('utf-8')
assert "Minimum=127.000, Maximum=127.000, Mean=127.000, StdDev=0.000" in info
@pytest.mark.gdalbin
def test_write_ubyte_multi_list(tmpdir):
name = str(tmpdir.mkdir("sub").join("test_write_ubyte_multi_list.tif"))
a = np.array([np.ones((100, 100), dtype=rasterio.ubyte) * 127])
with rasterio.open(
name, 'w',
driver='GTiff', width=100, height=100, count=1,
dtype=a.dtype) as s:
s.write(a, [1])
info = subprocess.check_output(["gdalinfo", "-stats", name]).decode('utf-8')
assert "Minimum=127.000, Maximum=127.000, Mean=127.000, StdDev=0.000" in info
@pytest.mark.gdalbin
def test_write_ubyte_multi_3(tmpdir):
name = str(tmpdir.mkdir("sub").join("test_write_ubyte_multi_list.tif"))
arr = np.array(3 * [np.ones((100, 100), dtype=rasterio.ubyte) * 127])
with rasterio.open(
name, 'w',
driver='GTiff', width=100, height=100, count=3,
dtype=arr.dtype) as s:
s.write(arr)
info = subprocess.check_output(["gdalinfo", "-stats", name]).decode('utf-8')
assert "Minimum=127.000, Maximum=127.000, Mean=127.000, StdDev=0.000" in info
@pytest.mark.gdalbin
def test_write_float(tmpdir):
name = str(tmpdir.join("test_write_float.tif"))
a = np.ones((100, 100), dtype=rasterio.float32) * 42.0
with rasterio.open(
name, 'w',
driver='GTiff', width=100, height=100, count=2,
dtype=rasterio.float32) as s:
assert s.dtypes == (rasterio.float32, rasterio.float32)
s.write(a, indexes=1)
s.write(a, indexes=2)
info = subprocess.check_output(["gdalinfo", "-stats", name]).decode('utf-8')
assert "Minimum=42.000, Maximum=42.000, Mean=42.000, StdDev=0.000" in info
@pytest.mark.gdalbin
def test_write_crs_transform(tmpdir):
name = str(tmpdir.join("test_write_crs_transform.tif"))
a = np.ones((100, 100), dtype=rasterio.ubyte) * 127
transform = affine.Affine(300.0379266750948, 0.0, 101985.0,
0.0, -300.041782729805, 2826915.0)
with rasterio.open(
name,
"w",
driver="GTiff",
width=100,
height=100,
count=1,
crs={
"units": "m",
"no_defs": True,
"datum": "WGS84",
"proj": "utm",
"zone": 18,
},
transform=transform,
dtype=rasterio.ubyte,
) as s:
s.write(a, indexes=1)
assert s.crs.to_epsg() == 32618
info = subprocess.check_output(["gdalinfo", name]).decode('utf-8')
# make sure that pixel size is nearly the same as transform
# (precision varies slightly by platform)
assert re.search(r'Pixel Size = \(300.03792\d+,-300.04178\d+\)', info)
@pytest.mark.gdalbin
def test_write_crs_transform_affine(tmpdir):
name = str(tmpdir.join("test_write_crs_transform.tif"))
a = np.ones((100, 100), dtype=rasterio.ubyte) * 127
transform = affine.Affine(300.0379266750948, 0.0, 101985.0,
0.0, -300.041782729805, 2826915.0)
with rasterio.open(
name,
"w",
driver="GTiff",
width=100,
height=100,
count=1,
crs={
"units": "m",
"no_defs": True,
"datum": "WGS84",
"proj": "utm",
"zone": 18,
},
transform=transform,
dtype=rasterio.ubyte,
) as s:
s.write(a, indexes=1)
assert s.crs.to_epsg() == 32618
info = subprocess.check_output(["gdalinfo", name]).decode('utf-8')
# make sure that pixel size is nearly the same as transform
# (precision varies slightly by platform)
assert re.search(r'Pixel Size = \(300.03792\d+,-300.04178\d+\)', info)
@pytest.mark.gdalbin
def test_write_crs_transform_2(tmpdir, monkeypatch):
"""Using 'EPSG:32618' as CRS."""
monkeypatch.delenv('GDAL_DATA', raising=False)
name = str(tmpdir.join("test_write_crs_transform.tif"))
a = np.ones((100, 100), dtype=rasterio.ubyte) * 127
transform = affine.Affine(300.0379266750948, 0.0, 101985.0,
0.0, -300.041782729805, 2826915.0)
with rasterio.open(
name, 'w',
driver='GTiff', width=100, height=100, count=1,
crs='EPSG:32618',
transform=transform,
dtype=rasterio.ubyte) as src:
src.write(a, indexes=1)
assert src.crs.to_epsg() == 32618
info = subprocess.check_output(["gdalinfo", name]).decode('utf-8')
assert 'UTM zone 18N' in info
# make sure that pixel size is nearly the same as transform
# (precision varies slightly by platform)
assert re.search(r'Pixel Size = \(300.03792\d+,-300.04178\d+\)', info)
@pytest.mark.gdalbin
def test_write_crs_transform_3(tmpdir):
"""Using WKT as CRS."""
name = str(tmpdir.join("test_write_crs_transform.tif"))
a = np.ones((100, 100), dtype=rasterio.ubyte) * 127
transform = affine.Affine(300.0379266750948, 0.0, 101985.0,
0.0, -300.041782729805, 2826915.0)
wkt = 'PROJCS["WGS 84 / UTM zone 18N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-75],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32618"]]'
with rasterio.open(
name, 'w',
driver='GTiff', width=100, height=100, count=1,
crs=wkt,
transform=transform,
dtype=rasterio.ubyte) as s:
s.write(a, indexes=1)
assert s.crs.to_epsg() == 32618
info = subprocess.check_output(["gdalinfo", name]).decode('utf-8')
assert 'UTM zone 18N' in info
# make sure that pixel size is nearly the same as transform
# (precision varies slightly by platform)
assert re.search(r'Pixel Size = \(300.03792\d+,-300.04178\d+\)', info)
@pytest.mark.gdalbin
def test_write_meta(tmpdir):
name = str(tmpdir.join("test_write_meta.tif"))
a = np.ones((100, 100), dtype=rasterio.ubyte) * 127
meta = dict(driver='GTiff', width=100, height=100, count=1)
with rasterio.open(name, 'w', dtype=a.dtype, **meta) as s:
s.write(a, indexes=1)
info = subprocess.check_output(["gdalinfo", "-stats", name]).decode('utf-8')
assert "Minimum=127.000, Maximum=127.000, Mean=127.000, StdDev=0.000" in info
@pytest.mark.gdalbin
def test_write_nodata(tmpdir):
name = str(tmpdir.join("test_write_nodata.tif"))
a = np.ones((100, 100), dtype=rasterio.ubyte) * 127
with rasterio.open(
name, 'w',
driver='GTiff', width=100, height=100, count=2,
dtype=a.dtype, nodata=0) as s:
s.write(a, indexes=1)
s.write(a, indexes=2)
info = subprocess.check_output(["gdalinfo", "-stats", name]).decode('utf-8')
assert "NoData Value=0" in info
def test_guard_nodata(tmpdir):
name = str(tmpdir.join("test_guard_nodata.tif"))
a = np.ones((100, 100), dtype=rasterio.ubyte) * 127
with pytest.raises(ValueError):
rasterio.open(
name, 'w',
driver='GTiff', width=100, height=100, count=2,
dtype=a.dtype, nodata=-1)
def test_write_noncontiguous(tmpdir):
name = str(tmpdir.join("test_write_nodata.tif"))
ROWS = 4
COLS = 10
BANDS = 6
# Create a 3-D random int array (rows, columns, bands)
total = ROWS * COLS * BANDS
arr = np.random.randint(
0, 10, size=total).reshape(
(ROWS, COLS, BANDS), order='F').astype(np.int32)
kwargs = {
'driver': 'GTiff',
'width': COLS,
'height': ROWS,
'count': BANDS,
'dtype': rasterio.int32
}
with rasterio.open(name, 'w', **kwargs) as dst:
for i in range(BANDS):
dst.write(arr[:, :, i], indexes=i + 1)
@pytest.mark.parametrize("driver", list(blacklist.keys()))
def test_write_blacklist(tmpdir, driver):
# Skip if we don't have driver support built in.
with Env() as env:
if driver not in env.drivers():
pytest.skip()
name = str(tmpdir.join("data.test"))
with pytest.raises(RasterioIOError) as exc_info:
rasterio.open(name, 'w', driver=driver, width=100, height=100,
count=1, dtype='uint8')
exc = str(exc_info.value)
assert exc.startswith("Blacklisted")
def test_creation_metadata_deprecation(tmpdir):
name = str(tmpdir.join("test.tif"))
with rasterio.open(name, 'w', driver='GTiff', height=1, width=1, count=1, dtype='uint8', BIGTIFF='YES') as dst:
dst.write(np.ones((1, 1, 1), dtype='uint8'))
assert dst.tags(ns='rio_creation_kwds') == {}
def test_wplus_transform(tmpdir):
"""Transform is set on a new dataset created in w+ mode (see issue #1359)"""
name = str(tmpdir.join("test.tif"))
transform = affine.Affine.translation(10.0, 10.0) * affine.Affine.scale(0.5, -0.5)
with rasterio.open(name, 'w+', driver='GTiff', crs='epsg:4326', transform=transform, height=10, width=10, count=1, dtype='uint8') as dst:
dst.write(np.ones((1, 10, 10), dtype='uint8'))
assert dst.transform == transform
def test_write_no_driver__issue_1203(tmpdir):
name = str(tmpdir.join("test.invalid"))
with pytest.raises(ValueError), rasterio.open(name, 'w', height=1, width=1, count=1, dtype='uint8'):
print("TEST FAILED IF THIS IS REACHED.")
@pytest.mark.parametrize("mode", ["w", "w+"])
def test_require_width(tmpdir, mode):
"""width and height are required for w and w+ mode"""
name = str(tmpdir.join("test.tif"))
with pytest.raises(TypeError):
with rasterio.open(name, mode, driver="GTiff", height=1, count=1, dtype='uint8'):
print("TEST FAILED IF THIS IS REACHED.")
def test_too_big_for_tiff(tmpdir):
"""RasterioIOError is raised when TIFF is too big"""
name = str(tmpdir.join("test.tif"))
with pytest.raises(RasterioIOError):
rasterio.open(name, 'w', driver='GTiff', height=100000, width=100000, count=1, dtype='uint8', BIGTIFF=False)
@pytest.mark.parametrize("extension, driver", [
('tif', 'GTiff'),
('tiff', 'GTiff'),
('png', 'PNG'),
('jpg', 'JPEG'),
('jpeg', 'JPEG'),
])
def test_write__autodetect_driver(tmpdir, extension, driver):
name = str(tmpdir.join(f"test.{extension}"))
with rasterio.open(name, 'w', height=1, width=1, count=1, dtype='uint8') as rds:
assert rds.driver == driver
@pytest.mark.parametrize("driver", ["PNG", "JPEG"])
def test_issue2088(tmpdir, capsys, driver):
"""Write a PNG or JPEG without error messages"""
with rasterio.open(
str(tmpdir.join("test")),
"w",
driver=driver,
dtype="uint8",
count=1,
height=256,
width=256,
transform=affine.Affine.identity(),
) as src:
data = np.ones((256, 256), dtype=np.uint8)
src.write(data, 1)
captured = capsys.readouterr()
assert "ERROR 4" not in captured.err
assert "ERROR 4" not in captured.out
def test_write_cog(tmpdir, path_rgb_byte_tif):
"""Show resolution of issue #2102"""
with rasterio.open(path_rgb_byte_tif) as src:
profile = src.profile
profile.update(driver="COG", extent=src.bounds, resampling=Resampling.bilinear)
with rasterio.open(str(tmpdir.join("test.tif")), "w", **profile) as cog:
cog.write(src.read())
def test_write_cog__from_numpy(tmp_path):
data = np.zeros((100, 100))
profile = {
"width": data.shape[0],
"height": data.shape[1],
"count": 1,
"dtype": np.uint8,
"transform": affine.Affine(3.5, 0.0, 558838.0, 0.0, -3.5, 5927362.0),
"crs": 32630,
"nodata": 0,
"driver": "COG",
"compress": "DEFLATE",
}
with open(tmp_path / "bar.tiff", "wb") as f2:
with rasterio.open(f2, mode="w", **profile) as ds:
ds.write(data, 1)
def test_write_masked(tmp_path):
"""Verify that masked arrays are filled when written."""
data = np.ma.masked_less_equal(np.array([[0, 1, 2]], dtype="uint8"), 1)
data.fill_value = 3
with rasterio.open(
tmp_path / "test.tif",
"w",
driver="GTiff",
count=1,
width=3,
height=1,
dtype="uint8",
) as dst:
dst.write(data, indexes=1)
# Expect the masked array's fill_value in the first two pixels.
with rasterio.open(tmp_path / "test.tif") as src:
assert src.mask_flag_enums == ([MaskFlags.all_valid],)
arr = src.read()
assert list(arr.flatten()) == [3, 3, 2]
def test_write_masked_nodata(tmp_path):
"""Verify that masked arrays are filled with nodata when written."""
data = np.ma.masked_less_equal(np.array([[0, 1, 2]], dtype="uint8"), 1)
with rasterio.open(
tmp_path / "test.tif",
"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_write_masked_true(tmp_path):
"""Verify that a mask is written when we write a masked array."""
data = np.ma.masked_less_equal(np.array([[0, 1, 2]], dtype="uint8"), 1)
with rasterio.open(
tmp_path / "test.tif",
"w",
driver="GTiff",
count=1,
width=3,
height=1,
dtype="uint8",
) as dst:
dst.write(data, indexes=1, masked=True)
# Expect masked values in the first two pixels.
with rasterio.open(tmp_path / "test.tif") as src:
assert src.mask_flag_enums == ([MaskFlags.per_dataset],)
arr = src.read(masked=True)
assert list(arr.flatten()) == [np.ma.masked, np.ma.masked, 2]
def test_write_masked_nomask(tmp_path):
"""Verify that a mask is written when we write an optimized masked array (mask == np.ma.nomask)."""
data = np.ma.masked_array([[0, 1, 2]], dtype="uint8")
with rasterio.open(
tmp_path / "test.tif",
"w",
driver="GTiff",
count=1,
width=3,
height=1,
dtype="uint8",
) as dst:
dst.write(data, indexes=1, masked=True)
# Expect no masked values.
with rasterio.open(tmp_path / "test.tif") as src:
assert src.mask_flag_enums == ([MaskFlags.per_dataset],)
arr = src.read(masked=True)
assert list(arr.flatten()) == [0, 1, 2]
def test_write_int64(tmp_path):
test_file = tmp_path / "test.tif"
data = np.array([np.ones((100, 100), dtype=rasterio.int64) * 127])
with rasterio.open(
test_file,
'w',
driver='GTiff',
width=100,
height=100,
count=1,
dtype=data.dtype
) as file:
file.write(data, [1])
assert file.dtypes == (rasterio.int64,)
with rasterio.open(test_file) as file:
assert file.dtypes == (rasterio.int64,)
def test_open_no_log(caplog, tmp_path):
"""See gh-2525."""
caplog.set_level(logging.DEBUG)
rasterio.open(tmp_path / "my.tif", "w", driver="GTiff", width=500, height=500, count=4, dtype=np.uint8, nodata=255)
assert "GDAL signalled an error" not in caplog.text
|