[GIS] Filtering Image in Google Earth Engine based on metadata

filtergoogle-earth-engine

I'm trying to create a raster layer of the USDA NASS Cropland dataset using only certain crop classes. Is ee.Filter.inList only used for ImageCollections and not Images? It seems more appropriate to import the layer as an Image, not an ImageCollection.

var cdl= ee.Image('USDA/NASS/CDL/2017').select('cropland')
var crops=ee.List([1,4,5,7,9,10, 31, 33, 36, 45, 63, 64, 86, 241])
var filtered=cdl.filter(ee.Filter.inList('cropland_class_names', crops))

This doesn't work because I get the error 'cdl.filter is not a function' on the last line. I need to export a new raster which only contains the crop types from a list. I also tried filtering manually for each without success.

What is the proper way to filter my raster down to only these crop types?

Best Answer

Stephen Van Gordon from the GEE users group was able to steer me in the right direction. As I suspected, filter operations are intended only for ImageCollections, not Images.

There are two main options to complete the task I intended. One is to use ee.Image().eq operators to chain together values of interest.

A more comprehensive way is to store the values I care about in an ee.List object and then use Image.remap, which will allow me to map all the desired classes to 1 and everything else to 0.

var cdl= ee.Image('USDA/NASS/CDL/2017').select('cropland')

 var crops=ee.List([1,4,5,7,9,10, 31, 33, 36, 45, 63, 64, 86, 241])

// Only pull two crop types for demonstration purposes.
 var mask = ee.Image(cdl)
  .eq(152)
  .or(cdl.eq(1))
var masked = ee.Image(cdl)
  .updateMask( cdl.eq(152).or(cdl.eq(1)) )

// Map.addLayer(filtered)
Map.addLayer(mask, {}, 'mask')
Map.addLayer(masked, {}, 'masked')

var remapped = cdl.remap({
  from: crops,
  to: ee.List.repeat(1, crops.size()),
  defaultValue: 0
})

Map.addLayer(remapped, {}, 'remapped')