Google Earth Engine: trying to get an array of values from a feature collection but I'm getting an empty array out

Viewed 2980

I have a point feature which I've used to reduce values from an image collection.

The values are obtained as a new feature's property by mapping over the images with this code (specifically getting ndvi values):

var my_vals = images.map(function (img){
    var ndviImg = img.normalizedDifference(['B5', 'B4']);
    return ee.Feature(area, ndviImg.reduceRegion(ee.Reducer.mean(), point)); 
}); 

I need these values to be put into an array so I can do additional calculation on them. However, the function Feature.getArray() returns an empty array.

var myArray = my_vals.getArray("nd");
print(my_vals);   // returns "[]"

However I know that the values do exist within properties, because if I run the following code I can print out a list of the properties.

var myList = my_vals.aggregate_array("nd");
print(myList)   // this prints a list of my values

My goal is to do divide one list of these values by another. As far as I am aware I need the values in an array in order to be able to do this (I could not find a way to do this with the object that .aggregate_array() returns).

Does anybody know why this may be, or a better way to be able to get these values in a way I can programatically do math using them?

Apologies for the beginner question here, this is an issue I've been struggling with for a while.

Thanks!

1 Answers

Why getArray doesn't work:

collection.getArray('x') is the same as ee.Array(collection.get('x')) — that is, it looks up properties of what you call it on. Since your collection does not itself have a property named 'nd', there is no data. The get family does not operate on the contents of collections.

What to do instead:

To get any sort of data out of a collection, use the reduceColumns operation; if you need all of them, use ee.Reducer.toList() as the reducer.

print(my_vals
    .reduceColumns(ee.Reducer.toList(), ['nd'])
    .get('list'));
Related