update app.py
Browse files
app.py
CHANGED
@@ -371,40 +371,58 @@ def calculate_ndwi(image, geometry, reducer_choice):
|
|
371 |
ndwi = image.normalizedDifference(['B3', 'B8']).rename('NDWI')
|
372 |
return ndwi
|
373 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
374 |
def calculate_custom_formula(image, geometry, custom_formula, reducer_choice, scale=30):
|
375 |
try:
|
376 |
-
# Check if the custom formula contains a comma (dual-band calculation)
|
377 |
if "," in custom_formula:
|
378 |
-
|
379 |
-
|
380 |
-
band2
|
381 |
-
|
382 |
-
# Ensure the bands exist in the image
|
383 |
-
if band1 not in image.bandNames().getInfo() or band2 not in image.bandNames().getInfo():
|
384 |
-
st.error(f"One or both bands ({band1}, {band2}) do not exist in the image.")
|
385 |
-
return None
|
386 |
-
|
387 |
-
# Perform the normalized difference calculation
|
388 |
result = image.normalizedDifference([band1, band2]).rename('custom_formula')
|
389 |
-
|
390 |
else:
|
391 |
-
# Single-band calculation
|
392 |
band = custom_formula.strip()
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
st.error(f"The band '{band}' does not exist in the image.")
|
397 |
-
return None
|
398 |
-
|
399 |
-
# Extract the band
|
400 |
result = image.select(band).rename('custom_formula')
|
401 |
-
|
402 |
return result
|
403 |
-
|
404 |
except Exception as e:
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
# Modify aggregation functions to return the correct time period and aggregated results
|
409 |
def aggregate_data_daily(collection):
|
410 |
# Extract day from the image date (using the exact date)
|
|
|
371 |
ndwi = image.normalizedDifference(['B3', 'B8']).rename('NDWI')
|
372 |
return ndwi
|
373 |
|
374 |
+
# def calculate_custom_formula(image, geometry, custom_formula, reducer_choice, scale=30):
|
375 |
+
# try:
|
376 |
+
# # Check if the custom formula contains a comma (dual-band calculation)
|
377 |
+
# if "," in custom_formula:
|
378 |
+
# # Dual-band calculation (e.g., NDVI, NDWI)
|
379 |
+
# band1 = custom_formula[:custom_formula.find(",")].strip()
|
380 |
+
# band2 = custom_formula[custom_formula.find(",") + 1:].strip()
|
381 |
+
|
382 |
+
# # Ensure the bands exist in the image
|
383 |
+
# if band1 not in image.bandNames().getInfo() or band2 not in image.bandNames().getInfo():
|
384 |
+
# st.error(f"One or both bands ({band1}, {band2}) do not exist in the image.")
|
385 |
+
# return None
|
386 |
+
|
387 |
+
# # Perform the normalized difference calculation
|
388 |
+
# result = image.normalizedDifference([band1, band2]).rename('custom_formula')
|
389 |
+
|
390 |
+
# else:
|
391 |
+
# # Single-band calculation
|
392 |
+
# band = custom_formula.strip()
|
393 |
+
|
394 |
+
# # Ensure the band exists in the image
|
395 |
+
# if band not in image.bandNames().getInfo():
|
396 |
+
# st.error(f"The band '{band}' does not exist in the image.")
|
397 |
+
# return None
|
398 |
+
|
399 |
+
# # Extract the band
|
400 |
+
# result = image.select(band).rename('custom_formula')
|
401 |
+
|
402 |
+
# return result
|
403 |
+
|
404 |
+
# except Exception as e:
|
405 |
+
# st.error(f"Error calculating custom formula: {e}")
|
406 |
+
# return None
|
407 |
+
|
408 |
def calculate_custom_formula(image, geometry, custom_formula, reducer_choice, scale=30):
|
409 |
try:
|
|
|
410 |
if "," in custom_formula:
|
411 |
+
band1, band2 = [b.strip() for b in custom_formula.split(",")]
|
412 |
+
band_names = image.bandNames().getInfo()
|
413 |
+
if band1 not in band_names or band2 not in band_names:
|
414 |
+
raise ValueError(f"One or both bands ({band1}, {band2}) do not exist in the image.")
|
|
|
|
|
|
|
|
|
|
|
|
|
415 |
result = image.normalizedDifference([band1, band2]).rename('custom_formula')
|
|
|
416 |
else:
|
|
|
417 |
band = custom_formula.strip()
|
418 |
+
band_names = image.bandNames().getInfo()
|
419 |
+
if band not in band_names:
|
420 |
+
raise ValueError(f"The band '{band}' does not exist in the image.")
|
|
|
|
|
|
|
|
|
421 |
result = image.select(band).rename('custom_formula')
|
|
|
422 |
return result
|
|
|
423 |
except Exception as e:
|
424 |
+
return ee.Image(0).rename('custom_formula').set('error', str(e))
|
425 |
+
|
|
|
426 |
# Modify aggregation functions to return the correct time period and aggregated results
|
427 |
def aggregate_data_daily(collection):
|
428 |
# Extract day from the image date (using the exact date)
|