Understanding how land is used and how it changes over time is vital for urban planning, environmental monitoring, and sustainable agriculture. In this post, we’ll walk through how to perform land use/land cover (LULC) classification for Nigeria using Sentinel-2 satellite imagery, the Google Earth Engine (GEE) Python API, and the Random Forest classifier.

Tools and Libraries

Google Earth Engine (GEE) – A powerful cloud platform for geospatial analysis.

Sentinel-2 imagery – High-resolution multispectral satellite data.

Python – For scripting and visualization.

Geemap – A Python package for interactive mapping with GEE.

Initialize Earth Engine and Define Nigeria’s Boundary

import ee
import geemap
import folium

# Trigger the authentication flow.
ee.Authenticate()

# Initialize the library.
ee.Initialize(project='toraaglobal')

# Trigger the authentication flow.
ee.Authenticate()

# Initialize the library.
ee.Initialize(project='toraaglobal')

Load and Preprocess Sentinel-2 Imagery

# Load and filter Sentinel-2 imagery (2022 dry season)
sentinel2 = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED") \
    .filterBounds(nigeria) \
    .filterDate("2022-11-01", "2023-03-31") \
    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20)) \
    .map(lambda img: img.divide(10000)) \
    .median() \
    .clip(nigeria)

# Select RGB and NIR bands
bands = ['B2', 'B3', 'B4', 'B8']  # Blue, Green, Red, NIR

Define Training Data

# Define training points (manual sampling or known labeled data)
# For demonstration, we're using synthetic geometries and labels
points = ee.FeatureCollection([
    ee.Feature(ee.Geometry.Point([7.5, 9.0]), {'landcover': 0}),  # Water
    ee.Feature(ee.Geometry.Point([7.0, 10.5]), {'landcover': 1}), # Forest
    ee.Feature(ee.Geometry.Point([8.5, 11.0]), {'landcover': 2}), # Urban
    ee.Feature(ee.Geometry.Point([6.5, 8.5]), {'landcover': 3}),  # Agriculture
    ee.Feature(ee.Geometry.Point([5.5, 9.5]), {'landcover': 4})   # Grassland
])

Train a Random Forest Classifier

# Sample the image at training point locations
training = sentinel2.select(bands).sampleRegions(
collection=points,
properties=['landcover'],
scale=10
)

# Apply the classifier to the image
classified = sentinel2.select(bands).classify(classifier)

Visualize the Results with Geemap

# Define visualization parameters
landcover_palette = ['0000ff', '008000', 'ff0000', 'ffff00', '00ffff']
landcover_names = ['Water', 'Forest', 'Urban', 'Agriculture', 'Grassland']
viz_params = {'min': 0, 'max': 4, 'palette': landcover_palette}

# Display using geemap
Map = geemap.Map(center=[9.1, 8.7], zoom=6)
Map.addLayer(classified, viz_params, "Land Use Classification")
Map.add_legend(title="Land Cover", labels=landcover_names, colors=landcover_palette)
Map.addLayer(nigeria, {}, "Nigeria Boundary")
Map

Interpreting the Results

Each pixel in the output map represents a predicted land cover class:

Blue: Water

Green: Forest

Red: Urban

Yellow: Agriculture

Cyan: Grassland


Leave a Reply

Your email address will not be published. Required fields are marked *