0

My objective is to count the number of pixel identified as NDVI from a collection of landsat images and convert it to area with a unit of square meters or hectares. Could you Please help me. :(

var image = ee.ImageCollection('LANDSAT/LE07/C01/T1_TOA')
     .median();

var image = ee.Image(
L7.filterBounds(geometry)
 .filterDate('2017-04-01', '2017-07-31')
 .sort('CLOUD_COVER')
 .first()
);

var clip = image.clipToCollection (aoi);

Map.addLayer(clip, {bands:'B3,B2,B1'},'RGB');

//Create Bands Variable
var B3_Red = image.select('B3');
var B4_NIR = image.select('B4');

//Calculation of NDVI using Gee Help Methods
var ndvi = B5_NIR.subtract(B4_Red).divide(B5_NIR.add(B4_Red));
  var ndvi_palette =
   'FFFFFF, CE7E45, DF923D, F1B555, FCD163, 99B718, 74A901, 66A000, 529400,'+
   '3E8601, 207401, 056201, 004C00, 023B01, 012E01, 011D01, 011301';

var clip1 = ndvi.clipToCollection(aoi)

//Map.addLayer;
Map.addLayer(ndvi, {min: -0.1, palette: ndvi_palette},'NDVI');
Map.addLayer(clip1, {var:'imageVisParam'}, 'NDVI Clip')

var addNDVI = function(image) {
  var ndvi = image.normalizedDifference(['B4', 'B3']).rename('NDVI');
  return image.addBands(ndvi);
};

var withNDVI = L7.map(addNDVI);

// Test the addNDVI function on a single image.
var ndvi = addNDVI(image).select('NDVI');

// Map a function over the Landsat 7 TOA collection to add an NDVI band.
var withNDVI = L7.map(function(image) {
  var ndvi = image.normalizedDifference(['B4', 'B3']).rename('NDVI');
  return image.addBands(ndvi);
});

// Make a "greenest" pixel composite.
var greenest = withNDVI.qualityMosaic('NDVI');

// Display the result.
var visParams = {bands: ['B4', 'B3', 'B2'], max: 0.3};
Map.addLayer(greenest, visParams, 'Greenest pixel composite');

// Create a chart.
var chart = ui.Chart.image.series({
  imageCollection: withNDVI.select('NDVI'),
  region: aoi,
  reducer: ee.Reducer.mean(),
  scale: 30
}).setOptions({title: 'NDVI over time'});

// Display the chart in the console.
print(chart);

var cloudlessNDVI = L7.map(function(image) {
// Get a cloud score in [0, 100].
var cloud = ee.Algorithms.Landsat.simpleCloudScore(image).select('cloud');

// Create a mask of cloudy pixels from an arbitrary threshold.
var mask = cloud.lte(20);

// Compute NDVI.
var ndvi = image.normalizedDifference(['B4', 'B3']).rename('NDVI');

// Return the masked image with an NDVI band.
return image.addBands(ndvi).updateMask(mask);
});

print(ui.Chart.image.series({
  imageCollection: cloudlessNDVI.select('NDVI'),
  region: aoi,
  reducer: ee.Reducer.mean(),
  scale: 30
}).setOptions({title: 'Cloud-masked NDVI over time'}));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.