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
|
"""tests rio_cogeo web."""
import os
import struct
import sys
import morecantile
import numpy
import pytest
import rasterio
from click.testing import CliRunner
from cogdumper.cog_tiles import COGTiff
from cogdumper.filedumper import Reader as FileReader
from rasterio.warp import transform_bounds
from rasterio.windows import from_bounds
from rio_cogeo.cogeo import cog_translate
from rio_cogeo.profiles import cog_profiles
from rio_cogeo.utils import get_zooms
from .conftest import requires_gdal31, requires_gdal35
raster_path_web = os.path.join(os.path.dirname(__file__), "fixtures", "image_web.tif")
raster_path_north = os.path.join(
os.path.dirname(__file__), "fixtures", "image_north.tif"
)
raster_geos = os.path.join(os.path.dirname(__file__), "fixtures", "image_geos.tif")
def test_cog_translate_webZooms():
"""
Test Web-Optimized COG.
- Test COG size is a multiple of 256 (mercator tile size)
- Test COG bounds are aligned with mercator grid at max zoom
- Test high resolution internal tiles are equal to mercator tile using
cogdumper and rio-tiler
- Test overview internal tiles are equal to mercator tile using
cogdumper and rio-tiler
"""
runner = CliRunner()
with runner.isolated_filesystem():
web_tms = morecantile.tms.get("WebMercatorQuad")
web_profile = cog_profiles.get("raw")
web_profile.update({"blockxsize": 256, "blockysize": 256})
config = {"GDAL_TIFF_OVR_BLOCKSIZE": "256"}
cog_translate(
raster_path_north,
"cogeo.tif",
web_profile,
quiet=True,
tms=web_tms,
config=config,
)
with rasterio.open("cogeo.tif") as out_dst:
_, maxzoom = get_zooms(out_dst)
assert maxzoom == 9
cog_translate(
raster_path_north,
"cogeo.tif",
web_profile,
quiet=True,
tms=web_tms,
zoom_level_strategy="lower",
config=config,
)
with rasterio.open("cogeo.tif") as out_dst:
_, maxzoom = get_zooms(out_dst)
assert maxzoom == 8
def test_cog_translate_web():
"""
Test Web-Optimized COG.
- Test COG size is a multiple of 256 (mercator tile size)
- Test COG bounds are aligned with mercator grid at max zoom
"""
tms = morecantile.tms.get("WebMercatorQuad")
runner = CliRunner()
with runner.isolated_filesystem():
web_profile = cog_profiles.get("raw")
web_profile.update({"blockxsize": 256, "blockysize": 256})
config = {"GDAL_TIFF_OVR_BLOCKSIZE": "256"}
cog_translate(
raster_path_web,
"cogeo.tif",
web_profile,
quiet=True,
tms=tms,
config=config,
aligned_levels=0,
)
with rasterio.open(raster_path_web) as src_dst:
with rasterio.open("cogeo.tif") as out_dst:
blocks = list(set(out_dst.block_shapes))
assert len(blocks) == 1
ts = blocks[0][0]
assert not out_dst.width % ts
assert not out_dst.height % ts
_, max_zoom = get_zooms(out_dst)
bounds = list(
transform_bounds(
src_dst.crs, "epsg:4326", *src_dst.bounds, densify_pts=21
)
)
ulTile = tms.xy_bounds(tms.tile(bounds[0], bounds[3], max_zoom))
assert out_dst.bounds.left == ulTile.left
assert out_dst.bounds.top == ulTile.top
lrTile = tms.xy_bounds(tms.tile(bounds[2], bounds[1], max_zoom))
assert out_dst.bounds.right == lrTile.right
assert round(out_dst.bounds.bottom, 6) == round(lrTile.bottom, 6)
tags = out_dst.tags()
assert tags["TILING_SCHEME_NAME"] == "WebMercatorQuad"
assert tags["TILING_SCHEME_ZOOM_LEVEL"]
assert "TILING_SCHEME_ALIGNED_LEVELS" not in tags
cog_translate(
raster_path_web,
"cogeo.tif",
web_profile,
quiet=True,
tms=tms,
config=config,
aligned_levels=4,
)
with rasterio.open("cogeo.tif") as out_dst:
tags = out_dst.tags()
assert tags["TILING_SCHEME_NAME"] == "WebMercatorQuad"
assert tags["TILING_SCHEME_ZOOM_LEVEL"]
assert tags["TILING_SCHEME_ALIGNED_LEVELS"] == "4"
cog_translate(
raster_path_web,
"cogeo.tif",
web_profile,
quiet=True,
tms=tms,
zoom_level=19,
config=config,
aligned_levels=4,
)
with rasterio.open("cogeo.tif") as out_dst:
tags = out_dst.tags()
assert tags["TILING_SCHEME_NAME"] == "WebMercatorQuad"
assert tags["TILING_SCHEME_ZOOM_LEVEL"] == "19"
assert tags["TILING_SCHEME_ALIGNED_LEVELS"] == "4"
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6 or higher")
def test_cog_translate_Internal():
"""
Test Web-Optimized COG.
- Test COG size is a multiple of 256 (mercator tile size)
- Test COG bounds are aligned with mercator grid at max zoom
- Test high resolution internal tiles are equal to mercator tile using
cogdumper and rio-tiler
- Test overview internal tiles are equal to mercator tile using
cogdumper and rio-tiler
"""
tms = morecantile.tms.get("WebMercatorQuad")
runner = CliRunner()
with runner.isolated_filesystem():
web_profile = cog_profiles.get("raw")
web_profile.update({"blockxsize": 256, "blockysize": 256})
config = {"GDAL_TIFF_OVR_BLOCKSIZE": "256"}
cog_translate(
raster_path_web,
"cogeo.tif",
web_profile,
quiet=True,
tms=tms,
config=config,
aligned_levels=0,
)
with rasterio.open(raster_path_web) as src_dst:
with rasterio.open("cogeo.tif") as out_dst:
blocks = list(set(out_dst.block_shapes))
assert len(blocks) == 1
ts = blocks[0][0]
assert not out_dst.width % ts
assert not out_dst.height % ts
_, max_zoom = get_zooms(out_dst)
bounds = list(
transform_bounds(
src_dst.crs, "epsg:4326", *src_dst.bounds, densify_pts=21
)
)
minimumTile = tms.tile(bounds[0], bounds[3], max_zoom)
maximumTile = tms.tile(bounds[2], bounds[1], max_zoom)
with open("cogeo.tif", "rb") as out_body:
reader = FileReader(out_body)
cog = COGTiff(reader.read)
# High resolution
# Top Left tile
_, tile = cog.get_tile(0, 0, 0)
tile_length = 256 * 256 * 3
t = struct.unpack_from("{}b".format(tile_length), tile)
arr = numpy.array(t).reshape(256, 256, 3).astype(numpy.uint8)
arr = numpy.transpose(arr, [2, 0, 1])
with rasterio.open("cogeo.tif") as src_dst:
w = from_bounds(*tms.xy_bounds(minimumTile), src_dst.transform)
data = src_dst.read(
window=w, out_shape=(src_dst.count, 256, 256)
)
numpy.testing.assert_array_equal(data, arr)
# Bottom right tile
_, tile = cog.get_tile(4, 3, 0)
tile_length = 256 * 256 * 3
t = struct.unpack_from("{}b".format(tile_length), tile)
arr = numpy.array(t).reshape(256, 256, 3).astype(numpy.uint8)
arr = numpy.transpose(arr, [2, 0, 1])
with rasterio.open("cogeo.tif") as src_dst:
w = from_bounds(*tms.xy_bounds(maximumTile), src_dst.transform)
data = src_dst.read(
window=w, out_shape=(src_dst.count, 256, 256)
)
numpy.testing.assert_array_equal(data, arr)
def test_cog_translate_web_align():
"""
Test Web-Optimized COG.
- Test COG bounds (thus block) is aligned with Zoom levels
"""
tms = morecantile.tms.get("WebMercatorQuad")
runner = CliRunner()
with runner.isolated_filesystem():
web_profile = cog_profiles.get("raw")
web_profile.update({"blockxsize": 256, "blockysize": 256})
config = {"GDAL_TIFF_OVR_BLOCKSIZE": "256"}
with rasterio.open(raster_path_web) as src_dst:
_, max_zoom = get_zooms(src_dst)
cog_translate(
raster_path_web,
"cogeo.tif",
web_profile,
quiet=True,
tms=tms,
config=config,
aligned_levels=2,
)
with rasterio.open(raster_path_web) as src_dst:
bounds = src_dst.bounds
with rasterio.open("cogeo.tif") as cog:
_, max_zoom = get_zooms(cog)
ulTile = tms.xy_bounds(tms.tile(bounds[0], bounds[3], max_zoom - 2))
assert round(cog.bounds[0], 5) == round(ulTile.left, 5)
assert round(cog.bounds[3], 5) == round(ulTile.top, 5)
lrTile = tms.xy_bounds(tms.tile(bounds[2], bounds[1], max_zoom - 2))
assert round(cog.bounds[2], 5) == round(lrTile.right, 5)
assert round(cog.bounds[1], 5) == round(lrTile.bottom, 5)
cog_translate(
raster_path_web,
"cogeo.tif",
web_profile,
quiet=True,
tms=tms,
config=config,
aligned_levels=3,
)
with rasterio.open(raster_path_web) as src_dst:
bounds = src_dst.bounds
with rasterio.open("cogeo.tif") as cog_dst:
_, max_zoom = get_zooms(cog_dst)
ulTile = tms.xy_bounds(tms.tile(bounds[0], bounds[3], max_zoom - 3))
assert round(cog_dst.bounds[0], 5) == round(ulTile.left, 5)
assert round(cog_dst.bounds[3], 5) == round(ulTile.top, 5)
lrTile = tms.xy_bounds(tms.tile(bounds[2], bounds[1], max_zoom - 3))
assert round(cog_dst.bounds[2], 5) == round(lrTile.right, 5)
assert round(cog_dst.bounds[1], 5) == round(lrTile.bottom, 5)
@requires_gdal31
def test_cog_translate_web_geos():
"""
Test Web-Optimized COG for input GEOS dataset.
- Test COG bounds (thus block) is aligned with Zoom levels
"""
runner = CliRunner()
with runner.isolated_filesystem():
web_tms = morecantile.tms.get("WebMercatorQuad")
profile = cog_profiles.get("jpeg")
profile.update({"blockxsize": 256, "blockysize": 256})
config = {"GDAL_TIFF_OVR_BLOCKSIZE": "256", "GDAL_TIFF_INTERNAL_MASK": True}
cog_translate(
raster_geos,
"cogeo.tif",
profile,
quiet=True,
tms=web_tms,
config=config,
)
cog_translate(
raster_geos,
"cogeo_gdal.tif",
profile,
quiet=True,
tms=web_tms,
use_cog_driver=True,
config=config,
)
with rasterio.open("cogeo.tif") as cog_dst, rasterio.open(
"cogeo_gdal.tif"
) as cog_gdal:
assert cog_dst.shape == cog_gdal.shape
for i in range(0, 4):
assert round(cog_dst.bounds[i], 5) == round(cog_gdal.bounds[i], 5)
@requires_gdal31
def test_web_align_cogeo_gdal():
"""Test Web-Optimized COG conformance with GDAL."""
runner = CliRunner()
with runner.isolated_filesystem():
web_tms = morecantile.tms.get("WebMercatorQuad")
web_profile = cog_profiles.get("raw")
web_profile.update({"blockxsize": 256, "blockysize": 256})
config = {"GDAL_TIFF_OVR_BLOCKSIZE": "256"}
cog_translate(
raster_path_web,
"cogeo.tif",
web_profile,
quiet=True,
tms=web_tms,
config=config,
)
cog_translate(
raster_path_web,
"cogeo_gdal.tif",
web_profile,
quiet=True,
tms=web_tms,
config=config,
use_cog_driver=True,
)
with rasterio.open("cogeo.tif") as cog_dst, rasterio.open(
"cogeo_gdal.tif"
) as cog_gdal:
assert cog_dst.shape == cog_gdal.shape
for i in range(0, 4):
assert round(cog_dst.bounds[i], 5) == round(cog_gdal.bounds[i], 5)
# Aligned Overviews
cog_translate(
raster_path_web,
"cogeo.tif",
web_profile,
quiet=True,
tms=web_tms,
config=config,
aligned_levels=4,
)
cog_translate(
raster_path_web,
"cogeo_gdal.tif",
web_profile,
quiet=True,
tms=web_tms,
config=config,
aligned_levels=4,
use_cog_driver=True,
)
with rasterio.open("cogeo.tif") as cog_dst, rasterio.open(
"cogeo_gdal.tif"
) as cog_gdal:
assert cog_dst.shape == cog_gdal.shape
for i in range(0, 4):
assert round(cog_dst.bounds[i], 5) == round(cog_gdal.bounds[i], 5)
@requires_gdal31
def test_gdal_zoom_options():
"""Test Web-Optimized GDAL with Zoom Options."""
runner = CliRunner()
with runner.isolated_filesystem():
web_tms = morecantile.tms.get("WebMercatorQuad")
web_profile = cog_profiles.get("raw")
web_profile.update({"blockxsize": 256, "blockysize": 256})
config = {"GDAL_TIFF_OVR_BLOCKSIZE": "256"}
cog_translate(
raster_path_web,
"cogeo_gdal.tif",
web_profile,
quiet=True,
tms=web_tms,
config=config,
use_cog_driver=True,
)
with rasterio.open("cogeo_gdal.tif") as out_dst:
tags = out_dst.tags()
assert tags["TILING_SCHEME_NAME"] == "WebMercatorQuad"
assert tags["TILING_SCHEME_ZOOM_LEVEL"] == "18"
assert "TILING_SCHEME_ALIGNED_LEVELS" not in tags
@requires_gdal35
def test_gdal_zoom_level_options():
"""Test Web-Optimized GDAL with Zoom Options."""
runner = CliRunner()
with runner.isolated_filesystem():
web_tms = morecantile.tms.get("WebMercatorQuad")
web_profile = cog_profiles.get("raw")
web_profile.update({"blockxsize": 256, "blockysize": 256})
config = {"GDAL_TIFF_OVR_BLOCKSIZE": "256"}
cog_translate(
raster_path_web,
"cogeo_gdal.tif",
web_profile,
quiet=True,
tms=web_tms,
config=config,
use_cog_driver=True,
zoom_level=19,
)
with rasterio.open("cogeo_gdal.tif") as out_dst:
tags = out_dst.tags()
assert tags["TILING_SCHEME_NAME"] == "WebMercatorQuad"
assert tags["TILING_SCHEME_ZOOM_LEVEL"] == "19"
assert "TILING_SCHEME_ALIGNED_LEVELS" not in tags
def test_web_optimized_arg_warning():
"""Verify a deprecation warning is emitted for `web_optimized` parameter"""
runner = CliRunner()
with runner.isolated_filesystem():
web_profile = cog_profiles.get("raw")
web_profile.update({"blockxsize": 256, "blockysize": 256})
with pytest.warns(
DeprecationWarning, match=r"^'web_optomized' option is deprecated.*"
):
cog_translate(
raster_path_north,
"cogeo.tif",
web_profile,
quiet=True,
web_optimized=True,
)
|