I applied an unsupervised classification to a collection of Sentinel 2 images and used ReducetoVector() within a loop to obtain each class of the Unsupervised classification as a different object.

Afterwards, I was able calculate the mean of Reflectance inside each different class obtained using ReduceRegion() wihin another loop, and I have those values stored into an array.

I want to export those values to my Drive as a CSV file but to do that using 'Export.table.toDrive' it seems that I need to create FeatureCollection with the values stored in the array, and that's what I'm not being able to do. Here's a bit of the code:

var mediana = colecao.median();
var regiao = ee.Geometry.Polygon(lagoa);

var treinamento = mediana.sample({
  region: lagoa,
  scale: 10,
  numPixels: 5000,

//define o tipo de cluster que será usado e parametros de treinamento
var agrupamento = ee.Clusterer.wekaXMeans(15,20).train(treinamento);

//diz qual cluster usar na imagem
var resultado = mediana.clip(lagoa).cluster(agrupamento);

//separando cada classe de cluster
var poly = [];
for (var i = 0; i < 11; i += 1){
//define o contador
var contador = ee.Number(i);
var subset = resultado.select("cluster").eq(i).selfMask();
poly[i] = subset.reduceToVectors(null, lagoa, 10);
//Map.addLayer(subset.randomVisualizer(), {}, 'only_'+i.toString());

var media_p = [];
for (var i = 0; i < 11; i += 1){
media_p[i] = mediana.reduceRegion(ee.Reducer.mean(), poly[i], 10, null, null, false, 1e13);
print('media_p'+i.toString(), media_p[i]);

collection: TBD,
folder: 'GEE',
fileFormat: 'CSV',

I've tried quite a few things and none of the worked.

Can somebody guide me on how to proceed?

Here is a link to the full code if needed: https://code.earthengine.google.com/72926cb481767d1b58ed7e31cf52c611

1 Answer 1


Maybe you're looking for something like this?

var media_clusters = ee.FeatureCollection(media_p
  .map(function (dict, i) {
    return ee.Feature(null, dict) 
    // return ee.Feature(poly[i].geometry(), dict) // If you want to include the geometries

Your Answer

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

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