-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspectral_indexes.py
More file actions
59 lines (45 loc) · 1.71 KB
/
spectral_indexes.py
File metadata and controls
59 lines (45 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import xarray as xr
def return_ndvi(data:xr.Dataset) -> xr.DataArray:
"""
Calculate and return NDVI.
"""
# Turn red band into floats
red = data["red"].astype("float")
# Turn near infrared band into floats
nir = data["nir"].astype("float")
# Calculate NDVI
ndvi = (nir - red) / (nir + red)
return ndvi
def return_ndbi(data: xr.Dataset) -> xr.DataArray:
"""
Calculate and return NDBI.
"""
swir = data["swir22"].astype("float") # e.g., band 11 for Sentinel-2
nir = data["nir"].astype("float") # e.g., band 8 for Sentinel-2
ndbi = (swir - nir) / (swir + nir)
return ndbi
def return_ndwi(data: xr.Dataset) -> xr.DataArray:
"""
Calculate and return NDWI (modified version).
"""
green = data["green"].astype("float") # e.g., band 3 for Sentinel-2
swir = data["swir22"].astype("float") # e.g., band 11 for Sentinel-2
ndwi = (green - swir) / (green + swir)
return ndwi
def return_nbr(data: xr.Dataset) -> xr.DataArray:
"""
Calculate and return NBR (Normalized Burn Ratio).
"""
nir = data["nir"].astype("float") # e.g., band 8 for Sentinel-2
swir = data["swir22"].astype("float") # e.g., band 12 or band 11 (depending on your workflow)
nbr = (nir - swir) / (nir + swir)
return nbr
def return_ibi(data: xr.Dataset) -> xr.DataArray:
"""
Calculate and return the Index-based Built-up Index (IBI).
"""
ndvi = (data["nir"] - data["red"]) / (data["nir"] + data["red"])
ndwi = (data["green"] - data["swir22"]) / (data["green"] + data["swir22"])
ndbi = (data["swir16"] - data["nir"]) / (data["swir16"] + data["nir"])
ibi = (ndbi - (ndvi + ndwi)) / (ndbi + (ndvi + ndwi))
return ibi