From 0e5f5ab5ddd607929272c0ac2f78b00f418d8b02 Mon Sep 17 00:00:00 2001 From: Olufunke Awowale Date: Sat, 11 Jul 2026 19:58:43 -0400 Subject: [PATCH 1/4] add rasterio backend --- advanced/backends/rasterio_backend.ipynb | 270 +++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 advanced/backends/rasterio_backend.ipynb diff --git a/advanced/backends/rasterio_backend.ipynb b/advanced/backends/rasterio_backend.ipynb new file mode 100644 index 00000000..b0d30e33 --- /dev/null +++ b/advanced/backends/rasterio_backend.ipynb @@ -0,0 +1,270 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "0", + "metadata": {}, + "source": [ + "# Xarray's Rasterio backend\n", + "\n", + "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", + "\n", + ":::{admonition} Learning Goals\n", + "- Learn about the GeoTIFF format\n", + "- Lean about xarray's \"rasterio\" backend and the \"rioxarray\" accessor\n", + "- Learn how to read, write and plot GeoTIFF files with xarray\n", + "- Explore how to perform reprojection operations on rasters\n", + ":::\n", + "\n", + "## What are GeoTIFFs?\n", + "\n", + "GeoTIFFS (Geographic Tagged Image File Format (GeoTIFF)) are an image format based on the TIFF (Tagged Image File Format), that contain georeferencing information embedded within a raster image. Rasters in GeoTIFFs are made up of gridded pixels (or arrays) that map to a geographic region. The pixels values represent a characteristic (variable) for the geographic region it maps to.\n", + "\n", + "![Raster_Image](https://docs.qgis.org/3.44/en/_images/raster_dataset.png)\n", + "\n", + "## Rasterio and Rioxarray Backends\n", + "\n", + "[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", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "1", + "metadata": {}, + "source": [ + "## Reading a GeoTIFF\n", + "\n", + "Xarray's \"rasterio\" backend supports reading GeoTIFFs.\n", + "\n", + "Lets read a GeoTIFF file as an `xr.DataArray` by selecting `engine='rasterio'`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2", + "metadata": {}, + "outputs": [], + "source": [ + "import xarray as xr" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3", + "metadata": {}, + "outputs": [], + "source": [ + "rds = xr.open_dataarray(\"../../../xarray-data/RGB.byte.tif\", engine=\"rasterio\")" + ] + }, + { + "cell_type": "markdown", + "id": "4", + "metadata": {}, + "source": [ + ":::{note} We can also read GeoTIFFs with `rioxarray` a wrapper around the `rasterio` library.\n", + "Both of these options return an `xr.DataArray`\n", + ":::" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5", + "metadata": {}, + "outputs": [], + "source": [ + "import rioxarray\n", + "\n", + "rioxarray.open_rasterio(\"../../../xarray-data/RGB.byte.tif\")" + ] + }, + { + "cell_type": "markdown", + "id": "6", + "metadata": {}, + "source": [ + "## Raster bands\n", + "\n", + "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.\n", + "\n", + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7", + "metadata": {}, + "outputs": [], + "source": [ + "rds.rio.count" + ] + }, + { + "cell_type": "markdown", + "id": "8", + "metadata": {}, + "source": [ + "## Selection by bands\n", + "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", + "\n", + "Lets get the first band of this dataset and try plotting it" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9", + "metadata": {}, + "outputs": [], + "source": [ + "rds[0].plot(cmap=\"pink\")" + ] + }, + { + "cell_type": "markdown", + "id": "10", + "metadata": {}, + "source": [ + "## Georeferencing\n", + "With our *rio* accessor we can get the spatial bounds for out dataset and affine transformations" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "11", + "metadata": {}, + "outputs": [], + "source": [ + "rds.rio.bounds()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "12", + "metadata": {}, + "outputs": [], + "source": [ + "rds.rio.transform()" + ] + }, + { + "cell_type": "markdown", + "id": "13", + "metadata": {}, + "source": [ + ":::{note}\n", + "The affine transformation matrix maps pixel locations in (col, row) coordinates to (x, y) spatial positions. The product of this matrix and (0, 0), the column and row coordinates of the upper left corner of the dataset, is the spatial position of the upper left corner.\n", + ":" + ] + }, + { + "cell_type": "markdown", + "id": "14", + "metadata": {}, + "source": [ + "### Coordinate Reference System (CRS)\n", + "We can also get the Coordinate Reference System for raster array and reproject them." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "15", + "metadata": {}, + "outputs": [], + "source": [ + "rds.rio.crs" + ] + }, + { + "cell_type": "markdown", + "id": "16", + "metadata": {}, + "source": [ + "### Reprojection\n", + "We can also reproject our raster from one CRS to another.\n", + "\n", + "Lets reproject our raster from \"EPSG:6326\" to \"EPSG:32612\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "17", + "metadata": {}, + "outputs": [], + "source": [ + "rds_reproj = rds.rio.reproject(\"EPSG:32612\")" + ] + }, + { + "cell_type": "markdown", + "id": "18", + "metadata": {}, + "source": [ + ":::{note}\n", + "We have to update our CRS system to the new projection with `rio.write_crs`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "19", + "metadata": {}, + "outputs": [], + "source": [ + "rds_reproj.rio.write_crs(\"EPSG:32612\", inplace=True)" + ] + }, + { + "cell_type": "markdown", + "id": "20", + "metadata": {}, + "source": [ + "## Exercise" + ] + }, + { + "cell_type": "markdown", + "id": "21", + "metadata": {}, + "source": [ + "::::{admonition} Exercise\n", + ":class: tip\n", + "\n", + "Can you reproject and update the CRS of second band of data to 'ESPG:3857' and plot your results?\n", + "\n", + ":::{admonition} Solution\n", + ":class: dropdown\n", + "\n", + "```python\n", + "rds[1].rio.reproject(\"EPSG:3857\").rio.write_crs(\"EPSG:3857\", inplace=True).plot()\n", + "```\n", + ":::\n", + "::::\n" + ] + } + ], + "metadata": { + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From ef01abbf72c35c827eba6d350e4ad5ee34828804 Mon Sep 17 00:00:00 2001 From: Olufunke Awowale Date: Sat, 11 Jul 2026 20:32:54 -0400 Subject: [PATCH 2/4] use tutorial --- _toc.yml | 1 + advanced/backends/rasterio_backend.ipynb | 1395 +++++++++++++++++++++- 2 files changed, 1347 insertions(+), 49 deletions(-) diff --git a/_toc.yml b/_toc.yml index cfe7f887..75429193 100644 --- a/_toc.yml +++ b/_toc.yml @@ -86,6 +86,7 @@ parts: sections: - file: advanced/backends/1.Backend_without_Lazy_Loading.ipynb - file: advanced/backends/2.Backend_with_Lazy_Loading.ipynb + - file: advanced/backends/rasterio_backend.ipynb - file: advanced/accessors/accessors.md sections: - file: advanced/accessors/01_accessor_examples.ipynb diff --git a/advanced/backends/rasterio_backend.ipynb b/advanced/backends/rasterio_backend.ipynb index b0d30e33..ec838dbf 100644 --- a/advanced/backends/rasterio_backend.ipynb +++ b/advanced/backends/rasterio_backend.ipynb @@ -12,7 +12,7 @@ ":::{admonition} Learning Goals\n", "- Learn about the GeoTIFF format\n", "- Lean about xarray's \"rasterio\" backend and the \"rioxarray\" accessor\n", - "- Learn how to read, write and plot GeoTIFF files with xarray\n", + "- Learn how to read and plot GeoTIFF files with xarray\n", "- Explore how to perform reprojection operations on rasters\n", ":::\n", "\n", @@ -37,7 +37,7 @@ "\n", "Xarray's \"rasterio\" backend supports reading GeoTIFFs.\n", "\n", - "Lets read a GeoTIFF file as an `xr.DataArray` by selecting `engine='rasterio'`" + "Lets read a GeoTIFF file as an `xr.Dataset` by selecting `engine='rasterio'`" ] }, { @@ -52,12 +52,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "id": "3", "metadata": {}, "outputs": [], "source": [ - "rds = xr.open_dataarray(\"../../../xarray-data/RGB.byte.tif\", engine=\"rasterio\")" + "rds = xr.tutorial.open_dataset(\"RGB.byte.tif\", engine=\"rasterio\")" ] }, { @@ -65,43 +65,655 @@ "id": "4", "metadata": {}, "source": [ - ":::{note} We can also read GeoTIFFs with `rioxarray` a wrapper around the `rasterio` library.\n", - "Both of these options return an `xr.DataArray`\n", + ":::{note} We can also read GeoTIFFs with `rioxarray.open_rasterio`.\n", + "\n", + "The following code snippet opens a GeoTIFF and returns a `DataArray` object\n", + "```python\n", + "import rioxarray\n", + "rioxarray.open_rasterio(\"RGB.byte.tif\")\n", + "```\n", + "\n", ":::" ] }, { - "cell_type": "code", - "execution_count": null, - "id": "5", + "cell_type": "markdown", + "id": "6", "metadata": {}, - "outputs": [], "source": [ - "import rioxarray\n", + "## Raster bands\n", "\n", - "rioxarray.open_rasterio(\"../../../xarray-data/RGB.byte.tif\")" + "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." ] }, { "cell_type": "markdown", - "id": "6", + "id": "d7f5e839", + "metadata": {}, + "source": [ + "Lets get the `DataArray` object (or variable) from our dataset. The variable name is \"band_data\"." + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "3bb32764", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
<xarray.DataArray 'band_data' (band: 3, y: 718, x: 791)> Size: 7MB\n",
+       "[1703814 values with dtype=float32]\n",
+       "Coordinates:\n",
+       "  * band         (band) int64 24B 1 2 3\n",
+       "  * y            (y) float64 6kB 2.827e+06 2.826e+06 ... 2.612e+06 2.612e+06\n",
+       "  * x            (x) float64 6kB 1.021e+05 1.024e+05 ... 3.389e+05 3.392e+05\n",
+       "    spatial_ref  int64 8B ...\n",
+       "Attributes:\n",
+       "    AREA_OR_POINT:       Area\n",
+       "    STATISTICS_MAXIMUM:  255\n",
+       "    STATISTICS_MEAN:     29.947726688477\n",
+       "    STATISTICS_MINIMUM:  0\n",
+       "    STATISTICS_STDDEV:   52.340921626611
" + ], + "text/plain": [ + " Size: 7MB\n", + "[1703814 values with dtype=float32]\n", + "Coordinates:\n", + " * band (band) int64 24B 1 2 3\n", + " * y (y) float64 6kB 2.827e+06 2.826e+06 ... 2.612e+06 2.612e+06\n", + " * x (x) float64 6kB 1.021e+05 1.024e+05 ... 3.389e+05 3.392e+05\n", + " spatial_ref int64 8B ...\n", + "Attributes:\n", + " AREA_OR_POINT: Area\n", + " STATISTICS_MAXIMUM: 255\n", + " STATISTICS_MEAN: 29.947726688477\n", + " STATISTICS_MINIMUM: 0\n", + " STATISTICS_STDDEV: 52.340921626611" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rda = rds['band_data']\n", + "rda" + ] + }, + { + "cell_type": "markdown", + "id": "e0272e99", "metadata": {}, "source": [ - "## Raster bands\n", - "\n", - "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.\n", - "\n", "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." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "id": "7", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "rds.rio.count" + "rda.rio.count" ] }, { @@ -109,7 +721,7 @@ "id": "8", "metadata": {}, "source": [ - "## Selection by bands\n", + "### Selection by bands\n", "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", "\n", "Lets get the first band of this dataset and try plotting it" @@ -122,46 +734,69 @@ "metadata": {}, "outputs": [], "source": [ - "rds[0].plot(cmap=\"pink\")" + "rda[0].plot(cmap=\"pink\")" ] }, { "cell_type": "markdown", - "id": "10", + "id": "37b29994", "metadata": {}, "source": [ - "## Georeferencing\n", - "With our *rio* accessor we can get the spatial bounds for out dataset and affine transformations" + "## Bounds\n", + "With `.rio.bounds()` we can get the spatial bounding box of our `DataArray`" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "id": "11", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(101985.0, 2611484.9999999995, 339314.99999999994, 2826915.0)" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "rds.rio.bounds()" + "rda.rio.bounds()" ] }, { - "cell_type": "code", - "execution_count": null, - "id": "12", + "cell_type": "markdown", + "id": "ee6fc5c8", "metadata": {}, - "outputs": [], "source": [ - "rds.rio.transform()" + "## Transformation\n", + "\n", + "With `.rio.transform()` we can get the affine transformation matrix that maps pixel locations in (col, row) coordinates to (x, y) spatial positions.\n" ] }, { - "cell_type": "markdown", - "id": "13", + "cell_type": "code", + "execution_count": 49, + "id": "12", "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Affine(300.0379266750948, 0.0, 101985.0,\n", + " 0.0, -300.041782729805, 2826915.0)" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - ":::{note}\n", - "The affine transformation matrix maps pixel locations in (col, row) coordinates to (x, y) spatial positions. The product of this matrix and (0, 0), the column and row coordinates of the upper left corner of the dataset, is the spatial position of the upper left corner.\n", - ":" + "rda.rio.transform()" ] }, { @@ -170,17 +805,28 @@ "metadata": {}, "source": [ "### Coordinate Reference System (CRS)\n", - "We can also get the Coordinate Reference System for raster array and reproject them." + "We `rio.crs` we can get the CRS of our raster and reproject our raster." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "id": "15", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "CRS.from_wkt('PROJCS[\"WGS 84 / UTM zone 18N\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",-75],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH],AUTHORITY[\"EPSG\",\"32618\"]]')" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "rds.rio.crs" + "rda.rio.crs" ] }, { @@ -196,12 +842,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "id": "17", "metadata": {}, "outputs": [], "source": [ - "rds_reproj = rds.rio.reproject(\"EPSG:32612\")" + "rda_reproj = rda.rio.reproject(\"EPSG:32612\")" ] }, { @@ -210,17 +856,668 @@ "metadata": {}, "source": [ ":::{note}\n", - "We have to update our CRS system to the new projection with `rio.write_crs`" + "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." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, "id": "19", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
<xarray.DataArray 'band_data' (band: 3, y: 912, x: 962)> Size: 11MB\n",
+       "array([[[nan, nan, nan, ..., nan, nan, nan],\n",
+       "        [nan, nan, nan, ..., nan, nan, nan],\n",
+       "        [nan, nan, nan, ..., nan, nan, nan],\n",
+       "        ...,\n",
+       "        [nan, nan, nan, ..., nan, nan, nan],\n",
+       "        [nan, nan, nan, ..., nan, nan, nan],\n",
+       "        [nan, nan, nan, ..., nan, nan, nan]],\n",
+       "\n",
+       "       [[nan, nan, nan, ..., nan, nan, nan],\n",
+       "        [nan, nan, nan, ..., nan, nan, nan],\n",
+       "        [nan, nan, nan, ..., nan, nan, nan],\n",
+       "        ...,\n",
+       "        [nan, nan, nan, ..., nan, nan, nan],\n",
+       "        [nan, nan, nan, ..., nan, nan, nan],\n",
+       "        [nan, nan, nan, ..., nan, nan, nan]],\n",
+       "\n",
+       "       [[nan, nan, nan, ..., nan, nan, nan],\n",
+       "        [nan, nan, nan, ..., nan, nan, nan],\n",
+       "        [nan, nan, nan, ..., nan, nan, nan],\n",
+       "        ...,\n",
+       "        [nan, nan, nan, ..., nan, nan, nan],\n",
+       "        [nan, nan, nan, ..., nan, nan, nan],\n",
+       "        [nan, nan, nan, ..., nan, nan, nan]]],\n",
+       "      shape=(3, 912, 962), dtype=float32)\n",
+       "Coordinates:\n",
+       "  * band         (band) int64 24B 1 2 3\n",
+       "  * y            (y) float64 7kB 3.332e+06 3.331e+06 ... 3.017e+06 3.016e+06\n",
+       "  * x            (x) float64 8kB 3.828e+06 3.828e+06 ... 4.16e+06 4.16e+06\n",
+       "    spatial_ref  int64 8B 0\n",
+       "Attributes:\n",
+       "    AREA_OR_POINT:       Area\n",
+       "    STATISTICS_MAXIMUM:  255\n",
+       "    STATISTICS_MEAN:     29.947726688477\n",
+       "    STATISTICS_MINIMUM:  0\n",
+       "    STATISTICS_STDDEV:   52.340921626611
" + ], + "text/plain": [ + " Size: 11MB\n", + "array([[[nan, nan, nan, ..., nan, nan, nan],\n", + " [nan, nan, nan, ..., nan, nan, nan],\n", + " [nan, nan, nan, ..., nan, nan, nan],\n", + " ...,\n", + " [nan, nan, nan, ..., nan, nan, nan],\n", + " [nan, nan, nan, ..., nan, nan, nan],\n", + " [nan, nan, nan, ..., nan, nan, nan]],\n", + "\n", + " [[nan, nan, nan, ..., nan, nan, nan],\n", + " [nan, nan, nan, ..., nan, nan, nan],\n", + " [nan, nan, nan, ..., nan, nan, nan],\n", + " ...,\n", + " [nan, nan, nan, ..., nan, nan, nan],\n", + " [nan, nan, nan, ..., nan, nan, nan],\n", + " [nan, nan, nan, ..., nan, nan, nan]],\n", + "\n", + " [[nan, nan, nan, ..., nan, nan, nan],\n", + " [nan, nan, nan, ..., nan, nan, nan],\n", + " [nan, nan, nan, ..., nan, nan, nan],\n", + " ...,\n", + " [nan, nan, nan, ..., nan, nan, nan],\n", + " [nan, nan, nan, ..., nan, nan, nan],\n", + " [nan, nan, nan, ..., nan, nan, nan]]],\n", + " shape=(3, 912, 962), dtype=float32)\n", + "Coordinates:\n", + " * band (band) int64 24B 1 2 3\n", + " * y (y) float64 7kB 3.332e+06 3.331e+06 ... 3.017e+06 3.016e+06\n", + " * x (x) float64 8kB 3.828e+06 3.828e+06 ... 4.16e+06 4.16e+06\n", + " spatial_ref int64 8B 0\n", + "Attributes:\n", + " AREA_OR_POINT: Area\n", + " STATISTICS_MAXIMUM: 255\n", + " STATISTICS_MEAN: 29.947726688477\n", + " STATISTICS_MINIMUM: 0\n", + " STATISTICS_STDDEV: 52.340921626611" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "rds_reproj.rio.write_crs(\"EPSG:32612\", inplace=True)" + "rda_reproj.rio.write_crs(\"EPSG:32612\", inplace=True)" ] }, { @@ -245,7 +1542,7 @@ ":class: dropdown\n", "\n", "```python\n", - "rds[1].rio.reproject(\"EPSG:3857\").rio.write_crs(\"EPSG:3857\", inplace=True).plot()\n", + "rda[1].rio.reproject(\"EPSG:3857\").rio.write_crs(\"EPSG:3857\", inplace=True).plot()\n", "```\n", ":::\n", "::::\n" From 7cb9b36351d2d5cff44ca77e9e4adaa4dde201c6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 12 Jul 2026 00:33:55 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- advanced/backends/rasterio_backend.ipynb | 1346 +--------------------- 1 file changed, 34 insertions(+), 1312 deletions(-) diff --git a/advanced/backends/rasterio_backend.ipynb b/advanced/backends/rasterio_backend.ipynb index ec838dbf..55ce37ed 100644 --- a/advanced/backends/rasterio_backend.ipynb +++ b/advanced/backends/rasterio_backend.ipynb @@ -52,7 +52,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "id": "3", "metadata": {}, "outputs": [], @@ -78,7 +78,7 @@ }, { "cell_type": "markdown", - "id": "6", + "id": "5", "metadata": {}, "source": [ "## Raster bands\n", @@ -88,7 +88,7 @@ }, { "cell_type": "markdown", - "id": "d7f5e839", + "id": "6", "metadata": {}, "source": [ "Lets get the `DataArray` object (or variable) from our dataset. The variable name is \"band_data\"." @@ -96,600 +96,18 @@ }, { "cell_type": "code", - "execution_count": 46, - "id": "3bb32764", + "execution_count": null, + "id": "7", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "
<xarray.DataArray 'band_data' (band: 3, y: 718, x: 791)> Size: 7MB\n",
-       "[1703814 values with dtype=float32]\n",
-       "Coordinates:\n",
-       "  * band         (band) int64 24B 1 2 3\n",
-       "  * y            (y) float64 6kB 2.827e+06 2.826e+06 ... 2.612e+06 2.612e+06\n",
-       "  * x            (x) float64 6kB 1.021e+05 1.024e+05 ... 3.389e+05 3.392e+05\n",
-       "    spatial_ref  int64 8B ...\n",
-       "Attributes:\n",
-       "    AREA_OR_POINT:       Area\n",
-       "    STATISTICS_MAXIMUM:  255\n",
-       "    STATISTICS_MEAN:     29.947726688477\n",
-       "    STATISTICS_MINIMUM:  0\n",
-       "    STATISTICS_STDDEV:   52.340921626611
" - ], - "text/plain": [ - " Size: 7MB\n", - "[1703814 values with dtype=float32]\n", - "Coordinates:\n", - " * band (band) int64 24B 1 2 3\n", - " * y (y) float64 6kB 2.827e+06 2.826e+06 ... 2.612e+06 2.612e+06\n", - " * x (x) float64 6kB 1.021e+05 1.024e+05 ... 3.389e+05 3.392e+05\n", - " spatial_ref int64 8B ...\n", - "Attributes:\n", - " AREA_OR_POINT: Area\n", - " STATISTICS_MAXIMUM: 255\n", - " STATISTICS_MEAN: 29.947726688477\n", - " STATISTICS_MINIMUM: 0\n", - " STATISTICS_STDDEV: 52.340921626611" - ] - }, - "execution_count": 46, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "rda = rds['band_data']\n", + "rda = rds[\"band_data\"]\n", "rda" ] }, { "cell_type": "markdown", - "id": "e0272e99", + "id": "8", "metadata": {}, "source": [ "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." @@ -697,28 +115,17 @@ }, { "cell_type": "code", - "execution_count": 47, - "id": "7", + "execution_count": null, + "id": "9", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 47, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "rda.rio.count" ] }, { "cell_type": "markdown", - "id": "8", + "id": "10", "metadata": {}, "source": [ "### Selection by bands\n", @@ -730,7 +137,7 @@ { "cell_type": "code", "execution_count": null, - "id": "9", + "id": "11", "metadata": {}, "outputs": [], "source": [ @@ -739,7 +146,7 @@ }, { "cell_type": "markdown", - "id": "37b29994", + "id": "12", "metadata": {}, "source": [ "## Bounds\n", @@ -748,28 +155,17 @@ }, { "cell_type": "code", - "execution_count": 48, - "id": "11", + "execution_count": null, + "id": "13", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "(101985.0, 2611484.9999999995, 339314.99999999994, 2826915.0)" - ] - }, - "execution_count": 48, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "rda.rio.bounds()" ] }, { "cell_type": "markdown", - "id": "ee6fc5c8", + "id": "14", "metadata": {}, "source": [ "## Transformation\n", @@ -779,29 +175,17 @@ }, { "cell_type": "code", - "execution_count": 49, - "id": "12", + "execution_count": null, + "id": "15", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "Affine(300.0379266750948, 0.0, 101985.0,\n", - " 0.0, -300.041782729805, 2826915.0)" - ] - }, - "execution_count": 49, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "rda.rio.transform()" ] }, { "cell_type": "markdown", - "id": "14", + "id": "16", "metadata": {}, "source": [ "### Coordinate Reference System (CRS)\n", @@ -810,28 +194,17 @@ }, { "cell_type": "code", - "execution_count": 54, - "id": "15", + "execution_count": null, + "id": "17", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "CRS.from_wkt('PROJCS[\"WGS 84 / UTM zone 18N\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",-75],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH],AUTHORITY[\"EPSG\",\"32618\"]]')" - ] - }, - "execution_count": 54, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "rda.rio.crs" ] }, { "cell_type": "markdown", - "id": "16", + "id": "18", "metadata": {}, "source": [ "### Reprojection\n", @@ -842,8 +215,8 @@ }, { "cell_type": "code", - "execution_count": 51, - "id": "17", + "execution_count": null, + "id": "19", "metadata": {}, "outputs": [], "source": [ @@ -852,7 +225,7 @@ }, { "cell_type": "markdown", - "id": "18", + "id": "20", "metadata": {}, "source": [ ":::{note}\n", @@ -861,668 +234,17 @@ }, { "cell_type": "code", - "execution_count": 53, - "id": "19", + "execution_count": null, + "id": "21", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "
<xarray.DataArray 'band_data' (band: 3, y: 912, x: 962)> Size: 11MB\n",
-       "array([[[nan, nan, nan, ..., nan, nan, nan],\n",
-       "        [nan, nan, nan, ..., nan, nan, nan],\n",
-       "        [nan, nan, nan, ..., nan, nan, nan],\n",
-       "        ...,\n",
-       "        [nan, nan, nan, ..., nan, nan, nan],\n",
-       "        [nan, nan, nan, ..., nan, nan, nan],\n",
-       "        [nan, nan, nan, ..., nan, nan, nan]],\n",
-       "\n",
-       "       [[nan, nan, nan, ..., nan, nan, nan],\n",
-       "        [nan, nan, nan, ..., nan, nan, nan],\n",
-       "        [nan, nan, nan, ..., nan, nan, nan],\n",
-       "        ...,\n",
-       "        [nan, nan, nan, ..., nan, nan, nan],\n",
-       "        [nan, nan, nan, ..., nan, nan, nan],\n",
-       "        [nan, nan, nan, ..., nan, nan, nan]],\n",
-       "\n",
-       "       [[nan, nan, nan, ..., nan, nan, nan],\n",
-       "        [nan, nan, nan, ..., nan, nan, nan],\n",
-       "        [nan, nan, nan, ..., nan, nan, nan],\n",
-       "        ...,\n",
-       "        [nan, nan, nan, ..., nan, nan, nan],\n",
-       "        [nan, nan, nan, ..., nan, nan, nan],\n",
-       "        [nan, nan, nan, ..., nan, nan, nan]]],\n",
-       "      shape=(3, 912, 962), dtype=float32)\n",
-       "Coordinates:\n",
-       "  * band         (band) int64 24B 1 2 3\n",
-       "  * y            (y) float64 7kB 3.332e+06 3.331e+06 ... 3.017e+06 3.016e+06\n",
-       "  * x            (x) float64 8kB 3.828e+06 3.828e+06 ... 4.16e+06 4.16e+06\n",
-       "    spatial_ref  int64 8B 0\n",
-       "Attributes:\n",
-       "    AREA_OR_POINT:       Area\n",
-       "    STATISTICS_MAXIMUM:  255\n",
-       "    STATISTICS_MEAN:     29.947726688477\n",
-       "    STATISTICS_MINIMUM:  0\n",
-       "    STATISTICS_STDDEV:   52.340921626611
" - ], - "text/plain": [ - " Size: 11MB\n", - "array([[[nan, nan, nan, ..., nan, nan, nan],\n", - " [nan, nan, nan, ..., nan, nan, nan],\n", - " [nan, nan, nan, ..., nan, nan, nan],\n", - " ...,\n", - " [nan, nan, nan, ..., nan, nan, nan],\n", - " [nan, nan, nan, ..., nan, nan, nan],\n", - " [nan, nan, nan, ..., nan, nan, nan]],\n", - "\n", - " [[nan, nan, nan, ..., nan, nan, nan],\n", - " [nan, nan, nan, ..., nan, nan, nan],\n", - " [nan, nan, nan, ..., nan, nan, nan],\n", - " ...,\n", - " [nan, nan, nan, ..., nan, nan, nan],\n", - " [nan, nan, nan, ..., nan, nan, nan],\n", - " [nan, nan, nan, ..., nan, nan, nan]],\n", - "\n", - " [[nan, nan, nan, ..., nan, nan, nan],\n", - " [nan, nan, nan, ..., nan, nan, nan],\n", - " [nan, nan, nan, ..., nan, nan, nan],\n", - " ...,\n", - " [nan, nan, nan, ..., nan, nan, nan],\n", - " [nan, nan, nan, ..., nan, nan, nan],\n", - " [nan, nan, nan, ..., nan, nan, nan]]],\n", - " shape=(3, 912, 962), dtype=float32)\n", - "Coordinates:\n", - " * band (band) int64 24B 1 2 3\n", - " * y (y) float64 7kB 3.332e+06 3.331e+06 ... 3.017e+06 3.016e+06\n", - " * x (x) float64 8kB 3.828e+06 3.828e+06 ... 4.16e+06 4.16e+06\n", - " spatial_ref int64 8B 0\n", - "Attributes:\n", - " AREA_OR_POINT: Area\n", - " STATISTICS_MAXIMUM: 255\n", - " STATISTICS_MEAN: 29.947726688477\n", - " STATISTICS_MINIMUM: 0\n", - " STATISTICS_STDDEV: 52.340921626611" - ] - }, - "execution_count": 53, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "rda_reproj.rio.write_crs(\"EPSG:32612\", inplace=True)" ] }, { "cell_type": "markdown", - "id": "20", + "id": "22", "metadata": {}, "source": [ "## Exercise" @@ -1530,7 +252,7 @@ }, { "cell_type": "markdown", - "id": "21", + "id": "23", "metadata": {}, "source": [ "::::{admonition} Exercise\n", From 6df91aff5c5685ae78c8173fedf29ce96172bb5e Mon Sep 17 00:00:00 2001 From: Olufunke Awowale Date: Mon, 13 Jul 2026 12:43:01 -0400 Subject: [PATCH 4/4] pr suggestions --- advanced/backends/rasterio_backend.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced/backends/rasterio_backend.ipynb b/advanced/backends/rasterio_backend.ipynb index 55ce37ed..2b3b9d49 100644 --- a/advanced/backends/rasterio_backend.ipynb +++ b/advanced/backends/rasterio_backend.ipynb @@ -18,7 +18,7 @@ "\n", "## What are GeoTIFFs?\n", "\n", - "GeoTIFFS (Geographic Tagged Image File Format (GeoTIFF)) are an image format based on the TIFF (Tagged Image File Format), that contain georeferencing information embedded within a raster image. Rasters in GeoTIFFs are made up of gridded pixels (or arrays) that map to a geographic region. The pixels values represent a characteristic (variable) for the geographic region it maps to.\n", + "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", "\n", "![Raster_Image](https://docs.qgis.org/3.44/en/_images/raster_dataset.png)\n", "\n",