1

I am pretty new to using the code editor. I am trying to export ten-day NDVI composites (images from Sentinel-2) to a table (as a CSV file). I think my problem lies with not knowing how to convert my ImageCollection to FeatureCollection (btw. in this version survey data is simplified).

var surveydata = ee.Geometry.Point (16.13298, 77.85595);

// study area
var studyArea = ee.FeatureCollection(surveydata);
var surveyData = ee.FeatureCollection(surveydata); 

// Load Sentinel-2 data.
var s2 = ee.ImageCollection('COPERNICUS/S2');

// Load cloud masking tool
var s2mask = require('users/fitoprincipe/geetools:cloud_masks').sentinel2;

// function to apply NDVI calculation to every image in collection
function addNDVI(image) {
  var NDVI = image.normalizedDifference(['B8', 'B4']).rename('NDVI');
  return image.addBands(NDVI);
}

//set a list of start dates
var startDate = '2020-05-01';
var endDate = '2020-09-30';
var interval = 10;
var increment = 'day';
var tendayDifference = ee.Date(startDate).advance(interval, increment).millis().subtract(ee.Date(startDate).millis());
var listMap = ee.List.sequence(ee.Date(startDate).millis(), ee.Date(endDate).millis(), tendayDifference);

//function to get a tenday composite
function gettendaySentinelComposite(date) {
  // Only include the NDVI
  var s2 = ee.ImageCollection('COPERNICUS/S2')
                      .filterBounds(studyArea)
                      .filterDate(date, date.advance(interval, increment))
                      .filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 60)
                      .map(s2mask())
                      .map(addNDVI)
                      .select('NDVI');
  var composite = s2.max()
                      .set('system:time_start', date.millis(), 'dateYMD', date.format('YYYY-MM-dd'), 'numbImages', s2.size());
  return composite;
}

var ndvi_tenday = ee.ImageCollection.fromImages(listMap.map(function(dateMillis){
  var date = ee.Date(dateMillis);
  return gettendaySentinelComposite(date);
}));

print(ndvi_tenday);

var tempTimeSeries = ui.Chart.image.series({
      imageCollection: ndvi_tenday,
      region: studyArea,
      reducer: ee.Reducer.max(),
      scale: 100,
      xProperty: 'system:time_start'
    })
    .setChartType('ScatterChart')
    .setOptions({
    title: 'NDVI-over-time',
    vAxis: {
      title: 'NDVI'
    },
    lineWidth: 1,
    pointSize: 4,
    series: {
      0: {color: 'blue'},
      1: {color: 'red'},
      2: {color: 'green'}
    }
});

print(tempTimeSeries);

// Convert the object to an image with one band for each tenday period
var ndvi_tenday = ee.ImageCollection(ndvi_tenday)
.toBands();

// code to plot one band (one tenday period)
var image = ndvi_tenday.select("9_NDVI");

Map.centerObject(studyArea, 9);
var ndviParams = {min: -1, max: 1, palette: ['blue', 'white', 'green']};
Map.addLayer(image, ndviParams, 'NDVI image');

print(ndvi_tenday);

I thought to use this code for exporting:

// Export the image to your google drive
Export.table.toDrive({
        collection: ndvi_tenday,
        description: 'NDVI', 
        folder: 'EE',
    fileNamePrefix: 'NDVI', 
    fileFormat: 'CSV'
    });

Here is the link for my code for clarification: https://code.earthengine.google.com/?noload=1&scriptPath=users%2Fanjadamlisby%2FTest%3AS2_NDVI_GPS_Table

1
  • Hi, Welcome to GIS Stack Exchange! In order for you to get the best help, it would be great if you could familiarize yourself with how to write a good post. Please take the tour and take a look at this post. I would suggest replacing the screenshots with code blocks in the text, and resharing your link, because it does not work on my side. – saQuist Apr 15 at 13:36
1

I might have found a solution now. This seems to do it:

// Get Zonal Statistics
var reduced = ndvi_tenday.reduceRegions({
  collection: surveyData,
  reducer: ee.Reducer.mean(),
  scale: 100,
});

print(reduced);


// the resulting mean is a FeatureCollection
// so you can export it as a table
Export.table.toDrive({
    collection: reduced, 
    description: 'NDVIpolygons', 
    folder: 'EE', 
    fileNamePrefix: 'NDVIpolygons', 
    fileFormat: 'CSV'
})

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.