{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Pydeck + Earth Engine: ImageCollection Animation\n", "\n", "This is an example of using [pydeck](https://pydeck.gl) to animate a Google Earth Engine `ImageCollection` object. To install and run this notebook locally, refer to the [Pydeck Earth Engine documentation](https://earthengine-layers.com/docs/developer-guide/pydeck-integration).\n", "\n", "To see this example online, view the [JavaScript version][js-example].\n", "\n", "[js-example]: https://earthengine-layers.com/examples/image-collection" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import required packages:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from pydeck_earthengine_layers import EarthEngineLayer\n", "import pydeck as pdk\n", "import ee" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Authenticate with Earth Engine\n", "\n", "Using Earth Engine requires authentication. If you don't have a Google account approved for use with Earth Engine, you'll need to request access. For more information and to sign up, go to https://signup.earthengine.google.com/." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": true }, "outputs": [], "source": [ "try:\n", " ee.Initialize()\n", "except Exception as e:\n", " ee.Authenticate()\n", " ee.Initialize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### GFS Temperature Forecast Dataset\n", "\n", "This example uses the [Global Forecast System 384-Hour Predicted Atmosphere Data][gfs] dataset, which contains a weather forecast model produced by the National Centers for Environmental Prediction (NCEP)\n", "\n", "[gfs]: https://developers.google.com/earth-engine/datasets/catalog/NOAA_GFS0P25" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import the dataset by creating an Earth Engine object that references it." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Initialize an ee.ImageColllection object referencing the Global Forecast System dataset\n", "image_collection = ee.ImageCollection('NOAA/GFS0P25')\n", "\n", "# Select images from December 22, 2018\n", "image_collection = image_collection.filterDate('2018-12-22', '2018-12-23')\n", "\n", "# Choose the first 24 images in the ImageCollection\n", "image_collection = image_collection.limit(24)\n", "\n", "# Select a single band to visualize\n", "image_collection = image_collection.select('temperature_2m_above_ground')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a `vis_params` object that defines how EarthEngine should style the `ImageCollection`" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Style temperature values between -40C and 35C, \n", "# with lower values shades of blue, purple, and cyan, \n", "# and higher values shades of green, yellow, and red\n", "vis_params = {\n", " 'min': -40.0,\n", " 'max': 35.0,\n", " 'palette': ['blue', 'purple', 'cyan', 'green', 'yellow', 'red']\n", "};" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a new `EarthEngineLayer` with this dataset that can then be passed to Pydeck." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "layer = EarthEngineLayer(\n", " image_collection,\n", " vis_params,\n", " animate=True,\n", " id=\"global_weather\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then just pass this layer to a `pydeck.Deck` instance, and call `.show()` to create a map:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "fc9ad416126540ee90b4a549a794b8d6", "version_major": 2, "version_minor": 0 }, "text/plain": [ "DeckGLWidget(custom_libraries=[{'libraryName': 'EarthEngineLayerLibrary', 'resourceUri': 'https://unpkg.com/@u…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "view_state = pdk.ViewState(latitude=36, longitude=10, zoom=1)\n", "r = pdk.Deck(\n", " layers=[layer], \n", " initial_view_state=view_state\n", ")\n", "r.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }