I'm new to GEE and Java scripts. I'm trying to do something that seems simple, but cannot find any way to do it.
I’ve an image (which is not that big, say 100 km * 100 km) with ~ 12 bands (and a 30 m spatial resolution in this case). My goal is to obtain the mean of each band (in order to center the bands, before a PCA). This is much harder than anticipated, because of user memory limit.
I tried several strategies, from the most naïve one (considering all the pixels and a ‘reduceRegion’ function) to a less computer-intensive one (considering a random subsample of pixels), and now trying to loop it band by band… in order to avoid reaching the user memory limit… while trying to obtain the output format of the initial “reduceRegion” function strategy.
Here is an example : [link] (https://code.earthengine.google.com/11e0e4b9f7ca71c42774f390ddecf0af)
In my last attempt, I've defined a simple function that returns the mean of a given band in a satellite image, and applied it with map() on the multi-band image:
var listofbands = ee.List(['B1', 'B2']); // say i focus on 2 bands, named B1 and B2
var map_m = function(i) {
i = ee.String(i)
var Band_i = image.select(i) // select the band i in the image
var Mean_i = Band_i.reduceRegion({ // compute the mean over "sample", with sample e.g. 10 sample points
reducer: ee.Reducer.mean(),
geometry: sample.geometry(),
});
return Mean_i} // return this mean
var listOfResults = listofbands.map(map_m).flatten() // apply the function to listofbands
print(listOfResults)
It returns a "List", which contains one object per band in the image (and each object as the band name and the mean value). So it's not too bad but I do not want a "List", i want a single "Object" with all the band names and mean values in it:
I'm therefore trying to "unlist" this "List" (so that i obtain a single "object"), or convert the list to a single object (Dictionary)... without luck so far. This should be quite straightfoward... any idea?
Best