LULC Mapping Using Google Earth Engine | Complete Guide with Code

Land Use Land Cover map generated in Google Earth Engine for Germany using dynamic legend and shapefile overlay

🔰 Introduction

Land Use Land Cover (LULC) maps help us understand how Earth's surface is being utilized. In this tutorial, we'll use the ESA WorldCover 2020 dataset to visualize Germany’s LULC in Google Earth Engine (GEE).

You’ll learn how to:

  • Load and clip LULC data

  • Remove shapefile borders (no dark outlines or shadows)

  • Add a beautiful interactive legend panel

Let’s dive into the code and understand each part


Line-by-Line Explanation of the Code

// 1. Load Germany boundary from FAO GAUL
var germany = ee.FeatureCollection("FAO/GAUL/2015/level0")
                .filter(ee.Filter.eq('ADM0_NAME', 'Germany'));
  • Explanation:
    We load a global administrative boundary dataset (from FAO GAUL 2015) and filter it to only include Germany using the filter() method. This provides the polygon geometry representing Germany's boundaries.


// 2. Load ESA WorldCover 2020 LULC dataset
var lulc = ee.Image("ESA/WorldCover/v100/2020");
  • Explanation:
    We load the ESA WorldCover 2020 dataset, which is a global land use/land cover (LULC) map at 10-meter resolution. This dataset classifies the Earth's surface into various categories like tree cover, water bodies, built-up areas, etc.


// 3. Clip LULC to Germany boundary
var lulc_germany = lulc.clip(germany);
  • Explanation:
    The clip() function is used to clip the LULC dataset to the boundary of Germany, ensuring that the analysis and visualization are only done for the German region.


// 4. Visualization parameters
var visParams = {
  min: 10,
  max: 100,
  palette: [
    '#006400', // Tree cover
    '#ffbb22', // Shrubland
    '#ffff4c', // Grassland
    '#f096ff', // Cropland
    '#fa0000', // Built-up
    '#b4b4b4', // Bare / sparse vegetation
    '#f0f0f0', // Snow and ice
    '#0064c8', // Water bodies
    '#0096a0', // Wetlands
    '#00cf75', // Mangroves
    '#fae6a0'  // Moss and lichen
  ]
};
  • Explanation:
    We define the visualization parameters (visParams) that control how the map will look. The parameters include:

    • min and max values that define the data range for visualization.

    • The palette array that maps each land cover class to a specific color. For example:

      • #006400 represents Tree Cover, and

      • #fa0000 represents Built-up areas.


// 5. Display LULC map
Map.centerObject(germany, 6);
Map.addLayer(lulc_germany, visParams, 'ESA WorldCover 2020');
  • Explanation:

  • We use Map.centerObject() to center the map on Germany and zoom in at level 6.

  • We then add the clipped LULC dataset to the map using the addLayer() function, applying our custom visualization parameters (visParams).


// 6. OPTIONAL: Add Germany boundary invisibly (no shadow/outline)
Map.addLayer(
  germany.style({
    color: 'ffffff00',        // Transparent border
    fillColor: 'ffffff00',    // Transparent fill
    width: 0                  // No outline
  }),
  {},
  'Invisible Germany Boundary'
);
  • Explanation:
    In this step, we add Germany’s boundary shapefile invisibly to the map:

    • The color: 'ffffff00' makes the boundary transparent (no outline).

    • The fillColor: 'ffffff00' ensures that the region is not filled with any color.

    • The width: 0 means the boundary will have no visible outline, effectively removing any shadows or borders.


// 7. Create a legend panel
var legend = ui.Panel({
  style: {
    position: 'bottom-left',
    padding: '8px 15px'
  }
});
  • Explanation:
    We create a UI panel where the legend will be displayed. The style property positions the panel in the bottom-left corner and adds some padding for spacing.


var legendTitle = ui.Label({
  value: 'ESA WorldCover Legend',
  style: {
    fontWeight: 'bold',
    fontSize: '14px',
    margin: '0 0 8px 0',
    padding: '0'
  }
});
legend.add(legendTitle);
  • Explanation:
    This adds a title at the top of the legend (i.e., "ESA WorldCover Legend"). The style options make the title bold and adjust its size and margins.


// 8. Define legend items
var legend_items = [
  {label: 'Tree Cover', color: '#006400'},
  {label: 'Shrubland', color: '#ffbb22'},
  {label: 'Grassland', color: '#ffff4c'},
  {label: 'Cropland', color: '#f096ff'},
  {label: 'Built-up', color: '#fa0000'},
  {label: 'Bare/Sparse', color: '#b4b4b4'},
  {label: 'Snow/Ice', color: '#f0f0f0'},
  {label: 'Water Bodies', color: '#0064c8'},
  {label: 'Wetlands', color: '#0096a0'},
  {label: 'Mangroves', color: '#00cf75'},
  {label: 'Moss/Lichen', color: '#fae6a0'}
];
  • Explanation:
    This defines an array of objects that represent each land cover class and its associated color from the palette. For example:

    • Tree Cover is represented by the color #006400.

    • Water Bodies is represented by #0064c8.


