From ed071b88a802b10e1d79815b080c48064e0e9bd1 Mon Sep 17 00:00:00 2001 From: Justin Braaten Date: Tue, 26 Sep 2023 14:50:46 -0700 Subject: [PATCH] folium to geemap in guide pages and notebooks PiperOrigin-RevId: 568655626 --- guides/linked/ee-api-colab-setup.ipynb | 51 +- guides/linked/generated/image_info.ipynb | 276 ++++---- guides/linked/generated/image_math.ipynb | 60 +- guides/linked/generated/image_objects.ipynb | 647 +++++++++--------- guides/linked/generated/image_overview.ipynb | 73 +- .../linked/generated/image_relational.ipynb | 49 +- .../linked/generated/image_transforms.ipynb | 54 +- .../generated/image_visualization.ipynb | 89 ++- samples/python/guides/api_monitoring.py | 9 +- samples/python/guides/cumulativeCost.py | 29 +- samples/python/guides/dependencies.py | 23 + samples/python/guides/images01.py | 30 +- samples/python/guides/images02.py | 56 +- samples/python/guides/images03.py | 20 +- samples/python/guides/images04.py | 9 +- samples/python/guides/images041.py | 13 +- samples/python/guides/images042.py | 7 +- samples/python/guides/images043.py | 23 +- samples/python/guides/images05.py | 25 +- samples/python/guides/images06.py | 18 +- samples/python/guides/images07.py | 2 +- samples/python/guides/images08.py | 20 +- samples/python/guides/images09.py | 10 +- samples/python/guides/images10.py | 22 +- samples/python/guides/images11.py | 15 +- samples/python/guides/images12.py | 10 +- samples/python/guides/images13.py | 4 +- samples/python/guides/images14.py | 10 +- samples/python/guides/images15.py | 30 +- samples/python/guides/images16.py | 7 +- samples/python/guides/images17.py | 23 +- samples/python/guides/images18.py | 38 +- samples/python/guides/register.py | 26 +- 33 files changed, 907 insertions(+), 871 deletions(-) diff --git a/guides/linked/ee-api-colab-setup.ipynb b/guides/linked/ee-api-colab-setup.ipynb index 9f635eb56..22535248f 100644 --- a/guides/linked/ee-api-colab-setup.ipynb +++ b/guides/linked/ee-api-colab-setup.ipynb @@ -42,11 +42,11 @@ "id": "aV1xZ1CPi3Nw" }, "source": [ - "
\n", - "\n", - " Run in Google Colab\n", - "\n", - " View source on GitHub
" + "\u003ctable class=\"ee-notebook-buttons\" align=\"left\"\u003e\u003ctd\u003e\n", + "\u003ca target=\"_blank\" href=\"http://colab.research.google.com/github/google/earthengine-community/blob/master/guides/linked/ee-api-colab-setup.ipynb\"\u003e\n", + " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003e Run in Google Colab\u003c/a\u003e\n", + "\u003c/td\u003e\u003ctd\u003e\n", + "\u003ca target=\"_blank\" href=\"https://github.com/google/earthengine-community/blob/master/guides/linked/ee-api-colab-setup.ipynb\"\u003e\u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003e View source on GitHub\u003c/a\u003e\u003c/td\u003e\u003c/table\u003e" ] }, { @@ -196,14 +196,12 @@ "source": [ "### Interactive map\n", "\n", - "The [`folium`](https://python-visualization.github.io/folium/)\n", + "The [geemap](https://github.com/gee-community/geemap)\n", "library can be used to display `ee.Image` objects on an interactive\n", - "[Leaflet](https://leafletjs.com/) map. Folium has no default\n", - "method for handling tiles from Earth Engine, so one must be defined\n", - "and added to the `folium.Map` module before use.\n", + "[ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet) map.\n", "\n", - "The following cell provides an example of adding a method for handing Earth Engine\n", - "tiles and using it to display an elevation model to a Leaflet map." + "The following cell provides an example of using the `geemap.Map` object to\n", + "display an elevation model." ] }, { @@ -212,22 +210,8 @@ "id": "VIiyf6azf4mU" }, "source": [ - "# Import the Folium library.\n", - "import folium\n", - "\n", - "# Define a method for displaying Earth Engine image tiles to folium map.\n", - "def add_ee_layer(self, ee_image_object, vis_params, name):\n", - " map_id_dict = ee.Image(ee_image_object).getMapId(vis_params)\n", - " folium.raster_layers.TileLayer(\n", - " tiles = map_id_dict['tile_fetcher'].url_format,\n", - " attr = 'Map Data © Google Earth Engine',\n", - " name = name,\n", - " overlay = True,\n", - " control = True\n", - " ).add_to(self)\n", - "\n", - "# Add EE drawing method to folium.\n", - "folium.Map.add_ee_layer = add_ee_layer\n", + "# Import the geemap library.\n", + "import geemap\n", "\n", "# Set visualization parameters.\n", "vis_params = {\n", @@ -235,17 +219,14 @@ " 'max': 4000,\n", " 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5']}\n", "\n", - "# Create a folium map object.\n", - "my_map = folium.Map(location=[20, 0], zoom_start=3)\n", + "# Create a map object.\n", + "m = geemap.Map(center=[20, 0], zoom=3)\n", "\n", "# Add the elevation model to the map object.\n", - "my_map.add_ee_layer(dem.updateMask(dem.gt(0)), vis_params, 'DEM')\n", - "\n", - "# Add a layer control panel to the map.\n", - "my_map.add_child(folium.LayerControl())\n", + "m.add_ee_layer(dem.updateMask(dem.gt(0)), vis_params, 'DEM')\n", "\n", "# Display the map.\n", - "display(my_map)" + "display(m)" ], "execution_count": null, "outputs": [] @@ -297,4 +278,4 @@ "outputs": [] } ] -} \ No newline at end of file +} diff --git a/guides/linked/generated/image_info.ipynb b/guides/linked/generated/image_info.ipynb index 8599b5937..4754e4274 100644 --- a/guides/linked/generated/image_info.ipynb +++ b/guides/linked/generated/image_info.ipynb @@ -1,134 +1,148 @@ { - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "#@title Copyright 2021 The Earth Engine Community Authors { display-mode: \"form\" }\n", - "#\n", - "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", - "# you may not use this file except in compliance with the License.\n", - "# You may obtain a copy of the License at\n", - "#\n", - "# https://www.apache.org/licenses/LICENSE-2.0\n", - "#\n", - "# Unless required by applicable law or agreed to in writing, software\n", - "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", - "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", - "# See the License for the specific language governing permissions and\n", - "# limitations under the License." - ] + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#@title Copyright 2021 The Earth Engine Community Authors { display-mode: \"form\" }\n", + "#\n", + "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", + "# you may not use this file except in compliance with the License.\n", + "# You may obtain a copy of the License at\n", + "#\n", + "# https://www.apache.org/licenses/LICENSE-2.0\n", + "#\n", + "# Unless required by applicable law or agreed to in writing, software\n", + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", + "# See the License for the specific language governing permissions and\n", + "# limitations under the License." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Image Information and Metadata\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Print image objects to explore band names, projection information, properties, and other metadata. The following examples demonstrate printing the entire set of image metadata as well as requesting specific metadata elements programmatically." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Setup" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Earth Engine setup" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import ee\n", + "ee.Authenticate()\n", + "ee.Initialize()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Import `geemap`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import geemap" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Getting metadata" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Load an image.\n", + "image = ee.Image('LANDSAT/LC08/C02/T1/LC08_044034_20140318')\n", + "\n", + "# All metadata.\n", + "display('All metadata:', image)\n", + "\n", + "# Get information about the bands as a list.\n", + "band_names = image.bandNames()\n", + "display('Band names:', band_names) # ee.List of band names\n", + "\n", + "# Get projection information from band 1.\n", + "b1_proj = image.select('B1').projection()\n", + "display('Band 1 projection:', b1_proj) # ee.Projection object\n", + "\n", + "# Get scale (in meters) information from band 1.\n", + "b1_scale = image.select('B1').projection().nominalScale()\n", + "display('Band 1 scale:', b1_scale) # ee.Number\n", + "\n", + "# Note that different bands can have different projections and scale.\n", + "b8_scale = image.select('B8').projection().nominalScale()\n", + "display('Band 8 scale:', b8_scale) # ee.Number\n", + "\n", + "# Get a list of all metadata properties.\n", + "properties = image.propertyNames()\n", + "display('Metadata properties:', properties) # ee.List of metadata properties\n", + "\n", + "# Get a specific metadata property.\n", + "cloudiness = image.get('CLOUD_COVER')\n", + "display('CLOUD_COVER:', cloudiness) # ee.Number\n", + "\n", + "# Get version number (ingestion timestamp as microseconds since Unix epoch).\n", + "version = image.get('system:version')\n", + "display('Version:', version) # ee.Number\n", + "display(\n", + " 'Version (as ingestion date):',\n", + " ee.Date(ee.Number(version).divide(1000)).format(),\n", + ") # ee.Date\n", + "\n", + "# Get the timestamp.\n", + "ee_date = ee.Date(image.get('system:time_start'))\n", + "display('Timestamp:', ee_date) # ee.Date\n", + "\n", + "# Date objects transferred to the client are milliseconds since UNIX epoch;\n", + "# convert to human readable date with ee.Date.format().\n", + "display('Datetime:', ee_date.format()) # ISO standard date string" + ] + } + ], + "metadata": { + "colab": { + "name": "Image Information and Metadata" + } }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Image Information and Metadata\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Print image objects to explore band names, projection information, properties, and other metadata. The following examples demonstrate printing the entire set of image metadata as well as requesting specific metadata elements programmatically." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Setup" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Earth Engine setup" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import ee\n", - "ee.Authenticate()\n", - "ee.Initialize()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Getting metadata" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import pprint\n", - "\n", - "# Load an image.\n", - "image = ee.Image('LANDSAT/LC08/C02/T1/LC08_044034_20140318')\n", - "\n", - "# All metadata.\n", - "print('All metadata:')\n", - "pprint.pprint(image.getInfo())\n", - "\n", - "# Get information about the bands as a list.\n", - "band_names = image.bandNames()\n", - "print('Band names:', band_names.getInfo()) # ee.List of band names\n", - "\n", - "# Get projection information from band 1.\n", - "b1_proj = image.select('B1').projection()\n", - "print('Band 1 projection:', b1_proj.getInfo()) # ee.Projection object\n", - "\n", - "# Get scale (in meters) information from band 1.\n", - "b1_scale = image.select('B1').projection().nominalScale()\n", - "print('Band 1 scale:', b1_scale.getInfo()) # ee.Number\n", - "\n", - "# Note that different bands can have different projections and scale.\n", - "b8_scale = image.select('B8').projection().nominalScale()\n", - "print('Band 8 scale:', b8_scale.getInfo()) # ee.Number\n", - "\n", - "# Get a list of all metadata properties.\n", - "properties = image.propertyNames()\n", - "print('Metadata properties:',\n", - " properties.getInfo()) # ee.List of metadata properties\n", - "\n", - "# Get a specific metadata property.\n", - "cloudiness = image.get('CLOUD_COVER')\n", - "print('CLOUD_COVER:', cloudiness.getInfo()) # ee.Number\n", - "\n", - "# Get version number (ingestion timestamp as microseconds since Unix epoch).\n", - "version = image.get('system:version')\n", - "print('Version:', version.getInfo()) # ee.Number\n", - "print('Version (as ingestion date):',\n", - " ee.Date(ee.Number(version).divide(1000)).format().getInfo()) # ee.Date\n", - "\n", - "# Get the timestamp.\n", - "ee_date = ee.Date(image.get('system:time_start'))\n", - "print('Timestamp:', ee_date.getInfo()) # ee.Date\n", - "\n", - "# Date objects transferred to the client are milliseconds since UNIX epoch;\n", - "# convert to human readable date with ee.Date.format().\n", - "print('Datetime:', ee_date.format().getInfo()) # ISO standard date string" - ] - } - ], - "metadata": { - "colab": { - "name": "Image Information and Metadata" - } - }, - "nbformat": 4, - "nbformat_minor": 2 + "nbformat": 4, + "nbformat_minor": 2 } diff --git a/guides/linked/generated/image_math.ipynb b/guides/linked/generated/image_math.ipynb index 194189842..4f1947b73 100644 --- a/guides/linked/generated/image_math.ipynb +++ b/guides/linked/generated/image_math.ipynb @@ -65,7 +65,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Folium setup (for interactive map display)" + "### Import `geemap`" ] }, { @@ -74,20 +74,7 @@ "metadata": {}, "outputs": [], "source": [ - "import folium\n", - "\n", - "\n", - "def add_ee_layer(self, ee_image_object, vis_params, name):\n", - " map_id_dict = ee.Image(ee_image_object).getMapId(vis_params)\n", - " folium.raster_layers.TileLayer(\n", - " tiles=map_id_dict['tile_fetcher'].url_format,\n", - " attr='Map Data © Google Earth Engine',\n", - " name=name,\n", - " overlay=True,\n", - " control=True\n", - " ).add_to(self)\n", - "\n", - "folium.Map.add_ee_layer = add_ee_layer" + "import geemap" ] }, { @@ -111,8 +98,11 @@ "landsat_1999 = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003')\n", "\n", "# Compute NDVI.\n", - "ndvi_1999 = (landsat_1999.select('B4').subtract(landsat_1999.select('B3'))\n", - " .divide(landsat_1999.select('B4').add(landsat_1999.select('B3'))))" + "ndvi_1999 = (\n", + " landsat_1999.select('B4')\n", + " .subtract(landsat_1999.select('B3'))\n", + " .divide(landsat_1999.select('B4').add(landsat_1999.select('B3')))\n", + ")" ] }, { @@ -143,16 +133,18 @@ "squared_difference = diff.pow(2)\n", "\n", "# Define a map centered on Australia.\n", - "map_diff = folium.Map(location=[-24.003, 133.565], zoom_start=5)\n", + "map_diff = geemap.Map(center=[-24.003, 133.565], zoom=5)\n", "\n", "# Add the image layers to the map and display it.\n", - "map_diff.add_ee_layer(diff,\n", - " {'bands': ['B4', 'B3', 'B2'], 'min': -32, 'max': 32},\n", - " 'diff.')\n", - "map_diff.add_ee_layer(squared_difference,\n", - " {'bands': ['B4', 'B3', 'B2'], 'max': 1000},\n", - " 'squared diff.')\n", - "display(map_diff.add_child(folium.LayerControl()))" + "map_diff.add_ee_layer(\n", + " diff, {'bands': ['B4', 'B3', 'B2'], 'min': -32, 'max': 32}, 'diff.'\n", + ")\n", + "map_diff.add_ee_layer(\n", + " squared_difference,\n", + " {'bands': ['B4', 'B3', 'B2'], 'max': 1000},\n", + " 'squared diff.',\n", + ")\n", + "display(map_diff)" ] }, { @@ -182,21 +174,21 @@ "\n", "# Compute the EVI using an expression.\n", "evi = image.expression(\n", - " '2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {\n", + " '2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))',\n", + " {\n", " 'NIR': image.select('B5'),\n", " 'RED': image.select('B4'),\n", - " 'BLUE': image.select('B2')\n", - " })\n", + " 'BLUE': image.select('B2'),\n", + " },\n", + ")\n", "\n", "# Define a map centered on San Francisco Bay.\n", - "map_evi = folium.Map(location=[37.4675, -122.1363], zoom_start=9)\n", + "map_evi = geemap.Map(center=[37.4675, -122.1363], zoom=9)\n", "\n", "# Add the image layer to the map and display it.\n", - "map_evi.add_ee_layer(evi, {\n", - " 'min': -1,\n", - " 'max': 1,\n", - " 'palette': ['a6611a', 'f5f5f5', '4dac26']\n", - "}, 'evi')\n", + "map_evi.add_ee_layer(\n", + " evi, {'min': -1, 'max': 1, 'palette': ['a6611a', 'f5f5f5', '4dac26']}, 'evi'\n", + ")\n", "display(map_evi)" ] }, diff --git a/guides/linked/generated/image_objects.ipynb b/guides/linked/generated/image_objects.ipynb index 18810f4f4..261bf028c 100644 --- a/guides/linked/generated/image_objects.ipynb +++ b/guides/linked/generated/image_objects.ipynb @@ -1,328 +1,325 @@ { - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "#@title Copyright 2021 The Earth Engine Community Authors { display-#\n", - "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", - "# you may not use this file except in compliance with the License.\n", - "# You may obtain a copy of the License at\n", - "#\n", - "# https://www.apache.org/licenses/LICENSE-2.0\n", - "#\n", - "# Unless required by applicable law or agreed to in writing, software\n", - "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", - "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", - "# See the License for the specific language governing permissions and\n", - "# limitations under the License." - ] + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#@title Copyright 2021 The Earth Engine Community Authors { display-mode: \"form\" }\n", + "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", + "# you may not use this file except in compliance with the License.\n", + "# You may obtain a copy of the License at\n", + "#\n", + "# https://www.apache.org/licenses/LICENSE-2.0\n", + "#\n", + "# Unless required by applicable law or agreed to in writing, software\n", + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", + "# See the License for the specific language governing permissions and\n", + "# limitations under the License." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Object-based methods\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Image objects are sets of connected pixels having the same integer value. Categorical, binned, and boolean image data are suitable for object analysis.\n", + "\n", + "Earth Engine offers methods for labeling each object with a unique ID, counting the number of pixels composing objects, and computing statistics for values of pixels that intersect objects.\n", + "\n", + " * [`connectedComponents()`](https://developers.google.com#label_objects): label each object with a unique identifier.\n", + " * [`connectedPixelCount()`](https://developers.google.com#object_size): compute the number of pixels in each object.\n", + " * [`reduceConnectedComponents()`](https://developers.google.com#zonal_statistics): compute a statistic for pixels in each object.\n", + "\n", + "\n", + "\n", + "**Caution:** results of object-based methods depend on scale, which is determined by: \n", + "\n", + " * the requested scale of an output (e.g., `Export.image.toAsset()` or `Export.image.toDrive()`).\n", + " * functions that require a scale of analysis (e.g., `reduceRegions()` or `reduceToVectors()`).\n", + " * Map zoom level.\n", + "\n", + "\n", + "\n", + "Take special note of scale determined by Map zoom level. Results of object-based methods will vary when viewing or inspecting image layers in the Map, as each pyramid layer has a different scale. To force a desired scale of analysis in Map exploration, use `reproject()`. However, it is strongly recommended that you **NOT** use `reproject()` because the entire area visible in the Map will be requested at the set scale and projection. At large extents this can cause too much data to be requested, often triggering errors. Within the image pyramid-based architecture of Earth Engine, scale and projection need only be set for operations that provide `scale` and `crs` as parameters. See [Scale of Analysis](https://developers.google.com/earth-engine/scale#scale-of-analysis) and [Reprojecting](https://developers.google.com/earth-engine/projections#reprojecting) for more information." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Setup" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Earth Engine setup" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import ee\n", + "ee.Authenticate()\n", + "ee.Initialize()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Import `geemap`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import geemap" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Thermal hotspots\n", + "\n", + "The following sections provide examples of object-based methods applied to Landsat 8 surface temperature with each section building on the former. Run the next snippet to generate the base (\u003e 303 degrees Kelvin) for a small region of San Francisco." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Make an area of interest geometry centered on San Francisco.\n", + "point = ee.Geometry.Point(-122.1899, 37.5010)\n", + "aoi = point.buffer(10000)\n", + "\n", + "# Import a Landsat 8 image, subset the thermal band, and clip to the\n", + "# area of interest.\n", + "kelvin = (\n", + " ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318')\n", + " .select(['B10'], ['kelvin'])\n", + " .clip(aoi)\n", + ")\n", + "\n", + "# Threshold the thermal band to set hot pixels as value 1, mask all else.\n", + "hotspots = kelvin.gt(303).selfMask().rename('hotspots')\n", + "\n", + "# Define a map centered on Redwood City, California.\n", + "map_objects = geemap.Map(center=[37.5010, -122.1899], zoom=13)\n", + "\n", + "# Add the image layers to the map.\n", + "map_objects.add_ee_layer(kelvin, {'min': 288, 'max': 305}, 'Kelvin')\n", + "map_objects.add_ee_layer(hotspots, {'palette': 'FF0000'}, 'Hotspots')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Label objects\n", + "\n", + "Labeling objects is often the first step in object analysis. Here, the `connectedComponents()` function is used to identify image objects and assign a unique ID to each; all pixels belonging to an object are assigned the same integer ID value. The result is a copy of the input image with an additional \"labels\" band associating pixels with an object ID value based on connectivity of pixels in the first band of the image." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Uniquely label the hotspot image objects.\n", + "object_id = hotspots.connectedComponents(\n", + " connectedness=ee.Kernel.plus(1), maxSize=128\n", + ")\n", + "\n", + "# Add the uniquely ID'ed objects to the map.\n", + "map_objects.add_ee_layer(object_id.randomVisualizer(), None, 'Objects')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note that the maximum patch size is set to 128 pixels; objects composed of more pixels are masked. The connectivity is specified by an `ee.Kernel.plus(1)` kernel, which defines four-neighbor connectivity; use `ee.Kernel.square(1)` for eight-neighbor." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Object size" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Number of pixels\n", + "\n", + "Calculate the number of pixels composing objects using the `connectedPixelCount()` image method. Knowing the number of pixels in an object can be helpful for masking objects by size and calculating object area. The following snippet applies `connectedPixelCount()` to the \"labels\" band of the `objectId` image defined in the previous section." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Compute the number of pixels in each object defined by the \"labels\" band.\n", + "object_size = object_id.select('labels').connectedPixelCount(\n", + " maxSize=128, eightConnected=False\n", + ")\n", + "\n", + "# Add the object pixel count to the map.\n", + "map_objects.add_ee_layer(object_size, None, 'Object n pixels')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`connectedPixelCount()` returns a copy of the input image where each pixel of each band contains the number of connected neighbors according to either the four- or eight-neighbor connectivity rule determined by a boolean argument passed to the `eightConnected` parameter. Note that connectivity is determined independently for each band of the input image. In this example, a single-band image (`objectId`) representing object ID was provided as input, so a single-band image was returned with a \"labels\" band (present as such in the input image), but now the values represent the number of pixels composing objects; every pixel of each object will have the same pixel count value." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Area\n", + "\n", + "Calculate object area by multiplying the area of a single pixel by the number of pixels composing an object (determined by `connectedPixelCount()`). Pixel area is provided by an image generated from `ee.Image.pixelArea()`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Get a pixel area image.\n", + "pixel_area = ee.Image.pixelArea()\n", + "\n", + "# Multiply pixel area by the number of pixels in an object to calculate\n", + "# the object area. The result is an image where each pixel\n", + "# of an object relates the area of the object in m^2.\n", + "object_area = object_size.multiply(pixel_area)\n", + "\n", + "# Add the object area to the map.\n", + "map_objects.add_ee_layer(\n", + " object_area,\n", + " {'min': 0, 'max': 30000, 'palette': ['0000FF', 'FF00FF']},\n", + " 'Object area m^2',\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The result is an image where each pixel of an object relates the area of the object in square meters. In this example, the `objectSize` image contains a single band, if it were multi-band, the multiplication operation would be applied to each band of the image." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Filter objects by size\n", + "\n", + "Object size can be used as a mask condition to focus your analysis on objects of a certain size (e.g., mask out objects that are too small). Here the `objectArea` image calculated in the previous step is used as a mask to remove objects whose area are less than one hectare." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Threshold the `object_area` image to define a mask that will mask out\n", + "# objects below a given size (1 hectare in this case).\n", + "area_mask = object_area.gte(10000)\n", + "\n", + "# Update the mask of the `object_id` layer defined previously using the\n", + "# minimum area mask just defined.\n", + "object_id = object_id.updateMask(area_mask)\n", + "map_objects.add_ee_layer(object_id, None, 'Large hotspots')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The result is a copy of the `objectId` image where objects less than one hectare are masked out." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Zonal statistics\n", + "\n", + "The `reduceConnectedComponents()` method applies a reducer to the pixels composing unique objects. The following snippet uses it to calculate the mean temperature of hotspot objects. `reduceConnectedComponents()` requires an input image with a band (or bands) to be reduced and a band that defines object labels. Here, the `objectID` \"labels\" image band is added to the `kelvin` temperature image to construct a suitable input image." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Make a suitable image for `reduceConnectedComponents()` by adding a label\n", + "# band to the `kelvin` temperature image.\n", + "kelvin = kelvin.addBands(object_id.select('labels'))\n", + "\n", + "# Calculate the mean temperature per object defined by the previously added\n", + "# \"labels\" band.\n", + "patch_temp = kelvin.reduceConnectedComponents(\n", + " reducer=ee.Reducer.mean(), labelBand='labels'\n", + ")\n", + "\n", + "# Add object mean temperature to the map and display it.\n", + "map_objects.add_ee_layer(\n", + " patch_temp,\n", + " {'min': 303, 'max': 304, 'palette': ['yellow', 'red']},\n", + " 'Mean temperature',\n", + ")\n", + "display(map_objects)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The result is a copy of the input image without the band used to define objects, where pixel values represent the result of the reduction per object, per band.\n", + "\n", + "**Note:** `reduceToVectors()` provides similar functionality, except that the result is an `ee.FeatureCollection`, where each feature of the collection represents an object and the reduction of the pixels in each objects is expressed as a feature property for each band in the input image. `reduceToVectors()` is resource intensive, so use `reduceConnectedComponents()` whenever possible. See [Raster to Vector Conversion](https://developers.google.com/earth-engine/reducers_reduce_to_vectors) for more information." + ] + } + ], + "metadata": { + "colab": { + "name": "Object-based methods" + } }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Object-based methods\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Image objects are sets of connected pixels having the same integer value. Categorical, binned, and boolean image data are suitable for object analysis.\n", - "\n", - "Earth Engine offers methods for labeling each object with a unique ID, counting the number of pixels composing objects, and computing statistics for values of pixels that intersect objects.\n", - "\n", - " * [`connectedComponents()`](https://developers.google.com#label_objects): label each object with a unique identifier.\n", - " * [`connectedPixelCount()`](https://developers.google.com#object_size): compute the number of pixels in each object.\n", - " * [`reduceConnectedComponents()`](https://developers.google.com#zonal_statistics): compute a statistic for pixels in each object.\n", - "\n", - "\n", - "\n", - "**Caution:** results of object-based methods depend on scale, which is determined by: \n", - "\n", - " * the requested scale of an output (e.g., `Export.image.toAsset()` or `Export.image.toDrive()`).\n", - " * functions that require a scale of analysis (e.g., `reduceRegions()` or `reduceToVectors()`).\n", - " * Map zoom level.\n", - "\n", - "\n", - "\n", - "Take special note of scale determined by Map zoom level. Results of object-based methods will vary when viewing or inspecting image layers in the Map, as each pyramid layer has a different scale. To force a desired scale of analysis in Map exploration, use `reproject()`. However, it is strongly recommended that you **NOT** use `reproject()` because the entire area visible in the Map will be requested at the set scale and projection. At large extents this can cause too much data to be requested, often triggering errors. Within the image pyramid-based architecture of Earth Engine, scale and projection need only be set for operations that provide `scale` and `crs` as parameters. See [Scale of Analysis](https://developers.google.com/earth-engine/scale#scale-of-analysis) and [Reprojecting](https://developers.google.com/earth-engine/projections#reprojecting) for more information." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Setup" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Earth Engine setup" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import ee\n", - "ee.Authenticate()\n", - "ee.Initialize()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Folium setup (for interactive map display)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import folium\n", - "\n", - "\n", - "def add_ee_layer(self, ee_image_object, vis_params, name):\n", - " map_id_dict = ee.Image(ee_image_object).getMapId(vis_params)\n", - " folium.raster_layers.TileLayer(\n", - " tiles=map_id_dict['tile_fetcher'].url_format,\n", - " attr='Map Data © Google Earth Engine',\n", - " name=name,\n", - " overlay=True,\n", - " control=True\n", - " ).add_to(self)\n", - "\n", - "folium.Map.add_ee_layer = add_ee_layer" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Thermal hotspots\n", - "\n", - "The following sections provide examples of object-based methods applied to Landsat 8 surface temperature with each section building on the former. Run the next snippet to generate the base (> 303 degrees Kelvin) for a small region of San Francisco." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Make an area of interest geometry centered on San Francisco.\n", - "point = ee.Geometry.Point(-122.1899, 37.5010)\n", - "aoi = point.buffer(10000)\n", - "\n", - "# Import a Landsat 8 image, subset the thermal band, and clip to the\n", - "# area of interest.\n", - "kelvin = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318').select(\n", - " ['B10'], ['kelvin']).clip(aoi)\n", - "\n", - "# Threshold the thermal band to set hot pixels as value 1, mask all else.\n", - "hotspots = (kelvin.gt(303).selfMask().rename('hotspots'))\n", - "\n", - "# Define a map centered on Redwood City, California.\n", - "map_objects = folium.Map(location=[37.5010, -122.1899], zoom_start=13)\n", - "\n", - "# Add the image layers to the map.\n", - "map_objects.add_ee_layer(kelvin, {'min': 288, 'max': 305}, 'Kelvin')\n", - "map_objects.add_ee_layer(hotspots, {'palette': 'FF0000'}, 'Hotspots')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Label objects\n", - "\n", - "Labeling objects is often the first step in object analysis. Here, the `connectedComponents()` function is used to identify image objects and assign a unique ID to each; all pixels belonging to an object are assigned the same integer ID value. The result is a copy of the input image with an additional \"labels\" band associating pixels with an object ID value based on connectivity of pixels in the first band of the image." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Uniquely label the hotspot image objects.\n", - "object_id = hotspots.connectedComponents(\n", - " connectedness=ee.Kernel.plus(1), maxSize=128)\n", - "\n", - "# Add the uniquely ID'ed objects to the map.\n", - "map_objects.add_ee_layer(object_id.randomVisualizer(), None, 'Objects')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Note that the maximum patch size is set to 128 pixels; objects composed of more pixels are masked. The connectivity is specified by an `ee.Kernel.plus(1)` kernel, which defines four-neighbor connectivity; use `ee.Kernel.square(1)` for eight-neighbor." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Object size" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Number of pixels\n", - "\n", - "Calculate the number of pixels composing objects using the `connectedPixelCount()` image method. Knowing the number of pixels in an object can be helpful for masking objects by size and calculating object area. The following snippet applies `connectedPixelCount()` to the \"labels\" band of the `objectId` image defined in the previous section." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Compute the number of pixels in each object defined by the \"labels\" band.\n", - "object_size = object_id.select('labels').connectedPixelCount(\n", - " maxSize=128, eightConnected=False)\n", - "\n", - "# Add the object pixel count to the map.\n", - "map_objects.add_ee_layer(object_size, None, 'Object n pixels')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`connectedPixelCount()` returns a copy of the input image where each pixel of each band contains the number of connected neighbors according to either the four- or eight-neighbor connectivity rule determined by a boolean argument passed to the `eightConnected` parameter. Note that connectivity is determined independently for each band of the input image. In this example, a single-band image (`objectId`) representing object ID was provided as input, so a single-band image was returned with a \"labels\" band (present as such in the input image), but now the values represent the number of pixels composing objects; every pixel of each object will have the same pixel count value." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Area\n", - "\n", - "Calculate object area by multiplying the area of a single pixel by the number of pixels composing an object (determined by `connectedPixelCount()`). Pixel area is provided by an image generated from `ee.Image.pixelArea()`." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Get a pixel area image.\n", - "pixel_area = ee.Image.pixelArea()\n", - "\n", - "# Multiply pixel area by the number of pixels in an object to calculate\n", - "# the object area. The result is an image where each pixel\n", - "# of an object relates the area of the object in m^2.\n", - "object_area = object_size.multiply(pixel_area)\n", - "\n", - "# Add the object area to the map.\n", - "map_objects.add_ee_layer(object_area,\n", - " {'min': 0, 'max': 30000, 'palette': ['0000FF', 'FF00FF']},\n", - " 'Object area m^2')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The result is an image where each pixel of an object relates the area of the object in square meters. In this example, the `objectSize` image contains a single band, if it were multi-band, the multiplication operation would be applied to each band of the image." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Filter objects by size\n", - "\n", - "Object size can be used as a mask condition to focus your analysis on objects of a certain size (e.g., mask out objects that are too small). Here the `objectArea` image calculated in the previous step is used as a mask to remove objects whose area are less than one hectare." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Threshold the `object_area` image to define a mask that will mask out\n", - "# objects below a given size (1 hectare in this case).\n", - "area_mask = object_area.gte(10000)\n", - "\n", - "# Update the mask of the `object_id` layer defined previously using the\n", - "# minimum area mask just defined.\n", - "object_id = object_id.updateMask(area_mask)\n", - "map_objects.add_ee_layer(object_id, None, 'Large hotspots')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The result is a copy of the `objectId` image where objects less than one hectare are masked out." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Zonal statistics\n", - "\n", - "The `reduceConnectedComponents()` method applies a reducer to the pixels composing unique objects. The following snippet uses it to calculate the mean temperature of hotspot objects. `reduceConnectedComponents()` requires an input image with a band (or bands) to be reduced and a band that defines object labels. Here, the `objectID` \"labels\" image band is added to the `kelvin` temperature image to construct a suitable input image." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Make a suitable image for `reduceConnectedComponents()` by adding a label\n", - "# band to the `kelvin` temperature image.\n", - "kelvin = kelvin.addBands(object_id.select('labels'))\n", - "\n", - "# Calculate the mean temperature per object defined by the previously added\n", - "# \"labels\" band.\n", - "patch_temp = kelvin.reduceConnectedComponents(\n", - " reducer=ee.Reducer.mean(), labelBand='labels')\n", - "\n", - "# Add object mean temperature to the map and display it.\n", - "map_objects.add_ee_layer(patch_temp,\n", - " {'min': 303, 'max': 304, 'palette': ['yellow', 'red']},\n", - " 'Mean temperature')\n", - "display(map_objects.add_child(folium.LayerControl()))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The result is a copy of the input image without the band used to define objects, where pixel values represent the result of the reduction per object, per band.\n", - "\n", - "**Note:** `reduceToVectors()` provides similar functionality, except that the result is an `ee.FeatureCollection`, where each feature of the collection represents an object and the reduction of the pixels in each objects is expressed as a feature property for each band in the input image. `reduceToVectors()` is resource intensive, so use `reduceConnectedComponents()` whenever possible. See [Raster to Vector Conversion](https://developers.google.com/earth-engine/reducers_reduce_to_vectors) for more information." - ] - } - ], - "metadata": { - "colab": { - "name": "Object-based methods" - } - }, - "nbformat": 4, - "nbformat_minor": 2 + "nbformat": 4, + "nbformat_minor": 2 } diff --git a/guides/linked/generated/image_overview.ipynb b/guides/linked/generated/image_overview.ipynb index da3b7e957..821f67ac9 100644 --- a/guides/linked/generated/image_overview.ipynb +++ b/guides/linked/generated/image_overview.ipynb @@ -65,7 +65,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Folium setup (for interactive map display)" + "### Import `geemap`" ] }, { @@ -74,20 +74,7 @@ "metadata": {}, "outputs": [], "source": [ - "import folium\n", - "\n", - "\n", - "def add_ee_layer(self, ee_image_object, vis_params, name):\n", - " map_id_dict = ee.Image(ee_image_object).getMapId(vis_params)\n", - " folium.raster_layers.TileLayer(\n", - " tiles=map_id_dict['tile_fetcher'].url_format,\n", - " attr='Map Data © Google Earth Engine',\n", - " name=name,\n", - " overlay=True,\n", - " control=True\n", - " ).add_to(self)\n", - "\n", - "folium.Map.add_ee_layer = add_ee_layer" + "import geemap" ] }, { @@ -130,19 +117,22 @@ "metadata": {}, "outputs": [], "source": [ - "first = (ee.ImageCollection('COPERNICUS/S2_SR')\n", - " .filterBounds(ee.Geometry.Point(-70.48, 43.3631))\n", - " .filterDate('2019-01-01', '2019-12-31')\n", - " .sort('CLOUDY_PIXEL_PERCENTAGE')\n", - " .first())\n", + "first = (\n", + " ee.ImageCollection('COPERNICUS/S2_SR')\n", + " .filterBounds(ee.Geometry.Point(-70.48, 43.3631))\n", + " .filterDate('2019-01-01', '2019-12-31')\n", + " .sort('CLOUDY_PIXEL_PERCENTAGE')\n", + " .first()\n", + ")\n", "\n", "# Define a map centered on southern Maine.\n", - "map_s2 = folium.Map(location=[43.7516, -70.8155], zoom_start=11)\n", + "m = geemap.Map(center=[43.7516, -70.8155], zoom=11)\n", "\n", "# Add the image layer to the map and display it.\n", - "map_s2.add_ee_layer(\n", - " first, {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 2000}, 'first')\n", - "display(map_s2)" + "m.add_ee_layer(\n", + " first, {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 2000}, 'first'\n", + ")\n", + "display(m)" ] }, { @@ -167,11 +157,13 @@ "metadata": {}, "outputs": [], "source": [ - "uri = ('gs://gcp-public-data-landsat/LC08/01/001/002/'\n", - " 'LC08_L1GT_001002_20160817_20170322_01_T2/'\n", - " 'LC08_L1GT_001002_20160817_20170322_01_T2_B5.TIF')\n", + "uri = (\n", + " 'gs://gcp-public-data-landsat/LC08/01/001/002/'\n", + " + 'LC08_L1GT_001002_20160817_20170322_01_T2/'\n", + " + 'LC08_L1GT_001002_20160817_20170322_01_T2_B5.TIF'\n", + ")\n", "cloud_image = ee.Image.loadGeoTIFF(uri)\n", - "print(cloud_image.getInfo())" + "display(cloud_image)" ] }, { @@ -196,30 +188,29 @@ "metadata": {}, "outputs": [], "source": [ - "from pprint import pprint\n", - "\n", - "print('Create a constant image:')\n", + "# Create a constant image.\n", "image_1 = ee.Image(1)\n", - "pprint(image_1.getInfo())\n", + "display(image_1)\n", "\n", - "print('\\nConcatenate two images into one multi-band image:')\n", + "# Concatenate two images into one multi-band image.\n", "image_2 = ee.Image(2)\n", "image_3 = ee.Image.cat([image_1, image_2])\n", - "pprint(image_3.getInfo())\n", + "display(image_3)\n", "\n", - "print('\\nCreate a multi-band image from a list of constants:')\n", + "# Create a multi-band image from a list of constants.\n", "multiband = ee.Image([1, 2, 3])\n", - "pprint(multiband.getInfo())\n", + "display(multiband)\n", "\n", - "print('\\nSelect and (optionally) rename bands:')\n", + "# Select and (optionally) rename bands.\n", "renamed = multiband.select(\n", " ['constant', 'constant_1', 'constant_2'], # old names\n", - " ['band1', 'band2', 'band3']) # new names\n", - "pprint(renamed.getInfo())\n", + " ['band1', 'band2', 'band3'], # new names\n", + ")\n", + "display(renamed)\n", "\n", - "print('\\nAdd bands to an image:')\n", + "# Add bands to an image.\n", "image_4 = image_3.addBands(ee.Image(42))\n", - "pprint(image_4.getInfo())" + "display(image_4)" ] } ], diff --git a/guides/linked/generated/image_relational.ipynb b/guides/linked/generated/image_relational.ipynb index a31b6b49a..3de40922e 100644 --- a/guides/linked/generated/image_relational.ipynb +++ b/guides/linked/generated/image_relational.ipynb @@ -65,7 +65,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Folium setup (for interactive map display)" + "### Import `geemap`" ] }, { @@ -74,20 +74,7 @@ "metadata": {}, "outputs": [], "source": [ - "import folium\n", - "\n", - "\n", - "def add_ee_layer(self, ee_image_object, vis_params, name):\n", - " map_id_dict = ee.Image(ee_image_object).getMapId(vis_params)\n", - " folium.raster_layers.TileLayer(\n", - " tiles=map_id_dict['tile_fetcher'].url_format,\n", - " attr='Map Data © Google Earth Engine',\n", - " name=name,\n", - " overlay=True,\n", - " control=True\n", - " ).add_to(self)\n", - "\n", - "folium.Map.add_ee_layer = add_ee_layer" + "import geemap" ] }, { @@ -124,7 +111,7 @@ "bare = ndvi.lt(0.2).And(ndwi.lt(0))\n", "\n", "# Define a map centered on San Francisco Bay.\n", - "map_bare = folium.Map(location=[37.7726, -122.3578], zoom_start=12)\n", + "map_bare = geemap.Map(center=[37.7726, -122.3578], zoom=12)\n", "\n", "# Add the masked image layer to the map and display it.\n", "map_bare.add_ee_layer(bare.selfMask(), None, 'bare')\n", @@ -154,12 +141,13 @@ "zones = lights.gt(30).add(lights.gt(55)).add(lights.gt(62))\n", "\n", "# Define a map centered on Paris, France.\n", - "map_zones = folium.Map(location=[48.8683, 2.373], zoom_start=8)\n", + "map_zones = geemap.Map(center=[48.8683, 2.373], zoom=8)\n", "\n", "# Display the thresholded image as three distinct zones near Paris.\n", "palette = ['000000', '0000FF', '00FF00', 'FF0000']\n", "map_zones.add_ee_layer(\n", - " zones, {'min': 0, 'max': 3, 'palette': palette}, 'development zones')\n", + " zones, {'min': 0, 'max': 3, 'palette': palette}, 'development zones'\n", + ")\n", "display(map_zones)" ] }, @@ -179,17 +167,20 @@ "outputs": [], "source": [ "# Create zones using an expression, display.\n", - "zones_exp = nl_2012.expression(\"(b('stable_lights') > 62) ? 3 \"\n", - " \": (b('stable_lights') > 55) ? 2 \"\n", - " \": (b('stable_lights') > 30) ? 1 \"\n", - " \": 0\")\n", + "zones_exp = nl_2012.expression(\n", + " \"(b('stable_lights') > 62) ? 3 \"\n", + " \": (b('stable_lights') > 55) ? 2 \"\n", + " \": (b('stable_lights') > 30) ? 1 \"\n", + " ': 0'\n", + ")\n", "\n", "# Define a map centered on Paris, France.\n", - "map_zones_exp = folium.Map(location=[48.8683, 2.373], zoom_start=8)\n", + "map_zones_exp = geemap.Map(center=[48.8683, 2.373], zoom=8)\n", "\n", "# Add the image layer to the map and display it.\n", "map_zones_exp.add_ee_layer(\n", - " zones_exp, {'min': 0, 'max': 3, 'palette': palette}, 'zones exp')\n", + " zones_exp, {'min': 0, 'max': 3, 'palette': palette}, 'zones exp'\n", + ")\n", "display(map_zones_exp)" ] }, @@ -209,24 +200,24 @@ "outputs": [], "source": [ "# Load a cloudy Sentinel-2 image.\n", - "image = ee.Image(\n", - " 'COPERNICUS/S2_SR/20210114T185729_20210114T185730_T10SEG')\n", + "image = ee.Image('COPERNICUS/S2_SR/20210114T185729_20210114T185730_T10SEG')\n", "\n", "# Load another image to replace the cloudy pixels.\n", "replacement = ee.Image(\n", - " 'COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG')\n", + " 'COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG'\n", + ")\n", "\n", "# Set cloudy pixels (greater than 5% probability) to the other image.\n", "replaced = image.where(image.select('MSK_CLDPRB').gt(5), replacement)\n", "\n", "# Define a map centered on San Francisco Bay.\n", - "map_replaced = folium.Map(location=[37.7349, -122.3769], zoom_start=11)\n", + "map_replaced = geemap.Map(center=[37.7349, -122.3769], zoom=11)\n", "\n", "# Display the images on a map.\n", "vis_params = {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 2000}\n", "map_replaced.add_ee_layer(image, vis_params, 'original image')\n", "map_replaced.add_ee_layer(replaced, vis_params, 'clouds replaced')\n", - "display(map_replaced.add_child(folium.LayerControl()))" + "display(map_replaced)" ] } ], diff --git a/guides/linked/generated/image_transforms.ipynb b/guides/linked/generated/image_transforms.ipynb index 2bb86b00d..c6ff7d5a0 100644 --- a/guides/linked/generated/image_transforms.ipynb +++ b/guides/linked/generated/image_transforms.ipynb @@ -65,7 +65,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Folium setup (for interactive map display)" + "### Import `geemap`" ] }, { @@ -74,20 +74,7 @@ "metadata": {}, "outputs": [], "source": [ - "import folium\n", - "\n", - "\n", - "def add_ee_layer(self, ee_image_object, vis_params, name):\n", - " map_id_dict = ee.Image(ee_image_object).getMapId(vis_params)\n", - " folium.raster_layers.TileLayer(\n", - " tiles=map_id_dict['tile_fetcher'].url_format,\n", - " attr='Map Data © Google Earth Engine',\n", - " name=name,\n", - " overlay=True,\n", - " control=True\n", - " ).add_to(self)\n", - "\n", - "folium.Map.add_ee_layer = add_ee_layer" + "import geemap" ] }, { @@ -113,21 +100,29 @@ "\n", "# Swap in the panchromatic band and convert back to RGB.\n", "sharpened = ee.Image.cat(\n", - " [hsv.select('hue'),\n", - " hsv.select('saturation'),\n", - " image.select('B8')]).hsvToRgb()\n", + " [hsv.select('hue'), hsv.select('saturation'), image.select('B8')]\n", + ").hsvToRgb()\n", "\n", "# Define a map centered on San Francisco, California.\n", - "map_sharpened = folium.Map(location=[37.76664, -122.44829], zoom_start=13)\n", + "map_sharpened = geemap.Map(center=[37.76664, -122.44829], zoom=13)\n", "\n", "# Add the image layers to the map and display it.\n", - "map_sharpened.add_ee_layer(image, {\n", - " 'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 0.25, 'gamma': [1.1, 1.1, 1]\n", - "}, 'rgb')\n", - "map_sharpened.add_ee_layer(sharpened, {\n", - " 'min': 0, 'max': 0.25, 'gamma': [1.3, 1.3, 1.3]\n", - "}, 'pan-sharpened')\n", - "display(map_sharpened.add_child(folium.LayerControl()))" + "map_sharpened.add_ee_layer(\n", + " image,\n", + " {\n", + " 'bands': ['B4', 'B3', 'B2'],\n", + " 'min': 0,\n", + " 'max': 0.25,\n", + " 'gamma': [1.1, 1.1, 1],\n", + " },\n", + " 'rgb',\n", + ")\n", + "map_sharpened.add_ee_layer(\n", + " sharpened,\n", + " {'min': 0, 'max': 0.25, 'gamma': [1.3, 1.3, 1.3]},\n", + " 'pan-sharpened',\n", + ")\n", + "display(map_sharpened)" ] }, { @@ -158,13 +153,14 @@ "fractions = image.unmix([urban, veg, water])\n", "\n", "# Define a map centered on San Francisco Bay.\n", - "map_fractions = folium.Map(location=[37.5010, -122.1899], zoom_start=10)\n", + "map_fractions = geemap.Map(center=[37.5010, -122.1899], zoom=10)\n", "\n", "# Add the image layers to the map and display it.\n", "map_fractions.add_ee_layer(\n", - " image, {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 128}, 'image')\n", + " image, {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 128}, 'image'\n", + ")\n", "map_fractions.add_ee_layer(fractions, None, 'unmixed')\n", - "display(map_fractions.add_child(folium.LayerControl()))" + "display(map_fractions)" ] } ], diff --git a/guides/linked/generated/image_visualization.ipynb b/guides/linked/generated/image_visualization.ipynb index 1d0bcc08e..b2063a4c4 100644 --- a/guides/linked/generated/image_visualization.ipynb +++ b/guides/linked/generated/image_visualization.ipynb @@ -77,7 +77,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Folium setup (for interactive map display)" + "### Import `geemap`" ] }, { @@ -86,20 +86,7 @@ "metadata": {}, "outputs": [], "source": [ - "import folium\n", - "\n", - "\n", - "def add_ee_layer(self, ee_image_object, vis_params, name):\n", - " map_id_dict = ee.Image(ee_image_object).getMapId(vis_params)\n", - " folium.raster_layers.TileLayer(\n", - " tiles=map_id_dict['tile_fetcher'].url_format,\n", - " attr='Map Data © Google Earth Engine',\n", - " name=name,\n", - " overlay=True,\n", - " control=True\n", - " ).add_to(self)\n", - "\n", - "folium.Map.add_ee_layer = add_ee_layer" + "import geemap" ] }, { @@ -125,11 +112,11 @@ " 'bands': ['B5', 'B4', 'B3'],\n", " 'min': 0,\n", " 'max': 0.5,\n", - " 'gamma': [0.95, 1.1, 1]\n", + " 'gamma': [0.95, 1.1, 1],\n", "}\n", "\n", "# Define a map centered on San Francisco Bay.\n", - "map_l8 = folium.Map(location=[37.5010, -122.1899], zoom_start=10)\n", + "map_l8 = geemap.Map(center=[37.5010, -122.1899], zoom=10)\n", "\n", "# Add the image layer to the map and display it.\n", "map_l8.add_ee_layer(image, image_viz_params, 'false color composite')\n", @@ -166,7 +153,7 @@ "ndwi_viz = {'min': 0.5, 'max': 1, 'palette': ['00FFFF', '0000FF']}\n", "\n", "# Define a map centered on San Francisco Bay.\n", - "map_ndwi = folium.Map(location=[37.5010, -122.1899], zoom_start=10)\n", + "map_ndwi = geemap.Map(center=[37.5010, -122.1899], zoom=10)\n", "\n", "# Add the image layer to the map and display it.\n", "map_ndwi.add_ee_layer(ndwi, ndwi_viz, 'NDWI')\n", @@ -199,7 +186,7 @@ "ndwi_masked = ndwi.updateMask(ndwi.gte(0.4))\n", "\n", "# Define a map centered on San Francisco Bay.\n", - "map_ndwi_masked = folium.Map(location=[37.5010, -122.1899], zoom_start=10)\n", + "map_ndwi_masked = geemap.Map(center=[37.5010, -122.1899], zoom=10)\n", "\n", "# Add the image layer to the map and display it.\n", "map_ndwi_masked.add_ee_layer(ndwi_masked, ndwi_viz, 'NDWI masked')\n", @@ -221,12 +208,8 @@ "metadata": {}, "outputs": [], "source": [ - "image_rgb = image.visualize(**{'bands': ['B5', 'B4', 'B3'], 'max': 0.5})\n", - "ndwi_rgb = ndwi_masked.visualize(**{\n", - " 'min': 0.5,\n", - " 'max': 1,\n", - " 'palette': ['00FFFF', '0000FF']\n", - "})" + "image_rgb = image.visualize(bands=['B5', 'B4', 'B3'], max=0.5)\n", + "ndwi_rgb = ndwi_masked.visualize(min=0.5, max=1, palette=['00FFFF', '0000FF'])" ] }, { @@ -248,7 +231,7 @@ "mosaic = ee.ImageCollection([image_rgb, ndwi_rgb]).mosaic()\n", "\n", "# Define a map centered on San Francisco Bay.\n", - "map_mosaic = folium.Map(location=[37.5010, -122.1899], zoom_start=10)\n", + "map_mosaic = geemap.Map(center=[37.5010, -122.1899], zoom=10)\n", "\n", "# Add the image layer to the map and display it.\n", "map_mosaic.add_ee_layer(mosaic, None, 'mosaic')\n", @@ -282,7 +265,7 @@ "mosaic_clipped = mosaic.clip(roi)\n", "\n", "# Define a map centered on San Francisco.\n", - "map_mosaic_clipped = folium.Map(location=[37.7599, -122.4481], zoom_start=10)\n", + "map_mosaic_clipped = geemap.Map(center=[37.7599, -122.4481], zoom=10)\n", "\n", "# Add the image layer to the map and display it.\n", "map_mosaic_clipped.add_ee_layer(mosaic_clipped, None, 'mosaic clipped')\n", @@ -333,16 +316,17 @@ " '33280d', # crop mosaic\n", " 'd7cdcc', # snow and ice\n", " 'f7e084', # barren\n", - " '6f6f6f' # tundra\n", + " '6f6f6f', # tundra\n", "]\n", "\n", "# Define a map centered on the United States.\n", - "map_palette = folium.Map(location=[40.413, -99.229], zoom_start=5)\n", + "map_palette = geemap.Map(center=[40.413, -99.229], zoom=5)\n", "\n", "# Add the image layer to the map and display it. Specify the min and max labels\n", "# and the color palette matching the labels.\n", "map_palette.add_ee_layer(\n", - " cover, {'min': 0, 'max': 17, 'palette': igbp_palette}, 'IGBP classes')\n", + " cover, {'min': 0, 'max': 17, 'palette': igbp_palette}, 'IGBP classes'\n", + ")\n", "display(map_palette)" ] }, @@ -392,7 +376,7 @@ "cover_sld = cover.sldStyle(sld_intervals)\n", "\n", "# Define a map centered on the United States.\n", - "map_sld_categorical = folium.Map(location=[40.413, -99.229], zoom_start=5)\n", + "map_sld_categorical = geemap.Map(center=[40.413, -99.229], zoom=5)\n", "\n", "# Add the image layer to the map and display it.\n", "map_sld_categorical.add_ee_layer(cover_sld, None, 'IGBP classes styled')\n", @@ -442,13 +426,14 @@ " \"\"\"\n", "\n", "# Define a map centered on the United States.\n", - "map_sld_interval = folium.Map(location=[40.413, -99.229], zoom_start=5)\n", + "map_sld_interval = geemap.Map(center=[40.413, -99.229], zoom=5)\n", "\n", "# Add the image layers to the map and display it.\n", "map_sld_interval.add_ee_layer(\n", - " image.sldStyle(sld_intervals), None, 'SLD intervals')\n", + " image.sldStyle(sld_intervals), None, 'SLD intervals'\n", + ")\n", "map_sld_interval.add_ee_layer(image.sldStyle(sld_ramp), None, 'SLD ramp')\n", - "display(map_sld_interval.add_child(folium.LayerControl()))" + "display(map_sld_interval)" ] }, { @@ -489,16 +474,17 @@ "normalize_sld = template_sld.replace('_enhance_', 'Normalize')\n", "\n", "# Define a map centered on San Francisco Bay.\n", - "map_sld_continuous = folium.Map(location=[37.5010, -122.1899], zoom_start=10)\n", + "map_sld_continuous = geemap.Map(center=[37.5010, -122.1899], zoom=10)\n", "\n", "# Add the image layers to the map and display it.\n", "map_sld_continuous.add_ee_layer(\n", - " image, {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 15000}, 'Linear')\n", - "map_sld_continuous.add_ee_layer(\n", - " image.sldStyle(equalize_sld), None, 'Equalized')\n", + " image, {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 15000}, 'Linear'\n", + ")\n", + "map_sld_continuous.add_ee_layer(image.sldStyle(equalize_sld), None, 'Equalized')\n", "map_sld_continuous.add_ee_layer(\n", - " image.sldStyle(normalize_sld), None, 'Normalized')\n", - "display(map_sld_continuous.add_child(folium.LayerControl()))" + " image.sldStyle(normalize_sld), None, 'Normalized'\n", + ")\n", + "display(map_sld_continuous)" ] }, { @@ -561,7 +547,13 @@ " 'min': 0,\n", " 'max': 3000,\n", " 'palette': [\n", - " '00A600', '63C600', 'E6E600', 'E9BD3A', 'ECB176', 'EFC2B3', 'F2F2F2'\n", + " '00A600',\n", + " '63C600',\n", + " 'E6E600',\n", + " 'E9BD3A',\n", + " 'ECB176',\n", + " 'EFC2B3',\n", + " 'F2F2F2',\n", " ],\n", " 'dimensions': 500,\n", " 'region': ee.Geometry.Rectangle([-84.6, -55.9, -32.9, 15.7]),\n", @@ -573,12 +565,19 @@ " 'min': 0,\n", " 'max': 3000,\n", " 'palette': [\n", - " '00A600', '63C600', 'E6E600', 'E9BD3A', 'ECB176', 'EFC2B3', 'F2F2F2'\n", + " '00A600',\n", + " '63C600',\n", + " 'E6E600',\n", + " 'E9BD3A',\n", + " 'ECB176',\n", + " 'EFC2B3',\n", + " 'F2F2F2',\n", " ],\n", - " 'region':\n", - " ee.Geometry.LinearRing([[-84.6, 15.7], [-84.6, -55.9], [-32.9, -55.9]]),\n", + " 'region': ee.Geometry.LinearRing(\n", + " [[-84.6, 15.7], [-84.6, -55.9], [-32.9, -55.9]]\n", + " ),\n", " 'dimensions': 500,\n", - " 'crs': 'EPSG:3857'\n", + " 'crs': 'EPSG:3857',\n", "})\n", "print('Linear ring region and specified crs:', thumbnail_3)" ] diff --git a/samples/python/guides/api_monitoring.py b/samples/python/guides/api_monitoring.py index eff152ffe..e17b582a7 100644 --- a/samples/python/guides/api_monitoring.py +++ b/samples/python/guides/api_monitoring.py @@ -15,15 +15,16 @@ """Examples for /earth-engine/cloud/api_monitoring page.""" # [START earthengine__cloud_api_monitoring__workload_tags] -import ee - # Authenticate, then initialize with your Cloud Project. ee.Initialize(project='your-project') # Set a default workload tag. ee.data.setDefaultWorkloadTag('landsat-compositing') -composite = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2').filterDate( - '2020-01-01', '2021-01-01').median() +composite = ( + ee.ImageCollection('LANDSAT/LC08/C02/T1_L2') + .filterDate('2020-01-01', '2021-01-01') + .median() +) # Set a workload tag for export. ee.data.setWorkloadTag('export-jobs') diff --git a/samples/python/guides/cumulativeCost.py b/samples/python/guides/cumulativeCost.py index 99294b93c..033573202 100644 --- a/samples/python/guides/cumulativeCost.py +++ b/samples/python/guides/cumulativeCost.py @@ -30,28 +30,25 @@ # Classes 60, 80, 110, 140 have cost 1. # Classes 40, 90, 120, 130, 170 have cost 2. # Classes 50, 70, 150, 160 have cost 3. -before_remap = [60, 80, 110, 140, - 40, 90, 120, 130, 170, - 50, 70, 150, 160] -after_remap = [1, 1, 1, 1, - 2, 2, 2, 2, 2, - 3, 3, 3, 3] +before_remap = [60, 80, 110, 140, 40, 90, 120, 130, 170, 50, 70, 150, 160] +after_remap = [1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3] cost = cover.remap(before_remap, after_remap, 0) # Compute the cumulative cost to traverse the land cover. -cumulative_cost = cost.cumulativeCost(**{ - 'source': sources, - 'maxDistance': 80 * 1000 # 80 kilometers -}) +cumulative_cost = cost.cumulativeCost( + source=sources, maxDistance=(80 * 1000) # 80 kilometers +) # Define a map centered on Central Africa. -map_cost = folium.Map(location=[4.2, 18.71], zoom_start=9) +map_cost = geemap.Map(center=[4.2, 18.71], zoom=9) # Add the image layers to the map and display it. map_cost.add_ee_layer(cover, None, 'Globcover') -map_cost.add_ee_layer(cumulative_cost, {'min': 0, 'max': 5e4}, 'accumulated cost') -map_cost.add_ee_layer(ee.Image().byte().paint(geometry), - {'palette': 'FF0000'}, - 'source geometry') -display(map_cost.add_child(folium.LayerControl())) +map_cost.add_ee_layer( + cumulative_cost, {'min': 0, 'max': 5e4}, 'accumulated cost' +) +map_cost.add_ee_layer( + ee.Image().byte().paint(geometry), {'palette': 'FF0000'}, 'source geometry' +) +display(map_cost) # [END earthengine__cumulativeCost__cost] diff --git a/samples/python/guides/dependencies.py b/samples/python/guides/dependencies.py index ea41e0eac..bb7424324 100644 --- a/samples/python/guides/dependencies.py +++ b/samples/python/guides/dependencies.py @@ -41,3 +41,26 @@ def add_ee_layer(self, ee_image_object, vis_params, name): folium.Map.add_ee_layer = add_ee_layer # [END earthengine__dependencies__folium_setup] + +# [START earthengine__dependencies__geemap_import] +import geemap +# [END earthengine__dependencies__geemap_import] + +# [START earthengine__dependencies__python_setup] +import ee +import geemap +# [END earthengine__dependencies__python_setup] + +# [START earthengine__dependencies__geemap_example] +# Initialize a map object. +m = geemap.Map() + +# Define an example image. +img = ee.Image.random() + +# Add the image to the map. +m.add_ee_layer(img, None, 'Random image') + +# Display the map (you can call the object directly if it is the final line). +display(m) +# [END earthengine__dependencies__geemap_example] diff --git a/samples/python/guides/images01.py b/samples/python/guides/images01.py index 05226f04d..80aa300c2 100644 --- a/samples/python/guides/images01.py +++ b/samples/python/guides/images01.py @@ -15,51 +15,49 @@ """Google Earth Engine Developer's Guide examples for 'Images - Image information'.""" # [START earthengine__images01__image_info] -import pprint - # Load an image. image = ee.Image('LANDSAT/LC08/C02/T1/LC08_044034_20140318') # All metadata. -print('All metadata:') -pprint.pprint(image.getInfo()) +display('All metadata:', image) # Get information about the bands as a list. band_names = image.bandNames() -print('Band names:', band_names.getInfo()) # ee.List of band names +display('Band names:', band_names) # ee.List of band names # Get projection information from band 1. b1_proj = image.select('B1').projection() -print('Band 1 projection:', b1_proj.getInfo()) # ee.Projection object +display('Band 1 projection:', b1_proj) # ee.Projection object # Get scale (in meters) information from band 1. b1_scale = image.select('B1').projection().nominalScale() -print('Band 1 scale:', b1_scale.getInfo()) # ee.Number +display('Band 1 scale:', b1_scale) # ee.Number # Note that different bands can have different projections and scale. b8_scale = image.select('B8').projection().nominalScale() -print('Band 8 scale:', b8_scale.getInfo()) # ee.Number +display('Band 8 scale:', b8_scale) # ee.Number # Get a list of all metadata properties. properties = image.propertyNames() -print('Metadata properties:', - properties.getInfo()) # ee.List of metadata properties +display('Metadata properties:', properties) # ee.List of metadata properties # Get a specific metadata property. cloudiness = image.get('CLOUD_COVER') -print('CLOUD_COVER:', cloudiness.getInfo()) # ee.Number +display('CLOUD_COVER:', cloudiness) # ee.Number # Get version number (ingestion timestamp as microseconds since Unix epoch). version = image.get('system:version') -print('Version:', version.getInfo()) # ee.Number -print('Version (as ingestion date):', - ee.Date(ee.Number(version).divide(1000)).format().getInfo()) # ee.Date +display('Version:', version) # ee.Number +display( + 'Version (as ingestion date):', + ee.Date(ee.Number(version).divide(1000)).format(), +) # ee.Date # Get the timestamp. ee_date = ee.Date(image.get('system:time_start')) -print('Timestamp:', ee_date.getInfo()) # ee.Date +display('Timestamp:', ee_date) # ee.Date # Date objects transferred to the client are milliseconds since UNIX epoch; # convert to human readable date with ee.Date.format(). -print('Datetime:', ee_date.format().getInfo()) # ISO standard date string +display('Datetime:', ee_date.format()) # ISO standard date string # [END earthengine__images01__image_info] diff --git a/samples/python/guides/images02.py b/samples/python/guides/images02.py index 89abd4c77..943f1a305 100644 --- a/samples/python/guides/images02.py +++ b/samples/python/guides/images02.py @@ -19,52 +19,56 @@ # [END earthengine__images02__load_image] # [START earthengine__images02__find_image] -first = (ee.ImageCollection('COPERNICUS/S2_SR') - .filterBounds(ee.Geometry.Point(-70.48, 43.3631)) - .filterDate('2019-01-01', '2019-12-31') - .sort('CLOUDY_PIXEL_PERCENTAGE') - .first()) +first = ( + ee.ImageCollection('COPERNICUS/S2_SR') + .filterBounds(ee.Geometry.Point(-70.48, 43.3631)) + .filterDate('2019-01-01', '2019-12-31') + .sort('CLOUDY_PIXEL_PERCENTAGE') + .first() +) # Define a map centered on southern Maine. -map_s2 = folium.Map(location=[43.7516, -70.8155], zoom_start=11) +m = geemap.Map(center=[43.7516, -70.8155], zoom=11) # Add the image layer to the map and display it. -map_s2.add_ee_layer( - first, {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 2000}, 'first') -display(map_s2) +m.add_ee_layer( + first, {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 2000}, 'first' +) +display(m) # [END earthengine__images02__find_image] # [START earthengine__images02__cloud_image] -uri = ('gs://gcp-public-data-landsat/LC08/01/001/002/' - 'LC08_L1GT_001002_20160817_20170322_01_T2/' - 'LC08_L1GT_001002_20160817_20170322_01_T2_B5.TIF') +uri = ( + 'gs://gcp-public-data-landsat/LC08/01/001/002/' + + 'LC08_L1GT_001002_20160817_20170322_01_T2/' + + 'LC08_L1GT_001002_20160817_20170322_01_T2_B5.TIF' +) cloud_image = ee.Image.loadGeoTIFF(uri) -print(cloud_image.getInfo()) +display(cloud_image) # [END earthengine__images02__cloud_image] # [START earthengine__images02__create_image] -from pprint import pprint - -print('Create a constant image:') +# Create a constant image. image_1 = ee.Image(1) -pprint(image_1.getInfo()) +display(image_1) -print('\nConcatenate two images into one multi-band image:') +# Concatenate two images into one multi-band image. image_2 = ee.Image(2) image_3 = ee.Image.cat([image_1, image_2]) -pprint(image_3.getInfo()) +display(image_3) -print('\nCreate a multi-band image from a list of constants:') +# Create a multi-band image from a list of constants. multiband = ee.Image([1, 2, 3]) -pprint(multiband.getInfo()) +display(multiband) -print('\nSelect and (optionally) rename bands:') +# Select and (optionally) rename bands. renamed = multiband.select( ['constant', 'constant_1', 'constant_2'], # old names - ['band1', 'band2', 'band3']) # new names -pprint(renamed.getInfo()) + ['band1', 'band2', 'band3'], # new names +) +display(renamed) -print('\nAdd bands to an image:') +# Add bands to an image. image_4 = image_3.addBands(ee.Image(42)) -pprint(image_4.getInfo()) +display(image_4) # [END earthengine__images02__create_image] diff --git a/samples/python/guides/images03.py b/samples/python/guides/images03.py index f42ee9546..4135c2ef6 100644 --- a/samples/python/guides/images03.py +++ b/samples/python/guides/images03.py @@ -23,11 +23,11 @@ 'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 0.5, - 'gamma': [0.95, 1.1, 1] + 'gamma': [0.95, 1.1, 1], } # Define a map centered on San Francisco Bay. -map_l8 = folium.Map(location=[37.5010, -122.1899], zoom_start=10) +map_l8 = geemap.Map(center=[37.5010, -122.1899], zoom=10) # Add the image layer to the map and display it. map_l8.add_ee_layer(image, image_viz_params, 'false color composite') @@ -43,7 +43,7 @@ ndwi_viz = {'min': 0.5, 'max': 1, 'palette': ['00FFFF', '0000FF']} # Define a map centered on San Francisco Bay. -map_ndwi = folium.Map(location=[37.5010, -122.1899], zoom_start=10) +map_ndwi = geemap.Map(center=[37.5010, -122.1899], zoom=10) # Add the image layer to the map and display it. map_ndwi.add_ee_layer(ndwi, ndwi_viz, 'NDWI') @@ -55,7 +55,7 @@ ndwi_masked = ndwi.updateMask(ndwi.gte(0.4)) # Define a map centered on San Francisco Bay. -map_ndwi_masked = folium.Map(location=[37.5010, -122.1899], zoom_start=10) +map_ndwi_masked = geemap.Map(center=[37.5010, -122.1899], zoom=10) # Add the image layer to the map and display it. map_ndwi_masked.add_ee_layer(ndwi_masked, ndwi_viz, 'NDWI masked') @@ -63,12 +63,8 @@ # [END earthengine__images03__mask] # [START earthengine__images03__visualize] -image_rgb = image.visualize(**{'bands': ['B5', 'B4', 'B3'], 'max': 0.5}) -ndwi_rgb = ndwi_masked.visualize(**{ - 'min': 0.5, - 'max': 1, - 'palette': ['00FFFF', '0000FF'] -}) +image_rgb = image.visualize(bands=['B5', 'B4', 'B3'], max=0.5) +ndwi_rgb = ndwi_masked.visualize(min=0.5, max=1, palette=['00FFFF', '0000FF']) # [END earthengine__images03__visualize] # [START earthengine__images03__mosaic] @@ -76,7 +72,7 @@ mosaic = ee.ImageCollection([image_rgb, ndwi_rgb]).mosaic() # Define a map centered on San Francisco Bay. -map_mosaic = folium.Map(location=[37.5010, -122.1899], zoom_start=10) +map_mosaic = geemap.Map(center=[37.5010, -122.1899], zoom=10) # Add the image layer to the map and display it. map_mosaic.add_ee_layer(mosaic, None, 'mosaic') @@ -89,7 +85,7 @@ mosaic_clipped = mosaic.clip(roi) # Define a map centered on San Francisco. -map_mosaic_clipped = folium.Map(location=[37.7599, -122.4481], zoom_start=10) +map_mosaic_clipped = geemap.Map(center=[37.7599, -122.4481], zoom=10) # Add the image layer to the map and display it. map_mosaic_clipped.add_ee_layer(mosaic_clipped, None, 'mosaic clipped') diff --git a/samples/python/guides/images04.py b/samples/python/guides/images04.py index 1b01c7b75..f3fbb7298 100644 --- a/samples/python/guides/images04.py +++ b/samples/python/guides/images04.py @@ -37,16 +37,17 @@ '33280d', # crop mosaic 'd7cdcc', # snow and ice 'f7e084', # barren - '6f6f6f' # tundra + '6f6f6f', # tundra ] # Define a map centered on the United States. -map_palette = folium.Map(location=[40.413, -99.229], zoom_start=5) +map_palette = geemap.Map(center=[40.413, -99.229], zoom=5) # Add the image layer to the map and display it. Specify the min and max labels # and the color palette matching the labels. map_palette.add_ee_layer( - cover, {'min': 0, 'max': 17, 'palette': igbp_palette}, 'IGBP classes') + cover, {'min': 0, 'max': 17, 'palette': igbp_palette}, 'IGBP classes' +) display(map_palette) # [END earthengine__images04__palettized] @@ -82,7 +83,7 @@ cover_sld = cover.sldStyle(sld_intervals) # Define a map centered on the United States. -map_sld_categorical = folium.Map(location=[40.413, -99.229], zoom_start=5) +map_sld_categorical = geemap.Map(center=[40.413, -99.229], zoom=5) # Add the image layer to the map and display it. map_sld_categorical.add_ee_layer(cover_sld, None, 'IGBP classes styled') diff --git a/samples/python/guides/images041.py b/samples/python/guides/images041.py index 718126297..a0f3b4b61 100644 --- a/samples/python/guides/images041.py +++ b/samples/python/guides/images041.py @@ -40,14 +40,15 @@ normalize_sld = template_sld.replace('_enhance_', 'Normalize') # Define a map centered on San Francisco Bay. -map_sld_continuous = folium.Map(location=[37.5010, -122.1899], zoom_start=10) +map_sld_continuous = geemap.Map(center=[37.5010, -122.1899], zoom=10) # Add the image layers to the map and display it. map_sld_continuous.add_ee_layer( - image, {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 15000}, 'Linear') + image, {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 15000}, 'Linear' +) +map_sld_continuous.add_ee_layer(image.sldStyle(equalize_sld), None, 'Equalized') map_sld_continuous.add_ee_layer( - image.sldStyle(equalize_sld), None, 'Equalized') -map_sld_continuous.add_ee_layer( - image.sldStyle(normalize_sld), None, 'Normalized') -display(map_sld_continuous.add_child(folium.LayerControl())) + image.sldStyle(normalize_sld), None, 'Normalized' +) +display(map_sld_continuous) # [END earthengine__images041__sld_stretch] diff --git a/samples/python/guides/images042.py b/samples/python/guides/images042.py index ef54a583f..6604ce631 100644 --- a/samples/python/guides/images042.py +++ b/samples/python/guides/images042.py @@ -45,11 +45,12 @@ """ # Define a map centered on the United States. -map_sld_interval = folium.Map(location=[40.413, -99.229], zoom_start=5) +map_sld_interval = geemap.Map(center=[40.413, -99.229], zoom=5) # Add the image layers to the map and display it. map_sld_interval.add_ee_layer( - image.sldStyle(sld_intervals), None, 'SLD intervals') + image.sldStyle(sld_intervals), None, 'SLD intervals' +) map_sld_interval.add_ee_layer(image.sldStyle(sld_ramp), None, 'SLD ramp') -display(map_sld_interval.add_child(folium.LayerControl())) +display(map_sld_interval) # [END earthengine__images042__sld_elevation] diff --git a/samples/python/guides/images043.py b/samples/python/guides/images043.py index 986283104..40402aa76 100644 --- a/samples/python/guides/images043.py +++ b/samples/python/guides/images043.py @@ -32,7 +32,13 @@ 'min': 0, 'max': 3000, 'palette': [ - '00A600', '63C600', 'E6E600', 'E9BD3A', 'ECB176', 'EFC2B3', 'F2F2F2' + '00A600', + '63C600', + 'E6E600', + 'E9BD3A', + 'ECB176', + 'EFC2B3', + 'F2F2F2', ], 'dimensions': 500, 'region': ee.Geometry.Rectangle([-84.6, -55.9, -32.9, 15.7]), @@ -44,12 +50,19 @@ 'min': 0, 'max': 3000, 'palette': [ - '00A600', '63C600', 'E6E600', 'E9BD3A', 'ECB176', 'EFC2B3', 'F2F2F2' + '00A600', + '63C600', + 'E6E600', + 'E9BD3A', + 'ECB176', + 'EFC2B3', + 'F2F2F2', ], - 'region': - ee.Geometry.LinearRing([[-84.6, 15.7], [-84.6, -55.9], [-32.9, -55.9]]), + 'region': ee.Geometry.LinearRing( + [[-84.6, 15.7], [-84.6, -55.9], [-32.9, -55.9]] + ), 'dimensions': 500, - 'crs': 'EPSG:3857' + 'crs': 'EPSG:3857', }) print('Linear ring region and specified crs:', thumbnail_3) # [END earthengine__images043__get_thumb] diff --git a/samples/python/guides/images05.py b/samples/python/guides/images05.py index 0089c40bf..fed183bc9 100644 --- a/samples/python/guides/images05.py +++ b/samples/python/guides/images05.py @@ -19,8 +19,11 @@ landsat_1999 = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003') # Compute NDVI. -ndvi_1999 = (landsat_1999.select('B4').subtract(landsat_1999.select('B3')) - .divide(landsat_1999.select('B4').add(landsat_1999.select('B3')))) +ndvi_1999 = ( + landsat_1999.select('B4') + .subtract(landsat_1999.select('B3')) + .divide(landsat_1999.select('B4').add(landsat_1999.select('B3'))) +) # [END earthengine__images05__ndvi] # [START earthengine__images05__per_band] @@ -35,14 +38,16 @@ squared_difference = diff.pow(2) # Define a map centered on Australia. -map_diff = folium.Map(location=[-24.003, 133.565], zoom_start=5) +map_diff = geemap.Map(center=[-24.003, 133.565], zoom=5) # Add the image layers to the map and display it. -map_diff.add_ee_layer(diff, - {'bands': ['B4', 'B3', 'B2'], 'min': -32, 'max': 32}, - 'diff.') -map_diff.add_ee_layer(squared_difference, - {'bands': ['B4', 'B3', 'B2'], 'max': 1000}, - 'squared diff.') -display(map_diff.add_child(folium.LayerControl())) +map_diff.add_ee_layer( + diff, {'bands': ['B4', 'B3', 'B2'], 'min': -32, 'max': 32}, 'diff.' +) +map_diff.add_ee_layer( + squared_difference, + {'bands': ['B4', 'B3', 'B2'], 'max': 1000}, + 'squared diff.', +) +display(map_diff) # [END earthengine__images05__per_band] diff --git a/samples/python/guides/images06.py b/samples/python/guides/images06.py index d54bf7d9b..7a5875132 100644 --- a/samples/python/guides/images06.py +++ b/samples/python/guides/images06.py @@ -20,20 +20,20 @@ # Compute the EVI using an expression. evi = image.expression( - '2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', { + '2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', + { 'NIR': image.select('B5'), 'RED': image.select('B4'), - 'BLUE': image.select('B2') - }) + 'BLUE': image.select('B2'), + }, +) # Define a map centered on San Francisco Bay. -map_evi = folium.Map(location=[37.4675, -122.1363], zoom_start=9) +map_evi = geemap.Map(center=[37.4675, -122.1363], zoom=9) # Add the image layer to the map and display it. -map_evi.add_ee_layer(evi, { - 'min': -1, - 'max': 1, - 'palette': ['a6611a', 'f5f5f5', '4dac26'] -}, 'evi') +map_evi.add_ee_layer( + evi, {'min': -1, 'max': 1, 'palette': ['a6611a', 'f5f5f5', '4dac26']}, 'evi' +) display(map_evi) # [END earthengine__images06__evi_expression] diff --git a/samples/python/guides/images07.py b/samples/python/guides/images07.py index 3f3eed571..d8c186a43 100644 --- a/samples/python/guides/images07.py +++ b/samples/python/guides/images07.py @@ -26,7 +26,7 @@ bare = ndvi.lt(0.2).And(ndwi.lt(0)) # Define a map centered on San Francisco Bay. -map_bare = folium.Map(location=[37.7726, -122.3578], zoom_start=12) +map_bare = geemap.Map(center=[37.7726, -122.3578], zoom=12) # Add the masked image layer to the map and display it. map_bare.add_ee_layer(bare.selfMask(), None, 'bare') diff --git a/samples/python/guides/images08.py b/samples/python/guides/images08.py index 85bb1b771..81551e76a 100644 --- a/samples/python/guides/images08.py +++ b/samples/python/guides/images08.py @@ -23,27 +23,31 @@ zones = lights.gt(30).add(lights.gt(55)).add(lights.gt(62)) # Define a map centered on Paris, France. -map_zones = folium.Map(location=[48.8683, 2.373], zoom_start=8) +map_zones = geemap.Map(center=[48.8683, 2.373], zoom=8) # Display the thresholded image as three distinct zones near Paris. palette = ['000000', '0000FF', '00FF00', 'FF0000'] map_zones.add_ee_layer( - zones, {'min': 0, 'max': 3, 'palette': palette}, 'development zones') + zones, {'min': 0, 'max': 3, 'palette': palette}, 'development zones' +) display(map_zones) # [END earthengine__images08__conditional] # [START earthengine__images08__conditional_exp] # Create zones using an expression, display. -zones_exp = nl_2012.expression("(b('stable_lights') > 62) ? 3 " - ": (b('stable_lights') > 55) ? 2 " - ": (b('stable_lights') > 30) ? 1 " - ": 0") +zones_exp = nl_2012.expression( + "(b('stable_lights') > 62) ? 3 " + ": (b('stable_lights') > 55) ? 2 " + ": (b('stable_lights') > 30) ? 1 " + ': 0' +) # Define a map centered on Paris, France. -map_zones_exp = folium.Map(location=[48.8683, 2.373], zoom_start=8) +map_zones_exp = geemap.Map(center=[48.8683, 2.373], zoom=8) # Add the image layer to the map and display it. map_zones_exp.add_ee_layer( - zones_exp, {'min': 0, 'max': 3, 'palette': palette}, 'zones exp') + zones_exp, {'min': 0, 'max': 3, 'palette': palette}, 'zones exp' +) display(map_zones_exp) # [END earthengine__images08__conditional_exp] diff --git a/samples/python/guides/images09.py b/samples/python/guides/images09.py index b3902d5f5..40fa2c7ee 100644 --- a/samples/python/guides/images09.py +++ b/samples/python/guides/images09.py @@ -16,22 +16,22 @@ # [START earthengine__images09__where_operator] # Load a cloudy Sentinel-2 image. -image = ee.Image( - 'COPERNICUS/S2_SR/20210114T185729_20210114T185730_T10SEG') +image = ee.Image('COPERNICUS/S2_SR/20210114T185729_20210114T185730_T10SEG') # Load another image to replace the cloudy pixels. replacement = ee.Image( - 'COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG') + 'COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG' +) # Set cloudy pixels (greater than 5% probability) to the other image. replaced = image.where(image.select('MSK_CLDPRB').gt(5), replacement) # Define a map centered on San Francisco Bay. -map_replaced = folium.Map(location=[37.7349, -122.3769], zoom_start=11) +map_replaced = geemap.Map(center=[37.7349, -122.3769], zoom=11) # Display the images on a map. vis_params = {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 2000} map_replaced.add_ee_layer(image, vis_params, 'original image') map_replaced.add_ee_layer(replaced, vis_params, 'clouds replaced') -display(map_replaced.add_child(folium.LayerControl())) +display(map_replaced) # [END earthengine__images09__where_operator] diff --git a/samples/python/guides/images10.py b/samples/python/guides/images10.py index d7eee1c20..a7bbb003d 100644 --- a/samples/python/guides/images10.py +++ b/samples/python/guides/images10.py @@ -25,15 +25,17 @@ smooth = image.convolve(boxcar) # Define a map centered on Oakland, California. -map_smooth = folium.Map(location=[37.8694, -121.9785], zoom_start=11) +map_smooth = geemap.Map(center=[37.8694, -121.9785], zoom=11) # Add the image layers to the map and display it. Compare the smoothed result to # the original. map_smooth.add_ee_layer( - image, {'bands': ['B5', 'B4', 'B3'], 'max': 0.5}, 'input image') + image, {'bands': ['B5', 'B4', 'B3'], 'max': 0.5}, 'input image' +) map_smooth.add_ee_layer( - smooth, {'bands': ['B5', 'B4', 'B3'], 'max': 0.5}, 'smoothed') -display(map_smooth.add_child(folium.LayerControl())) + smooth, {'bands': ['B5', 'B4', 'B3'], 'max': 0.5}, 'smoothed' +) +display(map_smooth) # [END earthengine__images10__smoothing] # [START earthengine__images10__edges] @@ -44,15 +46,17 @@ edgy = image.convolve(laplacian) # Define a map centered on Oakland, California. -map_edgy = folium.Map(location=[37.8694, -121.9785], zoom_start=11) +map_edgy = geemap.Map(center=[37.8694, -121.9785], zoom=11) # Add the image layers to the map and display it. Compare the edges result to # the original. map_edgy.add_ee_layer( - image, {'bands': ['B5', 'B4', 'B3'], 'max': 0.5}, 'input image') + image, {'bands': ['B5', 'B4', 'B3'], 'max': 0.5}, 'input image' +) map_edgy.add_ee_layer( - edgy, {'bands': ['B5', 'B4', 'B3'], 'max': 0.5, 'format': 'png'}, 'edges') -display(map_edgy.add_child(folium.LayerControl())) + edgy, {'bands': ['B5', 'B4', 'B3'], 'max': 0.5, 'format': 'png'}, 'edges' +) +display(map_edgy) # [END earthengine__images10__edges] # [START earthengine__images10__fixed] @@ -64,5 +68,5 @@ rows = [row, row, row, row, center_row, row, row, row, row] # Create the kernel from the weights. kernel = ee.Kernel.fixed(9, 9, rows, -4, -4, False) -print(kernel) +display(kernel) # [END earthengine__images10__fixed] diff --git a/samples/python/guides/images11.py b/samples/python/guides/images11.py index a3975368e..d5ab99848 100644 --- a/samples/python/guides/images11.py +++ b/samples/python/guides/images11.py @@ -16,22 +16,23 @@ # [START earthengine__images11__morphology] # Load a Landsat 8 image, select the NIR band, threshold, display. -image = (ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318') - .select(4).gt(0.2)) +image = ( + ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318').select(4).gt(0.2) +) # Define a kernel. kernel = ee.Kernel.circle(radius=1) # Perform an erosion followed by a dilation, display. -opened = (image - .focalMin(kernel=kernel, iterations=2) - .focalMax(kernel=kernel, iterations=2)) +opened = image.focalMin(kernel=kernel, iterations=2).focalMax( + kernel=kernel, iterations=2 +) # Define a map centered on Redwood City, California. -map_opened = folium.Map(location=[37.5010, -122.1899], zoom_start=13) +map_opened = geemap.Map(center=[37.5010, -122.1899], zoom=13) # Add the image layers to the map and display it. map_opened.add_ee_layer(image, None, 'NIR threshold') map_opened.add_ee_layer(opened, None, 'opened') -display(map_opened.add_child(folium.LayerControl())) +display(map_opened) # [END earthengine__images11__morphology] diff --git a/samples/python/guides/images12.py b/samples/python/guides/images12.py index 0348cae8c..e26ec355d 100644 --- a/samples/python/guides/images12.py +++ b/samples/python/guides/images12.py @@ -28,12 +28,14 @@ direction = xy_grad.select('y').atan2(xy_grad.select('x')) # Define a map centered on San Francisco Bay. -map_gradient = folium.Map(location=[37.7295, -122.054], zoom_start=10) +map_gradient = geemap.Map(center=[37.7295, -122.054], zoom=10) # Add the image layers to the map and display it. map_gradient.add_ee_layer( - direction, {'min': -2, 'max': 2, 'format': 'png'}, 'direction') + direction, {'min': -2, 'max': 2, 'format': 'png'}, 'direction' +) map_gradient.add_ee_layer( - gradient, {'min': -7, 'max': 7, 'format': 'png'}, 'gradient') -display(map_gradient.add_child(folium.LayerControl())) + gradient, {'min': -7, 'max': 7, 'format': 'png'}, 'gradient' +) +display(map_gradient) # [END earthengine__images12__gradients] diff --git a/samples/python/guides/images13.py b/samples/python/guides/images13.py index d09420a6e..217c1ede3 100644 --- a/samples/python/guides/images13.py +++ b/samples/python/guides/images13.py @@ -22,7 +22,7 @@ canny = ee.Algorithms.CannyEdgeDetector(image=image, threshold=10, sigma=1) # Define a map centered on San Francisco Bay and add the image layer to it. -map_canny = folium.Map(location=[37.7295, -122.054], zoom_start=10) +map_canny = geemap.Map(center=[37.7295, -122.054], zoom=10) map_canny.add_ee_layer(canny, None, 'canny') # [END earthengine__images13__canny] @@ -32,5 +32,5 @@ # Add the image layer to the map and display it. map_canny.add_ee_layer(hough, None, 'hough') -display(map_canny.add_child(folium.LayerControl())) +display(map_canny) # [END earthengine__images13__hough] diff --git a/samples/python/guides/images14.py b/samples/python/guides/images14.py index 6179ee564..f515e5482 100644 --- a/samples/python/guides/images14.py +++ b/samples/python/guides/images14.py @@ -20,7 +20,8 @@ # Define a "fat" Gaussian kernel. fat = ee.Kernel.gaussian( - radius=3, sigma=3, units='pixels', normalize=True, magnitude=-1) + radius=3, sigma=3, units='pixels', normalize=True, magnitude=-1 +) # Define a "skinny" Gaussian kernel. skinny = ee.Kernel.gaussian(radius=3, sigma=1, units='pixels', normalize=True) @@ -32,11 +33,12 @@ zero_xings = image.convolve(dog).zeroCrossing() # Define a map centered on San Francisco Bay. -map_zero_xings = folium.Map(location=[37.7295, -122.054], zoom_start=10) +map_zero_xings = geemap.Map(center=[37.7295, -122.054], zoom=10) # Add the image layers to the map and display it. map_zero_xings.add_ee_layer(image, {'max': 12000}, 'image') map_zero_xings.add_ee_layer( - zero_xings.selfMask(), {'palette': 'FF0000'}, 'zero crossings') -display(map_zero_xings.add_child(folium.LayerControl())) + zero_xings.selfMask(), {'palette': 'FF0000'}, 'zero crossings' +) +display(map_zero_xings) # [END earthengine__images14__zero_crossings] diff --git a/samples/python/guides/images15.py b/samples/python/guides/images15.py index 94e7a8b6d..12ccc7dc9 100644 --- a/samples/python/guides/images15.py +++ b/samples/python/guides/images15.py @@ -23,19 +23,27 @@ # Swap in the panchromatic band and convert back to RGB. sharpened = ee.Image.cat( - [hsv.select('hue'), - hsv.select('saturation'), - image.select('B8')]).hsvToRgb() + [hsv.select('hue'), hsv.select('saturation'), image.select('B8')] +).hsvToRgb() # Define a map centered on San Francisco, California. -map_sharpened = folium.Map(location=[37.76664, -122.44829], zoom_start=13) +map_sharpened = geemap.Map(center=[37.76664, -122.44829], zoom=13) # Add the image layers to the map and display it. -map_sharpened.add_ee_layer(image, { - 'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 0.25, 'gamma': [1.1, 1.1, 1] -}, 'rgb') -map_sharpened.add_ee_layer(sharpened, { - 'min': 0, 'max': 0.25, 'gamma': [1.3, 1.3, 1.3] -}, 'pan-sharpened') -display(map_sharpened.add_child(folium.LayerControl())) +map_sharpened.add_ee_layer( + image, + { + 'bands': ['B4', 'B3', 'B2'], + 'min': 0, + 'max': 0.25, + 'gamma': [1.1, 1.1, 1], + }, + 'rgb', +) +map_sharpened.add_ee_layer( + sharpened, + {'min': 0, 'max': 0.25, 'gamma': [1.3, 1.3, 1.3]}, + 'pan-sharpened', +) +display(map_sharpened) # [END earthengine__images15__pan_sharpening] diff --git a/samples/python/guides/images16.py b/samples/python/guides/images16.py index ba9d8c41a..fa39244eb 100644 --- a/samples/python/guides/images16.py +++ b/samples/python/guides/images16.py @@ -28,11 +28,12 @@ fractions = image.unmix([urban, veg, water]) # Define a map centered on San Francisco Bay. -map_fractions = folium.Map(location=[37.5010, -122.1899], zoom_start=10) +map_fractions = geemap.Map(center=[37.5010, -122.1899], zoom=10) # Add the image layers to the map and display it. map_fractions.add_ee_layer( - image, {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 128}, 'image') + image, {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 128}, 'image' +) map_fractions.add_ee_layer(fractions, None, 'unmixed') -display(map_fractions.add_child(folium.LayerControl())) +display(map_fractions) # [END earthengine__images16__unmixing] diff --git a/samples/python/guides/images17.py b/samples/python/guides/images17.py index 63c6b51ff..0b1a6348f 100644 --- a/samples/python/guides/images17.py +++ b/samples/python/guides/images17.py @@ -28,12 +28,13 @@ entropy = nir.entropy(square) # Define a map centered on Golden Gate Park, San Francisco. -map_texture = folium.Map(location=[37.769833, -122.466123], zoom_start=17) +map_texture = geemap.Map(center=[37.769833, -122.466123], zoom=17) # Add the image layers to the map. map_texture.add_ee_layer(image, {'max': 255}, 'image') map_texture.add_ee_layer( - entropy, {'min': 1, 'max': 5, 'palette': ['0000CC', 'CC0000']}, 'entropy') + entropy, {'min': 1, 'max': 5, 'palette': ['0000CC', 'CC0000']}, 'entropy' +) # [END earthengine__images17__entropy] # [START earthengine__images17__glcm] @@ -42,9 +43,11 @@ contrast = glcm.select('N_contrast') # Add the contrast layer to the map. -map_texture.add_ee_layer(contrast, - {'min': 0, 'max': 1500, 'palette': ['0000CC', 'CC0000']}, - 'contrast') +map_texture.add_ee_layer( + contrast, + {'min': 0, 'max': 1500, 'palette': ['0000CC', 'CC0000']}, + 'contrast', +) # [END earthengine__images17__glcm] # [START earthengine__images17__gearys] @@ -65,8 +68,10 @@ gearys = nir.subtract(neighs).pow(2).reduce(ee.Reducer.sum()).divide(pow(9, 2)) # Add the Geary's C layer to the map and display it. -map_texture.add_ee_layer(gearys, - {'min': 20, 'max': 2500, 'palette': ['0000CC', 'CC0000']}, - "Geary's C") -display(map_texture.add_child(folium.LayerControl())) +map_texture.add_ee_layer( + gearys, + {'min': 20, 'max': 2500, 'palette': ['0000CC', 'CC0000']}, + "Geary's C", +) +display(map_texture) # [END earthengine__images17__gearys] diff --git a/samples/python/guides/images18.py b/samples/python/guides/images18.py index 11cdbbcc2..1b1d678b7 100644 --- a/samples/python/guides/images18.py +++ b/samples/python/guides/images18.py @@ -21,14 +21,17 @@ # Import a Landsat 8 image, subset the thermal band, and clip to the # area of interest. -kelvin = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318').select( - ['B10'], ['kelvin']).clip(aoi) +kelvin = ( + ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318') + .select(['B10'], ['kelvin']) + .clip(aoi) +) # Threshold the thermal band to set hot pixels as value 1, mask all else. -hotspots = (kelvin.gt(303).selfMask().rename('hotspots')) +hotspots = kelvin.gt(303).selfMask().rename('hotspots') # Define a map centered on Redwood City, California. -map_objects = folium.Map(location=[37.5010, -122.1899], zoom_start=13) +map_objects = geemap.Map(center=[37.5010, -122.1899], zoom=13) # Add the image layers to the map. map_objects.add_ee_layer(kelvin, {'min': 288, 'max': 305}, 'Kelvin') @@ -38,7 +41,8 @@ # [START earthengine__images18__label_objects] # Uniquely label the hotspot image objects. object_id = hotspots.connectedComponents( - connectedness=ee.Kernel.plus(1), maxSize=128) + connectedness=ee.Kernel.plus(1), maxSize=128 +) # Add the uniquely ID'ed objects to the map. map_objects.add_ee_layer(object_id.randomVisualizer(), None, 'Objects') @@ -47,7 +51,8 @@ # [START earthengine__images18__object_size] # Compute the number of pixels in each object defined by the "labels" band. object_size = object_id.select('labels').connectedPixelCount( - maxSize=128, eightConnected=False) + maxSize=128, eightConnected=False +) # Add the object pixel count to the map. map_objects.add_ee_layer(object_size, None, 'Object n pixels') @@ -63,9 +68,11 @@ object_area = object_size.multiply(pixel_area) # Add the object area to the map. -map_objects.add_ee_layer(object_area, - {'min': 0, 'max': 30000, 'palette': ['0000FF', 'FF00FF']}, - 'Object area m^2') +map_objects.add_ee_layer( + object_area, + {'min': 0, 'max': 30000, 'palette': ['0000FF', 'FF00FF']}, + 'Object area m^2', +) # [END earthengine__images18__object_area] # [START earthengine__images18__area_mask] @@ -87,11 +94,14 @@ # Calculate the mean temperature per object defined by the previously added # "labels" band. patch_temp = kelvin.reduceConnectedComponents( - reducer=ee.Reducer.mean(), labelBand='labels') + reducer=ee.Reducer.mean(), labelBand='labels' +) # Add object mean temperature to the map and display it. -map_objects.add_ee_layer(patch_temp, - {'min': 303, 'max': 304, 'palette': ['yellow', 'red']}, - 'Mean temperature') -display(map_objects.add_child(folium.LayerControl())) +map_objects.add_ee_layer( + patch_temp, + {'min': 303, 'max': 304, 'palette': ['yellow', 'red']}, + 'Mean temperature', +) +display(map_objects) # [END earthengine__images18__reduce_objects] diff --git a/samples/python/guides/register.py b/samples/python/guides/register.py index f02a3de42..64f1b3aa8 100644 --- a/samples/python/guides/register.py +++ b/samples/python/guides/register.py @@ -17,9 +17,11 @@ # [START earthengine__register__displacement] # Load the two images to be registered. image1 = ee.Image( - 'SKYSAT/GEN-A/PUBLIC/ORTHO/MULTISPECTRAL/s01_20150502T082736Z') + 'SKYSAT/GEN-A/PUBLIC/ORTHO/MULTISPECTRAL/s01_20150502T082736Z' +) image2 = ee.Image( - 'SKYSAT/GEN-A/PUBLIC/ORTHO/MULTISPECTRAL/s01_20150305T081019Z') + 'SKYSAT/GEN-A/PUBLIC/ORTHO/MULTISPECTRAL/s01_20150305T081019Z' +) # Use bicubic resampling during registration. image1_orig = image1.resample('bicubic') @@ -30,11 +32,9 @@ image2_red_band = image2_orig.select('R') # Determine the displacement by matching only the 'R' bands. -displacement = image2_red_band.displacement(**{ - 'referenceImage': image1_red_band, - 'maxOffset': 50.0, - 'patchWidth': 100.0 -}) +displacement = image2_red_band.displacement( + referenceImage=image1_red_band, maxOffset=50.0, patchWidth=100.0 +) # Compute image offset and direction. offset = displacement.select('dx').hypot(displacement.select('dy')) @@ -44,7 +44,7 @@ import math # Define a map centered on Central Kenya. -map_registration = folium.Map(location=[0.58, 37.46], zoom_start=15) +map_registration = geemap.Map(center=[0.58, 37.46], zoom=15) # Add the image layers to the map. map_registration.add_ee_layer(offset, {'min': 0, 'max': 20}, 'offset') @@ -65,13 +65,11 @@ # [END earthengine__register__displace] # [START earthengine__register__register] -also_registered = image2_orig.register(**{ - 'referenceImage': image1_orig, - 'maxOffset': 50.0, - 'patchWidth': 100.0 -}) +also_registered = image2_orig.register( + referenceImage=image1_orig, maxOffset=50.0, patchWidth=100.0 +) # Add the image layer to the map and display it. map_registration.add_ee_layer(also_registered, vis_params, 'Also Registered') -display(map_registration.add_child(folium.LayerControl())) +display(map_registration) # [END earthengine__register__register]