|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Raster Resampling\n", |
| 8 | + "\n", |
| 9 | + "The `resample` function changes a raster's resolution (cell size) without\n", |
| 10 | + "changing its CRS. This is the operation you'd reach for when you need to\n", |
| 11 | + "match two rasters to a common grid or reduce a raster's memory footprint\n", |
| 12 | + "before analysis.\n", |
| 13 | + "\n", |
| 14 | + "**Methods**:\n", |
| 15 | + "\n", |
| 16 | + "| Method | Direction | Best for |\n", |
| 17 | + "|--------|-----------|----------|\n", |
| 18 | + "| `nearest` | up/down | Categorical data, fast preview |\n", |
| 19 | + "| `bilinear` | up/down | Smooth continuous surfaces |\n", |
| 20 | + "| `cubic` | up/down | High-quality continuous surfaces |\n", |
| 21 | + "| `average` | down only | Aggregating high-res to low-res |\n", |
| 22 | + "| `min`, `max` | down only | Extremes within each output cell |\n", |
| 23 | + "| `median` | down only | Robust centre, ignores outliers |\n", |
| 24 | + "| `mode` | down only | Majority class in categorical rasters |" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": null, |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "import numpy as np\n", |
| 34 | + "import xarray as xr\n", |
| 35 | + "import matplotlib.pyplot as plt\n", |
| 36 | + "\n", |
| 37 | + "from xrspatial import resample\n", |
| 38 | + "from xrspatial.terrain import generate_terrain" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "markdown", |
| 43 | + "metadata": {}, |
| 44 | + "source": [ |
| 45 | + "## Generate synthetic terrain" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": null, |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "dem = generate_terrain(width=200, height=200)\n", |
| 55 | + "# Assign a regular coordinate grid\n", |
| 56 | + "dem = dem.assign_coords(\n", |
| 57 | + " y=np.linspace(100, 0, dem.sizes['y']),\n", |
| 58 | + " x=np.linspace(0, 100, dem.sizes['x']),\n", |
| 59 | + ")\n", |
| 60 | + "dem.attrs['res'] = (0.5, 0.5)\n", |
| 61 | + "\n", |
| 62 | + "fig, ax = plt.subplots(figsize=(6, 5))\n", |
| 63 | + "dem.plot(ax=ax, cmap='terrain')\n", |
| 64 | + "ax.set_title(f'Original DEM ({dem.shape[0]}x{dem.shape[1]}, res={dem.attrs[\"res\"][0]:.1f}m)')\n", |
| 65 | + "plt.tight_layout()" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "markdown", |
| 70 | + "metadata": {}, |
| 71 | + "source": [ |
| 72 | + "## Downsample with `scale_factor`" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "code", |
| 77 | + "execution_count": null, |
| 78 | + "metadata": {}, |
| 79 | + "outputs": [], |
| 80 | + "source": [ |
| 81 | + "down = resample(dem, scale_factor=0.25, method='bilinear')\n", |
| 82 | + "\n", |
| 83 | + "fig, axes = plt.subplots(1, 2, figsize=(12, 5))\n", |
| 84 | + "dem.plot(ax=axes[0], cmap='terrain')\n", |
| 85 | + "axes[0].set_title(f'Original ({dem.shape[0]}x{dem.shape[1]})')\n", |
| 86 | + "down.plot(ax=axes[1], cmap='terrain')\n", |
| 87 | + "axes[1].set_title(f'Downsampled 4x ({down.shape[0]}x{down.shape[1]})')\n", |
| 88 | + "plt.tight_layout()" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "markdown", |
| 93 | + "metadata": {}, |
| 94 | + "source": [ |
| 95 | + "## Upsample with `target_resolution`" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": null, |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [], |
| 103 | + "source": [ |
| 104 | + "up = resample(down, target_resolution=0.5, method='cubic')\n", |
| 105 | + "\n", |
| 106 | + "fig, axes = plt.subplots(1, 2, figsize=(12, 5))\n", |
| 107 | + "down.plot(ax=axes[0], cmap='terrain')\n", |
| 108 | + "axes[0].set_title(f'Coarse ({down.shape[0]}x{down.shape[1]})')\n", |
| 109 | + "up.plot(ax=axes[1], cmap='terrain')\n", |
| 110 | + "axes[1].set_title(f'Upsampled to 0.5m ({up.shape[0]}x{up.shape[1]})')\n", |
| 111 | + "plt.tight_layout()" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "markdown", |
| 116 | + "metadata": {}, |
| 117 | + "source": [ |
| 118 | + "## Compare resampling methods" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "execution_count": null, |
| 124 | + "metadata": {}, |
| 125 | + "outputs": [], |
| 126 | + "source": [ |
| 127 | + "methods = ['nearest', 'bilinear', 'cubic', 'average']\n", |
| 128 | + "fig, axes = plt.subplots(1, 4, figsize=(16, 4))\n", |
| 129 | + "\n", |
| 130 | + "for ax, method in zip(axes, methods):\n", |
| 131 | + " out = resample(dem, scale_factor=0.1, method=method)\n", |
| 132 | + " out.plot(ax=ax, cmap='terrain', add_colorbar=False)\n", |
| 133 | + " ax.set_title(method)\n", |
| 134 | + " ax.set_aspect('equal')\n", |
| 135 | + "\n", |
| 136 | + "plt.suptitle('Downsample 10x with different methods', y=1.02)\n", |
| 137 | + "plt.tight_layout()" |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "markdown", |
| 142 | + "metadata": {}, |
| 143 | + "source": [ |
| 144 | + "## Categorical raster with `mode`" |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "code", |
| 149 | + "execution_count": null, |
| 150 | + "metadata": {}, |
| 151 | + "outputs": [], |
| 152 | + "source": [ |
| 153 | + "from xrspatial import equal_interval\n", |
| 154 | + "\n", |
| 155 | + "# Classify elevation into 5 zones\n", |
| 156 | + "classes = equal_interval(dem, k=5)\n", |
| 157 | + "classes.attrs = dem.attrs.copy()\n", |
| 158 | + "classes = classes.assign_coords(dem.coords)\n", |
| 159 | + "\n", |
| 160 | + "# Downsample: mode preserves class boundaries\n", |
| 161 | + "classes_down = resample(classes.astype('float32'),\n", |
| 162 | + " scale_factor=0.2, method='mode')\n", |
| 163 | + "\n", |
| 164 | + "fig, axes = plt.subplots(1, 2, figsize=(12, 5))\n", |
| 165 | + "classes.plot(ax=axes[0], cmap='Set2')\n", |
| 166 | + "axes[0].set_title(f'Classes ({classes.shape[0]}x{classes.shape[1]})')\n", |
| 167 | + "classes_down.plot(ax=axes[1], cmap='Set2')\n", |
| 168 | + "axes[1].set_title(f'Mode downsample ({classes_down.shape[0]}x{classes_down.shape[1]})')\n", |
| 169 | + "plt.tight_layout()" |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | + "cell_type": "markdown", |
| 174 | + "metadata": {}, |
| 175 | + "source": [ |
| 176 | + "## Works with Dask" |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "code", |
| 181 | + "execution_count": null, |
| 182 | + "metadata": {}, |
| 183 | + "outputs": [], |
| 184 | + "source": [ |
| 185 | + "import dask.array as da\n", |
| 186 | + "\n", |
| 187 | + "dask_dem = dem.copy()\n", |
| 188 | + "dask_dem.data = da.from_array(dem.values, chunks=(100, 100))\n", |
| 189 | + "\n", |
| 190 | + "result = resample(dask_dem, scale_factor=0.5, method='bilinear')\n", |
| 191 | + "print(f'Input: {dask_dem.shape} (dask, chunks={dask_dem.data.chunksize})')\n", |
| 192 | + "print(f'Output: {result.shape} (dask, chunks={result.data.chunksize})')\n", |
| 193 | + "print(f'Computed shape: {result.compute().shape}')" |
| 194 | + ] |
| 195 | + } |
| 196 | + ], |
| 197 | + "metadata": { |
| 198 | + "kernelspec": { |
| 199 | + "display_name": "Python 3", |
| 200 | + "language": "python", |
| 201 | + "name": "python3" |
| 202 | + }, |
| 203 | + "language_info": { |
| 204 | + "name": "python", |
| 205 | + "version": "3.14.0" |
| 206 | + } |
| 207 | + }, |
| 208 | + "nbformat": 4, |
| 209 | + "nbformat_minor": 4 |
| 210 | +} |
0 commit comments