|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "0", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Xarray's Rasterio backend\n", |
| 9 | + "\n", |
| 10 | + "In this lesson, we will learn how to use xarray's rasterio backend engine to open GeoTIFF rasters. By the end of the lesson, we will be able to:\n", |
| 11 | + "\n", |
| 12 | + ":::{admonition} Learning Goals\n", |
| 13 | + "- Learn about the GeoTIFF format\n", |
| 14 | + "- Lean about xarray's \"rasterio\" backend and the \"rioxarray\" accessor\n", |
| 15 | + "- Learn how to read and plot GeoTIFF files with xarray\n", |
| 16 | + "- Explore how to perform reprojection operations on rasters\n", |
| 17 | + ":::\n", |
| 18 | + "\n", |
| 19 | + "## What are GeoTIFFs?\n", |
| 20 | + "\n", |
| 21 | + "The TIFF (Tagged Image File Format) format is a metadata rich image format for raster data. GeoTIFF (Geographic Tagged Image File Format) files are TIFF files that use georeferencing information (such as map projection and coordinate systems) as metadata. A GeoTIFF file with a single band contains 2D raster data for a single characteristic (i.e., variable) and maps to a geographic region. A GeoTIFF file can have multiple bands that all map to the same geographic region.\n", |
| 22 | + "\n", |
| 23 | + "\n", |
| 24 | + "\n", |
| 25 | + "## Rasterio and Rioxarray Backends\n", |
| 26 | + "\n", |
| 27 | + "[Rasterio](https://rasterio.readthedocs.io/en/stable/intro.html) is a geospatial raster library that expresses GDAL ([Geospatial Data Abstraction Library](http://gdal.org)) data model with a Python API and CLI. [Rioxarray](https://corteva.github.io/rioxarray/stable/readme.html) is a wrapper around the rasterio library, that also extends the xarray api with the *rio* accessor. When you open a GeoTIFF file with the \"rasterio\" engine it returns the data as an xarray object with access to methods from the *rio* accessor.\n", |
| 28 | + "\n" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "markdown", |
| 33 | + "id": "1", |
| 34 | + "metadata": {}, |
| 35 | + "source": [ |
| 36 | + "## Reading a GeoTIFF\n", |
| 37 | + "\n", |
| 38 | + "Xarray's \"rasterio\" backend supports reading GeoTIFFs.\n", |
| 39 | + "\n", |
| 40 | + "Lets read a GeoTIFF file as an `xr.Dataset` by selecting `engine='rasterio'`" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": null, |
| 46 | + "id": "2", |
| 47 | + "metadata": {}, |
| 48 | + "outputs": [], |
| 49 | + "source": [ |
| 50 | + "import xarray as xr" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": null, |
| 56 | + "id": "3", |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [], |
| 59 | + "source": [ |
| 60 | + "rds = xr.tutorial.open_dataset(\"RGB.byte.tif\", engine=\"rasterio\")" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "markdown", |
| 65 | + "id": "4", |
| 66 | + "metadata": {}, |
| 67 | + "source": [ |
| 68 | + ":::{note} We can also read GeoTIFFs with `rioxarray.open_rasterio`.\n", |
| 69 | + "\n", |
| 70 | + "The following code snippet opens a GeoTIFF and returns a `DataArray` object\n", |
| 71 | + "```python\n", |
| 72 | + "import rioxarray\n", |
| 73 | + "rioxarray.open_rasterio(\"RGB.byte.tif\")\n", |
| 74 | + "```\n", |
| 75 | + "\n", |
| 76 | + ":::" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "markdown", |
| 81 | + "id": "5", |
| 82 | + "metadata": {}, |
| 83 | + "source": [ |
| 84 | + "## Raster bands\n", |
| 85 | + "\n", |
| 86 | + "GeoTIFFS can have multiple bands each representing a range or band in the electromagnetic spectrum. Arrays stored within bands in xarray are data variables and `DataArray`'s objects in the the Xarray Data Model." |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "markdown", |
| 91 | + "id": "6", |
| 92 | + "metadata": {}, |
| 93 | + "source": [ |
| 94 | + "Lets get the `DataArray` object (or variable) from our dataset. The variable name is \"band_data\"." |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": null, |
| 100 | + "id": "7", |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [], |
| 103 | + "source": [ |
| 104 | + "rda = rds[\"band_data\"]\n", |
| 105 | + "rda" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "markdown", |
| 110 | + "id": "8", |
| 111 | + "metadata": {}, |
| 112 | + "source": [ |
| 113 | + "Lets try getting the total number of bands for this GeoTIFF. Since `rioxarray` extends xarray with the *rio* accessor we can this accessor be able use many of the builtin `rasterio` methods." |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "code", |
| 118 | + "execution_count": null, |
| 119 | + "id": "9", |
| 120 | + "metadata": {}, |
| 121 | + "outputs": [], |
| 122 | + "source": [ |
| 123 | + "rda.rio.count" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "markdown", |
| 128 | + "id": "10", |
| 129 | + "metadata": {}, |
| 130 | + "source": [ |
| 131 | + "### Selection by bands\n", |
| 132 | + "We can also select by bands. Since there are 3 bands in this dataset we can return raster array for the first band of this `xr.DataArray` by with indexing\n", |
| 133 | + "\n", |
| 134 | + "Lets get the first band of this dataset and try plotting it" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "code", |
| 139 | + "execution_count": null, |
| 140 | + "id": "11", |
| 141 | + "metadata": {}, |
| 142 | + "outputs": [], |
| 143 | + "source": [ |
| 144 | + "rda[0].plot(cmap=\"pink\")" |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "markdown", |
| 149 | + "id": "12", |
| 150 | + "metadata": {}, |
| 151 | + "source": [ |
| 152 | + "## Bounds\n", |
| 153 | + "With `.rio.bounds()` we can get the spatial bounding box of our `DataArray`" |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "cell_type": "code", |
| 158 | + "execution_count": null, |
| 159 | + "id": "13", |
| 160 | + "metadata": {}, |
| 161 | + "outputs": [], |
| 162 | + "source": [ |
| 163 | + "rda.rio.bounds()" |
| 164 | + ] |
| 165 | + }, |
| 166 | + { |
| 167 | + "cell_type": "markdown", |
| 168 | + "id": "14", |
| 169 | + "metadata": {}, |
| 170 | + "source": [ |
| 171 | + "## Transformation\n", |
| 172 | + "\n", |
| 173 | + "With `.rio.transform()` we can get the affine transformation matrix that maps pixel locations in (col, row) coordinates to (x, y) spatial positions.\n" |
| 174 | + ] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "code", |
| 178 | + "execution_count": null, |
| 179 | + "id": "15", |
| 180 | + "metadata": {}, |
| 181 | + "outputs": [], |
| 182 | + "source": [ |
| 183 | + "rda.rio.transform()" |
| 184 | + ] |
| 185 | + }, |
| 186 | + { |
| 187 | + "cell_type": "markdown", |
| 188 | + "id": "16", |
| 189 | + "metadata": {}, |
| 190 | + "source": [ |
| 191 | + "### Coordinate Reference System (CRS)\n", |
| 192 | + "We `rio.crs` we can get the CRS of our raster and reproject our raster." |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "cell_type": "code", |
| 197 | + "execution_count": null, |
| 198 | + "id": "17", |
| 199 | + "metadata": {}, |
| 200 | + "outputs": [], |
| 201 | + "source": [ |
| 202 | + "rda.rio.crs" |
| 203 | + ] |
| 204 | + }, |
| 205 | + { |
| 206 | + "cell_type": "markdown", |
| 207 | + "id": "18", |
| 208 | + "metadata": {}, |
| 209 | + "source": [ |
| 210 | + "### Reprojection\n", |
| 211 | + "We can also reproject our raster from one CRS to another.\n", |
| 212 | + "\n", |
| 213 | + "Lets reproject our raster from \"EPSG:6326\" to \"EPSG:32612\"" |
| 214 | + ] |
| 215 | + }, |
| 216 | + { |
| 217 | + "cell_type": "code", |
| 218 | + "execution_count": null, |
| 219 | + "id": "19", |
| 220 | + "metadata": {}, |
| 221 | + "outputs": [], |
| 222 | + "source": [ |
| 223 | + "rda_reproj = rda.rio.reproject(\"EPSG:32612\")" |
| 224 | + ] |
| 225 | + }, |
| 226 | + { |
| 227 | + "cell_type": "markdown", |
| 228 | + "id": "20", |
| 229 | + "metadata": {}, |
| 230 | + "source": [ |
| 231 | + ":::{note}\n", |
| 232 | + "We have to update our CRS system to the new projection with `rio.write_crs`. We set `inplace=True` to write the CRS to the existing dataset." |
| 233 | + ] |
| 234 | + }, |
| 235 | + { |
| 236 | + "cell_type": "code", |
| 237 | + "execution_count": null, |
| 238 | + "id": "21", |
| 239 | + "metadata": {}, |
| 240 | + "outputs": [], |
| 241 | + "source": [ |
| 242 | + "rda_reproj.rio.write_crs(\"EPSG:32612\", inplace=True)" |
| 243 | + ] |
| 244 | + }, |
| 245 | + { |
| 246 | + "cell_type": "markdown", |
| 247 | + "id": "22", |
| 248 | + "metadata": {}, |
| 249 | + "source": [ |
| 250 | + "## Exercise" |
| 251 | + ] |
| 252 | + }, |
| 253 | + { |
| 254 | + "cell_type": "markdown", |
| 255 | + "id": "23", |
| 256 | + "metadata": {}, |
| 257 | + "source": [ |
| 258 | + "::::{admonition} Exercise\n", |
| 259 | + ":class: tip\n", |
| 260 | + "\n", |
| 261 | + "Can you reproject and update the CRS of second band of data to 'ESPG:3857' and plot your results?\n", |
| 262 | + "\n", |
| 263 | + ":::{admonition} Solution\n", |
| 264 | + ":class: dropdown\n", |
| 265 | + "\n", |
| 266 | + "```python\n", |
| 267 | + "rda[1].rio.reproject(\"EPSG:3857\").rio.write_crs(\"EPSG:3857\", inplace=True).plot()\n", |
| 268 | + "```\n", |
| 269 | + ":::\n", |
| 270 | + "::::\n" |
| 271 | + ] |
| 272 | + } |
| 273 | + ], |
| 274 | + "metadata": { |
| 275 | + "language_info": { |
| 276 | + "codemirror_mode": { |
| 277 | + "name": "ipython", |
| 278 | + "version": 3 |
| 279 | + }, |
| 280 | + "file_extension": ".py", |
| 281 | + "mimetype": "text/x-python", |
| 282 | + "name": "python", |
| 283 | + "nbconvert_exporter": "python", |
| 284 | + "pygments_lexer": "ipython3" |
| 285 | + } |
| 286 | + }, |
| 287 | + "nbformat": 4, |
| 288 | + "nbformat_minor": 5 |
| 289 | +} |
0 commit comments