YashMK89 commited on
Commit
3f3013a
·
verified ·
1 Parent(s): 13ab7bc

update app.oy

Browse files
Files changed (1) hide show
  1. app.py +26 -41
app.py CHANGED
@@ -65,9 +65,6 @@ st.header("Earth Engine Index Calculator")
65
  # Choose Index or Custom Formula (case-insensitive)
66
  index_choice = st.selectbox("Select an Index or Enter Custom Formula", ['NDVI', 'NDWI', 'Average NO₂', 'Custom Formula'])
67
 
68
- # Select the statistical operation (mean, max, min, sum, median)
69
- operation_choice = st.selectbox("Select Statistical Operation", ['mean', 'max', 'min', 'sum', 'median'])
70
-
71
  # Initialize custom_formula variable
72
  custom_formula = ""
73
 
@@ -82,29 +79,6 @@ elif index_choice.lower() == 'custom formula':
82
  custom_formula = st.text_input("Enter Custom Formula (e.g., 'B5 - B4 / B5 + B4')")
83
  st.write(f"Custom Formula: {custom_formula}") # Display the custom formula after the user inputs it
84
 
85
- # Function to perform a specified statistical operation
86
- def calculate_statistic(image, geometry, operation='mean', scale=30):
87
- if operation == 'mean':
88
- reducer = ee.Reducer.mean()
89
- elif operation == 'max':
90
- reducer = ee.Reducer.max()
91
- elif operation == 'min':
92
- reducer = ee.Reducer.min()
93
- elif operation == 'sum':
94
- reducer = ee.Reducer.sum()
95
- elif operation == 'median':
96
- reducer = ee.Reducer.median()
97
- else:
98
- raise ValueError("Invalid operation. Choose from 'mean', 'max', 'min', 'sum', or 'median'.")
99
-
100
- result = image.reduceRegion(
101
- reducer=reducer,
102
- geometry=geometry,
103
- scale=scale
104
- )
105
-
106
- return result
107
-
108
  # Function to check if the polygon geometry is valid and convert it to the correct format
109
  def convert_to_ee_geometry(geometry):
110
  # Ensure the polygon geometry is in the right format
@@ -178,28 +152,39 @@ if parameters_changed():
178
  'end_date_str': end_date_str
179
  }
180
 
181
- # Function to calculate NDVI (Normalized Difference Vegetation Index)
182
- def calculate_ndvi(image, geometry, operation='mean'):
183
  ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI')
184
- result = calculate_statistic(ndvi, geometry, operation)
 
 
 
 
185
  return result.get('NDVI')
186
 
187
- # Function to calculate NDWI (Normalized Difference Water Index)
188
- def calculate_ndwi(image, geometry, operation='mean'):
189
  ndwi = image.normalizedDifference(['B3', 'B8']).rename('NDWI')
190
- result = calculate_statistic(ndwi, geometry, operation)
 
 
 
 
191
  return result.get('NDWI')
192
 
193
- # Function to calculate average NO2 from Sentinel-5P data
194
- def calculate_avg_no2_sentinel5p(image, geometry, operation='mean'):
195
- no2 = image.select('NO2')
196
- result = calculate_statistic(no2, geometry, operation, scale=1000)
197
- return result.get('NO2')
 
 
198
 
199
- # Function to calculate a custom index using an expression
200
- def calculate_custom_formula(image, geometry, formula, operation='mean'):
201
- custom_index = image.expression(formula).rename('Custom Index')
202
- result = calculate_statistic(custom_index, geometry, operation)
 
 
203
  return result.get('Custom Index')
204
 
205
  # Check if the file uploaded is different from the previous file uploaded
 
65
  # Choose Index or Custom Formula (case-insensitive)
66
  index_choice = st.selectbox("Select an Index or Enter Custom Formula", ['NDVI', 'NDWI', 'Average NO₂', 'Custom Formula'])
67
 
 
 
 
68
  # Initialize custom_formula variable
69
  custom_formula = ""
70
 
 
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):
84
  # Ensure the polygon geometry is in the right format
 
152
  'end_date_str': end_date_str
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,
186
+ scale=30
187
+ )
188
  return result.get('Custom Index')
189
 
190
  # Check if the file uploaded is different from the previous file uploaded