2

I want to gather time series data (mean band values) from MODIS for multiple regions, but I want to filter the data so that I exclude images with low numbers of pixels in my regions.

Below is the code to use reduceRegions to get the pixel count and mean values, but how do I get exclude the images with low pixel counts?

var modis = ee.ImageCollection("MODIS/006/MOD09GA")
  .randomColumn()
  .filter(ee.Filter.lt('random', 0.1))
  .limit(10)

var fc = ee.FeatureCollection([
    ee.Feature(
        ee.Geometry.Polygon(
            [[[-142.36414308128087, 60.44668098352718],
              [-142.36414308128087, 60.171875701856855],
              [-141.62256593284337, 60.171875701856855],
              [-141.62256593284337, 60.44668098352718]]], null, false),
        {"site": "a"}),
    ee.Feature(
        ee.Geometry.Polygon(
            [[[-150.47861099878585, 60.14760911377782],
              [-150.47861099878585, 59.90886167691271],
              [-149.84140396753585, 59.90886167691271],
              [-149.84140396753585, 60.14760911377782]]], null, false),
        {"site": "b"})
]);


// Mask some pixels
var maskModis = function(image){
  image = ee.Image(image)
  var ndsi = image
    .normalizedDifference(['sur_refl_b04', 'sur_refl_b06'])
  var ndsiMask = ndsi.gt(0.8)

  return image.updateMask(ndsiMask)
}

// Apply the mask
var modis = modis.map(maskModis)
// var testImg = ee.Image(modis.first())
// Map.addLayer(testImg)


// 1. Count number of pixels in each feature.
// Return FeatureCollection with date, site, and pixelCount.

var getPixelCount = function(image){
  image = ee.Image(image)
  var count = image
    .select(1)
    .reduceRegions({
      collection: fc,
      reducer: ee.Reducer.count(),
      scale: 500
    })
  return count
}

var counts = modis
  .map(getPixelCount)
  .flatten()
print('counts', counts)

// 2. Get mean band values for each feature, each date
// Same appriach as (1), map reduceRegions

var getMeanVals = function(image){
  image = ee.Image(image)
  var date = ee.Date(image.get('system:time_start')).format('YYYY-MM-dd')
  var mean = image
    .select('sur_refl_b01', 'sur_refl_b04')
    .reduceRegions({
      collection: fc,
      reducer: ee.Reducer.mean(),
      scale: 500 
    })
  return mean //returns featureCollection
    // add date to each elt in collection
    .map(function(f){
      return f.set({'date': date})
    })
}

var means = modis
  .map(getMeanVals)
  .flatten()
print('means', means)

This gives me two featureCollections, but I only care about the 'means' collection. How do I filter the 'means' collection by the 'counts' collection?

https://code.earthengine.google.com/?scriptPath=users%2Fcaseyengstrom%2Freprex%3AmultipleReduceRegions

1

I filtered the 'counts' collection, then used an Inner Join to get the 'means' data. I could have used a Simple Join, but I figured I might want to keep the 'counts' data.

var countsFiltered = counts
  .filter(ee.Filter.gte('count', 1000))
print('filtered count size', countsFiltered.size()) // 7

// Criteria by which to join
var joinKey = ee.Filter.equals({
  leftField: 'system:index',
  rightField: 'system:index'
});

// Apply the join
var joined = ee.Join.inner()
  .apply(countsFiltered, means, joinKey)
  // tidy the output
  .map(function(f){
    var primary = ee.Feature(f.get('primary'))
    var secondary = ee.Feature(f.get('secondary'))
    var count = primary.get('count')
    return secondary
      .set('pixCount', count)
  })

print('filtered, joined collection', joined)

https://code.earthengine.google.com/?scriptPath=users%2Fcaseyengstrom%2Freprex%3AmultipleReduceRegions

Your Answer

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

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