Tutorial, cloud computing, geospatial analysis, coding

Understanding GEE’s (Google Earth Engine) Map Function (Python API)

Tutorial note, a method implementation

Tahjudil Witra
4 min readOct 24, 2022
Photo by Matheo JBT on Unsplash

Hi everyone,

In today's story, I would like to share my experience using the GEE’S map function. In the past, I found myself struggling to understand how to make use of that function and how it actually works particularly in python API. So I think it will be worth it if I can share some of my experiences. Hope you enjoy it :D

Definition

Based on my experience, I could define that the map function in GEE is another way to loop the earth engine (ee) object which fully makes use of the server-side operation. In other-word, during its iteration, there is no information back and forth to the client side. Therefore in the GEE guide, they insist on not mixing up between server and client functions.

The objects that can implement the map function

Basically, as mentioned above, we use the map for looping an object. The looping object should have another object inside it. With regard to the GEE object, we can use the map on collection (ee.FeatureCollection, ee.ImageCollection), List (ee.List), dictionary (ee.Dictionary), etc. However, my favourite one is mapping on the list object.

How does the map() work?

Basic

Basic implementation of the map function

Above is an example of how to implement the map function.

First, we construct the function that processes each ee object. We have to make sure there is no client-side operation inside the function if we want to use the map to loop the collection. Otherwise, it will turn out like this:

Error when there is a client-side operation during mapping the function

Based on the GEE’s guide, generally, the server-side objects are initialized as ee.Thing and any method on that object ee.Thing.method() as well. For more about the client vs server, please check the google guide.

Second, calling the function. We can use the lambda keyword to call the function and pass the object as in the 10th line above. And then it will return a collection to the result variable. The data type should be the same between the iterated input and the return.

This is my version of abstraction of the map() implementation: “I try to implement the process() on each object in an object collection. the process() should accept a parameter at an object/element level (not collection). the map() will loop the object collection. Then, through the lambda keyword, an object is picked and fed to process(). Eventually, a processed object collection is a return.”

Mapping with more than one parameters

The basic example above looping only to itself, how about we want to add one or more parameters to the function? For instance, we want to buffer features in a feature collection to a certain range. Hence we have to add a range parameter to the function. This is how I do it:

An example of using the map function with more than one argument

The idea is still similar to the basic however the map function only allows one parameter, therefore, we wrap the main function and provide other parameters from the outside of the main i.e., do_buffer() in this case. In addition, in implementation, we just need to call the wrapper function like the usual python function. The return is a buffered feature collection.

Mapping a collection but returning another type of collection

Suppose we want to loop over an ee.ImageCollection and then return an ee.FeatureCollection. Based on my experience, it will not work if we directly loop over the ee.imageCollection and push to return an ee.FeatureCollection. Instead, we can trick it by doing the mapping/looping over an ee.List. Here is the way I implement it:

An example of using the map to return to a different data type

Above is an example where we want to extract pixel values over an ee.ImageCollection. So, we need to loop over images in the image collection.

In this case, we make use of the method of sampleRectangle(). SampleRectangle() is a method to extract the pixel value of an image over a certain area or geometry. It will return a feature that contains the pixel value as a property.

In order to prevent some unexpected results, we need to transform the ee.ImageCollection into ee.List then do the map() over the list. So we could have a list of images (ee.List([ee.Image, ee.Image, … ])). Of course, wrap_do_sampleRect() will return a ee.List but it will contain the features (ee.List([ee.Feature, ee.Feature, … ])). From there we can use to transform them into an ee.FeatureCollection.

Wrapping up

We have learnt how to loop an object collection using map() in GEE python API. I shared three implementation cases: basic, map with more than one parameter, map to another format or map over the list.

Actually, I think there are many ways to implement them but at least those three I have experienced. Hope this will be helpful. :D

--

--

Tahjudil Witra

MSc at TU Berlin Specialization in GIS and Remote Sensing. Interested in Earth Observation, AI, business, human behaviour, and leadership.