update app.py
Browse files
app.py
CHANGED
@@ -1,140 +1,3 @@
|
|
1 |
-
# import streamlit as st
|
2 |
-
# import json
|
3 |
-
# import ee
|
4 |
-
# import os
|
5 |
-
# import pandas as pd
|
6 |
-
# import geopandas as gpd
|
7 |
-
# from datetime import datetime
|
8 |
-
# import leafmap.foliumap as leafmap
|
9 |
-
# import re
|
10 |
-
# from shapely.geometry import base
|
11 |
-
# from lxml import etree
|
12 |
-
# from xml.etree import ElementTree as ET
|
13 |
-
|
14 |
-
# # Set up the page layout
|
15 |
-
# st.set_page_config(layout="wide")
|
16 |
-
|
17 |
-
# # Custom button styling
|
18 |
-
# m = st.markdown(
|
19 |
-
# """
|
20 |
-
# <style>
|
21 |
-
# div.stButton > button:first-child {
|
22 |
-
# background-color: #006400;
|
23 |
-
# color:#ffffff;
|
24 |
-
# }
|
25 |
-
# </style>""",
|
26 |
-
# unsafe_allow_html=True,
|
27 |
-
# )
|
28 |
-
|
29 |
-
# # Logo
|
30 |
-
# st.write(
|
31 |
-
# f"""
|
32 |
-
# <div style="display: flex; justify-content: space-between; align-items: center;">
|
33 |
-
# <img src="https://huggingface.co/spaces/YashMK89/GEE_Calculator/resolve/main/ISRO_Logo.png" style="width: 20%; margin-right: auto;">
|
34 |
-
# <img src="https://huggingface.co/spaces/YashMK89/GEE_Calculator/resolve/main/SAC_Logo.png" style="width: 20%; margin-left: auto;">
|
35 |
-
# </div>
|
36 |
-
# """,
|
37 |
-
# unsafe_allow_html=True,
|
38 |
-
# )
|
39 |
-
|
40 |
-
# # Title
|
41 |
-
# st.markdown(
|
42 |
-
# f"""
|
43 |
-
# <h1 style="text-align: center;">Precision Analysis for Vegetation, Water, and Air Quality</h1>
|
44 |
-
# """,
|
45 |
-
# unsafe_allow_html=True,
|
46 |
-
# )
|
47 |
-
# st.write("<h2><div style='text-align: center;'>User Inputs</div></h2>", unsafe_allow_html=True)
|
48 |
-
|
49 |
-
# # Authenticate and initialize Earth Engine
|
50 |
-
# earthengine_credentials = os.environ.get("EE_Authentication")
|
51 |
-
|
52 |
-
# # Initialize Earth Engine with secret credentials
|
53 |
-
# os.makedirs(os.path.expanduser("~/.config/earthengine/"), exist_ok=True)
|
54 |
-
# with open(os.path.expanduser("~/.config/earthengine/credentials"), "w") as f:
|
55 |
-
# f.write(earthengine_credentials)
|
56 |
-
|
57 |
-
# ee.Initialize(project='ee-yashsacisro24')
|
58 |
-
|
59 |
-
# # Load the Sentinel dataset options from JSON file
|
60 |
-
# with open("sentinel_datasets.json") as f:
|
61 |
-
# data = json.load(f)
|
62 |
-
|
63 |
-
# # Display the title for the Streamlit app
|
64 |
-
# st.title("Sentinel Dataset")
|
65 |
-
|
66 |
-
# # Select dataset category (main selection)
|
67 |
-
# main_selection = st.selectbox("Select Sentinel Dataset Category", list(data.keys()))
|
68 |
-
|
69 |
-
# # If a category is selected, display the sub-options (specific datasets)
|
70 |
-
# if main_selection:
|
71 |
-
# sub_options = data[main_selection]["sub_options"]
|
72 |
-
# sub_selection = st.selectbox("Select Specific Dataset ID", list(sub_options.keys()))
|
73 |
-
|
74 |
-
# # Display the selected dataset ID based on user input
|
75 |
-
# if sub_selection:
|
76 |
-
# st.write(f"You selected: {main_selection} -> {sub_options[sub_selection]}")
|
77 |
-
# st.write(f"Dataset ID: {sub_selection}")
|
78 |
-
# dataset_id = sub_selection # Use the key directly as the dataset ID
|
79 |
-
|
80 |
-
# # Earth Engine Index Calculator Section
|
81 |
-
# st.header("Earth Engine Index Calculator")
|
82 |
-
|
83 |
-
# # Load band information based on selected dataset
|
84 |
-
# if main_selection and sub_selection:
|
85 |
-
# dataset_bands = data[main_selection]["bands"].get(sub_selection, [])
|
86 |
-
# st.write(f"Available Bands for {sub_options[sub_selection]}: {', '.join(dataset_bands)}")
|
87 |
-
|
88 |
-
# # Allow user to select 1 or 2 bands
|
89 |
-
# selected_bands = st.multiselect(
|
90 |
-
# "Select 1 or 2 Bands for Calculation",
|
91 |
-
# options=dataset_bands,
|
92 |
-
# default=[dataset_bands[0]] if dataset_bands else [],
|
93 |
-
# help="Select at least 1 band and up to 2 bands."
|
94 |
-
# )
|
95 |
-
|
96 |
-
# # Ensure minimum 1 and maximum 2 bands are selected
|
97 |
-
# if len(selected_bands) < 1:
|
98 |
-
# st.warning("Please select at least one band.")
|
99 |
-
# st.stop()
|
100 |
-
# elif len(selected_bands) > 2:
|
101 |
-
# st.warning("You can select a maximum of 2 bands.")
|
102 |
-
# st.stop()
|
103 |
-
|
104 |
-
# # Show custom formula input if bands are selected
|
105 |
-
# if selected_bands:
|
106 |
-
# default_formula = (
|
107 |
-
# f"{selected_bands[0]}" if len(selected_bands) == 1
|
108 |
-
# else f"({selected_bands[0]} - {selected_bands[1]}) / ({selected_bands[0]} + {selected_bands[1]})"
|
109 |
-
# )
|
110 |
-
# custom_formula = st.text_input(
|
111 |
-
# "Enter Custom Formula (e.g., 'B3*B5/2' or '(B8 - B4) / (B8 + B4)')",
|
112 |
-
# value=default_formula,
|
113 |
-
# help=f"Use {', '.join(selected_bands)} in your formula. Example: 'B3*B5/2'"
|
114 |
-
# )
|
115 |
-
|
116 |
-
# # Validate the formula
|
117 |
-
# def validate_formula(formula, selected_bands):
|
118 |
-
# allowed_chars = set(" +-*/()0123456789.")
|
119 |
-
# terms = re.findall(r'[a-zA-Z][a-zA-Z0-9]*', formula)
|
120 |
-
# invalid_terms = [term for term in terms if term not in selected_bands]
|
121 |
-
# if invalid_terms:
|
122 |
-
# return False, f"Invalid terms in formula: {', '.join(invalid_terms)}. Use only {', '.join(selected_bands)}."
|
123 |
-
# if not all(char in allowed_chars or char in ''.join(selected_bands) for char in formula):
|
124 |
-
# return False, "Formula contains invalid characters. Use only bands, numbers, and operators (+, -, *, /, ())"
|
125 |
-
# return True, ""
|
126 |
-
|
127 |
-
# is_valid, error_message = validate_formula(custom_formula, selected_bands)
|
128 |
-
# if not is_valid:
|
129 |
-
# st.error(error_message)
|
130 |
-
# st.stop()
|
131 |
-
# elif not custom_formula:
|
132 |
-
# st.warning("Please enter a custom formula to proceed.")
|
133 |
-
# st.stop()
|
134 |
-
|
135 |
-
# # Display the validated formula
|
136 |
-
# st.write(f"Custom Formula: {custom_formula}")
|
137 |
-
|
138 |
import streamlit as st
|
139 |
import json
|
140 |
import ee
|
@@ -235,7 +98,7 @@ if main_selection and sub_selection:
|
|
235 |
"Select 1 or 2 Bands for Calculation",
|
236 |
options=dataset_bands,
|
237 |
default=[dataset_bands[0]] if dataset_bands else [],
|
238 |
-
help="Select
|
239 |
)
|
240 |
|
241 |
# Ensure minimum 1 and maximum 2 bands are selected
|
@@ -248,20 +111,31 @@ if main_selection and sub_selection:
|
|
248 |
|
249 |
# Show custom formula input if bands are selected
|
250 |
if selected_bands:
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
custom_formula = st.text_input(
|
256 |
-
"Enter Custom Formula
|
257 |
value=default_formula,
|
258 |
-
help=f"Use {', '.join(selected_bands)}
|
259 |
)
|
260 |
|
261 |
# Validate the formula
|
262 |
def validate_formula(formula, selected_bands):
|
263 |
allowed_chars = set(" +-*/()0123456789.")
|
264 |
-
terms
|
|
|
265 |
invalid_terms = [term for term in terms if term not in selected_bands]
|
266 |
if invalid_terms:
|
267 |
return False, f"Invalid terms in formula: {', '.join(invalid_terms)}. Use only {', '.join(selected_bands)}."
|
@@ -280,7 +154,7 @@ if main_selection and sub_selection:
|
|
280 |
# Display the validated formula
|
281 |
st.write(f"Custom Formula: {custom_formula}")
|
282 |
|
283 |
-
# The rest of your code remains unchanged...
|
284 |
|
285 |
# Function to get the corresponding reducer based on user input
|
286 |
def get_reducer(reducer_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import json
|
3 |
import ee
|
|
|
98 |
"Select 1 or 2 Bands for Calculation",
|
99 |
options=dataset_bands,
|
100 |
default=[dataset_bands[0]] if dataset_bands else [],
|
101 |
+
help=f"Select 1 or 2 bands from: {', '.join(dataset_bands)}"
|
102 |
)
|
103 |
|
104 |
# Ensure minimum 1 and maximum 2 bands are selected
|
|
|
111 |
|
112 |
# Show custom formula input if bands are selected
|
113 |
if selected_bands:
|
114 |
+
# Provide a default formula based on the imagery base and selected bands
|
115 |
+
if imagery_base == "Sentinel":
|
116 |
+
default_formula = (
|
117 |
+
f"{selected_bands[0]}" if len(selected_bands) == 1
|
118 |
+
else f"({selected_bands[0]} - {selected_bands[1]}) / ({selected_bands[0]} + {selected_bands[1]})"
|
119 |
+
)
|
120 |
+
example = f"'{selected_bands[0]} * {selected_bands[1]} / 2' or '({selected_bands[0]} - {selected_bands[1]}) / ({selected_bands[0]} + {selected_bands[1]})'"
|
121 |
+
else: # Landsat
|
122 |
+
default_formula = (
|
123 |
+
f"{selected_bands[0]}" if len(selected_bands) == 1
|
124 |
+
else f"({selected_bands[0]} - {selected_bands[1]}) / ({selected_bands[0]} + {selected_bands[1]})"
|
125 |
+
)
|
126 |
+
example = f"'{selected_bands[0]} * {selected_bands[1]} / 2' or '({selected_bands[0]} - {selected_bands[1]}) / ({selected_bands[0]} + {selected_bands[1]})'"
|
127 |
+
|
128 |
custom_formula = st.text_input(
|
129 |
+
"Enter Custom Formula",
|
130 |
value=default_formula,
|
131 |
+
help=f"Use only these bands: {', '.join(selected_bands)}. Examples: {example}"
|
132 |
)
|
133 |
|
134 |
# Validate the formula
|
135 |
def validate_formula(formula, selected_bands):
|
136 |
allowed_chars = set(" +-*/()0123456789.")
|
137 |
+
# Match alphanumeric terms including underscores (for Landsat bands like SR_B1)
|
138 |
+
terms = re.findall(r'[a-zA-Z][a-zA-Z0-9_]*', formula)
|
139 |
invalid_terms = [term for term in terms if term not in selected_bands]
|
140 |
if invalid_terms:
|
141 |
return False, f"Invalid terms in formula: {', '.join(invalid_terms)}. Use only {', '.join(selected_bands)}."
|
|
|
154 |
# Display the validated formula
|
155 |
st.write(f"Custom Formula: {custom_formula}")
|
156 |
|
157 |
+
# The rest of your code (reducer, geometry conversion, date input, aggregation, etc.) remains unchanged...
|
158 |
|
159 |
# Function to get the corresponding reducer based on user input
|
160 |
def get_reducer(reducer_name):
|