Earth Engine implements morphological operations as focal operations, specifically focal_max()
, focal_min()
, focal_median()
, and focal_mode()
instance methods in the Image
class. (These are shortcuts for the more general reduceNeighborhood()
, which can input the pixels in a kernel to any reducer with a numeric output. See this page for more information on reducing neighborhoods). The morphological operators are useful for performing operations such as erosion, dilation, opening and closing. For example, to perform an opening operation, use focal_min()
followed by focal_max()
:
Install the Earth Engine Python API and geemap. The geemap Python package is built upon the ipyleaflet and folium packages and implements several methods for interacting with Earth Engine data layers, such as Map.addLayer()
, Map.setCenter()
, and Map.centerObject()
.
The following script checks if the geemap package has been installed. If not, it will install geemap, which automatically installs its dependencies, including earthengine-api, folium, and ipyleaflet.
Important note: A key difference between folium and ipyleaflet is that ipyleaflet is built upon ipywidgets and allows bidirectional communication between the front-end and the backend enabling the use of the map to capture user input, while folium is meant for displaying static data only (source). Note that Google Colab currently does not support ipyleaflet (source). Therefore, if you are using geemap with Google Colab, you should use import geemap.foliumap
. If you are using geemap with binder or a local Jupyter notebook server, you can use import geemap
, which provides more functionalities for capturing user input (e.g., mouse-clicking and moving).
# Installs geemap package
import subprocess
try:
import geemap
except ImportError:
print("geemap package not installed. Installing ...")
subprocess.check_call(["python", "-m", "pip", "install", "geemap"])
# Checks whether this notebook is running on Google Colab
try:
import google.colab
import geemap.foliumap as emap
except:
import geemap as emap
# Authenticates and initializes Earth Engine
import ee
try:
ee.Initialize()
except Exception as e:
ee.Authenticate()
ee.Initialize()
The default basemap is Google Satellite
. Additional basemaps can be added using the Map.add_basemap()
function.
Map = geemap.Map(center=[40, -100], zoom=4)
Map.add_basemap("ROADMAP") # Add Google Map
Map
image = ee.Image("LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318").select(4).gt(0.2)
Map.setCenter(-122.1899, 37.5010, 13)
Map.addLayer(image, {}, "NIR threshold")
# Define a kernel.
kernel = ee.Kernel.circle(**{"radius": 1})
# Perform an erosion followed by a dilation, display.
opened = image.focal_min(**{"kernel": kernel, "iterations": 2}).focal_max(
**{"kernel": kernel, "iterations": 2}
)
Map.addLayer(opened, {}, "opened")
Map.addLayerControl()
Map
Note that in the previous example, a kernel argument is provided to the morphological operator. The pixels covered by non-zero elements of the kernel are used in the computation. The iterations argument indicates how many times to apply the operator.