Google Earth Engine – Extract Property Values to Python List from Feature Collection

feature-collectiongoogle-earth-enginegoogle-earth-engine-python-api

The situation

I am using the regions boundary feature collection (fc) of Earth Engine (ee) named 'FAO/GAUL/2015/level2'. I am filtering this fc for a certain country (e.g., Ghana) and I would like to extract only the names (i.e., property values) of the "ADM1_NAME" (subnational region) property that lies within Ghana. All "ADM1_NAME" property values after filtering should then go into a Python list so I can use them as a dropdown in Plotly-Dash, e.g. ['Western', 'Ashanti', …].

I would like to achieve this using only ee server-side functions, except for the transformation of the server-side object to the Python-side list. This means: I would not want to use any other package like geemap, if possible, because I felt that turning a feature collection into a dataframe can take long. In addition, I would not want to export the feature collection to a table e.g. on Drive or locally to my computer.

What I tried

I read questions and/or answers Getting properties of each feature in feature collection using Google Earth Engine? and Extracting only some properties of each feature from a feature collection and creating a new a feature collection from them using Google Earth Engine but could not apply it to the case in Python.

I tried further:

boundaries = ee.FeatureCollection('FAO/GAUL/2015/level2')
country_subset = boundaries.filter(ee.Filter.eq('ADM0_NAME', 'Ghana')).select(['ADM0_NAME', 'ADM1_NAME'])
country_subset.map(lambda object: object.get('ADM1_NAME'))

Which returns the error:

ee.ee_exception.EEException: Error in map(ID=000000000000000026b4):
A mapped algorithm must return a Feature or Image.

The question

How can I filter and then retrieve the property values from an ee fc using ee server side functions and then turn the computed object into a Python list? I would be happy if you could point me generally to a tutorial that shows how to query feature collections in a Python-dataframe way.

Best Answer

I found that on the feature collection fc the fc.aggregate_array() function does the trick, see Debugging Guide of Earth Engine and search for "Collection.map: A mapped algorithm must return a Feature or Image."

The code:

adm1_values_ee = country_subset.aggregate_array('ADM1_NAME') # returns an ee.ee_list.List
adm1_values = adm1_values_ee.getInfo() # turns the server-side object into a Python (client-side) list

The output is as expected: ['Western', 'Ashanti', ...]