[GIS] Map function over ImageCollection in Python 2.7 in Earth Engine

google-earth-engine

I am trying to map a function over an image collection in python. I am able to run same process in JavaScript, but in Python I am having issues.

Here is the code in JavaScript, which returns a JSON full of values:

var smap = ee.ImageCollection('NASA_USDA/HSL/SMAP_soil_moisture').filterDate('2019-04-25', '2019-05-17')

var geom = ee.Geometry.Point([-94.440367, 42.1068856])

var res = smap.map(function(image) {
  var reduced = image.reduceRegion({geometry: geom, 
                                    reducer: ee.Reducer.mean(), 
                                    crs: 'EPSG:4326', 
                                    scale: 30})
  return image.set('value', reduced);
});

var vals = res.aggregate_array('value');
print(vals);

However, when I run this in python, I get an empty list:

smap = ee.ImageCollection('NASA_USDA/HSL/SMAP_soil_moisture').filterDate('2019-04-25', '2019-05-17')

geom = ee.Geometry.Point([-94.440367, 42.1068856])

def eeAlgo(image):
    reduced = image.reduceRegion(geometry=geom, reducer=ee.Reducer.mean(), crs='EPSG:4326', scale=30)
    image.set('value', reduced)

    return image

vals = smap.map(algorithm=eeAlgo)

vals.aggregate_array('value').getInfo()

> []

Is there some difference between running this in Python that I am missing? Or is this a bug that I should file at https://github.com/google/earthengine-api? I am using Python 2.7.

Best Answer

There is a little difference between you JS code and you python code, and that is where your code "breaks". This is your JS:

var res = smap.map(function(image) {
  var reduced = image.reduceRegion({geometry: geom, 
                                    reducer: ee.Reducer.mean(), 
                                    crs: 'EPSG:4326', 
                                    scale: 30})
  return image.set('value', reduced);
});

but in your python you make an extra line: image.set('value', reduced) and then return image, but image is not an object to which you can set a value, set is a function that return an image, so the code should be:

def eeAlgo(image):
    reduced = image.reduceRegion(geometry=geom, reducer=ee.Reducer.mean(), crs='EPSG:4326', scale=30)
    image = image.set('value', reduced)
    return image