|
import streamlit as st |
|
import json |
|
import ee |
|
import geemap |
|
import os |
|
import time |
|
import pandas as pd |
|
import geopandas as gpd |
|
from shapely.geometry import shape |
|
|
|
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) |
|
|
|
|
|
st.title("Sentinel Dataset and Index Calculator") |
|
|
|
|
|
main_selection = st.selectbox("Select Sentinel Dataset Category", list(data.keys())) |
|
|
|
|
|
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')") |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload CSV, GeoJSON, or KML file", type=["csv", "geojson", "kml"]) |
|
|
|
def parse_file(uploaded_file): |
|
""" |
|
Parse the uploaded file (CSV, GeoJSON, or KML) and return the geometry. |
|
""" |
|
if uploaded_file is not None: |
|
|
|
file_type = uploaded_file.type |
|
|
|
|
|
if file_type == "text/csv": |
|
df = pd.read_csv(uploaded_file) |
|
st.write("CSV file content:") |
|
st.write(df.head()) |
|
|
|
if 'latitude' in df.columns and 'longitude' in df.columns: |
|
|
|
geometry = gpd.GeoSeries.from_xy(df.longitude, df.latitude) |
|
return geometry |
|
|
|
|
|
elif file_type == "application/geo+json": |
|
gdf = gpd.read_file(uploaded_file) |
|
st.write("GeoJSON file content:") |
|
st.write(gdf.head()) |
|
return gdf.geometry |
|
|
|
|
|
elif file_type == "application/vnd.google-earth.kml+xml": |
|
gdf = gpd.read_file(uploaded_file) |
|
st.write("KML file content:") |
|
st.write(gdf.head()) |
|
return gdf.geometry |
|
|
|
else: |
|
st.error("Unsupported file format. Please upload a CSV, GeoJSON, or KML file.") |
|
return None |
|
else: |
|
st.warning("Please upload a file.") |
|
return None |
|
|
|
|
|
geometry = parse_file(uploaded_file) |
|
|
|
|
|
def calculate_ndvi(image, geometry): |
|
return image.normalizedDifference(['B5', 'B4']).rename('NDVI').reduceRegion( |
|
reducer=ee.Reducer.mean(), |
|
geometry=geometry, |
|
scale=30 |
|
) |
|
|
|
def calculate_ndwi(image, geometry): |
|
return image.normalizedDifference(['B3', 'B5']).rename('NDWI').reduceRegion( |
|
reducer=ee.Reducer.mean(), |
|
geometry=geometry, |
|
scale=30 |
|
) |
|
|
|
def calculate_avg_no2(image, geometry): |
|
return image.select('NO2').reduceRegion( |
|
reducer=ee.Reducer.mean(), |
|
geometry=geometry, |
|
scale=1000 |
|
).get('NO2') |
|
|
|
def calculate_custom_formula(image, geometry, formula): |
|
return image.expression(formula).rename('Custom Index').reduceRegion( |
|
reducer=ee.Reducer.mean(), |
|
geometry=geometry, |
|
scale=30 |
|
) |
|
|
|
|
|
if st.button("Calculate Index"): |
|
if geometry is not None: |
|
try: |
|
|
|
dataset_id = sub_options[sub_selection] |
|
if not ee.data.getInfo(dataset_id): |
|
st.error(f"The dataset '{dataset_id}' does not exist or is not accessible.") |
|
st.stop() |
|
|
|
|
|
collection = ee.ImageCollection(dataset_id).filterDate('2020-01-01', '2020-12-31') |
|
image = collection.first() |
|
|
|
|
|
if index_choice == 'NDVI': |
|
result = calculate_ndvi(image, geometry) |
|
st.write("NDVI result:", result.getInfo()) |
|
elif index_choice == 'NDWI': |
|
result = calculate_ndwi(image, geometry) |
|
st.write("NDWI result:", result.getInfo()) |
|
elif index_choice == 'Average NO₂': |
|
result = calculate_avg_no2(image, geometry) |
|
st.write("Average NO₂ Concentration:", result.getInfo()) |
|
elif index_choice == 'Custom Formula' and custom_formula: |
|
result = calculate_custom_formula(image, geometry, custom_formula) |
|
st.write("Custom Index result:", result.getInfo()) |
|
|
|
|
|
vis_params = {"min": 0, "max": 1, "palette": ["blue", "white", "green"]} |
|
|
|
|
|
with st.spinner('Loading the map, please wait...'): |
|
time.sleep(2) |
|
|
|
|
|
Map = geemap.Map() |
|
Map.setCenter(-100.0, 40.0, 4) |
|
Map.addLayer(image, vis_params, index_choice) |
|
Map.to_streamlit(height=600) |
|
|
|
except ee.EEException as e: |
|
st.error(f"Earth Engine Error: {e}") |
|
else: |
|
st.error("No geometry data found in the uploaded file.") |
|
|