YashMK89 commited on
Commit
fee251e
·
verified ·
1 Parent(s): c688eb2

update app.py

Browse files
Files changed (1) hide show
  1. app.py +264 -86
app.py CHANGED
@@ -878,8 +878,7 @@ def calculate_index_for_period(image, roi, index_choice, reducer_choice, custom_
878
  # return aggregated_output.to_dict(orient='records')
879
 
880
  # return []
881
-
882
- ####################################################
883
  def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id, index_choice, reducer_choice, shape_type, aggregation_period, custom_formula=""):
884
  aggregated_results = []
885
 
@@ -922,14 +921,9 @@ def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id,
922
 
923
  # Process each image in the collection
924
  image_list = collection.toList(collection.size())
925
- all_values = [] # To store calculated values for mean calculation
926
- start_date = None
927
- end_date = None
928
-
929
  for i in range(image_list.size().getInfo()):
930
  image = ee.Image(image_list.get(i))
931
 
932
- # Extract the timestamp based on the aggregation period
933
  if aggregation_period.lower() == 'daily':
934
  timestamp = image.get('day')
935
  elif aggregation_period.lower() == 'weekly':
@@ -942,12 +936,6 @@ def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id,
942
  # Format the timestamp as a valid date string
943
  date = ee.Date(timestamp).format('YYYY-MM-dd').getInfo()
944
 
945
- # Update start_date and end_date
946
- if start_date is None or date < start_date:
947
- start_date = date
948
- if end_date is None or date > end_date:
949
- end_date = date
950
-
951
  # Calculate the index for each period
952
  index_image = calculate_index_for_period(image, roi, index_choice, reducer_choice, custom_formula)
953
 
@@ -960,23 +948,20 @@ def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id,
960
 
961
  calculated_value = index_value.getInfo()
962
 
963
- # Append valid values for mean calculation
964
  if isinstance(calculated_value, (int, float)):
965
- all_values.append(calculated_value)
 
 
 
 
 
 
 
 
966
  except Exception as e:
967
  st.error(f"Error retrieving value for {location_name}: {e}")
968
 
969
- # Calculate mean value for this location
970
- if all_values:
971
- mean_value = sum(all_values) / len(all_values)
972
- aggregated_results.append({
973
- 'Location Name': location_name,
974
- 'Start Date': start_date,
975
- 'End Date': end_date,
976
- 'Mean Value': mean_value
977
-
978
- })
979
-
980
  # Update progress bar
981
  progress_percentage = (idx + 1) / total_steps
982
  progress_bar.progress(progress_percentage)
@@ -1010,14 +995,9 @@ def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id,
1010
 
1011
  # Process each image in the collection
1012
  image_list = collection.toList(collection.size())
1013
- all_values = [] # To store calculated values for mean calculation
1014
- start_date = None
1015
- end_date = None
1016
-
1017
  for i in range(image_list.size().getInfo()):
1018
  image = ee.Image(image_list.get(i))
1019
 
1020
- # Extract the timestamp based on the aggregation period
1021
  if aggregation_period.lower() == 'daily':
1022
  timestamp = image.get('day')
1023
  elif aggregation_period.lower() == 'weekly':
@@ -1030,12 +1010,6 @@ def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id,
1030
  # Format the timestamp as a valid date string
1031
  date = ee.Date(timestamp).format('YYYY-MM-dd').getInfo()
1032
 
1033
- # Update start_date and end_date
1034
- if start_date is None or date < start_date:
1035
- start_date = date
1036
- if end_date is None or date > end_date:
1037
- end_date = date
1038
-
1039
  # Calculate the index for each period
1040
  index_image = calculate_index_for_period(image, roi, index_choice, reducer_choice, custom_formula)
1041
 
@@ -1048,70 +1022,274 @@ def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id,
1048
 
1049
  calculated_value = index_value.getInfo()
1050
 
1051
- # Append valid values for mean calculation
1052
  if isinstance(calculated_value, (int, float)):
1053
- all_values.append(calculated_value)
 
 
 
 
 
 
1054
  except Exception as e:
1055
  st.error(f"Error retrieving value for {location_name}: {e}")
1056
 
1057
- # Calculate mean value for this location
1058
- if all_values:
1059
- mean_value = sum(all_values) / len(all_values)
1060
- aggregated_results.append({
1061
- 'Location Name': location_name,
1062
- 'Mean Value': mean_value,
1063
- 'Start Date': start_date,
1064
- 'End Date': end_date
1065
- })
1066
-
1067
  # Update progress bar
1068
  progress_percentage = (idx + 1) / total_steps
