1

I'm new to GEE.

I would like to calculate NDVI value per pixel for a set of S2_SR images and extract the values to a CSV file.

Pseudocode:
1. import S2_SR image collection(start date X, end date Y).
2. clip every image to a specific polygon that I have previously imported.
3. calculate NDVI value for every pixel in the clipped image.
4. output the calculated NDVI values to a CSV file.

(Let's say the clipped image is 100x100m size, which means we have 10x10 pixels. So the CSV file should be pixels 1-100 and their values).

My code so far is built like this:

//IMPORT SEN2 IMAGE
var S2 = ee.ImageCollection('COPERNICUS/S2_SR') 
  .filterDate('2019-09-01','2019-09-30')
  .filterBounds(poly)

// Define an index function (return only NDVI).
var NDVI = function(image) {
  return image.expression(
    '(NIR - RED) / (NIR + RED)', 
    {
      'NIR': image.select('B8'), 
      'RED': image.select('B4'), 
    }).rename('NDVI').copyProperties(image, image.propertyNames());
};

// Calculate NDVI for each image
var NDVIS2 = S2.map(NDVI)

// Empty Collection to fill
var ft = ee.FeatureCollection(ee.List([]))

var fill = function(img, ini) {
  // type cast
  var inift = ee.FeatureCollection(ini)

  // gets the values for the points in the current img
  var ft2 = img.reduceRegions(poly, ee.Reducer.first(),30)

  // gets the date of the img
  var date = img.date().format()

  // writes the date in each feature
  var ft3 = ft2.map(function(f){return f.set("date", date)})

  // merges the FeatureCollections
  return inift.merge(ft3)
}

// Iterates over the ImageCollection
var newft = ee.FeatureCollection(NDVIS2.iterate(fill, ft))

// Export
Export.table.toDrive(newft,
"anyDescription",
"anyFolder",
"anyNameYouWant")

But the result I get is a value for each image taken, As you can see here: CSV OUTPUT (I have selected only one of my fields in the displayed CSV output but it is the same for all.

Any suggestions?

1 Answer 1

3

You could call ee.Image.reduceRegion() on your NDVI image with an ee.Reducer.toList() reducer.

var region = ee.Geometry(Map.getBounds(true))
  .centroid(1).buffer(50).bounds(1)

var ndvi = ee.ImageCollection('COPERNICUS/S2_SR')
  .filterDate('2019-01-01', '2019-02-01')
  .map(function (image) {
    return image
      .normalizedDifference(['B8', 'B4'])
      .updateMask(image.select('QA60').not())
  })
  .median()
  .rename('ndvi')

var ndviFeatures = ee.FeatureCollection(ee.List(ndvi
  .reduceRegion({
    reducer: ee.Reducer.toList(),
    geometry: region,
    scale: 10
  })
  .get('ndvi'))
  .map(function (ndvi) {
    return ee.Feature(null, {ndvi: ndvi})
  })
)

print(ndviFeatures.aggregate_array('ndvi'))
Map.addLayer(ndvi, {min: 0, max: 1, palette: 'red,orange,yellow,green'}, 'ndvi')
Map.addLayer(region, {}, 'region')
Export.table.toDrive({
  collection: ndviFeatures,
  description: 'ndvi',
  selectors: ['ndvi']
})

https://code.earthengine.google.com/1a8225792714b648a346ef43a5feefb7

1
  • Thank you so much Daniel. It seems you have answered my question just as I have edited the question with my latest code written. I will try you version of the code, could you please check mine as well? Apr 6, 2020 at 14:28

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.