Skip to content

Commit

Permalink
Add a notebook example for using Google basemaps (#2029)
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs authored Jun 3, 2024
1 parent eba4759 commit e27e3f1
Show file tree
Hide file tree
Showing 5 changed files with 515 additions and 0 deletions.
256 changes: 256 additions & 0 deletions docs/notebooks/142_google_maps.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "0",
"metadata": {},
"source": [
"<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/examples/notebooks/142_google_maps.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\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\"] = \"<YOUR-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 <https://bit.ly/3UhbZKU> 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
}
1 change: 1 addition & 0 deletions docs/tutorials.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading

0 comments on commit e27e3f1

Please sign in to comment.