1

I am trying export NDVI result to CSV file for 35 shapefiles farmers in Earth Engine. But it did not work. I tried find any solution from internet. How can I solve this problem?

var Akdarya = ee.FeatureCollection("users/xodjayevshovkat/Akdarya_GEE_35");

//Define a geometry.
Map.addLayer(Akdarya);

// Make the geometry a feature collection.

// Import S2 image collection.
var S2 = ee.ImageCollection('COPERNICUS/S2')
  // Filter collection.
  .filterDate('2018-05-01','2018-09-30')
  .filterBounds(Akdarya)
  .filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 20);

// Define a cloud masking function.
var maskcloud1 = function(image) {
  var QA60 = image.select(['QA60']);
  return image.updateMask(QA60.lt(1))
};


//

// 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());
};

// Apply the cloud mask function.
var NDVIcol = S2.map(maskcloud1)
 // Apply the NDVI transformation.
 .map(NDVI);

// Calculate the per-pixel median/mean NDVI for all images in collection.
var NDVImed = NDVIcol.mean();

// Define vis params.
var NDVIvis = {min: -0.001, max: 0.9, palette: 'FFFFFF, CE7E45, DF923D, F1B555, FCD163, 99B718, 74A901, 66A000, 529400,' +
    '3E8601, 207401, 056201, 004C00, 023B01, 012E01, 011D01, 011301'};

//




// create a collection of the NDVI

var NDVIclip = NDVImed.clipToCollection(Akdarya)



// get the mean value for the region from each image
var ts = S2.map(function(image){
  var date = image.get("DATE_ACQUIRED")//.format("Y-M-D");
  var mean = image.reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: Akdarya,
    scale: 30
  });

// and return a feature with 'null' geometry with properties (dictionary)  
  return ee.Feature(null, {'mean': mean.get('NDVI'),
                            'date': date})
});

// Export a .csv table of date, mean NDVI for watershed
Export.table.toDrive({
  collection: ts,
  description: 'NDVI',
  fileFormat: 'CSV'
});

'''

1 Answer 1

1

You are mapping over the S2 collection and try to extract the mean NDVI. That collection doesn't contain NDVI. You probably wanted to map over NDVIcol.

When you're getting errors in exports, try to print the collection you're exporting. It will give you a lot faster feedback on what errors you might have.

var ts = NDVIcol.map(function(image){
  var date = image.get("DATE_ACQUIRED")//.format("Y-M-D");
  var mean = image.reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: Akdarya,
    scale: 30
  });

// and return a feature with 'null' geometry with properties (dictionary)  
  return ee.Feature(null, {'mean': mean.get('NDVI'),
                            'date': date})
});

https://code.earthengine.google.com/c0ef682554ed8c442840ea8a5169cebf

2
  • Thank you very much. Could you please advice how can I calculate and make NDVI result for each shapefile in one horizontal line in excel. I would be grateful for your efforts
    – Zara
    Commented Mar 16, 2020 at 16:16
  • 1
    Submit that as a separate question. Commented Mar 16, 2020 at 16:20

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.