Path: blob/master/tutorials/ImageCollection/04_mapping_over_image_collection.ipynb
2313 views
Mapping over an ImageCollection
To apply a function to every Image in an ImageCollection use imageCollection.map(). The only argument to map() is a function which takes one parameter: an ee.Image. For example, the following code adds a timestamp band to every image in the collection:
Install Earth Engine API and geemap
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).
Create an interactive map
The default basemap is Google Satellite. Additional basemaps can be added using the Map.add_basemap() function.
Add Earth Engine Python script
For one-line function, you can also use a lambda function, which is a small anonymous function.
Note that in the predefined function, the metadata() method is used to create a new Image from the value of a property. As discussed in the Reducing and Compositing sections, having that time band is useful for the linear modeling of change and for making composites.
The mapped function is limited in the operations it can perform. Specifically, it can’t modify variables outside the function; it can’t print anything; it can’t use JavaScript ‘if’ or ‘for’ statements. However, you can use ee.Algorithms.If() to perform conditional operations in a mapped function. For example:
Inspect the list of images in the output ImageCollection and note that the when the condition evaluated by the If() algorithm is true, the output contains a constant image. Although this demonstrates a server-side conditional function (learn more about client vs. server in Earth Engine on this page), avoid If() in general and use filters instead.
Use the lambda function
View source on GitHub
Run in Google Colab