-
Notifications
You must be signed in to change notification settings - Fork 3
/
espectro.py
210 lines (181 loc) · 6.16 KB
/
espectro.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import json
import numpy as np
import pandas as pd
import plotly.express as px
import requests
import streamlit as st
from PIL import Image
image = Image.open("_static/logo_white.png")
st.set_page_config(page_title="Espectro", layout="wide")
st.image(image, width=300)
st.markdown(
"""
The **Awesome Spectral Indices Streamlit App**.
Created by [David Montero Loaiza](https://github.com/davemlz).
Powered by [Awesome Spectral Indices](https://github.com/awesome-spectral-indices/awesome-spectral-indices).
"""
)
CSV = "https://raw.githubusercontent.com/awesome-spectral-indices/awesome-spectral-indices/main/output/spectral-indices-table.csv"
with open("data/bands.json", "r") as f:
bands = json.load(f)
bandsPerPlatform = []
for key in bands.keys():
keyList = list(bands[key].keys())
keyList.remove("short_name")
keyList.remove("long_name")
for platform in keyList:
dictToAdd = bands[key][platform]
dictToAdd["standard"] = key
bandsPerPlatform.append(dictToAdd)
BANDS = pd.DataFrame(bandsPerPlatform).rename(
columns={
"platform": "Platform",
"band": "Band",
"name": "Name",
"wavelength": "Wavelength (nm)",
"bandwidth": "Bandwidth (nm)",
}
)
BANDS["Bandwidth (nm)"] = BANDS["Bandwidth (nm)"] / 2.0
def toMath(x):
"""Convert the expression to a math-latex readable expression."""
x = x.replace(" ", "")
x = x.replace("**", "^")
x = x.replace("^2.0", "^{2.0}")
x = x.replace("^0.5", "^{0.5}")
x = x.replace("^nexp", "^{n}")
x = x.replace("^cexp", "^{c}")
x = x.replace("gamma", "\\gamma ")
x = x.replace("alpha", "\\alpha ")
x = x.replace("omega", "\\omega ")
x = x.replace("lambdaN", "\\lambda_{N} ")
x = x.replace("lambdaR", "\\lambda_{R} ")
x = x.replace("lambdaG", "\\lambda_{G} ")
x = x.replace("*", "\\times ")
return x
A, B, C = st.columns(3)
E, F = st.columns(2)
with A:
indexTypes = st.multiselect(
label="Filter by index type:",
options=(
"All",
"Vegetation",
"Burn",
"Water",
"Snow",
"Soil",
"Urban",
"Kernel",
"RADAR",
),
default=None,
)
with B:
bandsOptions = st.multiselect(
label="Filter by bands:",
options=(
"All",
"A: Aerosols",
"B: Blue",
"G: Green",
"G1: Green 1",
"Y: Yellow",
"R: Red",
"RE1: Red Edge 1",
"RE2: Red Edge 2",
"RE3: Red Edge 3",
"N: Near-Infrared (NIR)",
"N2: Narrow Near-Infrared (NIR) 2 (Red Edge 4 in Google Earth Engine)",
"S1: Short-wave Infrared (SWIR) 1",
"S2: Short-wave Infrared (SWIR) 2",
"T: Thermal Infrared ",
"T1: Thermal Infrared 1",
"T2: Thermal Infrared 2",
"HH: Backscattering Coefficient HH",
"HV: Backscattering Coefficient HV",
"VV: Backscattering Coefficient VV",
"VH: Backscattering Coefficient VH",
),
default=None,
)
def getBands(x):
"""Get the bands from the list-string."""
return (
x.replace('"', "")
.replace("'", "")
.replace(" ", "")
.replace("[", "")
.replace("]", "")
.split(",")
)
def checkBands(x):
"""Check if selected bands are available for an index."""
x = getBands(x)
return all(i in x for i in [option.split(":")[0] for option in bandsOptions])
with E:
spectral = pd.read_csv(CSV)
if len(indexTypes) == 0:
indexTypes = "All"
if "All" in indexTypes:
filtered = spectral
else:
filtered = spectral[spectral.application_domain.isin([x.lower() for x in indexTypes])]
if "All" not in bandsOptions:
filtered["checkBands"] = filtered.bands.apply(checkBands)
filtered = filtered[filtered.checkBands == True]
filtered = filtered.drop("checkBands", axis=1)
st.download_button(
label="Download Indices as CSV",
data=filtered.to_csv(index=False),
file_name="awesome-spectral-indices.csv",
mime="text/csv",
)
st.caption("Filtered Spectral Indices:")
st.dataframe(filtered, height=500)
with C:
idx = st.selectbox("Select Spectral Index:", filtered.short_name.unique())
if len(filtered.loc[filtered.short_name == idx, "long_name"].values) > 0:
idxData = filtered[filtered.short_name == idx]
with F:
st.caption("Spectral Index info:")
st.metric(
label=idxData["long_name"].values[0],
value=idxData["short_name"].values[0],
)
st.latex(toMath(idxData["formula"].values[0]))
st.markdown(
f"""
Reference: [{idxData["reference"].values[0]}]({idxData["reference"].values[0]}).
Contributed by [{idxData["contributor"].values[0].split('/')[-1]}]({idxData["contributor"].values[0]}) on {idxData["date_of_addition"].values[0]}
"""
)
bandsToPlot = getBands(idxData["bands"].values[0])
fig = px.scatter(
BANDS[BANDS.standard.isin(bandsToPlot)],
x="Wavelength (nm)",
y="Platform",
template="simple_white",
error_x="Bandwidth (nm)",
color="Name",
color_discrete_map={
"Aersols": "#B983FF",
"Blue": "#548CFF",
"Green": "#06FF00",
"Red": "#FF1700",
"Red Edge 1": "#FFF323",
"Red Edge 2": "#FFCA03",
"Red Edge 3": "#FF5403",
"Near-Infrared (NIR) 2 (Red Edge 4 in Google Earth Engine)": "#D22779",
"Near-Infrared (NIR)": "#FF008E",
"Short-wave Infrared (SWIR) 1": "#612897",
"Short-wave Infrared (SWIR) 2": "#0C1E7F",
"Thermal Infrared 1": "#8D448B",
"Thermal Infrared 2": "#5026A7",
},
hover_name="Platform",
log_x=True,
height=400,
title="Required Bands by Platform:",
)
st.plotly_chart(fig, use_container_width=True)