YashMK89 commited on
Commit
7daa312
·
verified ·
1 Parent(s): 1ceff46

update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -9
app.py CHANGED
@@ -757,13 +757,114 @@ def calculate_index_for_period(image, roi, index_choice, reducer_choice, custom_
757
  # st.warning("Please upload a file.")
758
 
759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
760
  def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id, index_choice, reducer_choice, shape_type, aggregation_period, custom_formula=""):
761
  aggregated_results = []
762
 
763
  # Check if the index_choice is 'custom formula' and the custom formula is empty
764
  if index_choice.lower() == 'custom_formula' and not custom_formula:
765
  st.error("Custom formula cannot be empty. Please provide a formula.")
766
- return aggregated_results # Return early to avoid further processing
767
 
768
  # Initialize progress bar
769
  total_steps = len(locations_df)
@@ -773,7 +874,6 @@ def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id,
773
  with st.spinner('Processing data...'):
774
  if shape_type.lower() == "point":
775
  for idx, row in locations_df.iterrows():
776
- # Check if the latitude and longitude columns exist and have values
777
  latitude = row.get('latitude')
778
  longitude = row.get('longitude')
779
  if pd.isna(latitude) or pd.isna(longitude):
@@ -818,13 +918,13 @@ def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id,
818
 
819
  # Format the timestamp based on aggregation period
820
  if aggregation_period.lower() == 'monthly':
821
- date = ee.Date(timestamp).format('YYYY-MM').getInfo() # Month-wise format
822
  elif aggregation_period.lower() == 'weekly':
823
- date = ee.Date(timestamp).format('YYYY-MM-dd').getInfo() # Week-wise format (week start)
824
  elif aggregation_period.lower() == 'yearly':
825
- date = ee.Date(timestamp).format('YYYY').getInfo() # Year-wise format
826
  else: # Daily
827
- date = ee.Date(timestamp).format('YYYY-MM-dd').getInfo() # Daily format
828
 
829
  # Calculate the index for each period
830
  index_image = calculate_index_for_period(image, roi, index_choice, reducer_choice, custom_formula)
@@ -838,13 +938,12 @@ def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id,
838
 
839
  calculated_value = index_value.getInfo()
840
 
841
- # Append the results if valid
842
  if isinstance(calculated_value, (int, float)):
