YashMK89 commited on
Commit
8bb820f
·
verified ·
1 Parent(s): a95456a

update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -52
app.py CHANGED
@@ -76,8 +76,20 @@ elif index_choice.lower() == 'ndwi':
76
  elif index_choice.lower() == 'average no₂':
77
  st.write("Formula for Average NO₂: Average NO₂ = Mean(NO2 band)")
78
  elif index_choice.lower() == 'custom formula':
79
- custom_formula = st.text_input("Enter Custom Formula (e.g., 'B5 - B4 / B5 + B4')")
80
- st.write(f"Custom Formula: {custom_formula}") # Display the custom formula after the user inputs it
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  # Function to check if the polygon geometry is valid and convert it to the correct format
83
  def convert_to_ee_geometry(geometry):
@@ -153,33 +165,11 @@ if parameters_changed():
153
  }
154
 
155
  # Function to perform index calculations
156
- def calculate_ndvi(image, geometry):
157
- ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI')
158
- result = ndvi.reduceRegion(
159
- reducer=ee.Reducer.mean(),
160
- geometry=geometry,
161
- scale=30
162
- )
163
- return result.get('NDVI')
164
-
165
- def calculate_ndwi(image, geometry):
166
- ndwi = image.normalizedDifference(['B3', 'B8']).rename('NDWI')
167
- result = ndwi.reduceRegion(
168
- reducer=ee.Reducer.mean(),
169
- geometry=geometry,
170
- scale=30
171
- )
172
- return result.get('NDWI')
173
-
174
- def calculate_avg_no2_sentinel5p(image, geometry):
175
- no2 = image.select('NO2').reduceRegion(
176
- reducer=ee.Reducer.mean(),
177
- geometry=geometry,
178
- scale=1000
179
- ).get('NO2')
180
- return no2
181
-
182
- def calculate_custom_formula(image, geometry, formula):
183
  result = image.expression(formula).rename('Custom Index').reduceRegion(
184
  reducer=ee.Reducer.mean(),
185
  geometry=geometry,
@@ -271,17 +261,8 @@ if file_upload:
271
 
272
  # Perform the calculation based on user selection
273
  result = None
274
- if index_choice.lower() == 'ndvi':
275
- result = calculate_ndvi(image, roi)
276
- elif index_choice.lower() == 'ndwi':
277
- result = calculate_ndwi(image, roi)
278
- elif index_choice.lower() == 'average no₂':
279
- if 'NO2' in image.bandNames().getInfo():
280
- result = calculate_avg_no2_sentinel5p(image, roi)
281
- else:
282
- st.warning(f"No NO2 band found for {location_name}. Please use Sentinel-5P for NO₂ data.")
283
- elif index_choice.lower() == 'custom formula' and custom_formula:
284
- result = calculate_custom_formula(image, roi, custom_formula)
285
 
286
  if result is not None:
287
  # Only store the numeric value (not the dictionary structure)
@@ -348,17 +329,8 @@ if file_upload:
348
 
349
  # Perform the calculation based on user selection
350
  result = None
351
- if index_choice.lower() == 'ndvi':
352
- result = calculate_ndvi(image, roi)
353
- elif index_choice.lower() == 'ndwi':
354
- result = calculate_ndwi(image, roi)
355
- elif index_choice.lower() == 'average no₂':
356
- if 'NO2' in image.bandNames().getInfo():
357
- result = calculate_avg_no2_sentinel5p(image, roi)
358
- else:
359
- st.warning(f"No NO2 band found for {location_name}. Please use Sentinel-5P for NO₂ data.")
360
- elif index_choice.lower() == 'custom formula' and custom_formula:
361
- result = calculate_custom_formula(image, roi, custom_formula)
362
 
363
  if result is not None:
364
  # Only store the numeric value (not the dictionary structure)
@@ -395,4 +367,3 @@ if st.session_state.results:
395
  file_name=filename,
396
  mime='text/csv'
397
  )
398
-
 
76
  elif index_choice.lower() == 'average no₂':
77
  st.write("Formula for Average NO₂: Average NO₂ = Mean(NO2 band)")
78
  elif index_choice.lower() == 'custom formula':
79
+ # Get the bands for the selected dataset category
80
+ bands = data[main_selection]["bands"]
81
+ # Allow the user to select up to 3 bands
82
+ band_selection = st.multiselect("Select up to 3 bands for custom calculation", bands, max_selections=3)
83
+
84
+ # Show the selected bands in the custom formula input
85
+ if len(band_selection) > 0:
86
+ st.write("You selected the following bands:")
87
+ st.write(band_selection)
88
+
89
+ # Allow the user to enter a custom formula with the selected bands
90
+ if band_selection:
91
+ custom_formula = st.text_input("Enter Custom Formula (e.g., 'B5 - B4 / B5 + B4')")
92
+ st.write(f"Custom Formula: {custom_formula}") # Display the custom formula after the user inputs it
93
 
94
  # Function to check if the polygon geometry is valid and convert it to the correct format
95
  def convert_to_ee_geometry(geometry):
 
165
  }
166
 
167
  # Function to perform index calculations
168
+ def calculate_custom_formula(image, geometry, formula, band_selection):
169
+ # Replace band names in the formula with actual band values
170
+ for i, band in enumerate(band_selection, 1):
171
+ formula = formula.replace(f'B{i}', band)
172
+ # Apply the formula using Earth Engine expression
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  result = image.expression(formula).rename('Custom Index').reduceRegion(
174
  reducer=ee.Reducer.mean(),
175
  geometry=geometry,
 
261
 
262
  # Perform the calculation based on user selection
263
  result = None
264
+ if index_choice.lower() == 'custom formula' and custom_formula:
265
+ result = calculate_custom_formula(image, roi, custom_formula, band_selection)
 
 
 
 
 
 
 
 
 
266
 
267
  if result is not None:
268
  # Only store the numeric value (not the dictionary structure)
 
329
 
330
  # Perform the calculation based on user selection
331
  result = None
332
+ if index_choice.lower() == 'custom formula' and custom_formula:
333
+ result = calculate_custom_formula(image, roi, custom_formula, band_selection)
 
 
 
 
 
 
 
 
 
334
 
335
  if result is not None:
336
  # Only store the numeric value (not the dictionary structure)
 
367
  file_name=filename,
368
  mime='text/csv'
369
  )