{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Pydeck Earth Engine Introduction\n", "\n", "This is an introduction to using [Pydeck](https://pydeck.gl) and [Deck.gl](https://deck.gl) with [Google Earth Engine](https://earthengine.google.com/) in Jupyter Notebooks." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you wish to run this locally, you'll need to install some dependencies. Installing into a new Conda environment is recommended. To create and enter the environment, run:\n", "```\n", "conda create -n pydeck-ee -c conda-forge python jupyter notebook pydeck earthengine-api requests -y\n", "source activate pydeck-ee\n", "\n", "# Install the pydeck-earthengine-layers package from pip\n", "pip install pydeck-earthengine-layers\n", "\n", "jupyter nbextension install --sys-prefix --symlink --overwrite --py pydeck\n", "jupyter nbextension enable --sys-prefix --py pydeck\n", "```\n", "then open Jupyter Notebook with `jupyter notebook`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now in a Python Jupyter Notebook, let's first 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": [ "## Authentication\n", "\n", "### 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": "markdown", "metadata": {}, "source": [ "If you haven't used Earth Engine in Python before, you'll need to run the following authentication command. If you've previously authenticated in Python or the command line, you can skip the next line.\n", "\n", "Note that this creates a prompt which waits for user input. If you don't see a prompt, you may need to authenticate on the command line with `earthengine authenticate` and then return here, skipping the Python authentication." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "ee.Authenticate()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Initialize Earth Engine\n", "\n", "The above authentication step creates credentials that are stored on your local computer. Those credentials need to be loaded so that Earth Engine and Pydeck will work." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "ee.Initialize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Elevation data example\n", "\n", "Now let's make a simple example using Shuttle Radar Topography Mission (SRTM) elevation data. Here we create an `ee.Image` object referencing that dataset." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "image = ee.Image('CGIAR/SRTM90_V4')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we're ready to create the Pydeck layer. The `EarthEngineLayer` makes this simple. Just pass the Earth Engine object to the class." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "ee_layer = EarthEngineLayer(image, vis_params={\"min\": 0, \"max\": 255})" ] }, { "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": 5, "metadata": { "scrolled": true }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "7746b478d8be4c0ba355e39853ea0826", "version_major": 2, "version_minor": 0 }, "text/plain": [ "DeckGLWidget(custom_libraries=[{'libraryName': 'EarthEngineLayerLibrary', 'resourceUri': 'https://cdn.jsdelivr…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "view_state = pdk.ViewState(latitude=37.7749295, longitude=-122.4194155, zoom=10, bearing=0, pitch=45)\n", "r = pdk.Deck(\n", " layers=[ee_layer], \n", " initial_view_state=view_state\n", ")\n", "r.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hillshade Example\n", "\n", "As a slightly more in-depth example, let's use the SRTM dataset to calculate hillshading. This example comes from " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# From https://github.com/giswqs/earthengine-py-notebooks/blob/master/Visualization/hillshade.ipynb\n", "# Add Earth Engine dataset\n", "import math\n", "\n", "def Radians(img):\n", " return img.toFloat().multiply(math.pi).divide(180)\n", "\n", "def Hillshade(az, ze, slope, aspect):\n", " \"\"\"Compute hillshade for the given illumination az, el.\"\"\"\n", " azimuth = Radians(ee.Image(az))\n", " zenith = Radians(ee.Image(ze))\n", " # Hillshade = cos(Azimuth - Aspect) * sin(Slope) * sin(Zenith) +\n", " # cos(Zenith) * cos(Slope)\n", " return (azimuth.subtract(aspect).cos()\n", " .multiply(slope.sin())\n", " .multiply(zenith.sin())\n", " .add(\n", " zenith.cos().multiply(slope.cos())))\n", "\n", "terrain = ee.Algorithms.Terrain(ee.Image('srtm90_v4'))\n", "slope_img = Radians(terrain.select('slope'))\n", "aspect_img = Radians(terrain.select('aspect'))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "scrolled": true }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a0eaf2af218e4b4bb753623444373790", "version_major": 2, "version_minor": 0 }, "text/plain": [ "DeckGLWidget(custom_libraries=[{'libraryName': 'EarthEngineLayerLibrary', 'resourceUri': 'https://cdn.jsdelivr…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "ee_object = Hillshade(0, 60, slope_img, aspect_img)\n", "ee_layer = EarthEngineLayer(ee_object)\n", "view_state = pdk.ViewState(latitude=36.0756, longitude=-111.9987, zoom=7, bearing=0, pitch=30)\n", "r = pdk.Deck(\n", " layers=[ee_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.6" } }, "nbformat": 4, "nbformat_minor": 4 }