1

I have multiple images that I am processing and would like to then export their values as a CSV table.

All the following code is available and should be easily reproducible:

var age = ee.Image('users/celsohlsj/public/secondary_vegetation_age_collection71_v5').select('classification_2020');
var amazon_biome = ee.FeatureCollection('projects/ee-ana-zonia/assets/amazon_biome_border');
var ecoregions = ee.FeatureCollection("RESOLVE/ECOREGIONS/2017");

// Clip to Amazon biome
var age = age.clip(amazon_biome).updateMask(age.gt(0));
var ecoregions = ecoregions.filterBounds(amazon_biome.geometry());
var ecoregions_list = ecoregions.toList(ecoregions.size());

var i = 2;
var ecoreg = ee.Feature(ecoregions_list.get(i));
var img_clipped = age.clip(ecoreg);

  // extract pixels into a FeatureCollection
  var points = img_clipped.sample({
    scale: 30,
    projection: img_clipped.projection()
  });
  
  // Use image.reduceRegions to convert to FeatureCollection for the selected ecoregion
  var featcol = img_clipped.reduceRegions({
    collection: points,
    reducer: ee.Reducer.first(),
    scale: 30});
  
// Export all features in a FeatureCollection as one file
  Export.table.toDrive({
    collection: featcol,
    fileNamePrefix: ee.String(ecoreg.get('ECO_ID')).cat('_age'),
    fileFormat: 'CSV'
  });

Ideally, I'd have one CSV file for each ecoregion.

is not working out as the value is too large:

myExportTableTask ID: QKMSEOB4DVUL3VPZH5UD3SNK Phase: Failed Runtime: 49s (started 2023-11-26 20:22:09 -0500) Attempted 1 time Error: Image.sample: Computed value is too large. (Error code: 3)

I saw this solution GEE export too large data but I don't understand how mosaic could help here, mainly as I am using Images, not ImageCollections.

I tried using an ImageCollection, as it just made sense to avoid repetitive code (I am doing the same for multiple images, not just "age"), but I am too much of a beginner and got really confused so just went with Images for now.

Also, these images don't seem to be so large, as right now I am exporting them as .tif and img_clipped was 66mb.

enter image description here

Any ideas on how I can easily export these large images as CSV?

1 Answer 1

1

The short answer: you shouldn't do this. You're much better off downloading a geotiff and converting it to CSV locally using, for instance, GDAL.

The longer answer: The ecoregion in this code has 215 million points in it. That means the CSV for this one ecoregion is going to be somewhere between 2GB and 6GB. Computed tables in Earth Engine have a limit of 100MB. You'd have to break this up into many smaller pieces in order to download it as CSV.

Also, some other things:

  1. sample already extracts all the values, there's no reason to also use reduceRegions. You're extracting the values twice.
  2. You can't use a computed value for the filenamePrefix. You'll notice in when you launch the task that that field contains some random JSON, not the string you were hoping for.
3
  • I understand, thanks! I'm exporting it to geotiff through the Python API and it works! Regarding "sample already extracts all the values, there's no reason to also use reduceRegions. You're extracting the values twice." I understood that since I wanted to export the value of one pixel per row, I needed to first make every non-NA pixel a "feature" in a feature collection and then clip the values from the images to these individual "features". Using reduceRegions directly, would it treat two identical adjacent pixels as only one feature, and export their values as only one row instead of two? Nov 27, 2023 at 16:09
  • 1
    You wouldn't use reduceRegions directly, you only need sample. It extracts each pixel's value already. Nov 28, 2023 at 17:08
  • Ah. That's much easier. Thanks for the clarification. Nov 29, 2023 at 1:26

Your Answer

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

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