From e27e3f1cff30073a3c6d9af6b01ae7f114682bb5 Mon Sep 17 00:00:00 2001 From: Qiusheng Wu Date: Sun, 2 Jun 2024 20:16:04 -0400 Subject: [PATCH] Add a notebook example for using Google basemaps (#2029) --- docs/notebooks/142_google_maps.ipynb | 256 +++++++++++++++++++++++ docs/tutorials.md | 1 + examples/README.md | 1 + examples/notebooks/142_google_maps.ipynb | 256 +++++++++++++++++++++++ mkdocs.yml | 1 + 5 files changed, 515 insertions(+) create mode 100644 docs/notebooks/142_google_maps.ipynb create mode 100644 examples/notebooks/142_google_maps.ipynb diff --git a/docs/notebooks/142_google_maps.ipynb b/docs/notebooks/142_google_maps.ipynb new file mode 100644 index 0000000000..2d3d9695a4 --- /dev/null +++ b/docs/notebooks/142_google_maps.ipynb @@ -0,0 +1,256 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "0", + "metadata": {}, + "source": [ + "\"Open\n", + "\n", + "**Using Google Maps as basemaps**\n", + "\n", + "Uncomment the following line to install [geemap](https://geemap.org) if needed." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1", + "metadata": {}, + "outputs": [], + "source": [ + "# %pip install -U geemap" + ] + }, + { + "cell_type": "markdown", + "id": "2", + "metadata": {}, + "source": [ + "To use Google Maps as basemaps, you need to sign up for a Google Maps API key. Follow the instructions on the [Google Maps Platform](https://developers.google.com/maps/get-started) website to get an API key. The following steps are required:\n", + "- Step 1: [Set up a Google Cloud project](https://developers.google.com/maps/get-started#create-project)\n", + "- Step 2: [Enable Google Maps Platform APIs and SDKs ](https://developers.google.com/maps/get-started#enable-api-sdk)\n", + "- Step 3: [Get an API key](https://developers.google.com/maps/get-started#api-key)\n", + "\n", + "\n", + "There is a cost for requesting Maps tiles, but there is a \\$200 monthly credit and we've been told that for the purposes of typical EE-type usage, it would be rare to exceed \\$200.\n", + "\n", + "Once you have your API key, you can set it as a Google Colab secret with the name `GOOGLE_MAPS_API_KEY`. Alternatively, you can set it as an environment variable on your local machine. Uncomment the following line to set the environment variable if you are running this notebook locally." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3", + "metadata": {}, + "outputs": [], + "source": [ + "# import os\n", + "# os.environ[\"GOOGLE_MAPS_API_KEY\"] = \"\"" + ] + }, + { + "cell_type": "markdown", + "id": "4", + "metadata": {}, + "source": [ + "Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5", + "metadata": {}, + "outputs": [], + "source": [ + "import ee\n", + "import geemap" + ] + }, + { + "cell_type": "markdown", + "id": "6", + "metadata": {}, + "source": [ + "If the `GOOGLE_MAPS_API_KEY` environment variable is set, geemap will automatically detect it and use it to request Google Maps tiles. By default, geemap will use Google Roadmap as the basemap." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "8", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/jm80iKK.png)" + ] + }, + { + "cell_type": "markdown", + "id": "9", + "metadata": {}, + "source": [ + "Use Google Hybrid basemap." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "10", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(basemap=\"Google.Hybrid\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "11", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/4vLGh6X.png)" + ] + }, + { + "cell_type": "markdown", + "id": "12", + "metadata": {}, + "source": [ + "To use multiple basemaps, you can use the `Map.add_basemap()` function." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "13", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add_basemap(\"Google.Hybrid\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "14", + "metadata": {}, + "source": [ + "Use Google Satellite basemap." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "15", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(basemap=\"Google.Satellite\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "16", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/mJyE1jT.png)" + ] + }, + { + "cell_type": "markdown", + "id": "17", + "metadata": {}, + "source": [ + "Use Google Terrain basemap." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "18", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(basemap=\"Google.Terrain\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "19", + "metadata": {}, + "source": [ + "Google Maps can be customized with various parameters. To create a custom Google Maps basemap, you can use the `GoogleMapsTileProvider` class. See for more information on the available parameters." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "20", + "metadata": {}, + "outputs": [], + "source": [ + "from geemap.basemaps import GoogleMapsTileProvider" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "21", + "metadata": {}, + "outputs": [], + "source": [ + "style = [\n", + " {\"stylers\": [{\"hue\": \"#00ffe6\"}, {\"saturation\": -20}]},\n", + " {\n", + " \"featureType\": \"road\",\n", + " \"elementType\": \"geometry\",\n", + " \"stylers\": [{\"lightness\": 100}, {\"visibility\": \"simplified\"}],\n", + " },\n", + "]\n", + "\n", + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "basemap = GoogleMapsTileProvider(\n", + " map_type=\"roadmap\",\n", + " region=\"CN\",\n", + " language=\"zh-Cn\",\n", + " scale=\"scaleFactor2x\",\n", + " highDpi=True,\n", + " style=style,\n", + ")\n", + "m.add_basemap(basemap)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "22", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/GKWDi4U.png)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/tutorials.md b/docs/tutorials.md index 5ad4a18aca..266d4d404a 100644 --- a/docs/tutorials.md +++ b/docs/tutorials.md @@ -153,3 +153,4 @@ More video tutorials for geemap and Earth Engine are available on my [YouTube ch 139. Converting an Earth Engine to an image ([notebook](https://geemap.org/notebooks/139_layer_to_image)) 140. Converting Earth Engine images to an Xarray Dataset ([notebook](https://geemap.org/notebooks/140_ee_to_xarray)) 141. Visualizing in-memory raster datasets and image arrays ([notebook](https://geemap.org/notebooks/141_image_array_viz)) +142. Using Google Maps as basemaps ([notebook](https://geemap.org/notebooks/142_google_maps)) diff --git a/examples/README.md b/examples/README.md index 5561b0b7e1..a38b4a8116 100644 --- a/examples/README.md +++ b/examples/README.md @@ -158,6 +158,7 @@ More video tutorials for geemap and Earth Engine are available on my [YouTube ch 139. Converting an Earth Engine to an image ([notebook](https://geemap.org/notebooks/139_layer_to_image)) 140. Converting Earth Engine images to an Xarray Dataset ([notebook](https://geemap.org/notebooks/140_ee_to_xarray)) 141. Visualizing in-memory raster datasets and image arrays ([notebook](https://geemap.org/notebooks/141_image_array_viz)) +142. Using Google Maps as basemaps ([notebook](https://geemap.org/notebooks/142_google_maps)) ### 1. Introducing the geemap Python package for interactive mapping with Google Earth Engine diff --git a/examples/notebooks/142_google_maps.ipynb b/examples/notebooks/142_google_maps.ipynb new file mode 100644 index 0000000000..2d3d9695a4 --- /dev/null +++ b/examples/notebooks/142_google_maps.ipynb @@ -0,0 +1,256 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "0", + "metadata": {}, + "source": [ + "\"Open\n", + "\n", + "**Using Google Maps as basemaps**\n", + "\n", + "Uncomment the following line to install [geemap](https://geemap.org) if needed." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1", + "metadata": {}, + "outputs": [], + "source": [ + "# %pip install -U geemap" + ] + }, + { + "cell_type": "markdown", + "id": "2", + "metadata": {}, + "source": [ + "To use Google Maps as basemaps, you need to sign up for a Google Maps API key. Follow the instructions on the [Google Maps Platform](https://developers.google.com/maps/get-started) website to get an API key. The following steps are required:\n", + "- Step 1: [Set up a Google Cloud project](https://developers.google.com/maps/get-started#create-project)\n", + "- Step 2: [Enable Google Maps Platform APIs and SDKs ](https://developers.google.com/maps/get-started#enable-api-sdk)\n", + "- Step 3: [Get an API key](https://developers.google.com/maps/get-started#api-key)\n", + "\n", + "\n", + "There is a cost for requesting Maps tiles, but there is a \\$200 monthly credit and we've been told that for the purposes of typical EE-type usage, it would be rare to exceed \\$200.\n", + "\n", + "Once you have your API key, you can set it as a Google Colab secret with the name `GOOGLE_MAPS_API_KEY`. Alternatively, you can set it as an environment variable on your local machine. Uncomment the following line to set the environment variable if you are running this notebook locally." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3", + "metadata": {}, + "outputs": [], + "source": [ + "# import os\n", + "# os.environ[\"GOOGLE_MAPS_API_KEY\"] = \"\"" + ] + }, + { + "cell_type": "markdown", + "id": "4", + "metadata": {}, + "source": [ + "Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5", + "metadata": {}, + "outputs": [], + "source": [ + "import ee\n", + "import geemap" + ] + }, + { + "cell_type": "markdown", + "id": "6", + "metadata": {}, + "source": [ + "If the `GOOGLE_MAPS_API_KEY` environment variable is set, geemap will automatically detect it and use it to request Google Maps tiles. By default, geemap will use Google Roadmap as the basemap." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "8", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/jm80iKK.png)" + ] + }, + { + "cell_type": "markdown", + "id": "9", + "metadata": {}, + "source": [ + "Use Google Hybrid basemap." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "10", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(basemap=\"Google.Hybrid\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "11", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/4vLGh6X.png)" + ] + }, + { + "cell_type": "markdown", + "id": "12", + "metadata": {}, + "source": [ + "To use multiple basemaps, you can use the `Map.add_basemap()` function." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "13", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add_basemap(\"Google.Hybrid\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "14", + "metadata": {}, + "source": [ + "Use Google Satellite basemap." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "15", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(basemap=\"Google.Satellite\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "16", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/mJyE1jT.png)" + ] + }, + { + "cell_type": "markdown", + "id": "17", + "metadata": {}, + "source": [ + "Use Google Terrain basemap." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "18", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(basemap=\"Google.Terrain\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "19", + "metadata": {}, + "source": [ + "Google Maps can be customized with various parameters. To create a custom Google Maps basemap, you can use the `GoogleMapsTileProvider` class. See for more information on the available parameters." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "20", + "metadata": {}, + "outputs": [], + "source": [ + "from geemap.basemaps import GoogleMapsTileProvider" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "21", + "metadata": {}, + "outputs": [], + "source": [ + "style = [\n", + " {\"stylers\": [{\"hue\": \"#00ffe6\"}, {\"saturation\": -20}]},\n", + " {\n", + " \"featureType\": \"road\",\n", + " \"elementType\": \"geometry\",\n", + " \"stylers\": [{\"lightness\": 100}, {\"visibility\": \"simplified\"}],\n", + " },\n", + "]\n", + "\n", + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "basemap = GoogleMapsTileProvider(\n", + " map_type=\"roadmap\",\n", + " region=\"CN\",\n", + " language=\"zh-Cn\",\n", + " scale=\"scaleFactor2x\",\n", + " highDpi=True,\n", + " style=style,\n", + ")\n", + "m.add_basemap(basemap)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "22", + "metadata": {}, + "source": [ + "![](https://i.imgur.com/GKWDi4U.png)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/mkdocs.yml b/mkdocs.yml index c983a7fcb3..9fec6c9ea4 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -291,6 +291,7 @@ nav: - notebooks/139_layer_to_image.ipynb - notebooks/140_ee_to_xarray.ipynb - notebooks/141_image_array_viz.ipynb + - notebooks/142_google_maps.ipynb # - miscellaneous: # - notebooks/cartoee_colab.ipynb # - notebooks/cartoee_colorbar.ipynb