{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\"Open\n", "\n", "**Creating a rectangular grid covering a region of interest for computing zonal statistics**\n", "\n", "Uncomment the following line to install [geemap](https://geemap.org) if needed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# %pip install git+https://github.com/gee-community/geemap.git" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import libraries" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Update the package and restart the kernel if you run into errors." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geemap.update_package()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add temperature data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "image = ee.ImageCollection('NOAA/GFS0P25').first().select('temperature_2m_above_ground')\n", "vis_params = {\n", " 'min': -40.0,\n", " 'max': 35.0,\n", " 'palette': ['blue', 'purple', 'cyan', 'green', 'yellow', 'red'],\n", "}\n", "Map.addLayer(image, vis_params, 'Temperature')\n", "Map.add_colorbar(vis_params, label=\"Temperature (°C)\")\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a grid covering the area of interest." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geometry = ee.Geometry.BBox(-130, 24, -68, 50)\n", "grid = geemap.create_grid(geometry, 2e5)\n", "Map.addLayer(grid, {}, 'Grid')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compute zonal statistics based on the image and grid. In this case, we are computing the mean temperature for each grid cell." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stats = geemap.zonal_stats(\n", " image, grid, stat_type='MEAN', scale=2e5, return_fc=True\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stats" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add the mean temperature grid to the map." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map.add_styled_vector(\n", " stats, column=\"mean\", palette='coolwarm', layer_name=\"Mean Temperature\"\n", ")\n", "Map.add_layer_manager()\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](https://i.imgur.com/Kwe5eom.png)" ] } ], "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.10.8" } }, "nbformat": 4, "nbformat_minor": 5 }