{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Palettes and Visualization" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> **Note:** \n", "> This notebook includes **interactive map visualizations** using the `geemap` package. \n", "> Additionally, you will likely need to install `ipykernel` to utilize the interactive elements within a Jupyter Notebook.\n", "> The `geemap` and `ipykernel` packages are **not installed automatically** with `RadGEEToolbox` since they are optional dependencies focused on visualizations. \n", "> If you have not already installed these packages, you can do so with:\n", ">\n", "> ```bash\n", "> pip install geemap ipykernel\n", "> ```\n", ">\n", "> Alternatively, using conda:\n", ">\n", "> ```bash\n", "> conda install conda-forge::geemap anaconda::ipykernel\n", "> ```\n", "> \n", "> If you do not install them, the map-based portions of this notebook will not work.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap\n", "from RadGEEToolbox import LandsatCollection\n", "from RadGEEToolbox import GetPalette\n", "from RadGEEToolbox import VisParams" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Earth Engine initialized successfully.\n" ] } ], "source": [ "# Store name of Google Cloud Project assosiated with Earth Engine - replace with your project ID/name\n", "PROJECT_ID = 'your-cloud-project-id'\n", "\n", "# Attempt to initialize Earth Engine\n", "try:\n", " ee.Initialize(project=PROJECT_ID)\n", " print(\"Earth Engine initialized successfully.\")\n", "except Exception as e:\n", " print(\"Initialization failed, attempting authentication...\")\n", " try:\n", " ee.Authenticate()\n", " ee.Initialize(project=PROJECT_ID)\n", " print(\"Authentication and initialization successful.\")\n", " except Exception as auth_error:\n", " print(\"Authentication failed. Error details:\", auth_error)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### RadGEEToolbox hosts two packages to help image visualization: 1) GetPalette and 2) VisParams\n", "- GetPalette has the get_palette() function which allows for easily getting the hex series for a variety of palettes\n", "- VisParams has the get_visualization_params() function which provides an alternative to visualization parameter dictionaries" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "['#00007F', '#002AFF', '#00D4FF', '#7FFF7F', '#FFD400', '#FF2A00', '#7F0000']\n" ] } ], "source": [ "# Example getting the jet palette\n", "# options: 'algae', 'dense', 'greens', 'haline', 'inferno', 'jet', 'matter', 'pubu', 'soft_blue_green_red', 'thermal', 'turbid', 'ylord'\n", "\n", "jet = GetPalette.get_palette('jet')\n", "print(jet)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### get_visualization_params() takes satellite, index, min_val, max_val, and palette as arguments\n", "- min_val, max_val, and palette are optional, however, as defaults are provided based on the satellite and index\n", "- providing optional values will override the defaults\n", "\n", "Options for satellite: 'Landsat', 'landsat', 'Sentinel2', or 'sentinel2'\n", "\n", "Options for index: 'TrueColor', 'NDVI', 'NDWI', 'halite', 'gypsum', 'LST', 'NDTI', 'KIVU', or '2BDA'\n", "\n", "______\n", "\n", "Below is an example of how to define the visualization parameters for a true color landsat image and then printing what the vis_params dictionary looks like" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "{'min': 0, 'max': 30000, 'bands': ['SR_B4', 'SR_B3', 'SR_B2']}\n" ] } ], "source": [ "true_color_vis_params = VisParams.get_visualization_params(satellite='landsat', index='TrueColor')\n", "print(true_color_vis_params)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another example, this time defining the visualization parameters for a NDVI landsat image and printing the dictionary" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "{'min': 0, 'max': 0.5, 'bands': ['ndvi'], 'palette': ['#f7fcf5', '#e5f5e0', '#c7e9c0', '#a1d99b', '#74c476', '#41ab5d', '#238b45', '#006d2c', '#00441b']}\n" ] } ], "source": [ "ndvi_vis_params = VisParams.get_visualization_params(satellite='landsat', index='NDVI')\n", "print(ndvi_vis_params)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pulling it all together\n", "**Lets**\n", "1) Define a collection, \n", "2) Process the collection for various products (ndwi, median, LST), \n", "3) Get list of dates of images, \n", "4) Visualize most recent image as true color and singeband products" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "['2023-06-21', '2023-06-29', '2023-07-07', '2023-07-15']\n" ] } ], "source": [ "col = LandsatCollection('2023-06-01', '2023-07-30', [32, 33], 38, 20) # 1.\n", "col = col.MosaicByDate # 1.\n", "ndwi = col.ndwi # 2\n", "LST = col.LST # 2\n", "mean_temperature = LST.mean # 2\n", "dates = col.dates # 3\n", "print(dates) # 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "True Color Visualization" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Date of displayed image: 2023-07-15\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9b16b559a2cb4a5c930ec0dce586bfe0", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map(center=[40.7514, -111.9064], controls=(WidgetControl(options=['position', 'transparent_bg'], widget=Search…" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Map = geemap.Map(center=(40.7514, -111.9064), zoom=9)\n", "Map.addLayer(col.image_grab(-1), vis_params=VisParams.get_visualization_params('Landsat', 'TrueColor'))\n", "print('Date of displayed image:', col.dates[-1])\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Example of True Color Map Output from Above Cell\n", "\n", "![Example of True Color Map Output from Above Cell](images/P&V_TrueColor_Example.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NDWI Visualization" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Date of displayed image: 2023-07-15\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "7f171a3d0a484630848011294ea4ef3f", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map(center=[40.7514, -111.9064], controls=(WidgetControl(options=['position', 'transparent_bg'], widget=Search…" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Map = geemap.Map(center=(40.7514, -111.9064), zoom=9)\n", "# min and max values are optional but allow for customization\n", "Map.addLayer(ndwi.image_grab(-1), vis_params=VisParams.get_visualization_params('Landsat', 'NDWI', min_val=-0.1, max_val=0.1))\n", "Map.add_colorbar(vis_params=VisParams.get_visualization_params('Landsat', 'NDWI', min_val=-0.1, max_val=0.1))\n", "print('Date of displayed image:', ndwi.dates[-1])\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Example of NDWI Map Output from Above Cell\n", "\n", "![Example of NDWI Map Output from Above Cell](images/P&V_NDWI_Example.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Land Surface Temperature Visualization" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Date of displayed image: 2023-07-15\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "781556389ed142a694e5776454f02e56", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map(center=[40.7514, -111.9064], controls=(WidgetControl(options=['position', 'transparent_bg'], widget=Search…" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Map = geemap.Map(center=(40.7514, -111.9064), zoom=9)\n", "vis_params=VisParams.get_visualization_params('Landsat', 'LST', min_val=5, max_val=60)\n", "Map.addLayer(LST.image_grab(-1), vis_params=vis_params)\n", "Map.add_colorbar(vis_params=vis_params)\n", "print('Date of displayed image:', LST.dates[-1])\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Example of Land Surface Temperature Map Output from Above Cell\n", "\n", "![Example of Land Surface Temperature Map Output from Above Cell](images/P&V_LST_Example.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mean LST image visualization" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "edc857484f98498abc5ad2429b903de3", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map(center=[40.7514, -111.9064], controls=(WidgetControl(options=['position', 'transparent_bg'], widget=Search…" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Map = geemap.Map(center=(40.7514, -111.9064), zoom=9)\n", "vis_params=VisParams.get_visualization_params('Landsat', 'LST', min_val=5, max_val=60)\n", "Map.addLayer(mean_temperature, vis_params=vis_params)\n", "Map.add_colorbar(vis_params=vis_params)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Example of Mean Land Surface Temperature Map Output from Above Cell\n", "\n", "![Example of Mean Land Surface Temperature Map Output from Above Cell](images/P&V_MeanLST_Example.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Finally, lets look at a chained example looking at turbidity in water pixels - but let's do it taking full advantage of the package\n", "##### Look how little code is needed to define the collection using multiple landsat tiles, mosaic images with the same date, mask the collection to water pixels, and calculate the relative turbidity of the water pixels - then display it!\n", "\n", "Note the location and date are for demonstrative purposes only." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "masked_turbidity_col = LandsatCollection('2023-04-01', '2023-07-30', [32, 33], 38, 20).MosaicByDate.masked_to_water_collection.turbidity\n", "image = masked_turbidity_col.image_grab(-1) #get latest image\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When processing turbidity, the band name become 'ndti' for Normalized Difference Turbidity Index" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Band name of turbidity image: ndti\n" ] } ], "source": [ "print('Band name of turbidity image: ', image.getInfo()['bands'][0]['id'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see what it looks like on the map!" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a21d78b9e2f24dbbad8000aaf1b62d8f", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map(center=[40.7514, -111.9064], controls=(WidgetControl(options=['position', 'transparent_bg'], widget=Search…" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Map = geemap.Map(center=(40.7514, -111.9064), zoom=9)\n", "vis_params=VisParams.get_visualization_params('Landsat', 'NDTI')\n", "Map.addLayer(image, vis_params=vis_params)\n", "Map.add_colorbar(vis_params=vis_params)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Example of Turbidity Map Output from Above Cell\n", "\n", "![Example of Turbidity Map Output from Above Cell](images/P&V_Turbidity_Example.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### *Please refer to the [RadGEEToolbox documentation](https://radgeetoolbox.readthedocs.io/en/latest/) for more information and a comprehensive list of available functionality*" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }