0

Let's say that I have a hypothetical study region and I just want to create a list object with all of the LULC classes within that region. How can a get it?

I got, for example, a MODIS LULC:

var ecoregions = ee.FeatureCollection('RESOLVE/ECOREGIONS/2017');
var caatinga = ecoregions.filterMetadata('ECO_NAME', 'equals', 'Caatinga')

var cover = ee.Image('MODIS/051/MCD12Q1/2012_01_01').select('Land_Cover_Type_1')
.clip(caatinga.geometry());

Map.addLayer(cover, {0: 15}, 'cover');
Map.centerObject(cover, 5.5)

var list = ee.List(cover.get('Land_Cover_Type_1'))

print(list)

But all that I get is a "null" information

I want to use this information further to calculate the area of each class within that region. But usually, I just create a sequence with all putative possible values like:

var classes = ee.List.sequence(1, 15)
print(classes)

An object like classes works just fine. However, not all classes between 1 and 15 are necessarily present and the outcome that I get is a table with empty columns for the inexistent classes. That wouldn't be a problem if I'm working with only a few empty classes, but in certain situations, I would like the outcome to have only the actually present classes without the burden to remove empty columns afterwards.

I feel like it should be something quite simple, but I couldn't figure it out yet. Any advice?

1 Answer 1

0

You can for instance reduce your region with a histogram reducer.

var histogram = ee.Array(
  cover
    .reduceRegion({
      reducer: ee.Reducer.fixedHistogram(1, 16, 15), 
      geometry: caatinga.geometry(), 
      scale: 100000, // Running this in a large scale to get some empty classes
      maxPixels: 1e13
    })
    .get('Land_Cover_Type_1')
)
var mask = histogram
  .slice(1, 1) // Extract second column - the counts
  .gt(0)
var classes = ee.List(
  histogram
    .mask(mask) // Remove rows where the count isn't > 0
    .slice(1, 0, 1) //  Extract the first column - the classes
    .project([0]) // Turn a 2D array into 1D
)

https://code.earthengine.google.com/0282d6af91be2ff05b20d9aa094c83b1

This is quite expensive at lower scales. If you have an option to use a static list of classes and add a step where you remove the empty "columns", it might be more efficient. But there's not enough information about the rest of your processing to judge if it makes sense or not.

2
  • Hi Daniel! First of all, thank you for your answer, I will try it out soon. Second: Yeah, the basic framework is using a static list of classes, but if a use a different study area, or the LULC input, I would need to keep changing the list time after time. In fact, that is what I've been "struggling". Between quotes because in fact I can just manually delete empty columns later. Howerver, adding a line of code to just remove empty columns would work just fine too, I just don't have the knowledge for that yet but I was (honestly) just thinking of that! Thank you for endorsing this idea too! Commented Apr 13, 2022 at 17:21
  • Yeah, I just tried the code you posted at a very low scale and it does not save much time. I will try to figure out how to delete empty columns from the table in GEE before exporting the file. I am still wondering if there is no way at all to just enlist the classes values of a raster. Commented Apr 14, 2022 at 17:03

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.