YashMK89 commited on
Commit
d3059d1
·
verified ·
1 Parent(s): f0b78fb

update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -13
app.py CHANGED
@@ -22,7 +22,7 @@ m = st.markdown(
22
  unsafe_allow_html=True,
23
  )
24
 
25
- # Authenticate and initialize Earth Engine
26
  earthengine_credentials = os.environ.get("EE_Authentication")
27
 
28
  # Initialize Earth Engine with secret credentials
@@ -57,27 +57,46 @@ custom_formula = ""
57
  if index_choice == 'Custom Formula':
58
  custom_formula = st.text_input("Enter Custom Formula (e.g., 'B5 - B4 / B5 + B4')")
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  # Function to read points from CSV with enhanced error handling
61
  def read_csv(file_path):
62
  try:
63
- # Check if file is empty
64
  file_content = file_path.getvalue().decode("utf-8")
65
  if not file_content.strip():
66
  raise ValueError("Uploaded CSV file is empty.")
67
-
68
- # Attempt to read the CSV file with auto-detected delimiter
69
- try:
70
- df = pd.read_csv(file_path)
71
- except pd.errors.ParserError:
72
- st.error("Error parsing the CSV file. Ensure the file is in correct CSV format.")
73
- return None
74
-
75
- # Check if necessary columns ('latitude' and 'longitude') are present
76
  if 'latitude' not in df.columns or 'longitude' not in df.columns:
77
  raise ValueError("CSV file must contain 'latitude' and 'longitude' columns.")
78
-
79
  return df
80
-
81
  except Exception as e:
82
  st.error(f"Error reading CSV file: {e}")
83
  return None
@@ -155,6 +174,9 @@ if file_upload:
155
  st.write("Map of Uploaded Points:")
156
  m.to_streamlit()
157
 
 
 
 
158
  # If there are polygons, display them on the map
159
  if polygons_df is not None:
160
  st.write("Preview of the uploaded polygons data:")
@@ -171,6 +193,9 @@ if file_upload:
171
  st.write("Map of Uploaded Polygons:")
172
  m.to_streamlit()
173
 
 
 
 
174
  # Function to perform index calculations (as before)
175
  def calculate_ndvi(image, geometry):
176
  ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI')
 
22
  unsafe_allow_html=True,
23
  )
24
 
25
+ # Initialize Earth Engine credentials
26
  earthengine_credentials = os.environ.get("EE_Authentication")
27
 
28
  # Initialize Earth Engine with secret credentials
 
57
  if index_choice == 'Custom Formula':
58
  custom_formula = st.text_input("Enter Custom Formula (e.g., 'B5 - B4 / B5 + B4')")
59
 
60
+ # Initialize session state for storing results and map (if not already done)
61
+ if 'results' not in st.session_state:
62
+ st.session_state.results = [] # Initialize results list
63
+ if 'last_params' not in st.session_state:
64
+ st.session_state.last_params = {} # Initialize last_params
65
+ if 'map_data' not in st.session_state:
66
+ st.session_state.map_data = None # Initialize map_data for storing map object
67
+
68
+ # Function to check if parameters have changed
69
+ def parameters_changed():
70
+ return (
71
+ st.session_state.last_params.get('main_selection') != main_selection or
72
+ st.session_state.last_params.get('sub_selection') != sub_selection or
73
+ st.session_state.last_params.get('index_choice') != index_choice or
74
+ st.session_state.last_params.get('start_date_str') != start_date_str or
75
+ st.session_state.last_params.get('end_date_str') != end_date_str
76
+ )
77
+
78
+ # If parameters have changed, reset the results
79
+ if parameters_changed():
80
+ st.session_state.results = [] # Clear the previous results
81
+ # Update the last parameters to the current ones
82
+ st.session_state.last_params = {
83
+ 'main_selection': main_selection,
84
+ 'sub_selection': sub_selection,
85
+ 'index_choice': index_choice,
86
+ 'start_date_str': start_date_str,
87
+ 'end_date_str': end_date_str
88
+ }
89
+
90
  # Function to read points from CSV with enhanced error handling
91
  def read_csv(file_path):
92
  try:
 
93
  file_content = file_path.getvalue().decode("utf-8")
94
  if not file_content.strip():
95
  raise ValueError("Uploaded CSV file is empty.")
96
+ df = pd.read_csv(file_path)
 
 
 
 
 
 
 
 
97
  if 'latitude' not in df.columns or 'longitude' not in df.columns:
98
  raise ValueError("CSV file must contain 'latitude' and 'longitude' columns.")
 
99
  return df
 
100
  except Exception as e:
101
  st.error(f"Error reading CSV file: {e}")
102
  return None
 
174
  st.write("Map of Uploaded Points:")
175
  m.to_streamlit()
176
 
177
+ # Store the map in session_state
178
+ st.session_state.map_data = m
179
+
180
  # If there are polygons, display them on the map
181
  if polygons_df is not None:
182
  st.write("Preview of the uploaded polygons data:")
 
193
  st.write("Map of Uploaded Polygons:")
194
  m.to_streamlit()
195
 
196
+ # Store the map in session_state
197
+ st.session_state.map_data = m
198
+
199
  # Function to perform index calculations (as before)
200
  def calculate_ndvi(image, geometry):
201
  ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI')