0

I am trying to get summary statistics of pixels within a buffer region. For this, I am selecting a geo-coordinate that I am interested in, defining a 10 km radius around it and counting the number of pixels which are no data, not water and water. This is the only information stored in the monthly water history data. I have a question regarding the following code. I don't understand how I can store the results I get from each iteration over the separate months in the image collection named water. Any suggestions on how to store results?

// 1. Import water data and filter months
var gsw = ee.ImageCollection("JRC/GSW1_1/MonthlyHistory");
var water=gsw.filterDate('2018-01-01','2018-12-31');
// 2. Sample coordinate
var test=ee.FeatureCollection(ee.Geometry.Point([38.8,7.97]));
//3. Define buffer region for the coordinate: 10,000 kms  
var buffer=test.map(function(feat){
  return(feat.buffer(10000));
});
// 4. Create an empty list to store the count of pixels of no data, not water and water in each month for the same buffer region
var month=ee.List([]);
var extract=water.iterate(function(image,first){
    var image0=image.updateMask(image.select('water').eq(0));
    var image1=image.updateMask(image.select('water').eq(1));
    var image2=image.updateMask(image.select('water').eq(2));

    //5. create the count of pixels of the three categories 'nodata', 'notwater','water'.
    var reduce0=image0.reduceRegion({
      reducer: ee.Reducer.count(),
      geometry: buffer.geometry(),
      scale: 30,
    });
    var reduce1=image1.reduceRegion({
      reducer: ee.Reducer.count(),
      geometry: buffer.geometry(),
      scale: 30,
    });
    var reduce2=image2.reduceRegion({
      reducer: ee.Reducer.count(),
      geometry: buffer.geometry(),
      scale: 30,
    });
    var reduce=image.reduceRegion({
      reducer: ee.Reducer.count(),
      geometry: buffer.geometry(),
      scale: 30,
    });

    var nodata=reduce0.get('water');
    var notwater=reduce1.get('water');
    var water=reduce2.get('water');
    var total=reduce.get('water');

    return month.add(nodata, notwater,water);
  });

1 Answer 1

1

Most easy would be using a map over the image collection and add the count of pixels as properties to each image of the collection.

// 4. Create an empty list to store the count of pixels of no data, not water and water in each month for the same buffer region
var extract=water.map(function(image){
    var image0=image.updateMask(image.select('water').eq(0));
    var image1=image.updateMask(image.select('water').eq(1));
    var image2=image.updateMask(image.select('water').eq(2));

    //5. create the count of pixels of the three categories 'nodata', 'notwater','water'.
    var reduce0=image0.reduceRegion({
      reducer: ee.Reducer.count(),
      geometry: buffer.geometry(),
      scale: 30,
    });
    var reduce1=image1.reduceRegion({
      reducer: ee.Reducer.count(),
      geometry: buffer.geometry(),
      scale: 30,
    });
    var reduce2=image2.reduceRegion({
      reducer: ee.Reducer.count(),
      geometry: buffer.geometry(),
      scale: 30,
    });
    var reduce=image.reduceRegion({
      reducer: ee.Reducer.count(),
      geometry: buffer.geometry(),
      scale: 30,
    });

    var nodata=reduce0.get('water');
    var notwater=reduce1.get('water');
    var water=reduce2.get('water');
    var total=reduce.get('water');

    return image.set({nodata: nodata, notwater: notwater,
                      water: water, total:total});
  });

  print(extract)
1

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.