1069
  progress_bar.progress(progress_percentage)
1070
  progress_text.markdown(f"Processing: {int(progress_percentage * 100)}%")
1071
 
1072
- # Convert results to DataFrame and return as dictionary
1073
  if aggregated_results:
1074
  result_df = pd.DataFrame(aggregated_results)
 
 
 
 
 
 
1075
 
1076
- # For point data, include latitude and longitude
1077
- if shape_type.lower() == "point":
1078
- # Check if 'name' column exists; if not, generate names based on latitude and longitude
1079
- if 'name' not in locations_df.columns:
1080
- if 'latitude' in locations_df.columns and 'longitude' in locations_df.columns:
1081
- locations_df['name'] = locations_df.apply(
1082
- lambda row: f"Location_{row.name}_{row['latitude']}_{row['longitude']}", axis=1
1083
- )
1084
- else:
1085
- st.error("Missing 'latitude' or 'longitude' columns in the input data.")
1086
- return [] # Exit early if required columns are missing
1087
 
1088
- # Ensure 'latitude' and 'longitude' columns exist before merging
1089
- if 'latitude' in locations_df.columns and 'longitude' in locations_df.columns:
1090
- result_df = result_df.merge(
1091
- locations_df[['name', 'latitude', 'longitude']],
1092
- left_on='Location Name',
1093
- right_on='name',
1094
- how='left'
1095
- )
1096
- result_df.rename(columns={'latitude': 'Latitude', 'longitude': 'Longitude'}, inplace=True)
1097
- result_df.drop(columns=['name'], inplace=True)
1098
- else:
1099
- st.error("Missing 'latitude' or 'longitude' columns in the input data.")
1100
- return [] # Exit early if required columns are missing
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1101
 
