TransWikia.com

Downloading many charts as CSV files at once

Geographic Information Systems Asked on March 2, 2021

My code generates 5-time series charts. Is there a way to download all of them at once to CSV files through the code and not through the arrow on the upper right?

code:

      //-----------------------------------------------------------------------------------
      // Set up basic variables (NDVI).
      //-----------------------------------------------------------------------------------
      var shpfile_name     = 'argentina/Argentina_Shifts_Divided_NEW_buffer_15m_proj20S'; // 
      Shapefile name (from Assets).
      var folder           = ''
      var image_collection = "COPERNICUS/S2_SR"//image collection.
      var first_date       = '2019-09-01' // First date in image collection
      var last_date        = '2020-03-30' // Last date in image collection
      var user_account     = 'users/yonatangoldwasser/'
      var fc_ID            = 'Group_'; // Field name of column with plot names.
      var min_v            = 0.05
      var max_v            = 0.75
      //
      //-----------------------------------------------------------------------------------
      // Load raster data (COPERNICUS/S2_SR).
      // Load vector data (shapefile).
      var fc = ee.FeatureCollection(user_account+folder+shpfile_name);
      var S2 = ee.ImageCollection(image_collection)
                  .filterDate(first_date, last_date)
                  .filterBounds(fc)
                  .filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 18);
      
      //-----------------------------------------------------------------------------------
      print(S2)
      print(fc)           // CRS must be WGS84 (for the shapefile)          
      
      //-----------------------------------------------------------------------------------
      //Image Expression for calculating NDVI----------------------------------------------
      var band_1 = 'B4'
      var band_2 = 'B8'
      
      var addNDVI = function(image) {
            var NDVI = image.expression(
            '(B8-B4)/(B8+B4)',
            {
              'B4': image.select('B4'),
              'B8': image.select('B8'),
       
            }).rename('NDVI');
            //NDVI index
            return image.addBands(NDVI.add(ee.Image(0.0)));
            //Add 0.0 value to NDVI (currently not in use)
      };
      
      
      // Calculate NDVIfor images in the Image collection 
      var S2_withNDVI = ee.ImageCollection(S2).map(addNDVI);          
      print(S2_withNDVI)
      
      
      // Show image of mean NDVI (over image collection) on GEE. 
      var scene = ee.Image(S2_withNDVI.mean());
      
      // Prepare palette for image display     
      var palette = ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718',
                    '74A901', '66A000', '529400', '3E8601', '207401', '056201',
                    '004C00', '023B01', '012E01', '011D01', '011301'];
                    //palette for NDVI image on GEE.
      
      // Add NDVI image and shapefile on GEE platform (min;max are range values)
      Map.addLayer(scene.select('NDVI'), {min: min_v, max: max_v, palette: palette}, 'NDVI', 1, 0.85);
      var styling = {color: 'red', fillColor: '00000000'};
      Map.addLayer(fc.style(styling)) 
      
      //Export NDVI Time Series into a Scatter Chart and a CSV format              
      var TimeSeries = Chart.image.seriesByRegion(
          S2_withNDVI, fc, ee.Reducer.mean(), 'NDVI', 3, 'system:time_start', fc_ID)
              .setChartType('ScatterChart')
              .setOptions({
                title: 'NDVI Time Series For Shift 1-2 Argentina',
                vAxis: {title: 'NDVI'},
                hAxis: {title: 'Date'},
                lineWidth: 1,
                pointSize: 4,
                series: {
      }});
      // Display.
      print(TimeSeries); 
      
      // Compute 3X3 kernel as texture of the NDVI.
      var addTexture_3X3 = function(image) {
          var texture = image.select('NDVI').reduceNeighborhood({
          reducer: ee.Reducer.mean(),
          kernel: ee.Kernel.square(1),
          }).rename('NDVI_3X3');
          return image.addBands(texture.add(ee.Image(0.0)));
      };
      var S2_withNDVI_Analysis = ee.ImageCollection(S2_withNDVI).map(addTexture_3X3);
      print(S2_withNDVI_Analysis)
      
      // Show image of 3X3 NDVI (over image collection) on GEE. 
      
      var scene2 = ee.Image(S2_withNDVI_Analysis.mean());
      // Add NDVI image and shapefile on GEE platform (min;max are range values)
      
      Map.addLayer(scene2.select('NDVI_3X3'), {min: min_v, max: max_v, palette: palette}, 'NDVI_3X3', 1, 0.85);
      
      
      
      //Export 3X3 kernel NDVI Time Series into a Scatter Chart and a CSV format              
      var TimeSeries = Chart.image.seriesByRegion(
          S2_withNDVI_Analysis, fc, ee.Reducer.mean(), 'NDVI_3X3', 3, 'system:time_start', fc_ID)
              .setChartType('ScatterChart')
              .setOptions({
                title: 'NDVI 3X3 Time Series For Shift 1-2 Argentina',
                vAxis: {title: 'NDVI mean'},
                hAxis: {title: 'Date'},
                lineWidth: 1,
                pointSize: 4,
                series: {
      }});
      // Display.
      print(TimeSeries); 
      
      // Compute 5X5 kernel as texture of the NDVI.
      var addTexture_5X5 = function(image) {
          var texture = image.select('NDVI').reduceNeighborhood({
          reducer: ee.Reducer.mean(),
          kernel: ee.Kernel.square(2),
          }).rename('NDVI_5X5');
          return image.addBands(texture.add(ee.Image(0.0)));
      };
      var S2_withNDVI_Analysis = ee.ImageCollection(S2_withNDVI_Analysis).map(addTexture_5X5);
      print(S2_withNDVI_Analysis)
      
      // Show image of 5X5 NDVI (over image collection) on GEE. /var scene2 = ee.Image(S2_withNDVI_Analysis.mean());
      var scene2 = ee.Image(S2_withNDVI_Analysis.mean());
      Map.addLayer(scene2.select('NDVI_5X5'), {min: min_v, max: max_v, palette: palette}, 'NDVI_5X5', 1, 0.85);
      
      //Export 5X5 kernel NDVI Time Series into a Scatter Chart and a CSV format              
      var TimeSeries = Chart.image.seriesByRegion(
          S2_withNDVI_Analysis, fc, ee.Reducer.mean(), 'NDVI_5X5', 3, 'system:time_start', fc_ID)
              .setChartType('ScatterChart')
              .setOptions({
                title: 'NDVI 5X5 kernel Time Series For Shift 1-2 Argentina',
                vAxis: {title: 'NDVI mean'},
                hAxis: {title: 'Date'},
                lineWidth: 1,
                pointSize: 4,
                series: {
      }});
      // Display.
      print(TimeSeries); 
      
      // Compute 3X3 standard deviation kernel (SD) as texture of the NDVI.
      var addTexture_3X3_stdDev = function(image) {
          var texture = image.select('NDVI').reduceNeighborhood({
          reducer: ee.Reducer.stdDev(),
          kernel: ee.Kernel.square(1),
          }).rename('NDVI_3X3_stdDev')
          return image.addBands(texture.add(ee.Image(0.0)));
      };
      var S2_withNDVI_Analysis = ee.ImageCollection(S2_withNDVI_Analysis).map(addTexture_3X3_stdDev);
      print(S2_withNDVI_Analysis)
      
      var scene2 = ee.Image(S2_withNDVI_Analysis.mean());
      Map.addLayer(scene2.select('NDVI_3X3_stdDev'), {min: min_v, max: max_v, palette: palette}, 'NDVI_3X3_stdDev', 1, 0.85);
      
      //Export 3X3 kernel NDVI stdDev Time Series into a Scatter Chart and a CSV format              
      var TimeSeries = Chart.image.seriesByRegion(
          S2_withNDVI_Analysis, fc, ee.Reducer.mean(), 'NDVI_3X3_stdDev', 3, 'system:time_start', fc_ID)
              .setChartType('ScatterChart')
              .setOptions({
                title: 'NDVI 3X3 stdDev kernel Time Series For Shift 1-2 Argentina',
                vAxis: {title: 'stdDev'},
                hAxis: {title: 'Date'},
                lineWidth: 1,
                pointSize: 4,
                series: {
      }});
      // Display.
      print(TimeSeries); 
      
      // Compute 5X5 standard deviation kernel (SD) as texture of the NDVI.
      var addTexture_5X5_stdDev = function(image) {
          var texture = image.select('NDVI').reduceNeighborhood({
          reducer: ee.Reducer.stdDev(),
          kernel: ee.Kernel.square(2),
          }).rename('NDVI_5X5_stdDev');
          return image.addBands(texture.add(ee.Image(0.0)));
      };
      var S2_withNDVI_Analysis = ee.ImageCollection(S2_withNDVI_Analysis).map(addTexture_5X5_stdDev);
      print(S2_withNDVI_Analysis)
      
      var scene2 = ee.Image(S2_withNDVI_Analysis.mean());
      Map.addLayer(scene2.select('NDVI_5X5_stdDev'), {min: min_v, max: max_v, palette: palette}, 'NDVI_5X5_stdDev', 1, 0.85);
      Map.addLayer(S2_withNDVI_Analysis,{bands:["B4","B3","B2"],min:0,max:5000},'Visual');//add Visual layer
      
      //Export 5X5 NDVI stdDev kernel Time Series into a Scatter Chart and a CSV format              
      var TimeSeries = Chart.image.seriesByRegion(
          S2_withNDVI_Analysis, fc, ee.Reducer.mean(), 'NDVI_5X5_stdDev', 3, 'system:time_start', fc_ID)
              .setChartType('ScatterChart')
              .setOptions({
                title: 'NDVI 5X5 stdDev kernel Time Series For Shift 1-2 Argentina',
                vAxis: {title: 'stdDev'},
                hAxis: {title: 'Date'},
                lineWidth: 1,
                pointSize: 4,
                series: {
      }});
      // Display.
      print(TimeSeries); 
      
      
      
      
      Map.setCenter(-63.2046658276515,-32.47022644315515, 15);
      
      

One Answer

The charts are all generated in the browser. There's no way to download them that doesn't involve clicking on them. However, you can export the underlying data directly to CSV though without going through the Chart API.

SeriesByRegion just maps a reduceRegions over the collection:

  var values = imageCollection
      .filterBounds(regions.geometry())
      .select(band)
      .map(function(img) {
        return img.reduceRegions({
          collection: fc, 
          reducer: ee.Reducer.mean(), 
          scale: scale
        }).map(function(f) {
          return f.set(xProperty, img.get(xProperty))
             .set('regionLabel', feature.get(seriesProperty));
        })
      })
      .flatten();

You can export this table using Export.table.toDrive(). One difference will be that this table will have 1 point per row, and the chart's CSV will have grouped them together, but presumably you can manage that once you've got the data locally.

Answered by Noel Gorelick on March 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP