1

I am trying to calculate the area of water pixels in two scenarios: 1) images where clouds are not masked, 2) images where clouds are masked. Previously, I managed to do this for Landsat 5 and 8 by plotting it as a chart, where I could directly export the values for both scenarios as a CSV. However, I can't do it in Landsat 4 because it apparently exceeds the maximum number of pixels.

How can I go about doing so? Thank you in advance.

// last modified on 9/7/2020
// script to extract total area of water pixels from given area (fp), using landsat 4
// two outputs: one where clouds are masked, one where clouds are not masked


// loading image collection 
var landsat8= ee.ImageCollection("LANDSAT/LT04/C01/T1_SR").filterBounds(fp).filterDate('1982-01-01', '1993-12-31')
  // .filter(ee.Filter.eq('WRS_PATH', 126))
  // .filter(ee.Filter.eq('WRS_ROW', 52));
var visParams = {
  bands: ['B3', 'B2', 'B1'],
  min: 0,
  max: 3000,
  gamma: 1.4,
};
Map.addLayer(landsat8, visParams,'original images');

// // defining roi
// var roi = ee.Geometry.Polygon([
//   [[16.5,-1.7], [16.2,-2.1], [16.3, -2.15], [16.7, -1.8], [16.5, -1.75]]
// ]);

// Function to extract water, then calculate area of water pixel 
var waterfunction = function(image){
  //add the NDWI band to the image
  var ndwi = image.normalizedDifference(['B2', 'B4']).rename('NDWI');
  //get pixels above the threshold
  var water01 = ndwi.gt(-0.2);
  //extract only water pixels 
  image = image.addBands(ndwi).updateMask(water01);

// now to calculate water area 
// first change all water pixel values to 1
// then multiply by ee.Image.pixelArea; since that image gives us the area of each pixel
// then rename the band 
  var waterArea = water01
                         .divide(water01)
                         .multiply(ee.Image.pixelArea())
                         .rename('waterArea')
                        .divide(1e6);

// adding area of water as a band
  image = image.addBands(waterArea);

// calculate area 
  var stats = waterArea.reduceRegion({
    reducer: ee.Reducer.sum(), 
    geometry: fp, 
    scale: 30,
  })
  ;

  return image.set(stats);
};

// function to mask clouds out
function maskL8sr(image) {
  // Bits 3 and 5 are cloud shadow and cloud, respectively.
  var cloudShadowBitMask = (1 << 3);
  var cloudsBitMask = (1 << 5);
  // Get the pixel QA band.
  var qa = image.select('pixel_qa');
  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
                .and(qa.bitwiseAnd(cloudsBitMask).eq(0));
  return image.updateMask(mask);
}

// mapping water function over my collection 
var collection = landsat8.map(waterfunction);
print(collection,'collection');
var visParams2 = {min: -0.2, 
  max: 1, 
    bands: 'NDWI', 
    palette: ['00FFFF', '0000FF']};

// var NDWICollection = collection.select('NDWI')
// Map.addLayer(NDWICollection.first(), visParams2,'water');
// //creating a chart 
// var title = {
//   title: 'Total area of water pixels (No clouds masked out)',
//   hAxis: {title: 'Time'},
//   vAxis: {title: 'Area (sq km)'},
// };

// var cloudChart = ui.Chart.image.series({
//   imageCollection: collection.select('waterArea'), 
//   region: fp, 
//   reducer: ee.Reducer.sum(), 
//   scale: 30,
// })
// .setOptions(title);
// print(cloudChart);

//masking clouds, then mapping water function over the collection
var cloudlessCollection = landsat8.map(maskL8sr).map(waterfunction);
var title2 = {
  title: 'Total area of water pixels (Clouds masked out)',
  hAxis: {title: 'Time'},
  vAxis: {title: 'Area (sq km)'},
};
var cloudlessChart = ui.Chart.image.series({
  imageCollection: cloudlessCollection.select('waterArea'), 
  region: fp, 
  reducer: ee.Reducer.sum(), 
  scale: 30,
})
.setOptions(title2);
print(cloudlessChart);
catlovingtaco
  • 123
  • 10
  • Your code runs without error but does not have an export function. You can enlarge the chart and export from there into CSV. – Philipp R Mar 28 '21 at 06:48

0 Answers0