update app.py
Browse files
app.py
CHANGED
@@ -90,8 +90,9 @@ if main_selection:
|
|
90 |
|
91 |
# Earth Engine Index Calculator Section
|
92 |
st.header("Earth Engine Index Calculator")
|
|
|
93 |
# Load band information based on selected dataset
|
94 |
-
if main_selection and sub_selection:
|
95 |
dataset_bands = data[main_selection]["bands"].get(sub_selection, [])
|
96 |
st.write(f"Available Bands for {sub_options[sub_selection]}: {', '.join(dataset_bands)}")
|
97 |
|
@@ -113,20 +114,21 @@ if main_selection and sub_selection:
|
|
113 |
|
114 |
# Show custom formula input if bands are selected
|
115 |
if selected_bands:
|
|
|
116 |
if len(selected_bands) == 1:
|
117 |
default_formula = f"{selected_bands[0]}"
|
118 |
example = f"'{selected_bands[0]} * 2' or '{selected_bands[0]} + 1'"
|
119 |
-
else:
|
120 |
default_formula = f"({selected_bands[0]} - {selected_bands[1]}) / ({selected_bands[0]} + {selected_bands[1]})"
|
121 |
example = f"'{selected_bands[0]} * {selected_bands[1]} / 2' or '({selected_bands[0]} - {selected_bands[1]}) / ({selected_bands[0]} + {selected_bands[1]})'"
|
122 |
|
123 |
custom_formula = st.text_input(
|
124 |
"Enter Custom Formula",
|
125 |
value=default_formula,
|
126 |
-
help=f"Use only these bands: {', '.join(selected_bands)}. Examples: {example}
|
127 |
)
|
128 |
|
129 |
-
# Validate the formula
|
130 |
def validate_formula(formula, selected_bands):
|
131 |
allowed_chars = set(" +-*/()0123456789.")
|
132 |
terms = re.findall(r'[a-zA-Z][a-zA-Z0-9_]*', formula)
|
@@ -366,15 +368,9 @@ def calculate_custom_formula(image, geometry, selected_bands, custom_formula, re
|
|
366 |
band_values = {}
|
367 |
band_names = image.bandNames().getInfo()
|
368 |
|
369 |
-
# Check if all selected bands are available in the image
|
370 |
-
missing_bands = [band for band in selected_bands if band not in band_names]
|
371 |
-
if missing_bands:
|
372 |
-
error_msg = f"The following bands are not available in this image: {', '.join(missing_bands)}. Available bands: {', '.join(band_names)}. Please adjust your formula or check the data for your region and date range."
|
373 |
-
st.error(error_msg)
|
374 |
-
return ee.Image(0).rename('custom_result').set('error', error_msg)
|
375 |
-
|
376 |
-
# Only proceed if all bands are available
|
377 |
for band in selected_bands:
|
|
|
|
|
378 |
band_values[band] = image.select(band)
|
379 |
|
380 |
reducer = get_reducer(reducer_choice)
|
@@ -409,7 +405,7 @@ def calculate_custom_formula(image, geometry, selected_bands, custom_formula, re
|
|
409 |
except Exception as e:
|
410 |
st.error(f"Unexpected error: {e}")
|
411 |
return ee.Image(0).rename('custom_result').set('error', str(e))
|
412 |
-
|
413 |
# Function to calculate index for a period
|
414 |
def calculate_index_for_period(image, roi, selected_bands, custom_formula, reducer_choice):
|
415 |
return calculate_custom_formula(image, roi, selected_bands, custom_formula, reducer_choice)
|
|
|
90 |
|
91 |
# Earth Engine Index Calculator Section
|
92 |
st.header("Earth Engine Index Calculator")
|
93 |
+
|
94 |
# Load band information based on selected dataset
|
95 |
+
if main_selection and sub_selection: # Now safe because sub_selection is initialized
|
96 |
dataset_bands = data[main_selection]["bands"].get(sub_selection, [])
|
97 |
st.write(f"Available Bands for {sub_options[sub_selection]}: {', '.join(dataset_bands)}")
|
98 |
|
|
|
114 |
|
115 |
# Show custom formula input if bands are selected
|
116 |
if selected_bands:
|
117 |
+
# Provide a default formula based on the number of selected bands
|
118 |
if len(selected_bands) == 1:
|
119 |
default_formula = f"{selected_bands[0]}"
|
120 |
example = f"'{selected_bands[0]} * 2' or '{selected_bands[0]} + 1'"
|
121 |
+
else: # len(selected_bands) == 2
|
122 |
default_formula = f"({selected_bands[0]} - {selected_bands[1]}) / ({selected_bands[0]} + {selected_bands[1]})"
|
123 |
example = f"'{selected_bands[0]} * {selected_bands[1]} / 2' or '({selected_bands[0]} - {selected_bands[1]}) / ({selected_bands[0]} + {selected_bands[1]})'"
|
124 |
|
125 |
custom_formula = st.text_input(
|
126 |
"Enter Custom Formula",
|
127 |
value=default_formula,
|
128 |
+
help=f"Use only these bands: {', '.join(selected_bands)}. Examples: {example}"
|
129 |
)
|
130 |
|
131 |
+
# Validate the formula
|
132 |
def validate_formula(formula, selected_bands):
|
133 |
allowed_chars = set(" +-*/()0123456789.")
|
134 |
terms = re.findall(r'[a-zA-Z][a-zA-Z0-9_]*', formula)
|
|
|
368 |
band_values = {}
|
369 |
band_names = image.bandNames().getInfo()
|
370 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
371 |
for band in selected_bands:
|
372 |
+
if band not in band_names:
|
373 |
+
raise ValueError(f"Band '{band}' not found in the dataset.")
|
374 |
band_values[band] = image.select(band)
|
375 |
|
376 |
reducer = get_reducer(reducer_choice)
|
|
|
405 |
except Exception as e:
|
406 |
st.error(f"Unexpected error: {e}")
|
407 |
return ee.Image(0).rename('custom_result').set('error', str(e))
|
408 |
+
|
409 |
# Function to calculate index for a period
|
410 |
def calculate_index_for_period(image, roi, selected_bands, custom_formula, reducer_choice):
|
411 |
return calculate_custom_formula(image, roi, selected_bands, custom_formula, reducer_choice)
|