|
import streamlit as st |
|
import json |
|
import ee |
|
import geemap |
|
import os |
|
|
|
st.set_page_config(layout="wide") |
|
m = st.markdown( |
|
""" |
|
<style> |
|
div.stButton > button:first-child { |
|
background-color: #006400; |
|
color:#ffffff; |
|
} |
|
</style>""", |
|
unsafe_allow_html=True, |
|
) |
|
|
|
|
|
earthengine_credentials = os.environ.get("EE_Authentication") |
|
|
|
|
|
os.makedirs(os.path.expanduser("~/.config/earthengine/"), exist_ok=True) |
|
with open(os.path.expanduser("~/.config/earthengine/credentials"), "w") as f: |
|
f.write(earthengine_credentials) |
|
|
|
ee.Initialize(project='ee-yashsacisro24') |
|
|
|
|
|
with open("sentinel_datasets.json") as f: |
|
data = json.load(f) |
|
|
|
|
|
def calculate_ndvi(image): |
|
"""Calculate NDVI.""" |
|
return image.normalizedDifference(['B5', 'B4']).rename('NDVI') |
|
|
|
def calculate_ndwi(image): |
|
"""Calculate NDWI.""" |
|
return image.normalizedDifference(['B3', 'B5']).rename('NDWI') |
|
|
|
def calculate_avg_no2(image): |
|
"""Calculate average NO2.""" |
|
return image.select('NO2').reduceRegion( |
|
reducer=ee.Reducer.mean(), |
|
geometry=image.geometry(), |
|
scale=1000 |
|
).get('NO2') |
|
|
|
def calculate_custom_formula(image, formula): |
|
"""Apply a custom formula to the image.""" |
|
return image.expression(formula).rename('Custom Index') |
|
|
|
|
|
st.title("Sentinel Dataset Selector and Earth Engine Index Calculator") |
|
|
|
|
|
st.header("Sentinel Dataset Selector") |
|
|
|
|
|
main_selection = st.selectbox("Select Sentinel Dataset Category", list(data.keys())) |
|
|
|
|
|
sub_selection = None |
|
if main_selection: |
|
sub_options = data[main_selection]["sub_options"] |
|
sub_selection = st.selectbox("Select Specific Dataset ID", list(sub_options.keys())) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.header("Earth Engine Index Calculator") |
|
|
|
|
|
index_choice = st.selectbox("Select an Index or Enter Custom Formula", ['NDVI', 'NDWI', 'Average NO₂', 'Custom Formula']) |
|
|
|
|
|
custom_formula = "" |
|
if index_choice == 'Custom Formula': |
|
custom_formula = st.text_input("Enter Custom Formula (e.g., 'B5 - B4 / B5 + B4')") |
|
|
|
|
|
if sub_selection and st.button("Calculate Index"): |
|
|
|
collection = ee.ImageCollection(sub_selection).filterDate('2020-01-01', '2020-1-31') |
|
image = collection.first() |
|
|
|
result_image = None |
|
if index_choice == 'NDVI': |
|
result_image = calculate_ndvi(image) |
|
elif index_choice == 'NDWI': |
|
result_image = calculate_ndwi(image) |
|
elif index_choice == 'Average NO₂': |
|
result = calculate_avg_no2(image) |
|
st.write("Average NO₂ Concentration:", result.getInfo()) |
|
elif index_choice == 'Custom Formula' and custom_formula: |
|
result_image = calculate_custom_formula(image, custom_formula) |
|
|
|
|
|
if result_image: |
|
Map = geemap.Map() |
|
Map.centerObject(image, 10) |
|
Map.addLayer(result_image, {}, index_choice) |
|
Map.to_streamlit(height=600) |
|
|