Lab 5#
Objective#
This lab introduces you to essential GIS raster operations using Python’s rasterio and matplotlib libraries. By the end of this lab, you will have learned to:
Read and explore raster data files.
Visualize and manipulate single-band and multi-band rasters.
Perform basic raster operations such as clipping, band math, and reprojection.
Work with Digital Elevation Models (DEMs) and multispectral satellite imagery.
Required Libraries#
Make sure you have the following libraries installed:
# %pip install rasterio matplotlib numpy
Sample Datasets#
Singlg-band image (DEM): opengeos/datasets
Multispectral image (Landsat): opengeos/datasets
Exercise 1: Reading and Exploring Raster Data#
Open the single-band DEM image using
rasterio
.Retrieve and print the raster metadata, including the CRS, resolution, bounds, number of bands, and data types.
Display the raster’s width, height, and pixel data types to understand the grid dimensions and data structure.
Exercise 2: Visualizing and Manipulating Raster Bands#
Visualize the single-band DEM using a custom colormap (e.g., cmap=’terrain’).
Open the multispectral image and visualize the first band using a suitable colormap.
Combine multiple bands from the multispectral image (e.g., Red, Green, and Blue) and stack them to create an RGB composite image.
Exercise 3: Raster Clipping with Array Indexing#
Open the multispectral image and clip a geographic subset using array indexing (specifying row and column ranges).
Visualize the clipped portion of the image using matplotlib to ensure the subset is correct.
Save the clipped raster subset to a new file named
clipped_multispectral.tif
.
Exercise 4: Calculating NDWI (Band Math)#
Open the multispectral image and extract the Green and Near-Infrared (NIR) bands.
Compute the Normalized Difference Water Index (NDWI) using the formula:
NDWI= (Green - NIR) / (Green + NIR)
Visualize the NDWI result using a water-friendly colormap (e.g., cmap=’Blues’) to highlight water bodies.
Save the resulting NDWI image as a new raster file named ndwi.tif.
Exercise 5: Reprojecting Raster Data#
Reproject the single-band DEM raster from its original CRS to EPSG:4326 (WGS 84) using the
rasterio.warp.reproject
function.Save the reprojected raster to a new GeoTIFF file named
reprojected_dem.tif
.Visualize both the original and reprojected DEM datasets to compare how the reprojection affects the spatial coverage and resolution.