Guest User

Untitled

a guest
Nov 19th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. import rasterio
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4.  
  5.  
  6. nirband = r"LC08_L1TP_015033_20170822_20170912_01_T1_B5.TIF"
  7.  
  8. redband =r"LC08_L1TP_015033_20170822_20170912_01_T1_B4.TIF"
  9.  
  10.  
  11. #rasterio.windows.Window(col_off, row_off, width, height)
  12. window = rasterio.windows.Window(2000,2000,800,600)
  13.  
  14. with rasterio.open(nirband) as src:
  15. subset = src.read(1, window=window)
  16.  
  17. plt.figure(figsize=(6,8.5))
  18. plt.imshow(subset)
  19. plt.title(f'Band 5 Subset')
  20. plt.xlabel('Column #')
  21. plt.ylabel('Row #')
  22.  
  23.  
  24.  
  25.  
  26.  
  27. rast = rasterio.open(nirband)
  28. rast2 = rasterio.open(redband)
  29. nir = rast.read(1)
  30. red = rast2.read(1)
  31.  
  32.  
  33. red = red.astype(float)
  34. nir = nir.astype(float)
  35. np.seterr(divide='ignore', invalid='ignore')
  36.  
  37.  
  38. ndvi = np.empty(rast.shape, dtype=rasterio.float32)
  39. check = np.logical_or ( red > 0, nir > 0 )
  40. ndvi = np.where ( check, (1.0*(nir - red )) / (1.0*( nir + red )),-2 )
  41.  
  42.  
  43. plt.figure(figsize=(6,8.5))
  44. plt.imshow(ndvi)
  45. plt.title(f'NDVI')
  46. plt.xlabel('Column #')
  47. plt.ylabel('Row #')
Add Comment
Please, Sign In to add comment