3

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?

  • It's not clear what you expect the output to be. You've already gotten "right to the cropland layer" in your first line. filter() is applied to imageCollections to return images from a collection. – Jon Mar 26 '18 at 21:09
  • I'm hoping the output is a raster layer with just the designated crop classes. filter() isn't working with an image, and I can't get to the cropland layer starting with an ImageCollection. – dwolf Mar 26 '18 at 22:41
  • I would probably try to mask the cdl image based on pixels that are equal to the values in your list. Hopefully someone else can help out with the code. – Jon Mar 27 '18 at 0:46
3

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')

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.