0

In earthengine-api (Python),I use the the function called .map(),for example:foo = col.map(lambda arg: arg),However ,I want to do some complex mapping operations,According to the official tutorial,Anonymous function mapping is achieved via lambda functions, which accept only a single expression. Use traditional named functions when complex mapping operations are required. so i write the code:

import ee
import geemap
ee.Initialize()

list1=[]
def function1(arg):
    list1.append("a")
    list1.append("b")
    return ee.Number(arg).add(1)
serverList = ee.List.sequence(0, 7)
a = serverList.map(lambda arg: function1(arg))
print(a.getInfo())

print(list1)

I want to use the map to do more things,while the element in serverList can add 1,the list1 can append two chars or do some oter things,but the I can not understand the result of the code:

[1, 2, 3, 4, 5, 6, 7, 8]
['a', 'b', 'a', 'b', 'a', 'b']

although each element in severList pluses 1,Only three groups of "a" and "b" have been appended in list1.I expected to add eight groups of 'a' and 'b' in list1.someone points me that i can not use the lambda expression in this way.Anonymous function mapping is achieved via lambda functions, which accept only a single expression. Use traditional named functions when complex mapping operations are required. So,How can I use the traditional named functions?

If i wirte the code,by this way:

import ee
import geemap
import os
ee.Initialize()

list1=[]
def function1(arg):
    list1.append("a")
    list1.append("b")
    return ee.Number(arg).add(1)
serverList = ee.List.sequence(0, 7)
a = serverList.map(function1(arg))
print(a.getInfo())
print(list1)

There will be an error that "arg" is not defined,so how can use traditional named functions when complex mapping operations are required.

someone also points me that I confuse the functions in Client,Server,and python built-in syntax,But my real goal is to simplify the code,I want to use the .map() funtion to do more things.Because According to theo fficial tutorial: Use map() to iterate over items in a collection. (For loops are NOT the right way to do that in Earth Engine and should be avoided).In python,I can do lots of things in a code block of For loops,so I noly want to use .map() to do more thing

How can I solve the problem?

1 Answer 1

1

You can just pass the function name to map():

a = serverList.map(function1)

But you are definitely improperly mixing client-side and server-side stuff by using the global client-side python list inside the mapped function. Mapped functions are expected to be independent and stateless. They get one input, and they return one output and they can't do anything else (like modifying a global variable).

As to what you're seeing: a mapped functions is usually called "once" inside your python client, and the results of what the function does to the input object are sent to the server, where it can be repeated over and over on however many things you're mapping over. So the a's and b's added to the list object are just what happens while the client library encodes what function1 does. It just so happened to call the function 3 times during encoding, but you should assume it's only going to get called once.

It would easier to help if you explained what you were actually trying to do instead of this toy hypothetical.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.