Sentinel-2 Derived NDVI Analysis of Karachi Using Google Colab
Introduction: This study is about the analysis of Normalized Difference Vegetation Index (NDVI) derived from Sentinel-2 satellite imagery using Google Colab. The Sentinel-2 mission provides multispectral data that is crucial for monitoring vegetation health and changes over time. Leveraging Google Colab, a cloud-based Python environment, allows efficient processing and visualization of these satellite images without the need for local computational resources.
Methodology:
-
Data Retrieval: Sentinel-2 data is retrieved from the Sentinel Hub or Copernicus Open Access Hub and stored in Google Drive for easy access and organization.
-
Data Preprocessing: GeoTIFF files are accessed from Google Drive using Python libraries like rasterio. Bands relevant to NDVI calculation (typically NIR and Red bands) are selected and loaded into the Colab environment.
-
NDVI Calculation: NDVI is computed using the formula: NDVI=NIR-Red / NIR+Red. This index helps in quantifying vegetation density and health.
-
Geospatial Visualization: The computed NDVI values are plotted on a geographic map using matplotlib. Geographic coordinates (latitude and longitude) are derived from the image metadata to accurately position the NDVI values spatially.
-
Analysis and Interpretation: The NDVI map provides insights into vegetation distribution, health, and changes over the observed area. This analysis can aid in environmental monitoring, agricultural planning, and ecosystem studies.
By utilizing Google Colab, this study ensures scalability, reproducibility, and accessibility of satellite image analysis, making it a valuable tool for remote sensing applications.
CODES:
Connect to gdrive:
from google.colab import drive
drive.mount(‘/content/drive’)
install rsterio
!pip install rasterio matplotlib
Import Libraries
import os
import rasterio
import matplotlib.pyplot as plt
import numpy as np
from rasterio.plot import show
from rasterio.warp import transform
connect folder that has geotiff files or sentinel bands
geotiff_dir = ‘/content/drive/MyDrive/SENT_KHI24/’
geotiff_files = [f for f in os.listdir(geotiff_dir) if f.endswith(‘.tif’)]
print(“Processing and displaying GeoTIFF files:”)
for file in geotiff_files:
print(file)
Individual band displys
for file in geotiff_files:
file_path = os.path.join(geotiff_dir, file)
with rasterio.open(file_path) as src:
data = src.read(1)
plt.figure(figsize=(10, 10))
plt.imshow(data, cmap=’gray’)
plt.colorbar()
plt.title(f’GeoTIFF Image — {file} — Band 1')
plt.show()
NDVI :
import rasterio
import matplotlib.pyplot as plt
import numpy as np
import os
from rasterio.plot import show
from rasterio.warp import transform
geotiff_dir = ‘/content/drive/MyDrive/SENT_KHI24/’
nir_band_file = ‘S2_Sindh_B8.tif’ # Adjust with your actual file names
red_band_file = ‘S2_Sindh_B4.tif’ # Adjust with your actual file names
if all(os.path.exists(os.path.join(geotiff_dir, band_file)) for band_file in [nir_band_file, red_band_file]):
with rasterio.open(os.path.join(geotiff_dir, nir_band_file)) as src_nir:
nir = src_nir.read(1)
nir_transform = src_nir.transform
nir_crs = src_nir.crs
with rasterio.open(os.path.join(geotiff_dir, red_band_file)) as src_red:
red = src_red.read(1)
ndvi = (nir — red) / (nir + red)
nrows, ncols = ndvi.shape
xs = np.linspace(0, ncols — 1, ncols)
ys = np.linspace(0, nrows — 1, nrows)
x_coords, y_coords = np.meshgrid(xs, ys)
lon, lat = rasterio.transform.xy(nir_transform, y_coords, x_coords, offset=’center’)
lon = np.array(lon)
lat = np.array(lat)
plt.figure(figsize=(10, 10))
plt.imshow(ndvi, cmap=’RdYlGn’, extent=(lon.min(), lon.max(), lat.min(), lat.max()))
plt.colorbar(label=’NDVI’)
plt.xlabel(‘Longitude’)
plt.ylabel(‘Latitude’)
plt.title(‘Normalized Difference Vegetation Index (NDVI)’)
plt.show()
else:
print(“One or more required band files are missing.”)