{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "\n", " \n", " \n", " \n", "
View source on GitHubNotebook Viewer Run in Google Colab
" ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "## Install Earth Engine API and geemap\n", "Install the [Earth Engine Python API](https://developers.google.com/earth-engine/python_install) and [geemap](https://github.com/gee-community/geemap). The **geemap** Python package is built upon the [ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet) and [folium](https://github.com/python-visualization/folium) packages and implements several methods for displaying Earth Engine data layers, such as `Map.addLayer()`, `Map.setCenter()`, and `Map.centerObject()`.\n", "The following script checks if the geemap package has been installed. If not, it will install geemap, which automatically installs its [dependencies](https://github.com/gee-community/geemap#dependencies), including earthengine-api, folium, and ipyleaflet." ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "# Installs geemap package\n", "import subprocess\n", "\n", "try:\n", " import geemap\n", "except ImportError:\n", " print(\"geemap package not installed. Installing ...\")\n", " subprocess.check_call([\"python\", \"-m\", \"pip\", \"install\", \"geemap\"])" ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "## Create an interactive map" ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "Map" ] }, { "cell_type": "markdown", "id": "6", "metadata": {}, "source": [ "## Add Earth Engine Python script" ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "# Add Earth Engine dataset\n", "image = ee.Image(\"USGS/SRTMGL1_003\")\n", "\n", "# Set visualization parameters.\n", "vis_params = {\n", " \"min\": 0,\n", " \"max\": 4000,\n", " \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n", "}\n", "\n", "# Print the elevation of Mount Everest.\n", "xy = ee.Geometry.Point([86.9250, 27.9881])\n", "elev = image.sample(xy, 30).first().get(\"elevation\").getInfo()\n", "print(\"Mount Everest elevation (m):\", elev)\n", "\n", "# Add Earth Engine layers to Map\n", "Map.addLayer(image, vis_params, \"SRTM DEM\", True, 0.5)\n", "Map.addLayer(xy, {\"color\": \"red\"}, \"Mount Everest\")\n", "\n", "# Center the map based on an Earth Engine object or coordinates (longitude, latitude)\n", "# Map.centerObject(xy, 4)\n", "Map.setCenter(86.9250, 27.9881, 4)" ] }, { "cell_type": "markdown", "id": "8", "metadata": {}, "source": [ "## Add layer control and display the map" ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "Map.addLayerControl()\n", "Map" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "## Add basemaps" ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "Map.add_basemap(\"Esri Ocean\")\n", "Map.add_basemap(\"Esri Physical Map\")\n", "Map.add_basemap(\"Esri National Geographic\")\n", "Map.addLayerControl()\n", "Map" ] }, { "cell_type": "markdown", "id": "12", "metadata": {}, "source": [ "## Add XYZ tile layer" ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "url = \"https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}\"\n", "Map.add_tile_layer(url, name=\"Google Map\", attribution=\"Google\")\n", "Map.addLayerControl()\n", "Map" ] }, { "cell_type": "markdown", "id": "14", "metadata": {}, "source": [ "## Add WMS layer" ] }, { "cell_type": "code", "execution_count": null, "id": "15", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=[40, -100], zoom=4)\n", "\n", "elev_url = \"https://elevation.nationalmap.gov/arcgis/services/3DEPElevation/ImageServer/WMSServer?\"\n", "Map.add_wms_layer(\n", " url=elev_url, layers=\"3DEPElevation:None\", name=\"3DEP Elevation\", shown=False\n", ")\n", "\n", "naip_url = \"https://services.nationalmap.gov/arcgis/services/USGSNAIPImagery/ImageServer/WMSServer?\"\n", "Map.add_wms_layer(\n", " url=naip_url, layers=\"0\", name=\"NAIP Imagery\", format=\"image/png\", shown=True\n", ")\n", "\n", "Map.addLayerControl()\n", "Map" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }