In [ ]:
# To start, we install Rasterio, a Python module for interacting with gridded spatial data
!pip install rasterio


# Reading and manipulating tiled GeoTIFF datasets¶

This notebook shows how to perform simple calculations with a GeoTIFF dataset using XArray and Dask. We load and rescale a Landsat 8 image and compute NDVI (Normalized difference vegetation index). This can be used to distinguish green vegetation from areas of bare land or water.

We'll use an image of the Denver, USA area taken in July 2018.

First, we download the dataset. We are using an image from the cloud-hosted Landsat 8 public dataset and each band is available as a separate GeoTIFF file.

In [ ]:
import os
import json
import rasterio
import requests

import matplotlib.pyplot as plt
%matplotlib inline

In [ ]:
nir_filename = 'https://landsat-pds.s3.amazonaws.com/c1/L8/033/033/LC08_L1TP_033033_20180706_20180717_01_T1/LC08_L1TP_033033_20180706_20180717_01_T1_B5.TIF'
red_filename = 'https://landsat-pds.s3.amazonaws.com/c1/L8/033/033/LC08_L1TP_033033_20180706_20180717_01_T1/LC08_L1TP_033033_20180706_20180717_01_T1_B4.TIF'
mtl_filename = 'https://landsat-pds.s3.amazonaws.com/c1/L8/033/033/LC08_L1TP_033033_20180706_20180717_01_T1/LC08_L1TP_033033_20180706_20180717_01_T1_MTL.json'

In [ ]:
def download_file(in_filename, out_filename):
if not os.path.exists(out_filename):
response = requests.get(in_filename)
with open(out_filename, 'wb') as f:
f.write(response.content)

In [ ]:
download_file(nir_filename, 'nir.tif')


Let's see if the image is tiled so we can select a chunk size.

In [ ]:
img = rasterio.open('red.tif')
print(img.is_tiled)

In [ ]:
img.block_shapes


The image has separate blocks for each band with block size 512 x 512.

## Create XArray datasets¶

In [ ]:
import xarray as xr
red = xr.open_rasterio('red.tif', chunks={'band': 1, 'x': 1024, 'y': 1024})
nir = xr.open_rasterio('nir.tif', chunks={'band': 1, 'x': 1024, 'y': 1024})
nir


Each dataset's data is a Dask array.

In [ ]:
red.variable.data


## Optional: create a Dask client¶

You can start a Dask client to monitor execution with the dashboard.

In [ ]:
import dask
client = Client(processes=False)
client


## Rescale bands using Landsat metadata¶

The Landsat Level 1 images are delivered in a quantized format. This has to be converted to top-of-atmosphere reflectance using the provided metadata.

First we define convenience functions to load the rescaling factors and transform a dataset. The red band is band 4 and near infrared is band 5.

In [ ]:
def load_scale_factors(filename, band_number):
with open(filename) as f:
['REFLECTANCE_MULT_BAND_{}'.format(band_number)]
return M_p, A_p

In [ ]:
def calculate_reflectance(ds, band_number, metafile='meta.json'):
toa = M_p * ds + A_p

In [ ]:
red_toa = calculate_reflectance(red, band_number=4)
nir_toa = calculate_reflectance(nir, band_number=5)


Because the transformation is composed of arithmetic operations, execution is delayed and the operations are parallelized automatically.

In [ ]:
print(red_toa.variable.data)


The resulting image has floating point data with magnitudes appropriate to reflectance. This can be checked by computing the range of values in an image:

In [ ]:
red_max, red_min, red_mean = dask.compute(
red_toa.max(dim=['x', 'y']),
red_toa.min(dim=['x', 'y']),
red_toa.mean(dim=['x', 'y'])
)
print(red_max.item())
print(red_min.item())
print(red_mean.item())


## Calculate and display NDVI¶

Now that we have the image as reflectance values, we are ready to compute NDVI.

$$NDVI = \frac{NIR - Red}{NIR + Red}$$

This highlights areas of healthy vegetation with high NDVI values, which appear as green in the image below.

In [ ]:
ndvi = (nir_toa - red_toa) / (nir_toa + red_toa)

In [ ]:
ndvi2d = ndvi.squeeze()

In [ ]:
plt.figure()
im = ndvi2d.compute().plot.imshow(cmap='BrBG', vmin=-0.5, vmax=1)
plt.axis('equal')
plt.show()