|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Cloud Optimized GeoTIFF: Overview (Pyramid) Generation\n", |
| 8 | + "\n", |
| 9 | + "Cloud Optimized GeoTIFFs (COGs) store internal overview images at reduced\n", |
| 10 | + "resolution, so map viewers and tiling servers can fetch the right zoom\n", |
| 11 | + "level without downloading the full file.\n", |
| 12 | + "\n", |
| 13 | + "xarray-spatial generates these overviews natively during `to_geotiff()`\n", |
| 14 | + "with `cog=True`. No GDAL or `gdaladdo` post-processing required.\n", |
| 15 | + "\n", |
| 16 | + "This notebook covers:\n", |
| 17 | + "- Writing a COG with automatic overviews\n", |
| 18 | + "- Choosing resampling methods\n", |
| 19 | + "- Specifying explicit overview levels\n", |
| 20 | + "- Verifying the result" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": null, |
| 26 | + "metadata": {}, |
| 27 | + "outputs": [], |
| 28 | + "source": [ |
| 29 | + "import numpy as np\n", |
| 30 | + "import xarray as xr\n", |
| 31 | + "import matplotlib.pyplot as plt\n", |
| 32 | + "\n", |
| 33 | + "from xrspatial.geotiff import open_geotiff, to_geotiff\n", |
| 34 | + "from xrspatial.geotiff._header import parse_header, parse_all_ifds" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "markdown", |
| 39 | + "metadata": {}, |
| 40 | + "source": [ |
| 41 | + "## Create synthetic terrain data\n", |
| 42 | + "\n", |
| 43 | + "A 256x256 elevation surface with some peaks, to give the overviews\n", |
| 44 | + "something visually meaningful to downsample." |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": null, |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [], |
| 52 | + "source": [ |
| 53 | + "rng = np.random.RandomState(42)\n", |
| 54 | + "y = np.linspace(45.0, 44.0, 256)\n", |
| 55 | + "x = np.linspace(-120.0, -119.0, 256)\n", |
| 56 | + "\n", |
| 57 | + "# Smooth terrain from Gaussian peaks\n", |
| 58 | + "xx, yy = np.meshgrid(x, y)\n", |
| 59 | + "terrain = (\n", |
| 60 | + " 800 * np.exp(-((xx + 119.5)**2 + (yy - 44.5)**2) / 0.02)\n", |
| 61 | + " + 600 * np.exp(-((xx + 119.3)**2 + (yy - 44.7)**2) / 0.05)\n", |
| 62 | + " + 100 * rng.rand(256, 256)\n", |
| 63 | + ").astype(np.float32)\n", |
| 64 | + "\n", |
| 65 | + "da = xr.DataArray(\n", |
| 66 | + " terrain, dims=['y', 'x'],\n", |
| 67 | + " coords={'y': y, 'x': x},\n", |
| 68 | + " attrs={'crs': 4326},\n", |
| 69 | + " name='elevation',\n", |
| 70 | + ")\n", |
| 71 | + "\n", |
| 72 | + "fig, ax = plt.subplots(figsize=(6, 5))\n", |
| 73 | + "da.plot(ax=ax, cmap='terrain')\n", |
| 74 | + "ax.set_title('Synthetic elevation')\n", |
| 75 | + "plt.tight_layout()\n", |
| 76 | + "plt.show()" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "markdown", |
| 81 | + "metadata": {}, |
| 82 | + "source": [ |
| 83 | + "## Write a COG with automatic overviews\n", |
| 84 | + "\n", |
| 85 | + "Pass `cog=True` and xarray-spatial handles the rest: it halves the\n", |
| 86 | + "dimensions until the smallest overview fits within a single tile,\n", |
| 87 | + "then writes all levels into the file with IFDs at the start (the COG\n", |
| 88 | + "layout requirement for efficient HTTP range requests)." |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": null, |
| 94 | + "metadata": {}, |
| 95 | + "outputs": [], |
| 96 | + "source": [ |
| 97 | + "import tempfile, os\n", |
| 98 | + "\n", |
| 99 | + "tmpdir = tempfile.mkdtemp(prefix='cog_demo_')\n", |
| 100 | + "cog_path = os.path.join(tmpdir, 'auto_overviews.tif')\n", |
| 101 | + "\n", |
| 102 | + "to_geotiff(da, cog_path, cog=True, compression='deflate')\n", |
| 103 | + "\n", |
| 104 | + "# Check how many IFDs (resolution levels) were written\n", |
| 105 | + "with open(cog_path, 'rb') as f:\n", |
| 106 | + " raw = f.read()\n", |
| 107 | + "\n", |
| 108 | + "header = parse_header(raw)\n", |
| 109 | + "ifds = parse_all_ifds(raw, header)\n", |
| 110 | + "for i, ifd in enumerate(ifds):\n", |
| 111 | + " label = 'Full resolution' if i == 0 else f'Overview {i}'\n", |
| 112 | + " print(f'{label}: {ifd.width} x {ifd.height}')" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "markdown", |
| 117 | + "metadata": {}, |
| 118 | + "source": [ |
| 119 | + "## Resampling methods\n", |
| 120 | + "\n", |
| 121 | + "Different data types need different resampling:\n", |
| 122 | + "- **mean** (default): continuous data like elevation or temperature\n", |
| 123 | + "- **nearest**: categorical or index data (land cover classes)\n", |
| 124 | + "- **mode**: majority-class downsampling for classified rasters\n", |
| 125 | + "- **min** / **max** / **median**: when you need conservative bounds" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "code", |
| 130 | + "execution_count": null, |
| 131 | + "metadata": {}, |
| 132 | + "outputs": [], |
| 133 | + "source": [ |
| 134 | + "methods = ['mean', 'nearest', 'min', 'max']\n", |
| 135 | + "fig, axes = plt.subplots(1, len(methods), figsize=(14, 3))\n", |
| 136 | + "\n", |
| 137 | + "for ax, method in zip(axes, methods):\n", |
| 138 | + " path = os.path.join(tmpdir, f'cog_{method}.tif')\n", |
| 139 | + " to_geotiff(da, path, cog=True, compression='deflate',\n", |
| 140 | + " overview_resampling=method)\n", |
| 141 | + "\n", |
| 142 | + " # Read back the first overview level\n", |
| 143 | + " result = open_geotiff(path, overview_level=1)\n", |
| 144 | + " result.plot(ax=ax, cmap='terrain', add_colorbar=False)\n", |
| 145 | + " ax.set_title(f'{method} ({result.shape[0]}x{result.shape[1]})')\n", |
| 146 | + " ax.set_xlabel('')\n", |
| 147 | + " ax.set_ylabel('')\n", |
| 148 | + "\n", |
| 149 | + "plt.suptitle('Overview level 1 by resampling method', y=1.02)\n", |
| 150 | + "plt.tight_layout()\n", |
| 151 | + "plt.show()" |
| 152 | + ] |
| 153 | + }, |
| 154 | + { |
| 155 | + "cell_type": "markdown", |
| 156 | + "metadata": {}, |
| 157 | + "source": [ |
| 158 | + "## Explicit overview levels\n", |
| 159 | + "\n", |
| 160 | + "For fine-grained control, pass `overview_levels` as a list of\n", |
| 161 | + "decimation factors. Each factor halves the previous level once." |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "code", |
| 166 | + "execution_count": null, |
| 167 | + "metadata": {}, |
| 168 | + "outputs": [], |
| 169 | + "source": [ |
| 170 | + "explicit_path = os.path.join(tmpdir, 'explicit_levels.tif')\n", |
| 171 | + "to_geotiff(da, explicit_path, cog=True, compression='deflate',\n", |
| 172 | + " overview_levels=[2, 4, 8])\n", |
| 173 | + "\n", |
| 174 | + "with open(explicit_path, 'rb') as f:\n", |
| 175 | + " raw = f.read()\n", |
| 176 | + "\n", |
| 177 | + "header = parse_header(raw)\n", |
| 178 | + "ifds = parse_all_ifds(raw, header)\n", |
| 179 | + "for i, ifd in enumerate(ifds):\n", |
| 180 | + " label = 'Full resolution' if i == 0 else f'Overview {i}'\n", |
| 181 | + " print(f'{label}: {ifd.width} x {ifd.height}')" |
| 182 | + ] |
| 183 | + }, |
| 184 | + { |
| 185 | + "cell_type": "markdown", |
| 186 | + "metadata": {}, |
| 187 | + "source": [ |
| 188 | + "## Verify round-trip\n", |
| 189 | + "\n", |
| 190 | + "Full-resolution pixel values are preserved through the COG write." |
| 191 | + ] |
| 192 | + }, |
| 193 | + { |
| 194 | + "cell_type": "code", |
| 195 | + "execution_count": null, |
| 196 | + "metadata": {}, |
| 197 | + "outputs": [], |
| 198 | + "source": [ |
| 199 | + "result = open_geotiff(cog_path)\n", |
| 200 | + "max_diff = float(np.max(np.abs(result.values - terrain)))\n", |
| 201 | + "print(f'Max pixel difference: {max_diff}')\n", |
| 202 | + "assert max_diff < 1e-5, 'Values should round-trip exactly'" |
| 203 | + ] |
| 204 | + }, |
| 205 | + { |
| 206 | + "cell_type": "code", |
| 207 | + "execution_count": null, |
| 208 | + "metadata": {}, |
| 209 | + "outputs": [], |
| 210 | + "source": [ |
| 211 | + "# Clean up\n", |
| 212 | + "import shutil\n", |
| 213 | + "shutil.rmtree(tmpdir)" |
| 214 | + ] |
| 215 | + } |
| 216 | + ], |
| 217 | + "metadata": { |
| 218 | + "kernelspec": { |
| 219 | + "display_name": "Python 3", |
| 220 | + "language": "python", |
| 221 | + "name": "python3" |
| 222 | + }, |
| 223 | + "language_info": { |
| 224 | + "name": "python", |
| 225 | + "version": "3.11.0" |
| 226 | + } |
| 227 | + }, |
| 228 | + "nbformat": 4, |
| 229 | + "nbformat_minor": 4 |
| 230 | +} |
0 commit comments