// 9. Add legend entries
legend_items.forEach(function(item) {
  var colorBox = ui.Label('', {
    backgroundColor: item.color,
    padding: '8px',
    margin: '0 0 4px 0'
  });
  var description = ui.Label(item.label, {
    margin: '0 0 4px 6px'
  });
  var entry = ui.Panel([colorBox, description], ui.Panel.Layout.Flow('horizontal'));
  legend.add(entry);
});
  • Explanation:
    For each legend item:

    • We create a color box (colorBox) with the corresponding color.

    • We also add the description text (description), which is the land cover class label (e.g., Tree Cover).

    • These elements are combined into a Panel and added to the legend.


// 10. Add legend to the map
Map.add(legend);
  • Explanation:
    Finally, we add the legend panel to the map, so the legend will be visible in the bottom-left corner.


Full Code

Now that we’ve covered the explanation, here is the full code:

// 1. Load Germany boundary from FAO GAUL
var germany = ee.FeatureCollection("FAO/GAUL/2015/level0")
                .filter(ee.Filter.eq('ADM0_NAME', 'Germany'));

// 2. Load ESA WorldCover 2020 LULC dataset
var lulc = ee.Image("ESA/WorldCover/v100/2020");

// 3. Clip LULC to Germany boundary
var lulc_germany = lulc.clip(germany);

// 4. Visualization parameters
var visParams = {
  min: 10,
  max: 100,
  palette: [
    '#006400', // Tree cover
    '#ffbb22', // Shrubland
    '#ffff4c', // Grassland
    '#f096ff', // Cropland
    '#fa0000', // Built-up
    '#b4b4b4', // Bare / sparse vegetation
    '#f0f0f0', // Snow and ice
    '#0064c8', // Water bodies
    '#0096a0', // Wetlands
    '#00cf75', // Mangroves
    '#fae6a0'  // Moss and lichen
  ]
};

// 5. Display LULC map
Map.centerObject(germany, 6);
Map.addLayer(lulc_germany, visParams, 'ESA WorldCover 2020');

// 6. OPTIONAL: Add Germany boundary invisibly (no shadow/outline)
Map.addLayer(
  germany.style({
    color: 'ffffff00',        // Transparent border
    fillColor: 'ffffff00',    // Transparent fill
    width: 0                  // No outline
  }),
  {},
  'Invisible Germany Boundary'
);

// 7. Create a legend panel
var legend = ui.Panel({
  style: {
    position: 'bottom-left',
    padding: '8px 15px'
  }
});

// 8. Add a title to the legend
var legendTitle = ui.Label({
  value: 'ESA WorldCover Legend',
  style: {
    fontWeight: 'bold',
    fontSize: '14px',
    margin: '0 0 8px 0',
    padding: '0'
  }
});
legend.add(legendTitle);

// 9. Define legend items
var legend_items = [
  {label: 'Tree Cover', color: '#006400'},
  {label: 'Shrubland', color: '#ffbb22'},
  {label: 'Grassland', color: '#ffff4c'},
  {label: 'Cropland', color: '#f096ff'},
  {label: 'Built-up', color: '#fa0000'},
  {label: 'Bare/Sparse', color: '#b4b4b4'},
  {label: 'Snow/Ice', color: '#f0f0f0'},
  {label: 'Water Bodies', color: '#0064c8'},
  {label: 'Wetlands', color: '#0096a0'},
  {label: 'Mangroves', color: '#00cf75'},
  {label: 'Moss/Lichen', color: '#fae6a0'}
];

// 10. Add legend entries
legend_items.forEach(function(item) {
  var colorBox = ui.Label('', {
    backgroundColor: item.color,
    padding: '8px',
    margin: '0 0 4px 0'
  });
  var description = ui.Label(item.label, {
    margin: '0 0 4px 6px'
  });
  var entry = ui.Panel([colorBox, description], ui.Panel.Layout.Flow('horizontal'));
  legend.add(entry);
});

// 11. Add legend to the map
Map.add(legend);

With this code, you will have a clean, visual representation of Germany's land cover in Google Earth Engine, complete with a custom legend to explain the color coding for different land cover types.

You can use this line for your area:

// 1. Load your area boundary from FAO GAUL
var area = ee.FeatureCollection("FAO/GAUL/2015/level0")
                .filter(ee.Filter.eq('ADM0_NAME', 'Your Area Name'));

Replace 'Your Area Name' with the name of your area of interest, for example:

// For Pakistan
var area = ee.FeatureCollection("FAO/GAUL/2015/level0")
                .filter(ee.Filter.eq('ADM0_NAME', 'Pakistan'));

Or:

// For Marinduque, Philippines
var area = ee.FeatureCollection("FAO/GAUL/2015/level0")
                .filter(ee.Filter.eq('ADM0_NAME', 'Philippines'))
                .filter(ee.Filter.eq('ADM1_NAME', 'Marinduque'));

This way, you can tailor it to any region you'd like.


Post a Comment

0 Comments