🌱 How to Calculate NDVI in Google Earth Engine (GEE)? | Complete Guide with Code & Explanation


NDVI calculation tutorial with Google Earth Engine code and vegetation map visualization

📘 What is NDVI?

NDVI (Normalized Difference Vegetation Index) is a simple but powerful index used in remote sensing to measure the health and density of vegetation. It uses light reflectance values captured by satellite sensors to determine whether the land contains live green vegetation or not.

🧮 NDVI Formula:

NDVI = (NIR - RED) / (NIR + RED)
  • NIR (Near Infrared): Highly reflected by healthy vegetation.

  • RED: Strongly absorbed by chlorophyll in plants.

📊 What NDVI Values Mean:

NDVI Value Vegetation Type
0.6 - 1.0 Dense forest / healthy crops
0.2 - 0.5 Sparse vegetation / grassland
0.0 - 0.2 Dry soil / barren land
< 0.0 Water, snow, urban surfaces

🌍 Why Use Google Earth Engine for NDVI?

Google Earth Engine (GEE) is a cloud-based platform for geospatial data analysis. You can:

  • Access satellite data for free

  • Perform fast NDVI computations

  • Visualize, export, and chart vegetation indices


🧑‍💻 Full NDVI Code in Google Earth Engine (GEE)

✅ Step 1: Define Area of Interest (AOI)

// Create a point (e.g., Lahore, Pakistan)
var aoi = ee.Geometry.Point([74.3587, 31.5204]);

// Center the map on AOI
Map.centerObject(aoi, 10);

🔍 Explanation:
You define the target location either as a point or polygon. This will be the region where NDVI is calculated.


✅ Step 2: Load Sentinel-2 Image Collection

// Load Sentinel-2 images, filter by date and cloud coverage
var s2 = ee.ImageCollection("COPERNICUS/S2")
  .filterBounds(aoi)
  .filterDate('2024-04-01', '2024-06-01')
  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10));

🔍 Explanation:
This loads images with less than 10% cloud cover over your region and time range. Sentinel-2 has 10m resolution, ideal for NDVI.


✅ Step 3: Add NDVI Band to Each Image

// Create a function to calculate NDVI
var addNDVI = function(image) {
  var ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI');
  return image.addBands(ndvi);
};

// Map the function over the image collection
var s2NDVI = s2.map(addNDVI);

🔍 Explanation:

  • B8 = NIR band

  • B4 = Red band

  • The .normalizedDifference() function handles the NDVI calculation.


✅ Step 4: Visualize NDVI

// Define visualization parameters
var ndviVis = {
  min: 0,
  max: 1,
  palette: ['white', 'green']
};

// Select the median NDVI image for visualization
var ndviImage = s2NDVI.median().select('NDVI');
Map.addLayer(ndviImage, ndviVis, 'NDVI');

🔍 Explanation:
This displays the NDVI layer on the map using green shades for vegetation. Median filtering helps reduce noise (like small clouds).


✅ Step 5: Export NDVI Image (Optional)

Export.image.toDrive({
  image: ndviImage,
  description: 'NDVI_Lahore',
  region: aoi,
  scale: 10,
  fileFormat: 'GeoTIFF'
});

🔍 Explanation:
Export the NDVI image as a GeoTIFF to your Google Drive for offline use, or to analyze in software like QGIS or ArcGIS.


✅ Step 6: NDVI Time Series Chart (Bonus)

// Create a time series chart for NDVI
var chart = ui.Chart.image.series({
  imageCollection: s2NDVI.select('NDVI'),
  region: aoi,
  reducer: ee.Reducer.mean(),
  scale: 10,
  xProperty: 'system:time_start'
}).setOptions({
  title: 'NDVI Time Series - Lahore',
  vAxis: {title: 'NDVI'},
  hAxis: {title: 'Date'},
  lineWidth: 2,
  pointSize: 4
});

print(chart);

🔍 Explanation:
This chart shows how vegetation changes over time. Useful for monitoring crop growth, drought, or seasonal patterns.


🌾 Use Cases of NDVI

Application Area How NDVI Helps
Agriculture Crop health, yield estimation
Forestry Deforestation, forest stress
Climate Studies Drought monitoring, vegetation cycles
Urban Planning Green cover tracking

✅ Conclusion

With Google Earth Engine, calculating NDVI becomes:

  • Fast

  • Accurate

  • Free to use

Whether you're a researcher, farmer, environmentalist, or student, NDVI is one of the easiest ways to track vegetation health at scale.


📌 What You Can Do Next:

  • Replace the location with your own farm or city.

  • Change the date range to monitor seasonal changes.

  • Use time series charts to compare year-by-year trends.

  • Export NDVI results for advanced analysis in QGIS or Excel.




Post a Comment

0 Comments