843
  aggregated_results.append({
844
  'Location Name': location_name,
845
  'Latitude': latitude,
846
  'Longitude': longitude,
847
- period_label: date, # Use dynamic period label (Date, Week, Month, Year)
848
  'Start Date': start_date_str,
849
  'End Date': end_date_str,
850
  'Calculated Value': calculated_value
@@ -854,7 +953,6 @@ def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id,
854
  except Exception as e:
855
  st.error(f"Error retrieving value for {location_name}: {e}")
856
 
857
- # Update progress bar
858
  progress_percentage = (idx + 1) / total_steps
859
  progress_bar.progress(progress_percentage)
860
  progress_text.markdown(f"Processing: {int(progress_percentage * 100)}%")
 
757
  # st.warning("Please upload a file.")
758
 
759
 
760
+ # def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id, index_choice, reducer_choice, shape_type, aggregation_period, custom_formula=""):
761
+ # aggregated_results = []
762
+
763
+ # # Check if the index_choice is 'custom formula' and the custom formula is empty
764
+ # if index_choice.lower() == 'custom_formula' and not custom_formula:
765
+ # st.error("Custom formula cannot be empty. Please provide a formula.")
766
+ # return aggregated_results # Return early to avoid further processing
767
+
768
+ # # Initialize progress bar
769
+ # total_steps = len(locations_df)
770
+ # progress_bar = st.progress(0)
771
+ # progress_text = st.empty()
772
+
773
+ # with st.spinner('Processing data...'):
774
+ # if shape_type.lower() == "point":
775
+ # for idx, row in locations_df.iterrows():
776
+ # # Check if the latitude and longitude columns exist and have values
777
+ # latitude = row.get('latitude')
778
+ # longitude = row.get('longitude')
779
+ # if pd.isna(latitude) or pd.isna(longitude):
780
+ # st.warning(f"Skipping location {idx} with missing latitude or longitude")
781
+ # continue
782
+
783
+ # location_name = row.get('name', f"Location_{idx}")
784
+ # roi = ee.Geometry.Point([longitude, latitude])
785
+
786
+ # collection = ee.ImageCollection(dataset_id) \
787
+ # .filterDate(ee.Date(start_date_str), ee.Date(end_date_str)) \
788
+ # .filterBounds(roi)
789
+
790
+ # # Aggregate data based on the selected period
791
+ # if aggregation_period.lower() == 'daily':
792
+ # collection = aggregate_data_daily(collection)
793
+ # elif aggregation_period.lower() == 'weekly':
794
+ # collection = aggregate_data_weekly(collection)
795
+ # elif aggregation_period.lower() == 'monthly':
796
+ # collection = aggregate_data_monthly(collection, start_date_str, end_date_str)
797
+ # elif aggregation_period.lower() == 'yearly':
798
+ # collection = aggregate_data_yearly(collection)
799
+
800
+ # # Process each image in the collection
801
+ # image_list = collection.toList(collection.size())
802
+ # for i in range(image_list.size().getInfo()):
803
+ # image = ee.Image(image_list.get(i))
804
+
805
+ # # Define timestamp and period label based on aggregation period
806
+ # if aggregation_period.lower() == 'daily':
807
+ # timestamp = image.get('day')
808
+ # period_label = 'Date'
809
+ # elif aggregation_period.lower() == 'weekly':
810
+ # timestamp = image.get('week_start')
811
+ # period_label = 'Week'
812
+ # elif aggregation_period.lower() == 'monthly':
813
+ # timestamp = image.get('month')
814
+ # period_label = 'Month'
815
+ # elif aggregation_period.lower() == 'yearly':
816
+ # timestamp = image.get('year')
817
+ # period_label = 'Year'
818
+
819
+ # # Format the timestamp based on aggregation period
820
+ # if aggregation_period.lower() == 'monthly':
821
+ # date = ee.Date(timestamp).format('YYYY-MM').getInfo() # Month-wise format
822
+ # elif aggregation_period.lower() == 'weekly':
823
+ # date = ee.Date(timestamp).format('YYYY-MM-dd').getInfo() # Week-wise format (week start)
824
+ # elif aggregation_period.lower() == 'yearly':
825
+ # date = ee.Date(timestamp).format('YYYY').getInfo() # Year-wise format
826
+ # else: # Daily
827
+ # date = ee.Date(timestamp).format('YYYY-MM-dd').getInfo() # Daily format
828
+
829
+ # # Calculate the index for each period
830
+ # index_image = calculate_index_for_period(image, roi, index_choice, reducer_choice, custom_formula)
831
+
832
+ # try:
833
+ # index_value = index_image.reduceRegion(
834
+ # reducer=get_reducer(reducer_choice),
835
+ # geometry=roi,
836
+ # scale=30
837
+ # ).get(index_image.bandNames().get(0))
838
+
839
+ # calculated_value = index_value.getInfo()
840
+
841
+ # # Append the results if valid
842
+ # if isinstance(calculated_value, (int, float)):
843
+ # aggregated_results.append({
844
+ # 'Location Name': location_name,
845
+ # 'Latitude': latitude,
846
+ # 'Longitude': longitude,
847
+ # period_label: date, # Use dynamic period label (Date, Week, Month, Year)
848
+ # 'Start Date': start_date_str,
849
+ # 'End Date': end_date_str,
850
+ # 'Calculated Value': calculated_value
851
+ # })
852
+ # else:
853
+ # st.warning(f"Skipping invalid value for {location_name} on {date}")
854
+ # except Exception as e:
855
+ # st.error(f"Error retrieving value for {location_name}: {e}")
856
+
857
+ # # Update progress bar
858
+ # progress_percentage = (idx + 1) / total_steps
859
+ # progress_bar.progress(progress_percentage)
860
+ # progress_text.markdown(f"Processing: {int(progress_percentage * 100)}%")
861
  def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id, index_choice, reducer_choice, shape_type, aggregation_period, custom_formula=""):
862
  aggregated_results = []
863
 
864
  # Check if the index_choice is 'custom formula' and the custom formula is empty
865
  if index_choice.lower() == 'custom_formula' and not custom_formula:
866
  st.error("Custom formula cannot be empty. Please provide a formula.")
867
+ return aggregated_results
868
 
869
  # Initialize progress bar
870
  total_steps = len(locations_df)
 
874
  with st.spinner('Processing data...'):
875
  if shape_type.lower() == "point":
876
  for idx, row in locations_df.iterrows():
 
877
  latitude = row.get('latitude')
878
  longitude = row.get('longitude')
879
  if pd.isna(latitude) or pd.isna(longitude):
 
918
 
919
  # Format the timestamp based on aggregation period
920
  if aggregation_period.lower() == 'monthly':
921
+ date = ee.Date(timestamp).format('YYYY-MM').getInfo()
922
  elif aggregation_period.lower() == 'weekly':
923
+ date = ee.Date(timestamp).format('YYYY-MM-dd').getInfo()
924
  elif aggregation_period.lower() == 'yearly':
925
+ date = ee.Date(timestamp).format('YYYY').getInfo()
926
  else: # Daily
927
+ date = ee.Date(timestamp).format('YYYY-MM-dd').getInfo()
928
 
929
  # Calculate the index for each period
930
  index_image = calculate_index_for_period(image, roi, index_choice, reducer_choice, custom_formula)
 
938
 
939
  calculated_value = index_value.getInfo()
940
 
 
941
  if isinstance(calculated_value, (int, float)):
942
  aggregated_results.append({
943
  'Location Name': location_name,
944
  'Latitude': latitude,
945
  'Longitude': longitude,
946
+ period_label: date,
947
  'Start Date': start_date_str,
948
  'End Date': end_date_str,
949
  'Calculated Value': calculated_value
 
953
  except Exception as e:
954
  st.error(f"Error retrieving value for {location_name}: {e}")
955
 
 
956
  progress_percentage = (idx + 1) / total_steps
957
  progress_bar.progress(progress_percentage)
958
  progress_text.markdown(f"Processing: {int(progress_percentage * 100)}%")