1

I need some help with my script in GEE. I’m working with a MODIS Terra Vegetation Continuous Fields Yearly (MOD44B.051 Terra Vegetation Continuous Fields Yearly Global 250m) and I’m trying to obtain the number of pixels of three bands (Percent Non vegetated, percent vegetated, percent tree cover) at municipal level (2, 458 units).

So far my script runs until it count the total of pixels inside the municipality, and its ok, but I need to know of the total of pixels (inside the municipality) how many belongs to non-vegetated, vegetated and tree cover. Below it’s my script

//Counting pixels per municipalities //

//Map of municipalities done in ArcGIS//

var Mun= ee.FeatureCollection ('users/ceciliasosaa/MUN_FINAL')
Map.addLayer (Mun)

//Load MODIS. Specify year and band of interest//
var NV2000= ee.Image ('MODIS/051/MOD44B/2000_03_05'). select ('Percent_NonVegetated')
Map.addLayer (NV2000)

// Center locality//
Map.setCenter (-100.55, 21.37); //Mexico

// Counting pixels in municipalities //
var MunNV = NV2000.reduceRegions({
  collection: Mun,
  reducer: ee.Reducer.count(),
  scale: 250
});

print(MunNV);

What I’m missing in my script?

1
  • This should probably be tagged with google-earth-engine instead of google-earth. Commented Sep 14, 2018 at 1:31

1 Answer 1

1

You have to define a threshold. For example, I want to count the pixels where 'non vegetation' is equal or greater than 20 percent

So, I'd:

var Mun= ee.FeatureCollection ('users/ceciliasosaa/MUN_FINAL')

//Load MODIS. Specify year and band of interest//
var NV2000= ee.Image ('MODIS/051/MOD44B/2000_03_05')

print('band names', NV2000.bandNames())

/*** MORE STATIC DATA ***/
// name of the feature property that holds the name
var name_prop = 'name'
// scale
var scale = 250
// region
var region = Mun
// image to count pixels from
var image = NV2000

/*** COUNT FUNCTION ***/
var count_band = function(band, threshold) {
  // count the number of pixels for the given band that
  // are over the threshold

  var selected = image.select(band)
  var mask = selected.gte(threshold) // modify here for 'less than' (.lt or .lte)
  var masked = selected.updateMask(mask)

  var count = masked.reduceRegions({
    collection: region,
    reducer: ee.Reducer.count(),
    scale: scale
  });
  Map.addLayer(masked, {}, name_prop)

  return ee.Dictionary(count.iterate(function(feat, i){
    return ee.Dictionary(i).set(feat.get(name_prop), 
                                feat.get('count'))
  }, ee.Dictionary({})))

}

/*** TEST ***/
print('Percent_Tree_Cover greater than 10', 
      count_band('Percent_Tree_Cover', 10))

print('Percent_NonVegetated greater than 20', 
      count_band('Percent_NonVegetated', 20))

My test: https://code.earthengine.google.com/e93485b703e29bb47831dc38e1d8fec6

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.