0

I am quite new to Google Earth Engine and Java scripting. I am delineating land cover data for an administrative area in Kenya (Samburu county) from the Copernicus Global Landcover 100m product. I've calculated the frequency & percentages for the different land cover classes in the county. I am trying to mask out each of the land cover classes and then do an NDVI time-series analysis for each of these land cover classes for my study area in Google Earth Engine.

How do I mask out the landcover classes?

Here is the code that I have scripted.

The link to the code is https://code.earthengine.google.com/?scriptPath=users%2Fwawerujohn%2Fhallo_world%3ASamburu_Landcover_100m_Copernicus%20(copy)

var globcover = ee.Image("COPERNICUS/Landcover/100m/Proba-V-C3/Global/2019")
.select('discrete_classification');

// Extract the landcover band
var landcover = globcover.select('discrete_classification');
   
// Clip the image to the polygon geometry
var landcover_roi = landcover.clip(geometry);
    
// Add a map layer of the landcover clipped to the polygon.
Map.addLayer(landcover_roi);
    
// Print out the frequency of landcover occurrence for the polygon.
var frequency = landcover.reduceRegion({
  reducer:ee.Reducer.frequencyHistogram(),
  geometry:geometry,
  scale:1000
});

//var maskShrub = landcover_roi.updateMask(value.eq(20));
//Map.addLayer(maskShrub, {min: 0, max: 1, palette: palette}, 'Shrub only')

var dict = ee.Dictionary(frequency.get('discrete_classification'));
var sum = ee.Array(dict.values()).reduce(ee.Reducer.sum(),[0]).get([0]);
var new_dict = dict.map(function(k,v) {
  return ee.Number(v).divide(sum).multiply(100);
});
print('Land Cover (%)',new_dict);

print('landcover frequency', frequency.get('discrete_classification'));

Map.setCenter(37, 1, 8);

Map.addLayer(globcover, {}, "Land Cover");

Export.image.toDrive({
  image: landcover_roi,
  description: 'Landcover',
  scale: 100,
  region: mask,
  fileFormat: 'GeoTIFF',
  formatOptions: {
    cloudOptimized: true
  }
});
2

You could select the land cover values you are interested in to create a mask and then apply it to all the images in the collection you will be using for your time series analysis. Here's an example using the landsat 8 surface reflectance collection.

// Select the class you are interested in analyzing
var LCclass112 = landcover_roi.eq(112);
// Filter collection to ROI and dates of interest
// Finally, apply the mask to every image in the collection using map
var l8sr4TS = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")
.filterBounds(geometry)
.filterDate('2015-01-01','2018-01-01')
.map(function(image){
  return image.updateMask(LCclass112);
});
3
  • Thank you so much for your comment and taking the time to answer my question. I was able to select and mask out the landcover classes I am interested in. I am now trying to do an ndvi time series for these classes and facing some errors. Can I share the code again?
    – Shiraz
    Jun 8 at 9:43
  • Sure. However, I think it might be a better idea to post another question addressing the problems you encounter with the time series analysis. That way more users might be able to solve your problem. Finally, you can also post the link to this question to give a broader context about the time series question. Jun 8 at 14:22
  • I will do just that since I am still getting an error with my timeseries analysis
    – Shiraz
    Jun 9 at 8:24

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.