{ "cells": [ { "cell_type": "markdown", "id": "fc1d07be", "metadata": {}, "source": [ "# Cloud mask in the Sentinel-2 data\n", "\n", "This is a short demo of the cloud mask in the sentinel-2 dataset.\n", "\n", "For a more sophisticated method of dealing with clouds, see the\n", "[Sentinel-2 Cloud Masking with s2cloudless][1] example.\n", "\n", "[1]:\n", "https://developers.google.com/earth-engine/tutorials/community/sentinel-2-s2cloudless" ] }, { "cell_type": "code", "execution_count": 1, "id": "081ca0da", "metadata": { "execution": { "iopub.execute_input": "2022-08-23T12:38:39.522807Z", "iopub.status.busy": "2022-08-23T12:38:39.522449Z", "iopub.status.idle": "2022-08-23T12:38:40.513300Z", "shell.execute_reply": "2022-08-23T12:38:40.512206Z" } }, "outputs": [], "source": [ "import ee\n", "import folium\n", "import pysoilmap.ee as psee" ] }, { "cell_type": "code", "execution_count": 2, "id": "e2c269dd", "metadata": { "execution": { "iopub.execute_input": "2022-08-23T12:38:40.517829Z", "iopub.status.busy": "2022-08-23T12:38:40.517437Z", "iopub.status.idle": "2022-08-23T12:38:42.091094Z", "shell.execute_reply": "2022-08-23T12:38:42.090092Z" } }, "outputs": [], "source": [ "psee.initialize()" ] }, { "cell_type": "markdown", "id": "f17cc6b3", "metadata": {}, "source": [ "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", "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": "00fa5210", "metadata": { "execution": { "iopub.execute_input": "2022-08-23T12:38:42.096062Z", "iopub.status.busy": "2022-08-23T12:38:42.095740Z", "iopub.status.idle": "2022-08-23T12:38:42.100747Z", "shell.execute_reply": "2022-08-23T12:38:42.099826Z" } }, "outputs": [], "source": [ "img = ee.Image(\"COPERNICUS/S2/20180430T103019_20180430T103520_T32UMU\")\n", "rgb = img.select([\"B2\", \"B3\", \"B4\"]).divide(10_000)" ] }, { "cell_type": "markdown", "id": "10cb4504", "metadata": {}, "source": [ "Bits 10 and 11 are opaque and cirrus clouds, respectively:" ] }, { "cell_type": "code", "execution_count": 4, "id": "847265a9", "metadata": { "execution": { "iopub.execute_input": "2022-08-23T12:38:42.104350Z", "iopub.status.busy": "2022-08-23T12:38:42.104077Z", "iopub.status.idle": "2022-08-23T12:38:42.108743Z", "shell.execute_reply": "2022-08-23T12:38:42.107839Z" } }, "outputs": [], "source": [ "clouds = img.select('QA60').bitwiseAnd(0xc00)\n", "opaque = clouds.bitwiseAnd(0x400)\n", "cirrus = clouds.bitwiseAnd(0x800)" ] }, { "cell_type": "markdown", "id": "721373e4", "metadata": {}, "source": [ "Try to detect cloud shadows by projecting the clouds on the surface\n", "(adapted from the 3_Sentinel2_CloudAndShadowMask notebook in [AGREE][1]):\n", "\n", "[1]: https://github.com/rdandrimont/AGREE" ] }, { "cell_type": "code", "execution_count": 5, "id": "0d3c2f53", "metadata": { "execution": { "iopub.execute_input": "2022-08-23T12:38:42.112502Z", "iopub.status.busy": "2022-08-23T12:38:42.112222Z", "iopub.status.idle": "2022-08-23T12:38:42.120844Z", "shell.execute_reply": "2022-08-23T12:38:42.119975Z" } }, "outputs": [], "source": [ "shad_o = psee.cast_shadows(img, opaque)\n", "shad_c = psee.cast_shadows(img, cirrus)" ] }, { "cell_type": "markdown", "id": "600b70af", "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": "9c2c7150", "metadata": { "execution": { "iopub.execute_input": "2022-08-23T12:38:42.124663Z", "iopub.status.busy": "2022-08-23T12:38:42.124379Z", "iopub.status.idle": "2022-08-23T12:38:42.128391Z", "shell.execute_reply": "2022-08-23T12:38:42.127540Z" } }, "outputs": [], "source": [ "folium.Map.addLayer = psee.add_map_layer" ] }, { "cell_type": "code", "execution_count": 7, "id": "380262b5", "metadata": { "execution": { "iopub.execute_input": "2022-08-23T12:38:42.132305Z", "iopub.status.busy": "2022-08-23T12:38:42.132030Z", "iopub.status.idle": "2022-08-23T12:38:42.136415Z", "shell.execute_reply": "2022-08-23T12:38:42.135516Z" } }, "outputs": [], "source": [ "mask = (lambda mask: mask.updateMask(mask.neq(0)))\n", "vis = (lambda color: {'min': 0, 'max': 1, 'palette': color})" ] }, { "cell_type": "code", "execution_count": 8, "id": "22b12dc9", "metadata": { "execution": { "iopub.execute_input": "2022-08-23T12:38:42.140130Z", "iopub.status.busy": "2022-08-23T12:38:42.139834Z", "iopub.status.idle": "2022-08-23T12:38:43.706620Z", "shell.execute_reply": "2022-08-23T12:38:43.705592Z" } }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Map = folium.Map(location=psee.center(img), zoom_start=9)\n", "Map.addLayer(rgb, {'min': 0, 'max': 0.3}, \"RGB\")\n", "Map.addLayer(mask(opaque), vis('yellow'), name=\"Opaque Clouds\")\n", "Map.addLayer(mask(cirrus), vis('red'), name=\"Cirrus Clouds\")\n", "Map.addLayer(mask(shad_o), vis('teal'), name=\"Opaque Shadows\")\n", "Map.addLayer(mask(shad_c), vis('pink'), name=\"Cirrus Shadows\")\n", "Map.add_child(folium.LayerControl())\n", "Map" ] } ], "metadata": { "jupytext": { "encoding": "# -*- coding: utf-8 -*-", "formats": "py:light,ipynb" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }