{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "\"Open\n", "\n", "# Using geemap with Google Colab\n", "\n", "## Introduction\n", "\n", "This notebook demonstrates how to use [geemap](https://geemap.org) with Google Colab. To use geemap and the Earth Engine Python API, you must [register](https://code.earthengine.google.com/register) for an Earth Engine account and follow the instructions [here](https://docs.google.com/document/d/1ZGSmrNm6_baqd8CHt33kIBWOlvkh-HLr46bODgJN1h0/edit?usp=sharing) to create a Cloud Project. Earth Engine is free for [noncommercial and research use](https://earthengine.google.com/noncommercial)." ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "## Install geemap\n", "\n", "The geemap package is pre-installed in Google Colab and is updated to the latest minor or major release every few weeks. If you would like to install the latest version, you can uncomment and run the following command:" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "# %pip install -U geemap" ] }, { "cell_type": "markdown", "id": "3", "metadata": {}, "source": [ "## Import libraries\n", "\n", "Import the Earth Engine Python API." ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "import ee" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "## EE Authentication\n", "\n", "Running the following cell will start the Earth Engine authentication. Follow the instructions [here](https://book.geemap.org/chapters/01_introduction.html#earth-engine-authentication) to authenticate Earth Engine." ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "ee.Authenticate()" ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "ee.Initialize()" ] }, { "cell_type": "markdown", "id": "8", "metadata": {}, "source": [ "## Core features\n", "\n", "You can import the geemap package using the following conventions:\n", "\n", "- To import only the core features of geemap, use: `import geemap.core as geemap`\n", "- To import all the features of geemap, use: `import geemap`\n", "\n", "Let's import the geemap package using the second option:" ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "import geemap.core as geemap" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "## Basemap selector\n", "\n", "Select a basemap from the dropdown list and add it to the map." ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [], "source": [ "m = geemap.Map()\n", "m.add(\"basemap_selector\")\n", "m" ] }, { "cell_type": "markdown", "id": "12", "metadata": {}, "source": [ "## Layer Manager\n", "\n", "Toggle the checkbox to show or hide the layer. Drag and move the slider to change the transparency level of the layer." ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [], "source": [ "m = geemap.Map(center=(40, -100), zoom=4)\n", "dem = ee.Image(\"USGS/SRTMGL1_003\")\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "vis_params = {\n", " \"min\": 0,\n", " \"max\": 4000,\n", " \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n", "}\n", "m.add_layer(dem, vis_params, \"SRTM DEM\")\n", "m.add_layer(states, {}, \"US States\")\n", "m.add(\"layer_manager\")\n", "m" ] }, { "cell_type": "markdown", "id": "14", "metadata": {}, "source": [ "## Inspector\n", "\n", "Click on the map to query Earth Engine data at a specific location." ] }, { "cell_type": "code", "execution_count": null, "id": "15", "metadata": {}, "outputs": [], "source": [ "m = geemap.Map(center=(40, -100), zoom=4)\n", "dem = ee.Image(\"USGS/SRTMGL1_003\")\n", "landsat7 = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\")\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "vis_params = {\n", " \"min\": 0,\n", " \"max\": 4000,\n", " \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n", "}\n", "m.add_layer(dem, vis_params, \"SRTM DEM\")\n", "m.add_layer(\n", " landsat7,\n", " {\"bands\": [\"B4\", \"B3\", \"B2\"], \"min\": 20, \"max\": 200, \"gamma\": 2.0},\n", " \"Landsat 7\",\n", ")\n", "m.add_layer(states, {}, \"US States\")\n", "m.add(\"inspector\")\n", "m" ] }, { "cell_type": "markdown", "id": "16", "metadata": {}, "source": [ "## Layer Editor\n", "\n", "You can change the visualization parameters of the Earth Engine data using the layer editor.\n", "\n", "### Single-band image" ] }, { "cell_type": "code", "execution_count": null, "id": "17", "metadata": {}, "outputs": [], "source": [ "m = geemap.Map(center=(40, -100), zoom=4)\n", "dem = ee.Image(\"USGS/SRTMGL1_003\")\n", "vis_params = {\n", " \"min\": 0,\n", " \"max\": 4000,\n", " \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n", "}\n", "m.add_layer(dem, vis_params, \"SRTM DEM\")\n", "m.add(\"layer_editor\", layer_dict=m.ee_layers[\"SRTM DEM\"])\n", "m" ] }, { "cell_type": "markdown", "id": "18", "metadata": {}, "source": [ "### Multi-band image" ] }, { "cell_type": "code", "execution_count": null, "id": "19", "metadata": {}, "outputs": [], "source": [ "m = geemap.Map(center=(40, -100), zoom=4)\n", "landsat7 = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\")\n", "m.add_layer(\n", " landsat7,\n", " {\"bands\": [\"B4\", \"B3\", \"B2\"], \"min\": 20, \"max\": 200, \"gamma\": 2.0},\n", " \"Landsat 7\",\n", ")\n", "m.add(\"layer_editor\", layer_dict=m.ee_layers[\"Landsat 7\"])\n", "m" ] }, { "cell_type": "markdown", "id": "20", "metadata": {}, "source": [ "### Feature collection" ] }, { "cell_type": "code", "execution_count": null, "id": "21", "metadata": {}, "outputs": [], "source": [ "m = geemap.Map(center=(40, -100), zoom=4)\n", "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", "m.add_layer(states, {}, \"US States\")\n", "m.add(\"layer_editor\", layer_dict=m.ee_layers[\"US States\"])\n", "m" ] }, { "cell_type": "markdown", "id": "22", "metadata": {}, "source": [ "## Draw control\n", "\n", "You can draw shapes on the map using the draw control. The drawn features will be automatically converted to Earth Engine objects, which can be accessed in one of the following ways:\n", "\n", "- To return the last drawn feature as an `ee.Geometry()`, use: `m._draw_control.last_geometry`\n", "- To return all the drawn feature as an `ee.Feature()`, use: `m._draw_control.last_feature`\n", "- To return all the drawn features as an `ee.FeatureCollection()`, use: `m._draw_control.collection`" ] }, { "cell_type": "code", "execution_count": null, "id": "23", "metadata": {}, "outputs": [], "source": [ "m = geemap.Map(center=(40, -100), zoom=4)\n", "dem = ee.Image(\"USGS/SRTMGL1_003\")\n", "vis_params = {\n", " \"min\": 0,\n", " \"max\": 4000,\n", " \"palette\": \"terrain\",\n", "}\n", "m.add_layer(dem, vis_params, \"SRTM DEM\")\n", "m.add(\"layer_manager\")\n", "m" ] }, { "cell_type": "markdown", "id": "24", "metadata": {}, "source": [ "Use the draw control to draw a polygon on the map." ] }, { "cell_type": "code", "execution_count": null, "id": "25", "metadata": {}, "outputs": [], "source": [ "geometry = m._draw_control.last_geometry\n", "geometry" ] }, { "cell_type": "code", "execution_count": null, "id": "26", "metadata": {}, "outputs": [], "source": [ "feature = m._draw_control.last_feature\n", "feature" ] }, { "cell_type": "code", "execution_count": null, "id": "27", "metadata": {}, "outputs": [], "source": [ "collection = m._draw_control.collection\n", "collection" ] }, { "cell_type": "markdown", "id": "28", "metadata": {}, "source": [ "Clip the DEM data using the drawn polygon." ] }, { "cell_type": "code", "execution_count": null, "id": "29", "metadata": {}, "outputs": [], "source": [ "if geometry is not None:\n", " image = dem.clipToCollection(collection)\n", " m.layers[1].visible = False\n", " m.add_layer(image, vis_params, \"Clipped DEM\")\n", "m" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }