0

I need to export data from GEE to get processed in MATLAB. I want to export CSV files of a region, aggregating meteo data ( CAMS, ERA5, MODIS AOT) with sentinel-2 images. These images have already been processed to have 12 images, where each one is the mean over a month. I wanna reduce this images to a single number,with a mean over a region (called plant), then construct an array, then export it to a csv file.

Each row of the file is a month, each column is a feature (meteo data) or an output (sentinel-2 bands). Then I will import this data for further processing in MATLAB.

I know that I have to use reducers for this matter, like ee.Reducer.mean(),but how exacly can I obtain such a structure, and what is the right reducer to get a spatial mean, and not a temporal one?

So far, this is my code: https://code.earthengine.google.com/874acca5c75fd2cc6135c3c7055ba8e9

0

what is the right reducer to get a spatial mean, and not a temporal one?

This is determined not by the reducer, but what you use the reducer in. Both of these perform spatial reductions (combining many pixels in an image into one value):

  • ee.Image.reduceRegion takes a region of interest (geometry) and reduces all unmasked pixels of the image that intersect the region.
  • ee.Image.reduceRegions takes a feature collection and reduces all unmasked pixels of the image that intersect each feature's geometry — it's what you should use when you have multiple regions of interest.

Since you also have one image per month, you'll need to write reduceRegions inside of map over the collection of month images.

This is fairly straightforward; the messy part will be the further work to convert the plant features into columns of the table, which will be something like:

var results = monthImages.map(function (image) {
  // Compute the reduction.
  var plantsWithReductions = image.select('my_band').reduceRegions({
    collection: plants,
    reducer: ee.Reducer.mean(),
  });

  // Extract three parallel lists of the plants' feature IDs, geometries,
  // and reduction results.
  var plantIds = plants.reduceToColumns(ee.Reducer.toList(), ['system:index']);
  var plantGeometries = plants.reduceToColumns(ee.Reducer.toList(), ['.geo']);
  var plantResults = plantsWithReductions.reduceToColumns(ee.Reducer.toList(), ['my_band']);

  // Convert the parallel lists into feature properties and put those properties
  // on the image. (You could also use ee.Feature(null) if you don't want to 
  // copy any properties from the image.)
  return image
    .setMulti(ee.Dictionary.fromLists(
      plantIds.map(function (id) { return ee.String(id).cat('_geo'); }),
      plantGeometries))
    .setMulti(ee.Dictionary.fromLists(
      plantIds.map(function (id) { return ee.String(id).cat('_mean'); }),
      plantResults));
});

Export.table.toDrive({
  collection: results,
  ...
}); 

If you'd be satisfied with a CSV that contains one row per month-plant combination, that's much simpler:

var results = monthImages.map(function (image) {
  return image.select('my_band').reduceRegions({
    collection: plants,
    reducer: ee.Reducer.mean(),
  });
}).flatten();

.flatten() turns a feature collection (months) containing feature collections (plants for each month) into a collection of all of the features inside the inner collections.

6
  • Thanks Kevin, since I need to use it in MATLAB I can than reshape the CSV, so I could in theory use any structure to do this. I don't fully understand what you have proposed in your solution, in this case I would have one CSV file per band? I need to export like 25/30 bands, if I could do it all togheter would be a lot more compact. So far, I created an array concatenation with some of the data, do you mind take a look and tell me if this structure (meteo array) could be turned into something that can then be exported? code.earthengine.google.com/f4708aa5375ae73c5b0c8f817efef3b6 – Pellicc Sep 2 '20 at 8:31
  • Another problem that i found is that sometimes, where there are no images available (I have to avoid both clouds and shadows so I just filtered images with less than 1% cloulds), on a whole month I get 0 images, and then I get in my array a zero bands image. I need to keep a number there for consistency with other data, but I don't know how to do it – Pellicc Sep 2 '20 at 10:22
  • @Pellicc No, I'm not proposing one CSV per band. In both of the approaches I have sketched out, results is a feature collection, which you can export to one CSV. There are no arrays needed, and using arrays will not help you achieve a CSV export, because CSVs are always generated from features and properties, not arrays. – Kevin Reid Sep 2 '20 at 15:14
  • Thanks for clarifying. I could do one row per month-plant, it could work, and then try to export another CSV to get the labels for each plant. That might help later – Pellicc Sep 3 '20 at 14:30
  • I tried your solution, but it keeps getting me this error: Unable to use a collection in an algorithm that requires a feature or image. This may happen when trying to use a collection of collections where a collection of features is expected; use flatten, or map a function to convert inner collections to features. Use clipToCollection (instead of clip) to clip an image to a collection. This is the script: code.earthengine.google.com/f7822164f8406220a07000a86d21aea8 – Pellicc Sep 3 '20 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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