1

I am trying to filter a raster asset that uses numeric values to define land uses. My initial purpose is to use a list of values to filter all the areas corresponding to bushes (codes 410, 420 and 430).

With the updateMask method this can be easily done with the following code:

var landuse = ee.Image('users/xxx/cover2014');
var masked = landuse.updateMask(landuse.eq(410).or(landuse.eq(420)).or(landuse.eq(430)));

My problem relies on the fact that I want to use another land uses through a checkbox, so I was thinking in a code like this (I did not include the code used for panel and checkbox):

var forest = ee.List([211, 212, 221, 222, 231, 232, 241, 251]);
var bush = ee.List([410, 420, 430, 440, 450]);

var filterList = forest.cat(bush);

var landuse = ee.Image('users/xxx/cover2014');
var masked = landuse.updateMask(landuse.eq(filterList));

Map.addLayer(masked);

Sadly, the updateMask method gives an error because I am using a list instead of a image. I have also tried with the operator 'in', 'inList', without success.

Is there any way to use values contained in a list to update the mask or filtering a raster asset?

EDIT:

The final code, using the provided solution is:

var forest = ee.List([211, 212, 221, 222, 231, 232, 241, 251]);
var bush = ee.List([410, 420, 430, 440, 450]);

var filterList = forest.cat(bush);

var landuse = ee.Image('users/xxx/cover2014');

var masked = landuse.updateMask(landuse.eq(ee.Image.constant(filterList)).reduce(ee.Reducer.anyNonZero()));

Map.addLayer(masked);

1 Answer 1

3

ee.Image.eq() actually returns a multiband image here. Try the following to fix it:

var masked = landuse.updateMask(landuse.eq(filterList).reduce(ee.Reducer.anyNonZero()))

The filterList may need to be wrapped with an image to make sure it can be used in ee.Image.eq():

filterList = ee.Image.constant(filterList)
3
  • The code does not work, it gives me the following error: Layer 1: Layer error: Image.eq, argument 'image2': Invalid type. Expected: Image<unknown bands>. Actual: List<Long>.
    – gtapko
    Jan 14, 2018 at 23:25
  • Try to convert your filterList from a list of longs to an image, like this: code.earthengine.google.com/d3d7adf81904d7803a5a153460068faa Jan 15, 2018 at 0:51
  • As you mentioned in your comment edition, the ee.Image.constant() command did the trick. This piece of code, included in your link, was the solution: var masked = landuse.updateMask(landuse.eq(ee.Image.constant(filterList)).reduce(ee.Reducer.anyNonZero()))
    – gtapko
    Jan 15, 2018 at 1:17

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.