Raster calculationsΒΆ
Conducting calculations between bands or raster is another common GIS task. Here, we will be calculating NDVI
(Normalized difference vegetation index)
based on the Landsat dataset that we have downloaded from Helsinki region. Conducting calculations with rasterio
is fairly straightforward if the extent etc. matches because the values of the rasters are stored as numpy
arrays
(similar to the columns stored in Geo/Pandas, i.e. Series
).
- Let’s start by importing the necessary modules
rasterio
andnumpy
In [1]: import rasterio
In [2]: import numpy as np
In [3]: from rasterio.plot import show
- Let’s read the file that we masked for Helsinki Region
# Filepath
In [4]: fp = r"C:\HY-DATA\HENTENKA\CSC\Data\Helsinki_masked_p188r018_7t20020529_z34__LV-FIN.tif"
In [5]: raster = rasterio.open(fp)
For calculating the NDVI (Normalized difference vegetation index) you need two bands: band-4 which is the Red channel and band-5 which is the Near Infrared (NIR)
- Let’s read those bands from our raster source
In [6]: red = raster.read(4)
In [7]: nir = raster.read(5)
In [8]: red
Out[8]:
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)
In [9]: nir