3

I am currently looking at land cover for two years, e.g. 2001 and 2010.

The code I am writing adds the Land Cover layers to the map for both years, so a map of 2001 and one of 2010. I would like my code to automatically deselect the 2010 LC layer.

// --- SELECT STUDY AREA ---

var studyArea = ee.Geometry.Rectangle([-99.55454883653354,19.126233922295793, -98.93794117051792,19.612392241681285]);
Map.addLayer(studyArea, {}, 'studyArea');
Map.centerObject(studyArea);

var baseYear = 2001;
var studyYear = 2010;

// --- GET IMAGES ---

var landCoverStudy = ee.ImageCollection('MODIS/006/MCD12Q1')
  .filter(ee.Filter.date(studyYear + '-01-01',  studyYear + '-12-31'))
  .select('LC_Type1').reduce('mode').clip(studyArea);

var landCoverBase = ee.ImageCollection('MODIS/006/MCD12Q1')
  .filter(ee.Filter.date(baseYear + '-01-01',  baseYear + '-12-31'))
  .select('LC_Type1').reduce('mode').clip(studyArea);

var modisStyling = {
  min: 1,
  max: 17,
  palette: ['05450a','086a10','54a708','78d203','9900','c6b044',
    'dcd159','dade48','fbff13','b6ff05','27ff87','c24f44',
    'a5a5a5','ff6d4c','69fff8','f9ffa4','1c0dff']
};

Map.addLayer(landCoverBase, modisStyling, 'Land Cover (' + baseYear +')');
Map.addLayer(landCoverStudy, modisStyling, 'Land Cover (' + studyYear +')');

In the screenshot below I have deselected the 2010 layer, but I had to do it manually. enter image description here

Instead I would like to be able to code a command that deselects the layer without manual intervention.

1 Answer 1

3

Found the solution! First I have to assign a name to the mapped layer, e.g.:

var mappedLayer = Map.addLayer(landCoverStudy, modisStyling, 'Land Cover - 2010');

And when called for all I need to do is add:

mappedLayer.setShown(0); // setShown is a boolean so 0 means hide

Note: Naming the mapped layer doesn't always work. I recommend instead using the get function: Map.layers().get(0).setShown(0)

1
  • Actually, naming the mapped layer doesn't always work. I recommend instead using the get function: Map.layers().get(0).setShown(0)
    – M. Nicolas
    Jan 5, 2021 at 21:11

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.