Reputation: 75
sorry for the basic question, I'm a GEE beginner.
Essentially, what I want to do it extract the value of a certain band in a pixel from each image in a collection, and put it into an array.
I understand how to do this if the output is to a chart, e.g.:
print(ui.Chart.image.series(with_ndvi.select("nd"),area));
Where with_ndvi is my image collection, "nd" is the band I'm interested in, and area is a point feature.
However, I need to get these values into an array, because I need to perform a calculation on each value.
Is there an easy funciton to map over a collection to extract the values as numbers to work with?
Thanks for any help.
Upvotes: 0
Views: 3784
Reputation: 43733
In general, in order to get particular values out of an image, you use reduceRegion
. Since you have a single point, there isn't exactly any reduction, but the same operation can be used to get the mean, median, maximum, etc. from an area and you need to choose a reducer to perform the operation. (ui.Chart.image.series
defaults to the mean
reducer if you don't specify otherwise).
I constructed this example from the images used in the Normalized Difference example script:
var imageCollection = ee.ImageCollection('MODIS/006/MOD09GA')
.filterDate('2019-01-01', '2019-01-31');
var ndviCollection = imageCollection.map(function (img) {
var ndImage = img.normalizedDifference(['sur_refl_b02', 'sur_refl_b01']);
return ee.Feature(area, ndImage.reduceRegion(ee.Reducer.mean(), area));
});
print(ndviCollection);
Here, ndviCollection
is a FeatureCollection
where each feature has the original point as geometry (useful if you have multiple points of interest, but otherwise you could make it be null
instead) and the NDVI at that point as a property named nd
.
If you absolutely need a list of numbers rather than a feature collection, you can obtain that:
print(ndviCollection
.toList(100) // set this number to the maximum number of elements you expect
.map(function (feature) {
return ee.Feature(feature).get('nd');
}));
but you should not do this if you can avoid it as lists are always kept in memory as a whole rather than processed in a streaming fashion. Instead, perform your computations on the feature collection using map
and/or reduceColumns
.
Upvotes: 1