1102
- # For polygon data, add default names if 'name' column is missing
1103
- elif shape_type.lower() == "polygon":
1104
- if 'name' not in locations_df.columns:
1105
- locations_df['name'] = locations_df.index.map(lambda idx: f"Polygon_{idx}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1106
 
1107
- # Merge with 'name' column only for polygons
1108
- result_df = result_df.merge(
1109
- locations_df[['name']],
1110
- left_on='Location Name',
1111
- right_on='name',
1112
- how='left'
1113
- )
1114
- result_df.drop(columns=['name'], inplace=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1115
  ####################################################
1116
 
1117
  # def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id, index_choice, reducer_choice, shape_type, aggregation_period, custom_formula=""):
 
878
  # return aggregated_output.to_dict(orient='records')
879
 
880
  # return []
881
+ #####################################################
 
882
  def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id, index_choice, reducer_choice, shape_type, aggregation_period, custom_formula=""):
883
  aggregated_results = []
884
 
 
921
 
922
  # Process each image in the collection
923
  image_list = collection.toList(collection.size())
 
 
 
 
924
  for i in range(image_list.size().getInfo()):
925
  image = ee.Image(image_list.get(i))
926
 
 
927
  if aggregation_period.lower() == 'daily':
928
  timestamp = image.get('day')
929
  elif aggregation_period.lower() == 'weekly':
 
936
  # Format the timestamp as a valid date string
937
  date = ee.Date(timestamp).format('YYYY-MM-dd').getInfo()
938
 
 
 
 
 
 
 
939
  # Calculate the index for each period
940
  index_image = calculate_index_for_period(image, roi, index_choice, reducer_choice, custom_formula)
941
 
 
948
 
949
  calculated_value = index_value.getInfo()
950
 
951
+ # Append the results if valid
952
  if isinstance(calculated_value, (int, float)):
953
+ aggregated_results.append({
954
+ 'Location Name': location_name,
955
+ 'Latitude': latitude, # Include latitude
956
+ 'Longitude': longitude, # Include longitude
957
+ 'Date': date,
958
+ 'Calculated Value': calculated_value
959
+ })
960
+ else:
961
+ st.warning(f"Skipping invalid value for {location_name} on {date}")
962
  except Exception as e:
963
  st.error(f"Error retrieving value for {location_name}: {e}")
964
 
 
 
 
 
 
 
 
 
 
 
 
965
  # Update progress bar
966
  progress_percentage = (idx + 1) / total_steps
967
  progress_bar.progress(progress_percentage)
 
995
 
996
  # Process each image in the collection
997
  image_list = collection.toList(collection.size())
 
 
 
 
998
  for i in range(image_list.size().getInfo()):
999
  image = ee.Image(image_list.get(i))
1000
 
 
1001
  if aggregation_period.lower() == 'daily':
1002
  timestamp = image.get('day')
1003
  elif aggregation_period.lower() == 'weekly':
 
1010
  # Format the timestamp as a valid date string
1011
  date = ee.Date(timestamp).format('YYYY-MM-dd').getInfo()
1012
 
 
 
 
 
 
 
1013
  # Calculate the index for each period
1014
  index_image = calculate_index_for_period(image, roi, index_choice, reducer_choice, custom_formula)
1015
 
 
1022
 
1023
  calculated_value = index_value.getInfo()
1024
 
1025
+ # Append the results if valid
1026
  if isinstance(calculated_value, (int, float)):
1027
+ aggregated_results.append({
1028
+ 'Location Name': location_name,
1029
+ 'Date': date,
1030
+ 'Calculated Value': calculated_value
1031
+ })
1032
+ else:
1033
+ st.warning(f"Skipping invalid value for {location_name} on {date}")
1034
  except Exception as e:
1035
  st.error(f"Error retrieving value for {location_name}: {e}")
1036
 
 
 
 
 
 
 
 
 
 
 
1037
  # Update progress bar
1038
  progress_percentage = (idx + 1) / total_steps
1039
  progress_bar.progress(progress_percentage)
1040
  progress_text.markdown(f"Processing: {int(progress_percentage * 100)}%")
1041
 
1042
+ # Perform final aggregation (e.g., mean) for each location
1043
  if aggregated_results:
1044
  result_df = pd.DataFrame(aggregated_results)
1045
+ # Group by 'Location Name' and calculate the mean of 'Calculated Value'
1046
+ aggregated_output = result_df.groupby('Location Name').agg({
1047
+ 'Latitude': 'first', # Keep the first latitude value
1048
+ 'Longitude': 'first', # Keep the first longitude value
1049
+ 'Calculated Value': 'mean' # Calculate the mean of the calculated values
1050
+ }).reset_index()
1051
 
1052
+ # Rename columns for clarity
1053
+ aggregated_output.rename(columns={'Calculated Value': 'Aggregated Value'}, inplace=True)
 
 
 
 
 
 
 
 
 
1054
 
1055
+ # Convert the DataFrame to a dictionary for output
1056
+ return aggregated_output.to_dict(orient='records')
1057
+
1058
+ return []
1059
+ ####################################################
1060
+ ####################################################
1061
+ # def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id, index_choice, reducer_choice, shape_type, aggregation_period, custom_formula=""):
1062
+ # aggregated_results = []
1063
+
1064
+ # # Check if the index_choice is 'custom formula' and the custom formula is empty
1065
+ # if index_choice.lower() == 'custom_formula' and not custom_formula:
1066
+ # st.error("Custom formula cannot be empty. Please provide a formula.")
1067
+ # return aggregated_results # Return early to avoid further processing
1068
+
1069
+ # # Initialize progress bar
1070
+ # total_steps = len(locations_df)
1071
+ # progress_bar = st.progress(0)
1072
+ # progress_text = st.empty()
1073
+
1074
+ # with st.spinner('Processing data...'):
1075
+ # if shape_type.lower() == "point":
1076
+ # for idx, row in locations_df.iterrows():
1077
+ # # Check if the latitude and longitude columns exist and have values
1078
+ # latitude = row.get('latitude')
1079
+ # longitude = row.get('longitude')
1080
+ # if pd.isna(latitude) or pd.isna(longitude):
1081
+ # st.warning(f"Skipping location {idx} with missing latitude or longitude")
1082
+ # continue
1083
+
1084
+ # location_name = row.get('name', f"Location_{idx}")
1085
+ # roi = ee.Geometry.Point([longitude, latitude])
1086
+
1087
+ # collection = ee.ImageCollection(dataset_id) \
1088
+ # .filterDate(ee.Date(start_date_str), ee.Date(end_date_str)) \
1089
+ # .filterBounds(roi)
1090
+
1091
+ # # Aggregate data based on the selected period
1092
+ # if aggregation_period.lower() == 'daily':
1093
+ # collection = aggregate_data_daily(collection)
1094
+ # elif aggregation_period.lower() == 'weekly':
1095
+ # collection = aggregate_data_weekly(collection)
1096
+ # elif aggregation_period.lower() == 'monthly':
1097
+ # collection = aggregate_data_monthly(collection, start_date_str, end_date_str)
1098
+ # elif aggregation_period.lower() == 'yearly':
1099
+ # collection = aggregate_data_yearly(collection)
1100
+
1101
+ # # Process each image in the collection
1102
+ # image_list = collection.toList(collection.size())
1103
+ # all_values = [] # To store calculated values for mean calculation
1104
+ # start_date = None
1105
+ # end_date = None
1106
+
1107
+ # for i in range(image_list.size().getInfo()):
1108
+ # image = ee.Image(image_list.get(i))
1109
+
1110
+ # # Extract the timestamp based on the aggregation period
1111
+ # if aggregation_period.lower() == 'daily':
1112
+ # timestamp = image.get('day')
1113
+ # elif aggregation_period.lower() == 'weekly':
1114
+ # timestamp = image.get('week_start') # Use week start date
1115
+ # elif aggregation_period.lower() == 'monthly':
1116
+ # timestamp = image.get('month')
1117
+ # elif aggregation_period.lower() == 'yearly':
1118
+ # timestamp = image.get('year')
1119
+
1120
+ # # Format the timestamp as a valid date string
1121
+ # date = ee.Date(timestamp).format('YYYY-MM-dd').getInfo()
1122
+
1123
+ # # Update start_date and end_date
1124
+ # if start_date is None or date < start_date:
1125
+ # start_date = date
1126
+ # if end_date is None or date > end_date:
1127
+ # end_date = date
1128
+
1129
+ # # Calculate the index for each period
1130
+ # index_image = calculate_index_for_period(image, roi, index_choice, reducer_choice, custom_formula)
1131
+
1132
+ # try:
1133
+ # index_value = index_image.reduceRegion(
1134
+ # reducer=get_reducer(reducer_choice),
1135
+ # geometry=roi,
1136
+ # scale=30
1137
+ # ).get(index_image.bandNames().get(0))
1138
+
1139
+ # calculated_value = index_value.getInfo()
1140
+
1141
+ # # Append valid values for mean calculation
1142
+ # if isinstance(calculated_value, (int, float)):
1143
+ # all_values.append(calculated_value)
1144
+ # except Exception as e:
1145
+ # st.error(f"Error retrieving value for {location_name}: {e}")
1146
+
1147
+ # # Calculate mean value for this location
1148
+ # if all_values:
1149
+ # mean_value = sum(all_values) / len(all_values)
1150
+ # aggregated_results.append({
1151
+ # 'Location Name': location_name,
1152
+ # 'Start Date': start_date,
1153
+ # 'End Date': end_date,
1154
+ # 'Mean Value': mean_value
1155
+
1156
+ # })
1157
+
1158
+ # # Update progress bar
1159
+ # progress_percentage = (idx + 1) / total_steps
1160
+ # progress_bar.progress(progress_percentage)
1161
+ # progress_text.markdown(f"Processing: {int(progress_percentage * 100)}%")
1162
 
1163
+ # elif shape_type.lower() == "polygon":
1164
+ # for idx, row in locations_df.iterrows():
1165
+ # polygon_name = row.get('name', f"Polygon_{idx}")
1166
+ # polygon_geometry = row.get('geometry')
1167
+ # location_name = polygon_name
1168
+
1169
+ # try:
1170
+ # roi = convert_to_ee_geometry(polygon_geometry)
1171
+ # except ValueError as e:
1172
+ # st.warning(f"Skipping invalid polygon {polygon_name}: {e}")
1173
+ # continue
1174
+
1175
+ # collection = ee.ImageCollection(dataset_id) \
1176
+ # .filterDate(ee.Date(start_date_str), ee.Date(end_date_str)) \
1177
+ # .filterBounds(roi)
1178
+
1179
+ # # Aggregate data based on the selected period
1180
+ # if aggregation_period.lower() == 'daily':
1181
+ # collection = aggregate_data_daily(collection)
1182
+ # elif aggregation_period.lower() == 'weekly':
1183
+ # collection = aggregate_data_weekly(collection)
1184
+ # elif aggregation_period.lower() == 'monthly':
1185
+ # collection = aggregate_data_monthly(collection, start_date_str, end_date_str)
1186
+ # elif aggregation_period.lower() == 'yearly':
1187
+ # collection = aggregate_data_yearly(collection)
1188
+
1189
+ # # Process each image in the collection
1190
+ # image_list = collection.toList(collection.size())
1191
+ # all_values = [] # To store calculated values for mean calculation
1192
+ # start_date = None
1193
+ # end_date = None
1194
+
1195
+ # for i in range(image_list.size().getInfo()):
1196
+ # image = ee.Image(image_list.get(i))
1197
+
1198
+ # # Extract the timestamp based on the aggregation period
1199
+ # if aggregation_period.lower() == 'daily':
1200
+ # timestamp = image.get('day')
1201
+ # elif aggregation_period.lower() == 'weekly':
1202
+ # timestamp = image.get('week_start') # Use week start date
1203
+ # elif aggregation_period.lower() == 'monthly':
1204
+ # timestamp = image.get('month')
1205
+ # elif aggregation_period.lower() == 'yearly':
1206
+ # timestamp = image.get('year')
1207
+
1208
+ # # Format the timestamp as a valid date string
1209
+ # date = ee.Date(timestamp).format('YYYY-MM-dd').getInfo()
1210
+
1211
+ # # Update start_date and end_date
1212
+ # if start_date is None or date < start_date:
1213
+ # start_date = date
1214
+ # if end_date is None or date > end_date:
1215
+ # end_date = date
1216
+
1217
+ # # Calculate the index for each period
1218
+ # index_image = calculate_index_for_period(image, roi, index_choice, reducer_choice, custom_formula)
1219
+
1220
+ # try:
1221
+ # index_value = index_image.reduceRegion(
1222
+ # reducer=get_reducer(reducer_choice),
1223
+ # geometry=roi,
1224
+ # scale=30
1225
+ # ).get(index_image.bandNames().get(0))
1226
+
1227
+ # calculated_value = index_value.getInfo()
1228
+
1229
+ # # Append valid values for mean calculation
1230
+ # if isinstance(calculated_value, (int, float)):
1231
+ # all_values.append(calculated_value)
1232
+ # except Exception as e:
1233
+ # st.error(f"Error retrieving value for {location_name}: {e}")
1234
+
1235
+ # # Calculate mean value for this location
1236
+ # if all_values:
1237
+ # mean_value = sum(all_values) / len(all_values)
1238
+ # aggregated_results.append({
1239
+ # 'Location Name': location_name,
1240
+ # 'Mean Value': mean_value,
1241
+ # 'Start Date': start_date,
1242
+ # 'End Date': end_date
1243
+ # })
1244
+
1245
+ # # Update progress bar
1246
+ # progress_percentage = (idx + 1) / total_steps
1247
+ # progress_bar.progress(progress_percentage)
1248
+ # progress_text.markdown(f"Processing: {int(progress_percentage * 100)}%")
1249
+
1250
+ # # Convert results to DataFrame and return as dictionary
1251
+ # if aggregated_results:
1252
+ # result_df = pd.DataFrame(aggregated_results)
1253
 
1254
+ # # For point data, include latitude and longitude
1255
+ # if shape_type.lower() == "point":
1256
+ # # Check if 'name' column exists; if not, generate names based on latitude and longitude
1257
+ # if 'name' not in locations_df.columns:
1258
+ # if 'latitude' in locations_df.columns and 'longitude' in locations_df.columns:
1259
+ # locations_df['name'] = locations_df.apply(
1260
+ # lambda row: f"Location_{row.name}_{row['latitude']}_{row['longitude']}", axis=1
1261
+ # )
1262
+ # else:
1263
+ # st.error("Missing 'latitude' or 'longitude' columns in the input data.")
1264
+ # return [] # Exit early if required columns are missing
1265
+
1266
+ # # Ensure 'latitude' and 'longitude' columns exist before merging
1267
+ # if 'latitude' in locations_df.columns and 'longitude' in locations_df.columns:
1268
+ # result_df = result_df.merge(
1269
+ # locations_df[['name', 'latitude', 'longitude']],
1270
+ # left_on='Location Name',
1271
+ # right_on='name',
1272
+ # how='left'
1273
+ # )
1274
+ # result_df.rename(columns={'latitude': 'Latitude', 'longitude': 'Longitude'}, inplace=True)
1275
+ # result_df.drop(columns=['name'], inplace=True)
1276
+ # else:
1277
+ # st.error("Missing 'latitude' or 'longitude' columns in the input data.")
1278
+ # return [] # Exit early if required columns are missing
1279
+
1280
+ # # For polygon data, add default names if 'name' column is missing
1281
+ # elif shape_type.lower() == "polygon":
1282
+ # if 'name' not in locations_df.columns:
1283
+ # locations_df['name'] = locations_df.index.map(lambda idx: f"Polygon_{idx}")
1284
+
1285
+ # # Merge with 'name' column only for polygons
1286
+ # result_df = result_df.merge(
1287
+ # locations_df[['name']],
1288
+ # left_on='Location Name',
1289
+ # right_on='name',
1290
+ # how='left'
1291
+ # )
1292
+ # result_df.drop(columns=['name'], inplace=True)
1293
  ####################################################
1294
 
1295
  # def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id, index_choice, reducer_choice, shape_type, aggregation_period, custom_formula=""):