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 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
|
"""tests rio_cogeo.cogeo."""
import os
import morecantile
import pytest
import rasterio
from rio_cogeo.scripts.cli import cogeo
from rio_cogeo.utils import get_zooms, has_mask_band
raster_path_rgb = os.path.join(os.path.dirname(__file__), "fixtures", "image_rgb.tif")
raster_path_rgba = os.path.join(os.path.dirname(__file__), "fixtures", "image_rgba.tif")
raster_path_nan = os.path.join(os.path.dirname(__file__), "fixtures", "image_nan.tif")
raster_path_nodata = os.path.join(
os.path.dirname(__file__), "fixtures", "image_nodata.tif"
)
raster_path_missingnodata = os.path.join(
os.path.dirname(__file__), "fixtures", "image_missing_nodata.tif"
)
raster_invalid_cog = os.path.join(
os.path.dirname(__file__), "fixtures", "validate", "image_dec.tif"
)
raster_path_gcps = os.path.join(os.path.dirname(__file__), "fixtures", "slc.tif")
raster_band_tags = os.path.join(
os.path.dirname(__file__), "fixtures", "cog_band_tags.tif"
)
@pytest.fixture(autouse=True)
def testing_env_var(monkeypatch):
"""Set GDAL env."""
monkeypatch.setenv("GDAL_DISABLE_READDIR_ON_OPEN", "TRUE")
monkeypatch.setenv("GDAL_TIFF_INTERNAL_MASK", "TRUE")
monkeypatch.delenv("GDAL_TIFF_OVR_BLOCKSIZE", raising=False)
def test_cogeo_valid(runner):
"""Should work as expected."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo, ["create", raster_path_rgb, "output.tif", "--add-mask", "--quiet"]
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
assert src.height == 512
assert src.width == 512
assert src.meta["dtype"] == "uint8"
assert all([(512, 512) == (h, w) for (h, w) in src.block_shapes])
assert src.compression.value == "DEFLATE"
assert not src.photometric
assert src.interleaving.value == "PIXEL"
assert not src.overviews(1)
assert has_mask_band(src)
def test_cogeo_valid_external_mask(monkeypatch, runner):
"""Should work as expected."""
monkeypatch.setenv("GDAL_TIFF_INTERNAL_MASK", "FALSE")
monkeypatch.setenv("GDAL_DISABLE_READDIR_ON_OPEN", "TRUE")
with runner.isolated_filesystem():
result = runner.invoke(
cogeo, ["create", raster_path_rgb, "output.tif", "--add-mask"]
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
assert has_mask_band(src)
assert "output.tif.msk" in src.files
def test_cogeo_validbidx(runner):
"""Should work as expected."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"-b",
"1",
"-p",
"raw",
"--add-mask",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
assert has_mask_band(src)
assert src.count == 1
def test_cogeo_invalidbidx(runner):
"""Should exit with invalid band indexes."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo, ["create", raster_path_rgb, "output.tif", "-b", "0"]
)
assert result.exception
assert result.exit_code == 1
def test_cogeo_invalidbidxString(runner):
"""Should exit with invalid band indexes."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo, ["create", raster_path_rgb, "output.tif", "-b", "a"]
)
assert result.exception
assert result.exit_code == 1
def test_cogeo_invalidThread(runner):
"""Should exit with invalid thread parameters."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo, ["create", raster_path_rgb, "output.tif", "--threads", "all"]
)
assert result.exception
assert result.exit_code == 1
def test_cogeo_validnodata(runner):
"""Should work as expected."""
with runner.isolated_filesystem():
with pytest.warns(UserWarning):
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"--nodata",
"0",
"--cog-profile",
"jpeg",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
assert not src.nodata
assert has_mask_band(src)
result = runner.invoke(
cogeo,
[
"create",
raster_path_nodata,
"output.tif",
"--co",
"BLOCKXSIZE=64",
"--co",
"BLOCKYSIZE=64",
"--cog-profile",
"deflate",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
assert src.nodata == -9999
assert not has_mask_band(src)
def test_cogeo_validGdalOptions(runner):
"""Should work as expected."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"-p",
"raw",
"--co",
"COMPRESS=DEFLATE",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
assert src.compression.value == "DEFLATE"
def test_cogeo_validOvrOption(runner):
"""Should work as expected."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"--overview-level",
2,
"--overview-resampling",
"bilinear",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
assert all([(512, 512) == (h, w) for (h, w) in src.block_shapes])
assert src.overviews(1) == [2, 4]
def test_cogeo_decimation_base_option(runner):
"""Should work as expected."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"--decimation-base",
3,
"--overview-level",
6,
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
assert len(src.overviews(1)) == 6
assert src.overviews(1)[0] == 3
def test_cogeo_overviewTilesize(monkeypatch, runner):
"""Should work as expected."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"--quiet",
"--co",
"BLOCKXSIZE=128",
"--co",
"BLOCKYSIZE=128",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
assert all([(128, 128) == (h, w) for (h, w) in src.block_shapes])
assert src.overviews(1)
with rasterio.open("output.tif", OVERVIEW_LEVEL=1) as src:
assert src.block_shapes[0] == (128, 128)
with runner.isolated_filesystem():
result = runner.invoke(
cogeo,
["create", raster_path_rgb, "output.tif", "--quiet", "--blocksize", "128"],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
assert all([(128, 128) == (h, w) for (h, w) in src.block_shapes])
assert src.overviews(1)
with rasterio.open("output.tif", OVERVIEW_LEVEL=1) as src:
assert src.block_shapes[0] == (128, 128)
with runner.isolated_filesystem():
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"--quiet",
"--co",
"BLOCKXSIZE=128",
"--co",
"BLOCKYSIZE=128",
"--overview-blocksize",
"64",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif", OVERVIEW_LEVEL=1) as src:
assert src.block_shapes[0] == (64, 64)
monkeypatch.setenv("GDAL_TIFF_OVR_BLOCKSIZE", "64")
with runner.isolated_filesystem():
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"--quiet",
"--co",
"BLOCKXSIZE=128",
"--co",
"BLOCKYSIZE=128",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif", OVERVIEW_LEVEL=1) as src:
assert src.block_shapes[0] == (64, 64)
def test_cogeo_web(runner):
"""Should work as expected."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo, ["create", raster_path_rgb, "output.tif", "--web-optimized"]
)
assert not result.exception
assert result.exit_code == 0
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"--web-optimized",
"--zoom-level-strategy",
"lower",
],
)
assert not result.exception
assert result.exit_code == 0
def test_cogeo_validgdalBlockOption(runner):
"""Should work as expected."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"--co",
"BLOCKXSIZE=128",
"--co",
"BLOCKYSIZE=128",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
assert all([(128, 128) == (h, w) for (h, w) in src.block_shapes])
assert src.overviews(1) == [2, 4]
def test_cogeo_validNodataCustom(runner):
"""Should work as expected."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo,
[
"create",
raster_path_nan,
"output.tif",
"--cog-profile",
"deflate",
"--nodata",
"nan",
"--co",
"BLOCKXSIZE=64",
"--co",
"BLOCKYSIZE=64",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
assert src.meta["dtype"] == "float64"
assert src.nodata
assert src.compression.value == "DEFLATE"
assert not src.dataset_mask().all()
assert src.dataset_mask()[0][0] == 0
result = runner.invoke(
cogeo,
[
"create",
raster_path_missingnodata,
"output.tif",
"--cog-profile",
"deflate",
"--nodata",
"-9999",
"--co",
"BLOCKXSIZE=64",
"--co",
"BLOCKYSIZE=64",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
assert src.nodata == -9999
assert src.compression.value == "DEFLATE"
assert not src.dataset_mask().all()
assert src.dataset_mask()[0][0] == 0
assert src.dataset_mask()[-1][-1] == 255
result = runner.invoke(
cogeo,
[
"create",
raster_path_missingnodata,
"output.tif",
"--cog-profile",
"deflate",
"--nodata",
"none",
"--co",
"BLOCKXSIZE=64",
"--co",
"BLOCKYSIZE=64",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
assert src.compression.value == "DEFLATE"
assert src.dataset_mask().all()
result = runner.invoke(
cogeo,
[
"create",
raster_path_nan,
"output.tif",
"--cog-profile",
"deflate",
"--nodata",
"non",
],
)
assert result.exception
assert result.exit_code == 1
def test_cogeo_validTempFile(monkeypatch, runner):
"""Should work as expected."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo, ["create", raster_path_rgb, "output.tif", "--no-in-memory"]
)
assert not result.exception
assert result.exit_code == 0
monkeypatch.setenv("IN_MEMORY_THRESHOLD", "100")
with runner.isolated_filesystem():
result = runner.invoke(cogeo, ["create", raster_path_rgb, "output.tif"])
assert not result.exception
assert result.exit_code == 0
def test_cogeo_validCompress(monkeypatch, runner):
"""Should work as expected."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"--allow-intermediate-compression",
],
)
assert not result.exception
assert result.exit_code == 0
def test_cogeo_invalidresampling(runner):
"""Should exit with invalid resampling."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo, ["create", raster_path_rgb, "output.tif", "-r", "gauss", "-w"]
)
assert result.exception
assert result.exit_code == 2
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"--overview-resampling",
"max",
"-w",
],
)
assert result.exception
assert result.exit_code == 2
def test_cogeo_validate(runner):
"""Should work as expected."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo, ["create", raster_path_rgb, "output.tif", "--quiet"]
)
result = runner.invoke(cogeo, ["validate", "output.tif"])
assert "is a valid cloud optimized GeoTIFF" in result.output
assert not result.exception
assert result.exit_code == 0
result = runner.invoke(cogeo, ["validate", raster_invalid_cog])
assert "is NOT a valid cloud optimized GeoTIFF" in result.output
assert not result.exception
assert result.exit_code == 0
def test_cogeo_validUpercaseProfile(monkeypatch, runner):
"""Should work as expected."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo,
["create", raster_path_rgb, "output.tif", "--cog-profile", "DEFLATE"],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
assert src.compression.value == "DEFLATE"
@pytest.mark.parametrize(
"src_path",
[
raster_path_rgb,
raster_invalid_cog,
raster_band_tags,
raster_path_gcps,
],
)
def test_cogeo_info(src_path, runner):
"""Should work as expected."""
with runner.isolated_filesystem():
result = runner.invoke(cogeo, ["info", src_path])
assert not result.exception
assert result.exit_code == 0
result = runner.invoke(cogeo, ["info", src_path, "--json"])
assert not result.exception
assert result.exit_code == 0
def test_cogeo_zoom_level(runner):
"""Should work as expected."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"--web-optimized",
"--zoom-level",
"18",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
_, max_zoom = get_zooms(src)
assert max_zoom == 18
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"--web-optimized",
"--zoom-level",
"19",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
_, max_zoom = get_zooms(src)
assert max_zoom == 19
def test_create_web_tms(runner):
"""Should work as expected."""
with runner.isolated_filesystem():
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"--web-optimized",
"--aligned-levels",
2,
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
meta = src.profile
with rasterio.open("output.tif", OVERVIEW_LEVEL=0) as src:
meta_ovr = src.profile
assert meta_ovr["blockysize"] == meta["blockysize"]
with open("webmercator.json", "w") as f:
tms = morecantile.tms.get("WebMercatorQuad")
f.write(tms.model_dump_json())
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"--aligned-levels",
"2",
"--tms",
"webmercator.json",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
meta_tms = src.profile
assert meta_tms["crs"] == meta["crs"]
assert meta_tms["transform"] == meta["transform"]
assert meta_tms["blockysize"] == meta["blockysize"] == 256
assert meta_tms["width"] == meta["width"]
assert meta_tms["height"] == meta["height"]
with rasterio.open("output.tif", OVERVIEW_LEVEL=0) as src:
meta_ovr = src.profile
assert meta_ovr["blockysize"] == meta_tms["blockysize"] == 256
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"--aligned-levels",
"2",
"--blocksize",
512,
"--tms",
"webmercator.json",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif") as src:
meta_tms = src.profile
with rasterio.open("output.tif", OVERVIEW_LEVEL=0) as src:
meta_ovr = src.profile
assert meta_ovr["blockysize"] == meta_tms["blockysize"] == 512
result = runner.invoke(
cogeo,
[
"create",
raster_path_rgb,
"output.tif",
"--aligned-levels",
"2",
"--blocksize",
512,
"--overview-blocksize",
128,
"--tms",
"webmercator.json",
],
)
assert not result.exception
assert result.exit_code == 0
with rasterio.open("output.tif", OVERVIEW_LEVEL=0) as src:
meta_ovr = src.profile
assert meta_ovr["blockysize"] == 128
|