YashMK89 commited on
Commit
0ea6b9c
·
verified ·
1 Parent(s): 4ac4e90

update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -37
app.py CHANGED
@@ -79,6 +79,16 @@ elif index_choice.lower() == 'custom formula':
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 read points from CSV
83
  def read_csv(file_path):
84
  df = pd.read_csv(file_path)
@@ -173,14 +183,6 @@ def calculate_custom_formula(image, geometry, formula):
173
  )
174
  return result.get('Custom Index')
175
 
176
- # Helper function to ensure valid geometry for EE processing
177
- def convert_to_ee_geometry(geometry):
178
- if geometry.is_valid:
179
- geojson = geometry.__geo_interface__ # Convert geometry to GeoJSON format
180
- return ee.Geometry(geojson)
181
- else:
182
- raise ValueError("Invalid geometry: The polygon geometry is not valid.")
183
-
184
  # Process each point or polygon
185
  if file_upload:
186
  locations_df = None # Initialize locations_df to None
@@ -285,35 +287,38 @@ if file_upload:
285
  'Calculated Value': calculated_value
286
  })
287
 
288
- # Process polygons
289
- if polygons_df is not None:
290
- # Display a preview of the polygons data
291
- st.write("Preview of the uploaded polygons data:")
292
- st.dataframe(polygons_df.head())
 
 
 
 
 
 
 
 
 
 
 
 
 
293
 
294
- # Create a LeafMap object to display the polygons
295
- m = leafmap.Map(center=[polygons_df.geometry.centroid.y.mean(), polygons_df.geometry.centroid.x.mean()], zoom=10)
296
-
297
- # Add polygons to the map
298
- for _, row in polygons_df.iterrows():
299
- polygon = row['geometry']
300
- if polygon.is_valid: # Check if the geometry is valid
301
- m.add_gdf(gdf=row, layer_name=row.get('name', 'Unnamed Polygon'))
302
-
303
- # Display map
304
- st.write("Map of Uploaded Polygons:")
305
- m.to_streamlit()
306
 
307
- # Store the map in session_state
308
- st.session_state.map_data = m
309
 
310
- # Process each polygon for index calculation
311
- for idx, row in polygons_df.iterrows():
312
- polygon = row['geometry']
313
- location_name = row.get('name', f"Polygon_{idx}")
314
 
315
- try:
316
- # Convert polygon to Earth Engine geometry
317
  roi = convert_to_ee_geometry(polygon)
318
 
319
  # Load Sentinel-2 image collection
@@ -352,9 +357,7 @@ if polygons_df is not None:
352
  'Location Name': location_name,
353
  'Calculated Value': calculated_value
354
  })
355
- except ValueError as e:
356
- st.error(f"Error processing {location_name}: {str(e)}")
357
-
358
  # After processing, show the results
359
  if st.session_state.results:
360
  # Convert the results to a DataFrame for better visualization
@@ -375,4 +378,4 @@ if st.session_state.results:
375
  data=result_df.to_csv(index=False).encode('utf-8'),
376
  file_name=filename,
377
  mime='text/csv'
378
- )
 
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 convert GeoDataFrame geometry to Earth Engine format
83
+ def convert_to_ee_geometry(geometry):
84
+ """
85
+ Convert a GeoDataFrame geometry (Polygon, MultiPolygon, etc.) to an Earth Engine geometry.
86
+ """
87
+ if geometry.is_valid:
88
+ return ee.Geometry(geometry.__geo_interface__)
89
+ else:
90
+ raise ValueError(f"Invalid geometry: {geometry}")
91
+
92
  # Function to read points from CSV
93
  def read_csv(file_path):
94
  df = pd.read_csv(file_path)
 
183
  )
184
  return result.get('Custom Index')
185
 
 
 
 
 
 
 
 
 
186
  # Process each point or polygon
187
  if file_upload:
188
  locations_df = None # Initialize locations_df to None
 
287
  'Calculated Value': calculated_value
288
  })
289
 
290
+ # Check if polygons_df is populated for polygons
291
+ if polygons_df is not None:
292
+ # Display a preview of the polygons data
293
+ st.write("Preview of the uploaded polygons data:")
294
+ st.dataframe(polygons_df.head())
295
+
296
+ # Create a LeafMap object to display the polygons
297
+ m = leafmap.Map(center=[polygons_df.geometry.centroid.y.mean(), polygons_df.geometry.centroid.x.mean()], zoom=10)
298
+
299
+ # Add polygons to the map
300
+ for _, row in polygons_df.iterrows():
301
+ polygon = row['geometry']
302
+ if polygon.is_valid: # Check if the geometry is valid
303
+ # Convert geometry to Earth Engine format
304
+ roi = convert_to_ee_geometry(polygon)
305
+
306
+ # Add the valid GeoDataFrame to the map
307
+ m.add_geojson(roi, layer_name=row.get('name', 'Unnamed Polygon'))
308
 
309
+ # Display map
310
+ st.write("Map of Uploaded Polygons:")
311
+ m.to_streamlit()
 
 
 
 
 
 
 
 
 
312
 
313
+ # Store the map in session_state
314
+ st.session_state.map_data = m
315
 
316
+ # Process each polygon for index calculation
317
+ for idx, row in polygons_df.iterrows():
318
+ polygon = row['geometry']
319
+ location_name = row.get('name', f"Polygon_{idx}")
320
 
321
+ # Define the region of interest (ROI)
 
322
  roi = convert_to_ee_geometry(polygon)
323
 
324
  # Load Sentinel-2 image collection
 
357
  'Location Name': location_name,
358
  'Calculated Value': calculated_value
359
  })
360
+
 
 
361
  # After processing, show the results
362
  if st.session_state.results:
363
  # Convert the results to a DataFrame for better visualization
 
378
  data=result_df.to_csv(index=False).encode('utf-8'),
379
  file_name=filename,
380
  mime='text/csv'
381
+ )