2

I would like to identify the list of land cover classes in particular area and use them as an item for ui.Select. And I would like to use below given label for identified land cover values. I have drafted a code, but I am facing with an error. Is anyone can help me to improve it?

https://code.earthengine.google.com/74284d0174febea1be10a4dd9dccffc6?as_external

Error:

enter image description here

    var block = ee.Geometry.Polygon(
            [[[10.884499359130846, 50.55773169053401],
              [10.884499359130846, 49.37053314015084],
              [13.433327484130846, 49.37053314015084],
              [13.433327484130846, 50.55773169053401]]]);

    var dataset = ee.ImageCollection('MODIS/006/MCD12Q1')
      .select('LC_Type1')
      .filterDate('2018-01-01','2018-05-01');

    var landcover = dataset.first();
    var block_landcover = landcover.clip(block);

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

    Map.addLayer(block_landcover, Vis, 'Land cover');

    print(block_landcover);

    var block_landcover_vec = block_landcover.reduceToVectors({
      geometry: block,
      crs: landcover.first().projection(),
      geometryType: 'polygon'
    }); 
    print(block_landcover_vec);

    Map.addLayer(block_landcover_vec);

    var land_cover_types = ee.List(block_landcover_vec.aggregate_array('value'));

    var land_cover_select = ui.Select({
                  items: land_cover_types,
                  placeholder: 'Select a Mask',
                  onChange: function(value) {
                    var land_cover_value = value;
                    },
                  style: {width: '200px'}
                  });  

print(land_cover_select);

    [{label: 'Evergreen Needleleaf Forests', value: '1'}, 
    {label: 'Evergreen Broadleaf Forests:', value: '2'},
    {label: 'Deciduous Needleleaf Forests', value: '3'}, 
    {label: 'Deciduous Broadleaf Forests', value: '4'},
    {label: 'Mixed Forests', value: '5'}, 
    {label: 'Closed Shrublands', value: '6'},
    {label: 'Open Shrublands', value: '7'}, 
    {label: 'Woody Savannas', value: '8'},
    {label: 'Savannas', value: '9'}, 
    {label: 'Grasslands', value: '10'},
    {label: 'Permanent Wetlands', value: '11'}, 
    {label: 'Croplands', value: '12'},
    {label: 'Urban and Built-up Lands', value: '13'}, 
    {label: 'Cropland/Natural Vegetation Mosaics', value: '14'},
    {label: 'Permanent Snow and Ice', value: '15'}, 
    {label: 'Barren', value: '16'},
    {label: 'Water Bodies', value: '17'}]
  • Based in your comment about a way for directly determining areas from raster file, instead of converting it to vector and making a list, I posted an answer. I hope that it helps. – xunilk Mar 28 at 4:35
3

Based in your comment about a way for directly determining areas from raster file, instead of converting it to vector and making a list, I developed following code for determining area (in hectares), for all classes of MODIS landcover, in your block_landcover image. Code link is here.

var block = 
    /* color: #d63000 */
    /* shown: false */
    /* displayProperties: [
      {
        "type": "rectangle"
      }
    ] */
    ee.Geometry.Polygon(
        [[[10.884499359130846, 50.55773169053401],
          [10.884499359130846, 49.37053314015084],
          [13.433327484130846, 49.37053314015084],
          [13.433327484130846, 50.55773169053401]]], null, false);

var dataset = ee.ImageCollection('MODIS/006/MCD12Q1')
  .select('LC_Type1')
  .filterDate('2018-01-01','2018-05-01');

var landcover = dataset.first();
var block_landcover = landcover.clip(block);
var scale = block_landcover.projection().nominalScale();

print ("MODIS Scale", scale);


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

Map.centerObject(block_landcover, 8);
Map.addLayer(block_landcover, Vis, 'Land cover');

var classes_list = ['Evergreen Needleleaf Forests', 'Evergreen Broadleaf Forests', 
  'Deciduous Needleleaf Forests', 'Deciduous Broadleaf Forests', 
  'Mixed Forests', 'Closed Shrublands', 'Open Shrublands', 'Woody Savannas',
  'Savannas', 'Grasslands', 'Permanent Wetlands', 'Croplands', 'Urban and Built-up Lands', 
  'Cropland, Natural Vegetation, Mosaic', 'Permanent Snow and Ice', 'Barren', 'Water Bodies'];

var list = ee.List.sequence(1, 17, 1);

var areas = list.map(function(l){

    var iter = ee.Number(l).int();

    var areas_list = ee.List([]);

    var areas = block_landcover
     .eq(iter)
     .multiply(ee.Image.pixelArea().divide(10000))
     .reduceRegion({
        reducer: ee.Reducer.sum(),
        geometry: block,  // a geometry
        scale: 463.3127165275,   // scale for MODIS
        maxPixels: 1e9  
    });

    areas_list = areas_list.add(areas.values()).get(0);

    return areas_list;

}).flatten();

var fus_list2 = ee.List(classes_list).zip(areas);

print(fus_list2);

After running the script, it can be observed in Console Tab printed areas for all classes in block_landcover image visualized in Map View of GEE.

enter image description here

| improve this answer | |
1

Your specific error you are asking for: you tried to get the 'first` image of an image. It is an function which only works on collections (you already got the first image in var landcover).

var block_landcover_vec = block_landcover.reduceToVectors({
  geometry: block,
  crs: landcover.projection(), // remove .first()
  geometryType: 'polygon'
}); 

There are more errors popping up then. Use a client-side list for ui.Select items and define a function correctly for the onChange argument.

| improve this answer | |
  • I have improved the code: code.earthengine.google.com/…. Anyway I could not get what I want. I was wondering is there a way to list available land cover classes in particular area directly from raster file instead of converting it to vector and making a list? – Sarvarbek Eltazarov Mar 27 at 22:49

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.