2

I'm trying to find area covered by plants on my study area, but I'm getting this error:

ndvi.filterDate is not a function

This is my code:

// Define a region of interest  (i.e. an imported shapefile or a polygon drawn in GEE)
var cliplayer = clipper;
var roi = clipper;

// var s2 = ee.ImageCollection("LANDSAT/LT05/C01/T1")
//   .filterDate('1995-05-01', '2003-07-31')
//   .filterBounds(roi)
//   .filter(ee.Filter.eq('WRS_PATH', 172))
//   .filter(ee.Filter.eq('WRS_ROW', 71))
//   .select("B2","B3","B4")
//   .limit(35)
//   print(s2);
// //I set a limit to test the code so it would not return too many results...remove to see all results
// function addImage(image) { // display each image in collection - clipped to roi
//   var id = image.id
//   var imagee = ee.Image(image.id)
//   var clipimage = imagee.clip(clipper)
// //  Map.addLayer(clipimage, {bands: ['B4', 'B3', 'B2'], min: 6000, max: 12000}, id)
//   Map.addLayer(clipimage, {bands: ['B5', 'B4', 'B3'], min: 0, max: 255}, id)
// }

// s2.evaluate(function(s2) {  // use map on client-side
//   s2.features.map(addImage)
// })

//////////////////////////////////NDVI Calculation for Plants//////////////////////////

Map.centerObject(roi, 13);
var image = ee.Image(ee.ImageCollection("LANDSAT/LT05/C01/T1")  
.filterDate('1995-05-01', '2003-07-31')
.sort('CLOUD_COVER')
.first());

// print (image);

// Compute the Normalized Difference Vegetation Index (NDVI).
var ndvi = image.normalizedDifference(['B5', 'B4']);

// //Thresholding based mapping  
// var waterbody = ndvi.lte(0);
// // print(waterbody)

//////////////////////Calculating area//////////////////////////////

var s2image = ndvi.filterDate('1995-05-01', '2003-07-31').mean();

var s2cliped = s2image.clip(cliplayer).select("B2","B3","B4");

var area = ee.Image.pixelArea().divide(1e5).addBands(s2cliped).reduceRegion({
  reducer : ee.Reducer.sum(),
  geometry : cliplayer,
  scale :  10,
  bestEffort: true,
});
print(area)

Code link is here: https://code.earthengine.google.com/15024379590dfebb0ce49ffdfd56ecd4

2
  • filterDate is a method of an ImageCollection not an Image.
    – user2856
    Commented Nov 27, 2021 at 23:03
  • Based in the analysis of your requirement and provided code, I think code in my answer fixes your issues and calculates desired area in square kilometers. I hope it helps.
    – xunilk
    Commented Feb 10, 2022 at 17:51

2 Answers 2

2

After you filter your image collection, you take only a first image .first() - it's an image. You cannot apply .filter() to an image, but only to a collection. Also, if you take only a first image, in some cases you may end up with an image that is not covering your region of interest completely.

Overall, it's difficult to guess, what the sequence of steps that you are trying to do, but if you want to create a mosaic, I would rather recommend applying a couple additional filters on the beginning and than creating it, e.g.:

.filterBounds(cliplayer)
.filterMetadata('CLOUD_COVER', "less_than", 80)
.mean()

Link: https://code.earthengine.google.com/d58321a6130e23cd1d7d83c565d6abd9

The more advanced way to do this would be creating per pixel mosaic using some quality score for each pixel.

0

Based in the analysis of your requirement and provided code, I think following code snippet fixes your issues and calculates desired area in square kilometers (complete code here).

//////////////////////////////////NDVI Calculation for Plants//////////////////////////

Map.centerObject(roi, 13);
var col = ee.ImageCollection("LANDSAT/LT05/C01/T1")  
  .filterDate('1995-05-01', '2003-07-31')
  .sort('CLOUD_COVER');

 print ("count col", col.size());

// Compute the Normalized Difference Vegetation Index (NDVI).
var ndviCol = col.map(function (img) {
  
  return img.normalizedDifference(['B5', 'B4']);
  
});

print ("count ndviCol", ndviCol.size());

var waterbody = ndviCol
  .mean()
  .clip(roi)
  .lte(0)
  .multiply(ndviCol.mean())
  .rename('ndvi_mean')
  .select('ndvi_mean');

print("waterbody", waterbody);

var imageVisParam = {"opacity":1,
                     "bands":["ndvi_mean"],
                     "min":-0.13006064295768738,
                     "max":0,
                     "gamma":1};

// //Thresholding based mapping 
Map.addLayer(waterbody, imageVisParam, 'waterbody');

//////////////////////Calculating area//////////////////////////////
//dividing by 1e6 for km2
var area = ee.Image.pixelArea().divide(1e6).addBands(waterbody).reduceRegion({
  reducer : ee.Reducer.sum(),
  geometry : cliplayer,
  scale :  10,
  bestEffort: true,
});

print("area km2", area.get('area'));

After running it in GEE code editor, I got following result. Area was calculated in km2 for ndvi values between -0.13006064295768738 (min) and 0 (max); as expected threshold.

enter image description here

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.