Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -745,86 +745,86 @@ if file_upload is not None:
|
|
745 |
st.write("Map of Uploaded Polygons:")
|
746 |
m.to_streamlit()
|
747 |
|
748 |
-
|
749 |
-
|
750 |
-
|
751 |
-
|
752 |
-
|
753 |
-
|
754 |
-
|
755 |
-
|
756 |
-
|
757 |
-
|
758 |
-
|
759 |
-
|
760 |
-
|
761 |
-
|
762 |
-
|
763 |
-
|
764 |
-
|
765 |
-
|
766 |
-
|
767 |
-
|
768 |
-
|
769 |
-
|
770 |
-
|
771 |
-
|
772 |
-
|
773 |
-
|
774 |
-
|
775 |
-
|
776 |
-
|
777 |
-
|
778 |
-
|
779 |
-
|
780 |
|
781 |
-
#
|
782 |
-
|
783 |
|
784 |
-
#
|
785 |
-
|
786 |
-
|
787 |
-
|
788 |
-
|
789 |
-
|
790 |
-
|
791 |
-
|
792 |
-
|
793 |
-
|
794 |
-
|
795 |
-
|
796 |
-
|
797 |
-
|
798 |
|
799 |
-
|
800 |
|
801 |
-
#
|
802 |
-
|
803 |
-
|
804 |
|
805 |
-
#
|
806 |
-
|
807 |
-
|
808 |
|
809 |
-
#
|
810 |
-
|
811 |
-
|
812 |
-
|
813 |
-
|
814 |
-
|
815 |
-
|
816 |
-
|
817 |
-
|
818 |
-
|
819 |
|
820 |
-
|
821 |
-
|
822 |
-
|
823 |
|
824 |
-
|
825 |
-
|
826 |
-
|
827 |
-
|
828 |
|
829 |
|
830 |
|
@@ -935,141 +935,141 @@ if file_upload is not None:
|
|
935 |
# else:
|
936 |
# st.warning("Please upload a valid file to proceed.")
|
937 |
|
938 |
-
if st.button(f"Calculate {custom_formula}"):
|
939 |
-
|
940 |
-
|
941 |
-
|
942 |
-
|
943 |
-
|
944 |
-
|
945 |
-
|
946 |
-
|
947 |
-
|
948 |
-
|
949 |
-
|
950 |
-
|
951 |
-
|
952 |
-
|
953 |
-
|
954 |
-
|
955 |
-
|
956 |
-
|
957 |
-
|
958 |
-
|
959 |
-
|
960 |
|
961 |
-
|
962 |
-
|
963 |
-
|
964 |
-
|
965 |
-
|
966 |
-
|
967 |
-
|
968 |
-
|
969 |
-
|
970 |
-
|
971 |
-
|
972 |
|
973 |
-
|
974 |
-
|
975 |
-
|
976 |
-
|
977 |
|
978 |
-
|
979 |
-
|
980 |
|
981 |
-
|
982 |
-
|
983 |
|
984 |
-
|
985 |
-
|
986 |
-
|
987 |
-
|
988 |
-
|
989 |
-
|
990 |
-
|
991 |
-
|
992 |
|
993 |
-
|
994 |
|
995 |
-
|
996 |
-
|
997 |
-
|
998 |
-
|
999 |
|
1000 |
-
|
1001 |
-
|
1002 |
-
|
1003 |
|
1004 |
-
|
1005 |
|
1006 |
-
|
1007 |
-
|
1008 |
-
|
1009 |
|
1010 |
-
|
1011 |
-
|
1012 |
-
|
1013 |
-
|
1014 |
-
|
1015 |
-
|
1016 |
-
|
1017 |
-
|
1018 |
-
|
1019 |
-
|
1020 |
-
|
1021 |
-
|
1022 |
|
1023 |
-
|
1024 |
-
|
1025 |
-
|
1026 |
-
|
1027 |
-
|
1028 |
-
|
1029 |
-
|
1030 |
-
|
1031 |
-
|
1032 |
-
|
1033 |
-
|
1034 |
|
1035 |
-
|
1036 |
-
|
1037 |
-
|
1038 |
-
|
1039 |
-
|
1040 |
-
|
1041 |
-
|
1042 |
-
|
1043 |
-
|
1044 |
-
|
1045 |
-
|
1046 |
-
|
1047 |
-
|
1048 |
-
|
1049 |
-
|
1050 |
-
|
1051 |
-
|
1052 |
-
|
1053 |
-
|
1054 |
-
|
1055 |
-
|
1056 |
-
|
1057 |
|
1058 |
-
|
1059 |
-
|
1060 |
-
|
1061 |
-
|
1062 |
-
|
1063 |
-
|
1064 |
-
|
1065 |
-
|
1066 |
-
|
1067 |
|
1068 |
-
|
1069 |
-
|
1070 |
-
|
1071 |
|
1072 |
-
|
1073 |
-
|
1074 |
-
|
1075 |
-
|
|
|
745 |
st.write("Map of Uploaded Polygons:")
|
746 |
m.to_streamlit()
|
747 |
|
748 |
+
if st.button(f"Calculate {custom_formula}"):
|
749 |
+
if not locations_df.empty:
|
750 |
+
with st.spinner("Processing Data..."):
|
751 |
+
try:
|
752 |
+
results, processing_time = process_aggregation(
|
753 |
+
locations_df,
|
754 |
+
start_date_str,
|
755 |
+
end_date_str,
|
756 |
+
dataset_id,
|
757 |
+
selected_bands,
|
758 |
+
reducer_choice,
|
759 |
+
shape_type,
|
760 |
+
aggregation_period,
|
761 |
+
original_lat_col,
|
762 |
+
original_lon_col,
|
763 |
+
custom_formula,
|
764 |
+
kernel_size,
|
765 |
+
include_boundary,
|
766 |
+
cloud_threshold=cloud_threshold
|
767 |
+
)
|
768 |
+
if results:
|
769 |
+
result_df = pd.DataFrame(results)
|
770 |
+
st.write(f"Processed Results Table ({aggregation_period}) for Formula: {custom_formula}")
|
771 |
+
st.dataframe(result_df)
|
772 |
+
filename = f"{main_selection}_{dataset_id}_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}_{aggregation_period.lower()}.csv"
|
773 |
+
st.download_button(
|
774 |
+
label="Download results as CSV",
|
775 |
+
data=result_df.to_csv(index=False).encode('utf-8'),
|
776 |
+
file_name=filename,
|
777 |
+
mime='text/csv'
|
778 |
+
)
|
779 |
+
st.success(f"Processing complete! Total processing time: {processing_time:.2f} seconds.")
|
780 |
|
781 |
+
# Graph Visualization Section
|
782 |
+
st.markdown("<h5>Graph Visualization</h5>", unsafe_allow_html=True)
|
783 |
|
784 |
+
# Dynamically identify the time column
|
785 |
+
if aggregation_period.lower() == 'custom (start date to end date)':
|
786 |
+
x_column = 'Date Range'
|
787 |
+
elif 'Date' in result_df.columns:
|
788 |
+
x_column = 'Date'
|
789 |
+
elif 'Week' in result_df.columns:
|
790 |
+
x_column = 'Week'
|
791 |
+
elif 'Month' in result_df.columns:
|
792 |
+
x_column = 'Month'
|
793 |
+
elif 'Year' in result_df.columns:
|
794 |
+
x_column = 'Year'
|
795 |
+
else:
|
796 |
+
st.warning("No valid time column found for plotting.")
|
797 |
+
st.stop()
|
798 |
|
799 |
+
y_column = 'Calculated Value'
|
800 |
|
801 |
+
# Line Chart
|
802 |
+
st.subheader("Line Chart")
|
803 |
+
st.line_chart(result_df.set_index(x_column)[y_column])
|
804 |
|
805 |
+
# Bar Chart
|
806 |
+
st.subheader("Bar Chart")
|
807 |
+
st.bar_chart(result_df.set_index(x_column)[y_column])
|
808 |
|
809 |
+
# Advanced Plot (Plotly)
|
810 |
+
st.subheader("Advanced Interactive Plot (Plotly)")
|
811 |
+
fig = px.line(
|
812 |
+
result_df,
|
813 |
+
x=x_column,
|
814 |
+
y=y_column,
|
815 |
+
color='Location Name',
|
816 |
+
title=f"{custom_formula} Over Time"
|
817 |
+
)
|
818 |
+
st.plotly_chart(fig)
|
819 |
|
820 |
+
else:
|
821 |
+
st.warning("No results were generated. Check your inputs or formula.")
|
822 |
+
st.info(f"Total processing time: {processing_time:.2f} seconds.")
|
823 |
|
824 |
+
except Exception as e:
|
825 |
+
st.error(f"An error occurred during processing: {str(e)}")
|
826 |
+
else:
|
827 |
+
st.warning("Please upload a valid file to proceed.")
|
828 |
|
829 |
|
830 |
|
|
|
935 |
# else:
|
936 |
# st.warning("Please upload a valid file to proceed.")
|
937 |
|
938 |
+
# if st.button(f"Calculate {custom_formula}"):
|
939 |
+
# if not locations_df.empty:
|
940 |
+
# with st.spinner("Processing Data..."):
|
941 |
+
# try:
|
942 |
+
# results, processing_time = process_aggregation(
|
943 |
+
# locations_df,
|
944 |
+
# start_date_str,
|
945 |
+
# end_date_str,
|
946 |
+
# dataset_id,
|
947 |
+
# selected_bands,
|
948 |
+
# reducer_choice,
|
949 |
+
# shape_type,
|
950 |
+
# aggregation_period,
|
951 |
+
# original_lat_col,
|
952 |
+
# original_lon_col,
|
953 |
+
# custom_formula,
|
954 |
+
# kernel_size,
|
955 |
+
# include_boundary,
|
956 |
+
# cloud_threshold=cloud_threshold
|
957 |
+
# )
|
958 |
+
# if results:
|
959 |
+
# result_df = pd.DataFrame(results)
|
960 |
|
961 |
+
# # Reorder and rename columns
|
962 |
+
# column_mapping = {
|
963 |
+
# 'Location Name': 'Location Name',
|
964 |
+
# 'Start Date': 'Start Date',
|
965 |
+
# 'End Date': 'End Date',
|
966 |
+
# 'Date Range': 'Date Range',
|
967 |
+
# original_lat_col: 'Latitude',
|
968 |
+
# original_lon_col: 'Longitude',
|
969 |
+
# 'Aggregated Value': 'Calculated Value',
|
970 |
+
# 'Calculated Value': 'Calculated Value'
|
971 |
+
# }
|
972 |
|
973 |
+
# # Keep only columns that exist in the results
|
974 |
+
# available_columns = [col for col in column_mapping.keys() if col in result_df.columns]
|
975 |
+
# result_df = result_df[available_columns]
|
976 |
+
# result_df = result_df.rename(columns={k:v for k,v in column_mapping.items() if k in available_columns})
|
977 |
|
978 |
+
# st.write(f"Processed Results Table ({aggregation_period}) for Formula: {custom_formula}")
|
979 |
+
# st.dataframe(result_df)
|
980 |
|
981 |
+
# # Graph Visualization Section
|
982 |
+
# st.markdown("<h5>Graph Visualization</h5>", unsafe_allow_html=True)
|
983 |
|
984 |
+
# # Determine time column based on aggregation period
|
985 |
+
# time_column_map = {
|
986 |
+
# 'custom (start date to end date)': 'Date Range',
|
987 |
+
# 'daily': 'Date',
|
988 |
+
# 'weekly': 'Week',
|
989 |
+
# 'monthly': 'Month',
|
990 |
+
# 'yearly': 'Year'
|
991 |
+
# }
|
992 |
|
993 |
+
# x_column = time_column_map.get(aggregation_period.lower())
|
994 |
|
995 |
+
# if x_column not in result_df.columns:
|
996 |
+
# # Try to find any time-related column
|
997 |
+
# time_columns = ['Date Range', 'Date', 'Week', 'Month', 'Year', 'day', 'month', 'year']
|
998 |
+
# x_column = next((col for col in time_columns if col in result_df.columns), None)
|
999 |
|
1000 |
+
# if x_column is None:
|
1001 |
+
# st.warning("No time column found for plotting. Showing data without time axis.")
|
1002 |
+
# x_column = 'Location Name'
|
1003 |
|
1004 |
+
# value_column = 'Calculated Value'
|
1005 |
|
1006 |
+
# if value_column not in result_df.columns:
|
1007 |
+
# st.error("No calculated values found for plotting.")
|
1008 |
+
# st.stop()
|
1009 |
|
1010 |
+
# # Line Chart
|
1011 |
+
# try:
|
1012 |
+
# st.subheader("Line Chart")
|
1013 |
+
# if x_column == 'Location Name':
|
1014 |
+
# st.line_chart(result_df.set_index(x_column)[value_column])
|
1015 |
+
# else:
|
1016 |
+
# # Convert to datetime for better sorting
|
1017 |
+
# result_df[x_column] = pd.to_datetime(result_df[x_column], errors='ignore')
|
1018 |
+
# result_df = result_df.sort_values(x_column)
|
1019 |
+
# st.line_chart(result_df.set_index(x_column)[value_column])
|
1020 |
+
# except Exception as e:
|
1021 |
+
# st.error(f"Error creating line chart: {str(e)}")
|
1022 |
|
1023 |
+
# # Bar Chart
|
1024 |
+
# try:
|
1025 |
+
# st.subheader("Bar Chart")
|
1026 |
+
# if x_column == 'Location Name':
|
1027 |
+
# st.bar_chart(result_df.set_index(x_column)[value_column])
|
1028 |
+
# else:
|
1029 |
+
# result_df[x_column] = pd.to_datetime(result_df[x_column], errors='ignore')
|
1030 |
+
# result_df = result_df.sort_values(x_column)
|
1031 |
+
# st.bar_chart(result_df.set_index(x_column)[value_column])
|
1032 |
+
# except Exception as e:
|
1033 |
+
# st.error(f"Error creating bar chart: {str(e)}")
|
1034 |
|
1035 |
+
# # Advanced Plot (Plotly)
|
1036 |
+
# try:
|
1037 |
+
# st.subheader("Advanced Interactive Plot (Plotly)")
|
1038 |
+
# if x_column == 'Location Name':
|
1039 |
+
# fig = px.bar(
|
1040 |
+
# result_df,
|
1041 |
+
# x=x_column,
|
1042 |
+
# y=value_column,
|
1043 |
+
# color='Location Name',
|
1044 |
+
# title=f"{custom_formula} by Location"
|
1045 |
+
# )
|
1046 |
+
# else:
|
1047 |
+
# fig = px.line(
|
1048 |
+
# result_df,
|
1049 |
+
# x=x_column,
|
1050 |
+
# y=value_column,
|
1051 |
+
# color='Location Name',
|
1052 |
+
# title=f"{custom_formula} Over Time"
|
1053 |
+
# )
|
1054 |
+
# st.plotly_chart(fig)
|
1055 |
+
# except Exception as e:
|
1056 |
+
# st.error(f"Error creating interactive plot: {str(e)}")
|
1057 |
|
1058 |
+
# # Download button
|
1059 |
+
# filename = f"{main_selection}_{dataset_id}_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}_{aggregation_period.lower()}.csv"
|
1060 |
+
# st.download_button(
|
1061 |
+
# label="Download results as CSV",
|
1062 |
+
# data=result_df.to_csv(index=False).encode('utf-8'),
|
1063 |
+
# file_name=filename,
|
1064 |
+
# mime='text/csv'
|
1065 |
+
# )
|
1066 |
+
# st.success(f"Processing complete! Total processing time: {processing_time:.2f} seconds.")
|
1067 |
|
1068 |
+
# else:
|
1069 |
+
# st.warning("No results were generated. Check your inputs or formula.")
|
1070 |
+
# st.info(f"Total processing time: {processing_time:.2f} seconds.")
|
1071 |
|
1072 |
+
# except Exception as e:
|
1073 |
+
# st.error(f"An error occurred during processing: {str(e)}")
|
1074 |
+
# else:
|
1075 |
+
# st.warning("Please upload a valid file to proceed.")
|