Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 5

image image

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:

Required Libraries

Make sure you have the following libraries installed:

# %pip install rasterio matplotlib numpy

Sample Datasets

Exercise 1: Reading and Exploring Raster Data

  1. Open the single-band DEM image using rasterio.

  2. Retrieve and print the raster metadata, including the CRS, resolution, bounds, number of bands, and data types.

  3. 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

  1. Visualize the single-band DEM using a custom colormap (e.g., cmap=‘terrain’).

  2. Open the multispectral image and visualize the first band using a suitable colormap.

  3. 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

  1. Open the multispectral image and clip a geographic subset using array indexing (specifying row and column ranges).

  2. Visualize the clipped portion of the image using matplotlib to ensure the subset is correct.

  3. Save the clipped raster subset to a new file named clipped_multispectral.tif.

Exercise 4: Calculating NDWI (Band Math)

  1. Open the multispectral image and extract the Green and Near-Infrared (NIR) bands. This multispectral image has the following band order:

    • Band 2: Blue

    • Band 3: Green

    • Band 4: Red

    • Band 5: NIR

  2. Compute the Normalized Difference Water Index (NDWI) using the formula:

    NDWI= (Green - NIR) / (Green + NIR)

  3. Visualize the NDWI result using a water-friendly colormap (e.g., cmap=‘Blues’) to highlight water bodies.

  4. Save the resulting NDWI image as a new raster file named ndwi.tif.

Exercise 5: Reprojecting Raster Data

  1. Reproject the single-band DEM raster from its original CRS to EPSG:4326 (WGS 84) using the rasterio.warp.reproject function.

  2. Save the reprojected raster to a new GeoTIFF file named reprojected_dem.tif.

  3. Visualize both the original and reprojected DEM datasets to compare how the reprojection affects the spatial coverage and resolution.