#!/usr/bin/env python # coding: utf-8 # # # # #
View source on GitHubNotebook Viewer Run in Google Colab
# # Morphological Operations # 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](https://developers.google.com/earth-engine/reducers_reduce_neighborhood) 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](http://en.wikipedia.org/wiki/Opening_(morphology)), use `focal_min()` followed by `focal_max()`: # ## Install Earth Engine API and geemap # Install the [Earth Engine Python API](https://developers.google.com/earth-engine/python_install) and [geemap](https://github.com/gee-community/geemap). The **geemap** Python package is built upon the [ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet) and [folium](https://github.com/python-visualization/folium) packages and implements several methods for 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](https://github.com/gee-community/geemap#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](https://blog.jupyter.org/interactive-gis-in-jupyter-with-ipyleaflet-52f9657fa7a)). Note that [Google Colab](https://colab.research.google.com/) currently does not support ipyleaflet ([source](https://github.com/googlecolab/colabtools/issues/60#issuecomment-596225619)). Therefore, if you are using geemap with Google Colab, you should use [`import geemap.foliumap`](https://github.com/gee-community/geemap/blob/master/geemap/foliumap.py). If you are using geemap with [binder](https://mybinder.org/) or a local Jupyter notebook server, you can use [`import geemap`](https://github.com/gee-community/geemap/blob/master/geemap/geemap.py), which provides more functionalities for capturing user input (e.g., mouse-clicking and moving). # In[ ]: # 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() # ## Create an interactive map # The default basemap is `Google Satellite`. [Additional basemaps](https://github.com/gee-community/geemap/blob/master/geemap/geemap.py#L13) can be added using the `Map.add_basemap()` function. # In[ ]: Map = geemap.Map(center=[40, -100], zoom=4) Map.add_basemap("ROADMAP") # Add Google Map Map # ## Add Earth Engine Python script # In[ ]: 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.