csv – Exporting Time Series of Multiple Polygons to CSV Format Table

csvgoogle-earth-enginetime series

I wanted to export a time series of Rainfall (CHIRPS) on *.CSV format without using the ui.Chart.image.seriesByRegion command. I cannot build the chart because there is too much data (daily data from 2012-2017) and I get an error.

Similar to what have been proposed here.

I'm using the following code, from which I get in rows the date, and by column the value corresponding to my polygon. However in the last column (.geo) it is the coordinates of each polygon.

Because my polygons are complex (not as in this example below) the document is becoming very heavy (around 1 Gb).

How do I remove this last column before exporting the data?

var regions = ee.FeatureCollection([
  ee.Feature(    // San Francisco.
    ee.Geometry.Rectangle(-122.45, 37.74, -122.4, 37.8), {label: 'City'}),
  ee.Feature(  // Tahoe National Forest.
    ee.Geometry.Rectangle(-121, 39.4, -120.8, 39.8), {label: 'Forest'}),
  ee.Feature(  // Black Rock Desert.
    ee.Geometry.Rectangle(-119.15, 40.8, -119, 41), {label: 'Desert'})
]);

var temps2013 = ee.ImageCollection('LANDSAT/LC8_L1T_32DAY_TOA')
    .filterDate('2012-12-25', '2013-12-25')
    .select('B11');
var triplets = temps2013.map(function(image) {
  return image.select('B11').reduceRegions({
    collection: regions.select(['label']), 
    reducer: ee.Reducer.mean(), 
    scale: 30
  }).filter(ee.Filter.neq('mean', null))
    .map(function(f) { 
      return f.set('imageId', image.id());
    });
}).flatten();
print(triplets.first());
var format = function(table, rowId, colId) {
  var rows = table.distinct(rowId);

  var joined = ee.Join.saveAll('matches').apply({
    primary: rows, 
    secondary: table, 
    condition: ee.Filter.equals({
      leftField: rowId, 
      rightField: rowId
    })
  });

  return joined.map(function(row) {
      var values = ee.List(row.get('matches'))

        .map(function(feature) {
          feature = ee.Feature(feature);
          return [feature.get(colId), feature.get('mean')];
        });
      return row.select([rowId]).set(ee.Dictionary(values.flatten()));
    });
};

var table1 = format(triplets, 'imageId', 'label');

var desc1 = 'table_demo_'; 
Export.table.toDrive({
  collection: table1, 
  description: desc1, 
  fileNamePrefix: desc1,
  fileFormat: 'CSV'
});

Best Answer

You can remove the geometry by using .setGeometry(null) on a feature.

To remove every geometry you need to map over the collection. In your script that could be in the same function as setting the imageID

return f.set('imageId', image.id()).setGeometry(null);

As a full script:

var regions = ee.FeatureCollection([
  ee.Feature(    // San Francisco.
    ee.Geometry.Rectangle(-122.45, 37.74, -122.4, 37.8), {label: 'City'}),
  ee.Feature(  // Tahoe National Forest.
    ee.Geometry.Rectangle(-121, 39.4, -120.8, 39.8), {label: 'Forest'}),
  ee.Feature(  // Black Rock Desert.
    ee.Geometry.Rectangle(-119.15, 40.8, -119, 41), {label: 'Desert'})
]);

var temps2013 = ee.ImageCollection('LANDSAT/LC8_L1T_32DAY_TOA')
    .filterDate('2012-12-25', '2013-12-25')
    .select('B11');
var triplets = temps2013.map(function(image) {
  return image.select('B11').reduceRegions({
    collection: regions.select(['label']), 
    reducer: ee.Reducer.mean(), 
    scale: 30
  }).filter(ee.Filter.neq('mean', null))
    .map(function(f) { 
      return f.set('imageId', image.id()).setGeometry(null);
    });
}).flatten();
print(triplets.first());
var format = function(table, rowId, colId) {
  var rows = table.distinct(rowId);

  var joined = ee.Join.saveAll('matches').apply({
    primary: rows, 
    secondary: table, 
    condition: ee.Filter.equals({
      leftField: rowId, 
      rightField: rowId
    })
  });

  return joined.map(function(row) {
      var values = ee.List(row.get('matches'))

        .map(function(feature) {
          feature = ee.Feature(feature);
          return [feature.get(colId), feature.get('mean')];
        });
      return row.select([rowId]).set(ee.Dictionary(values.flatten()));
    });
};

var table1 = format(triplets, 'imageId', 'label');

var desc1 = 'table_demo_'; 
Export.table.toDrive({
  collection: table1, 
  description: desc1, 
  fileNamePrefix: desc1,
  fileFormat: 'CSV'
});
Related Question