diff --git a/_quarto.yml b/_quarto.yml index 6932ea1e..b09b5567 100644 --- a/_quarto.yml +++ b/_quarto.yml @@ -125,6 +125,7 @@ website: - user-guide/notebooks/tutorials/mapping-fires.ipynb - user-guide/notebooks/tutorials/stac_ipyleaflet.ipynb - user-guide/notebooks/tutorials/zonal-statistics-validation.ipynb + - user-guide/notebooks/tutorials/fancy-mapping-tutorial.ipynb - section: Datasets contents: - user-guide/notebooks/datasets/ocean-npp-timeseries-analysis.ipynb diff --git a/user-guide/notebooks/tutorials/fancy-mapping-tutorial.ipynb b/user-guide/notebooks/tutorials/fancy-mapping-tutorial.ipynb new file mode 100644 index 00000000..abaeb0c9 --- /dev/null +++ b/user-guide/notebooks/tutorials/fancy-mapping-tutorial.ipynb @@ -0,0 +1,1613 @@ +{ + "cells": [ + { + "cell_type": "raw", + "id": "cdbd74a6-c904-45b4-8c20-df6652465329", + "metadata": {}, + "source": [ + "---\n", + "Tutorial: How to Create Interactive Mapping Similar to the VEDA Environment\n", + " - Andrew Blackford \n", + "date: December 9, 2025\n", + "execute:\n", + " freeze: true\n", + "---" + ] + }, + { + "cell_type": "markdown", + "id": "f301792b-8325-4249-b9e5-f5069835bcd8", + "metadata": {}, + "source": [ + "# Run This Notebook" + ] + }, + { + "cell_type": "markdown", + "id": "05a44fbf-b98d-411b-a743-d43a14c87fa8", + "metadata": {}, + "source": [ + "

🚀 Launch in VEDA JupyterHub (requires access)

\n", + "\n", + "

To obtain credentials to VEDA Hub, follow this link for more information.

" + ] + }, + { + "cell_type": "markdown", + "id": "38f2affe-06cd-4dbd-ad60-7789e8ce2a86", + "metadata": {}, + "source": [ + "
\n", + "Disclaimer: it is highly recommended to run a tutorial within NASA VEDA JupyterHub, which already includes functions for processing and visualizing data specific to VEDA stories. Running the tutorial outside of the VEDA JupyterHub may lead to errors, specifically related to EarthData authentication. Additionally, it is recommended to use the Pangeo workspace within the VEDA JupyterHub, since certain packages relevant to this tutorial are already installed.
\n", + "\n", + "

If you do not have a VEDA Jupyterhub Account you can launch this notebook on your local environment using MyBinder by clicking the icon below.

\n", + "
\n", + "\n", + "\"Binder\" " + ] + }, + { + "cell_type": "markdown", + "id": "b3b644fa-3f83-4f4c-a0e0-cd118df0c8d1", + "metadata": {}, + "source": [ + "## Environment Setup" + ] + }, + { + "cell_type": "markdown", + "id": "7522b0ca-b0bc-40fb-84ae-80ebef8eacd9", + "metadata": {}, + "source": [ + "If running this notebook outside of the VEDA JupyterHub, install the following packages:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "ec1de5e8-a5c7-48f6-bf76-15ecda6ea98c", + "metadata": {}, + "outputs": [], + "source": [ + "# Load libraries\n", + "\n", + "import requests\n", + "import matplotlib.pyplot as plt\n", + "import plotutils as putils\n", + "from pystac_client import Client\n", + "import folium" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "31f75db5-cc3e-4c9d-b3a7-0bab3aaa0a3b", + "metadata": {}, + "outputs": [], + "source": [ + "# For retrieving data already catalogued in VEDA STAC\n", + "STAC_API_URL = \"https://openveda.cloud/api/stac\"\n", + "RASTER_API_URL = \"https://openveda.cloud/api/raster\"\n", + "\n", + "# Open STAC client designed for interacting with SpatioTemporal Asset Catalog (STAC) APIs and Catalogs\n", + "client_STAC = Client.open(STAC_API_URL)" + ] + }, + { + "cell_type": "markdown", + "id": "65389892-0169-4d52-a6bd-92bb511533c5", + "metadata": {}, + "source": [ + "This example will show how to create an interactive mapping experience similar to what is used in the VEDA environment. Using an example dataset from the VEDA STAC catalog, this notebook utilizes the Folium package to create this experience. " + ] + }, + { + "cell_type": "markdown", + "id": "1bac1ae7-17e4-4d33-b291-0c831633246c", + "metadata": {}, + "source": [ + "## Processing steps:\n", + "1.) Choose collection ID from the STAC catalog and date for analysis
\n", + "2.) Retrieve collection information and items from VEDA STAC catalog
\n", + "3.) Retrieve item statistics and tiling information
\n", + "4.) Plot data
" + ] + }, + { + "cell_type": "markdown", + "id": "adeb6bd5-4ef4-41f1-bc2b-43da922872d6", + "metadata": {}, + "source": [ + "## Choose variable and retrieve json from VEDA STAC catalogue" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "5cee838d-6182-4a73-88bf-3e5001e1e0e6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "
\n", + "
\n", + " <CollectionClient id=landslides-ndvi>\n", + "
\n", + "\n", + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "collection_id = \"landslides-ndvi\"\n", + "date = \"2024-10-12\"\n", + "\n", + "results = client_STAC.search(collections=[collection_id], datetime=date)\n", + "\n", + "# ── VEDA Collection Request ─────────────────────────────────────────────────────────────────────────────────────\n", + "\n", + "items = list(results.items())\n", + "assert len(items) != 0, \"No items found\"\n", + "item = items[0]\n", + "collection = item.get_collection()\n", + "\n", + "# grab the dashboard render block\n", + "dashboard_render = collection.extra_fields[\"renders\"][\"dashboard\"]\n", + "\n", + "assets = dashboard_render[\"assets\"][0]\n", + "((vmin, vmax),) = dashboard_render[\"rescale\"]\n", + "\n", + "collection" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "b6072940-bfd6-4de5-aa6f-2399b6f04b01", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'tilejson': '2.2.0', 'version': '1.0.0', 'scheme': 'xyz', 'tiles': ['https://openveda.cloud/api/raster/collections/landslides-ndvi/items/S2_NDVI_Diff_2024-10-12/tiles/WebMercatorQuad/{z}/{x}/{y}@1x?assets=cog_default&color_formula=gamma+r+1.05&colormap_name=coolwarm_r&rescale=-0.5%2C0.5'], 'minzoom': 0, 'maxzoom': 24, 'bounds': [-84.37131820919367, 34.20966703314409, -80.89024067922162, 37.04653228059104], 'center': [-82.63077944420765, 35.62809965686756, 0]}\n" + ] + } + ], + "source": [ + "# ── VEDA Tile Request ─────────────────────────────────────────────────────────────────────────────────────\n", + "colormap_name = \"coolwarm_r\"\n", + "\n", + "# Build endpoint URL without worrying about trailing slashes\n", + "response = requests.get(\n", + " f\"{RASTER_API_URL.rstrip('/')}/collections/{collection_id}\"\n", + " f\"/items/{item.id}/WebMercatorQuad/tilejson.json?\"\n", + " f\"&assets={assets}\"\n", + " f\"&color_formula=gamma+r+1.05&colormap_name={colormap_name}\"\n", + " f\"&rescale={vmin},{vmax}\",\n", + ")\n", + "\n", + "response.raise_for_status()\n", + "\n", + "tiles = response.json()\n", + "print(tiles)" + ] + }, + { + "cell_type": "markdown", + "id": "c8cf4e11-4a94-4b4c-8f5e-090f4642dcd1", + "metadata": {}, + "source": [ + "## Plot data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "73ceb0c1-74b3-4924-80c9-ac4c61fadd5f", + "metadata": {}, + "outputs": [], + "source": [ + "# First we will present the different basemaps that we have access to underlay beneath our tile requests\n", + "# For the first map, we will utilize the 'esri-satellite-labels' map layer\n", + "putils.get_available_basemaps()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "88cdb033-48fc-42fd-b07d-04099f827e0a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Visualization of vegetation damage done by Hurricane Helene in western North Caroline in September 2024.\n" + ] + }, + { + "data": { + "text/html": [ + "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Use the new plot_folium_from_VEDA_STAC function\n", + "m = putils.plot_folium_from_VEDA_STAC(\n", + " tiles_url_template=tiles[\"tiles\"][0],\n", + " center_coords=[35.44368, -82.26868],\n", + " zoom_level=12.5,\n", + " rescale=(vmin, vmax),\n", + " colormap_name=colormap_name,\n", + " capitalize_cmap=False, # to better match VEDA colors and matplotlib colors\n", + " layer_name=\"Sentinel 2 NDVI Difference: Hurricane Helene\",\n", + " date=f\"{date}T00:00:00Z\",\n", + " colorbar_caption=\"Normalized Difference Vegetation Index (Difference)\",\n", + " attribution=\"Sentinel 2 NDVI Difference: Hurricane Helene\",\n", + " tile_name=\"Sentinel 2 NDVI Difference: Hurricane Helene\",\n", + " opacity=0.8,\n", + " height=\"800px\",\n", + " basemap_style=\"cartodb-positron\" # Use light basemap for good contrast\n", + ")\n", + "\n", + "print(\n", + " \"Visualization of vegetation damage done by Hurricane Helene in western North Caroline in September 2024.\"\n", + ")\n", + "# Display the map\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "92ad6dd1-1c83-4ce1-b8ed-3065b25a7cc5", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "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.12.11" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}