Last active
October 20, 2018 19:39
-
-
Save celoyd/3ef1fdffd5ca52f237ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Very simple rasterio NDVI (or general normalized difference ratio) demo. | |
# python ndvi.py $landsat_scene/*B{4,5}.TIF ndvi.tif | |
import rasterio as rio | |
from sys import argv | |
import numpy as np | |
math_type = np.float32 # type for internal calculations | |
red_file, nir_file = (rio.open(img) for img in argv[1:3]) | |
red, nir = (f.read(1).astype(math_type) for f in (red_file, nir_file)) | |
dst_type = red_file.dtypes[0] | |
largest = np.iinfo(dst_type).max # for scaling | |
with rio.open(argv[3], 'w', **red_file.meta) as dst: | |
np.seterr(divide='ignore') | |
# NDVI = (nir - red)/(nir + red), which is in -1..1, | |
# which we output as 0..(largest representable value) | |
scale_ndvi = (nir * largest)/(red + nir) | |
dst.write_band(1, scale_ndvi.astype(dst_type)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment