From 3b163667d6a4c23befbf10ea0a1e91975b979ab5 Mon Sep 17 00:00:00 2001 From: "Azadpour, Elmera" <eazadpour@usgs.gov> Date: Fri, 4 Oct 2024 14:20:25 -0700 Subject: [PATCH] add python notebook to make slider --- ...t 9 Slider - Unequal access to water.ipynb | 226 ++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100644 2021 NLCD Pasture and Croplands vs Landsat 9 Slider - Unequal access to water.ipynb diff --git a/2021 NLCD Pasture and Croplands vs Landsat 9 Slider - Unequal access to water.ipynb b/2021 NLCD Pasture and Croplands vs Landsat 9 Slider - Unequal access to water.ipynb new file mode 100644 index 0000000..756f9e7 --- /dev/null +++ b/2021 NLCD Pasture and Croplands vs Landsat 9 Slider - Unequal access to water.ipynb @@ -0,0 +1,226 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "1", + "metadata": {}, + "outputs": [], + "source": [ + "# pip install geemap\n", + "# pip install ee " + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "2", + "metadata": {}, + "outputs": [], + "source": [ + "# Import the Earth Engine (ee) module, which allows interaction with Google Earth Engine.\n", + "import ee\n", + "\n", + "# Import the geemap module, which provides a Python interface for interactive mapping with Google Earth Engine.\n", + "import geemap" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "3", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "c6e926d803204b36ac0901c05b331082", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Map(center=[40, -100], controls=(WidgetControl(options=['position', 'transparent_bg'], widget=HBox(children=(T…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# Add baseamp\n", + "# Note: Earth Engine will notify you to authorize access from a google account. More info here: https://developers.google.com/earth-engine/guides/access\n", + "Map = geemap.Map(center=[40,-100], zoom=4)\n", + "Map.add_basemap('CartoDB.DarkMatterNoLabels')\n", + "Map" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "02297066", + "metadata": {}, + "outputs": [], + "source": [ + "# Import the NLCD collection\n", + "dataset = ee.ImageCollection('USGS/NLCD_RELEASES/2021_REL/NLCD')\n", + "\n", + "# Filter collection\n", + "nlcd2021 = dataset.filter(ee.Filter.eq('system:index', '2021')).first()\n", + "\n", + "# Select land cover band\n", + "landcover = nlcd2021.select('landcover')\n", + "\n", + "# Load US states FeatureCollection\n", + "states = ee.FeatureCollection('TIGER/2018/States')\n", + "\n", + "# Filter the states to include only the specified states, West of the Mississippi River \n", + "state_list = ['Washington', 'Oregon', 'California', 'Idaho', 'Nevada', \n", + " 'Utah', 'Arizona', 'Montana', 'Wyoming', 'Colorado', \n", + " 'New Mexico', 'North Dakota', 'South Dakota', 'Nebraska', \n", + " 'Kansas', 'Oklahoma', 'Texas', 'Minnesota', 'Iowa', \n", + " 'Missouri', 'Arkansas', 'Louisiana']\n", + "\n", + "selected_states = states.filter(ee.Filter.inList('NAME', state_list))\n", + "\n", + "# Clip the land cover data to the selected states\n", + "landcover_clipped = landcover.clipToCollection(selected_states)\n", + "\n", + "# Create a mask for Pasture/Hay (81) and Cultivated Crops (82)\n", + "mask = landcover_clipped.eq(81).Or(landcover_clipped.eq(82))\n", + "\n", + "# Apply the mask to keep only the desired classes\n", + "landcover_filtered = landcover_clipped.updateMask(mask)\n", + "\n", + "# Display the filtered land cover data - select \n", + "Map.addLayer(landcover_filtered, {'min': 0, 'max': 82, 'palette': ['white', 'green', 'green']}, 'NLCD 2021 Pasture/Hay & Cultivated Crops')\n", + "\n", + "# Center the map over the filtered data\n", + "Map.centerObject(landcover_filtered)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "c9f2b271", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "c6e926d803204b36ac0901c05b331082", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Map(bottom=992.0, center=[50.28933925329178, -82.08984375000001], controls=(WidgetControl(options=['position',…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# Load the Landsat 9 ImageCollection (Level 2, Tier 1 data) from Google Earth Engine.\n", + "collection = ee.ImageCollection(\"LANDSAT/LC09/C02/T1_L2\")\n", + "\n", + "# Compute the median value for each pixel across the collection. This helps to reduce noise like clouds.\n", + "median = collection.median()\n", + "\n", + "# Function to apply scale factors to the Landsat image bands for optical and thermal corrections.\n", + "def apply_scale_factors(image):\n", + " # Scale optical bands (surface reflectance) by multiplying by 0.0000275 and adding an offset of -0.2.\n", + " opticalBands = image.select(\"SR_B.\").multiply(0.0000275).add(-0.2)\n", + " \n", + " # Scale thermal bands (brightness temperature) by multiplying by 0.00341802 and adding an offset of 149.0.\n", + " thermalBands = image.select(\"ST_B.*\").multiply(0.00341802).add(149.0)\n", + " \n", + " # Add the scaled optical and thermal bands back to the image, replacing the original bands.\n", + " return image.addBands(opticalBands, None, True).addBands(thermalBands, None, True)\n", + "\n", + "# Apply the scale factors to the median Landsat image.\n", + "landsat = apply_scale_factors(median)\n", + "\n", + "# Clip the Landsat image to a selected region (in this case, to the selected states).\n", + "landsat_clipped = landsat.clipToCollection(selected_states)\n", + "\n", + "# Define visualization parameters for displaying the image in natural color (bands 4, 3, and 2 for red, green, blue).\n", + "vis_natural = {\n", + " \"bands\": [\"SR_B4\", \"SR_B3\", \"SR_B2\"], # Specify the bands for the true color image (red, green, blue).\n", + " \"min\": 0.0, # Minimum reflectance value for visualization.\n", + " \"max\": 0.3, # Maximum reflectance value for visualization.\n", + "}\n", + "\n", + "# Add the clipped Landsat image to the map using the true color visualization.\n", + "Map.addLayer(landsat_clipped, vis_natural, \"Landsat 9 True Color (432)\")\n", + "\n", + "# Display the map.\n", + "Map" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "2bbe12a5", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "m.add_basemap('CartoDB.DarkMatterNoLabels')" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "c374e86f", + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "4a9aec747ad94aa4ab01a251dcde18c5", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Map(center=[40, -100], controls=(ZoomControl(options=['position', 'zoom_in_text', 'zoom_in_title', 'zoom_out_t…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# Create split panel map with NLCD 2021 Pasture/Hay & Cultivated Crop on left and Landsat 2021 on right\n", + "# Apply teal color pallete for NLCD that corresponds to Living Conditions \n", + "left_layer = geemap.ee_tile_layer(landcover_filtered,{'min': 0, 'max': 82, 'palette': ['white', '#45a196', '#45a196']}, name= 'NLCD 2021 Pasture/Hay & Cultivated Crops')\n", + "right_layer = geemap.ee_tile_layer(landsat_clipped, vis_natural, name=\"Landsat 2021\")\n", + "\n", + "m.split_map(left_layer, right_layer)\n", + "m\n" + ] + } + ], + "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.9.15" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} -- GitLab