{ "cells": [ { "cell_type": "markdown", "id": "a3a0fd61", "metadata": {}, "source": [ "# Spectral indices from Sentinel-2 data\n", "\n", "This is a short demo that shows how to download Sentinel-2 data and\n", "compute several spectral indices." ] }, { "cell_type": "code", "execution_count": 1, "id": "8293024e", "metadata": { "execution": { "iopub.execute_input": "2022-08-23T12:38:44.942618Z", "iopub.status.busy": "2022-08-23T12:38:44.942262Z", "iopub.status.idle": "2022-08-23T12:38:45.659088Z", "shell.execute_reply": "2022-08-23T12:38:45.658018Z" } }, "outputs": [], "source": [ "import ee\n", "import folium\n", "import pysoilmap.ee as psee" ] }, { "cell_type": "code", "execution_count": 2, "id": "860a47e1", "metadata": { "execution": { "iopub.execute_input": "2022-08-23T12:38:45.663803Z", "iopub.status.busy": "2022-08-23T12:38:45.663355Z", "iopub.status.idle": "2022-08-23T12:38:46.919987Z", "shell.execute_reply": "2022-08-23T12:38:46.918924Z" } }, "outputs": [], "source": [ "psee.initialize()" ] }, { "cell_type": "markdown", "id": "31431a12", "metadata": {}, "source": [ "### Sentinel-2 data\n", "\n", "The full dataset and documentation is available here:\n", "\n", "https://developers.google.com/earth-engine/datasets/catalog/COPERNICUS_S2\n", "\n", "When using a dataset, always make sure to refer to the documentation\n", "for resolution, band names and definitions, etc.\n", "\n", "The following app is very useful to identify images with only\n", "few clouds during a desired time interval:\n", "\n", "https://showcase.earthengine.app/view/s2-sr-browser-s2cloudless-nb\n", "\n", "For the purpose of this example, we'll select a specific image\n", "taken on a specific flyover to keep the computation time short." ] }, { "cell_type": "code", "execution_count": 3, "id": "fd119309", "metadata": { "execution": { "iopub.execute_input": "2022-08-23T12:38:46.924445Z", "iopub.status.busy": "2022-08-23T12:38:46.924186Z", "iopub.status.idle": "2022-08-23T12:38:46.928110Z", "shell.execute_reply": "2022-08-23T12:38:46.927319Z" } }, "outputs": [], "source": [ "img = ee.Image(\"COPERNICUS/S2/20180427T102019_20180427T102022_T32UNU\")\n", "img = img.divide(10_000)" ] }, { "cell_type": "markdown", "id": "c37c0101", "metadata": {}, "source": [ "We'll calculate a selection of derived variables based on the\n", "RGB and near-infrared bands, according to Table 2 in [^1]:\n", "\n", "| Name | Var | Formula |\n", "|:---------------------------------------|:------|:------------------------|\n", "| Brightness Index | BI | √(R² + G² + B²) / √3 |\n", "| Saturation Index | SI | (R - B) / (R + B) |\n", "| Hue Index | HI | (2·R - G - B) / (G - B) |\n", "| Coloration Index | CI | (R - G) / (R + G) |\n", "| Redness Index | RI | R² / (B·G³) |\n", "| Normalized Difference Vegatation Index | NDVI | (NIR - R) / (NIR + R) |\n", "\n", "[^1]: Hounkpatin, et al., 2018, Predicting reference soil groups using\n", " legacy data: A data pruning and Random Forest approach for tropical\n", " environment (Dano catchment, Burkina Faso)" ] }, { "cell_type": "code", "execution_count": 4, "id": "72b9565f", "metadata": { "execution": { "iopub.execute_input": "2022-08-23T12:38:46.931967Z", "iopub.status.busy": "2022-08-23T12:38:46.931728Z", "iopub.status.idle": "2022-08-23T12:38:46.936424Z", "shell.execute_reply": "2022-08-23T12:38:46.935510Z" } }, "outputs": [], "source": [ "RGB = img.select([\"B4\", \"B3\", \"B2\"])\n", "NIR = img.select(\"B8\")\n", "R = img.select(\"B4\")\n", "G = img.select(\"B3\")\n", "B = img.select(\"B2\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "a48f5bae", "metadata": { "execution": { "iopub.execute_input": "2022-08-23T12:38:46.940064Z", "iopub.status.busy": "2022-08-23T12:38:46.939825Z", "iopub.status.idle": "2022-08-23T12:38:46.945646Z", "shell.execute_reply": "2022-08-23T12:38:46.944653Z" } }, "outputs": [], "source": [ "BI = R.pow(2).add(G.pow(2)).add(B.pow(2)).divide(3).sqrt()\n", "SI = R.subtract(B).divide(R.add(B))\n", "CI = R.subtract(G).divide(R.add(G))\n", "RI = R.pow(2).divide(B.multiply(G.pow(3)))\n", "HI = R.multiply(2).subtract(G).subtract(B).divide(G.subtract(B))\n", "NDVI = NIR.subtract(R).divide(NIR.add(R))" ] }, { "cell_type": "markdown", "id": "9043e820", "metadata": {}, "source": [ "These derived images can now be added in javascript using\n", "[Map.addLayer()][1] or in python using e.g. [geemap.Map.addLayer()][2].\n", "\n", "[1]: https://developers.google.com/earth-engine/apidocs/map-addlayer\n", "[2]: https://geemap.org/geemap/#geemap.geemap.Map.addLayer\n", "\n", "For the purpose of this notebook, we'll add it to a\n", "`folium.Map` using `pysoilmap.ee.add_map_layer()`:" ] }, { "cell_type": "code", "execution_count": 6, "id": "4f0827ac", "metadata": { "execution": { "iopub.execute_input": "2022-08-23T12:38:46.949811Z", "iopub.status.busy": "2022-08-23T12:38:46.949561Z", "iopub.status.idle": "2022-08-23T12:38:46.953549Z", "shell.execute_reply": "2022-08-23T12:38:46.952222Z" } }, "outputs": [], "source": [ "folium.Map.addLayer = psee.add_map_layer" ] }, { "cell_type": "code", "execution_count": 7, "id": "efcb44e4", "metadata": { "execution": { "iopub.execute_input": "2022-08-23T12:38:46.957259Z", "iopub.status.busy": "2022-08-23T12:38:46.956716Z", "iopub.status.idle": "2022-08-23T12:38:56.034764Z", "shell.execute_reply": "2022-08-23T12:38:56.033811Z" } }, "outputs": [ { "data": { "text/html": [ "