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

//Define a geometry.

// Make the geometry a feature collection.

// Import S2 image collection.
var S2 = ee.ImageCollection('COPERNICUS/S2')
  // Filter collection.
  .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))//.clip(aaaaaa);

// Create Clip collection function    
var clipToShp = function(image){
  var clipped = image.clip(Akdarya)
  return clipped

// clip all images to this shp
var clippedCollection = S2.map(clipToShp)


// 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.

// Calculate the per-pixel median 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 ndviCollection = clippedCollection.map(NDVI)

// create a feature so we can iterate through it later
var fet = ee.Feature(Akdarya)

// add the NDVItimeseries to the SHP
var addPropNDVI = function(img, feature){

  var newf = ee.Feature(feature)
  var featureNDVI = ee.Number(img.reduceRegion(ee.Reducer.mean(),Akdarya))
  var theDate = img.get("DATE_ACQUIRED")//.format("Y-M-D");
  var ndviDate = ee.String("NDVI_").cat(theDate)
  return ee.Feature(newf.set(ndviDate,featureNDVI));

var featNDVI = ee.Feature(ndviCollection.iterate(addPropNDVI, fet));

//Make a collection of the features
var featureCollectionNDVI = ee.FeatureCollection([

// Export a .csv table of date, mean NDVI for Akdarya

  collection: featureCollectionNDVI,
  description: "NDVI_Export",
  regions: Akdarya,
  reducer: ee.Reducer.mean(),
  seriesProperty: 'name_e',
  • 1
    It would be nice if you added text which explained your problem in detail. Please don't expect the potential answerers to understand just from title and long code lines what your problem is. – Kadir Şahbaz Mar 15 '20 at 16:05
  • Please do not change your question to ask something new when your original question has received an answer. – PolyGeo Mar 15 '20 at 21:10

On this line

var clippedCollection = collection.map(clipToShp)

you refer to the variable collection, but it is not defined anywhere above. Perhaps you meant to write S2.map(clipToShp)?

Whenever you see an error like is not defined in this scope it means that you have made a mistake in the variable names in your script. You should examine where in your script it is mentioned and make sure that the name is correctly spelled and, if it is, that it is also declared somewhere above/outside the mention of it. The troubleshooting procedure is the same no matter what kind of variable it is.

  • Thank your for advice. I'm new user to the GEE platform. How can I export NDVI result in CSV using a particular shapefile? I created code for analysis NDVI using shapefile and export for chart but I can export one by one, it takes too long time. Could you please advice – user156402 Mar 15 '20 at 16:34
  • 1
    @user156402 If you want to know about exporting, ask a new question about exporting; don't edit your existing question to be different, as that makes things disorganized and unhelpful for the next person. And, as Kadir Şahbaz mentioned, make sure your question has an explanation and not just a dump of code. – Kevin Reid Mar 15 '20 at 20:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.