YashMK89 commited on
Commit
6a0c780
·
verified ·
1 Parent(s): 8bb4ece

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -15
app.py CHANGED
@@ -300,37 +300,46 @@ reducer_choice = st.selectbox(
300
 
301
  # Function to convert geometry to Earth Engine format
302
  def convert_to_ee_geometry(geometry):
 
303
  if isinstance(geometry, base.BaseGeometry):
304
  if geometry.is_valid:
305
  geojson = geometry.__geo_interface__
 
306
  return ee.Geometry(geojson)
307
  else:
308
  raise ValueError("Invalid geometry: The polygon geometry is not valid.")
309
- elif isinstance(geometry, dict) or isinstance(geometry, str):
 
 
 
 
 
310
  try:
311
- if isinstance(geometry, str) and geometry.lower().endswith(".kml"):
312
- # Handle KML string directly with xml.etree.ElementTree
 
 
 
 
 
 
 
313
  root = XET.fromstring(geometry)
314
  ns = {'kml': 'http://www.opengis.net/kml/2.2'}
315
  coords_elem = root.find('.//kml:Polygon//kml:coordinates', ns)
316
  if coords_elem is not None:
317
  coords_text = ' '.join(coords_elem.text.split())
 
318
  coords = [tuple(map(float, coord.split(','))) for coord in coords_text.split()]
319
  geojson = {"type": "Polygon", "coordinates": [coords]}
320
  return ee.Geometry(geojson)
321
  else:
322
- raise ValueError("KML does not contain valid coordinates.")
323
- elif isinstance(geometry, str):
324
- geometry = json.loads(geometry)
325
- if 'type' in geometry and 'coordinates' in geometry:
326
- return ee.Geometry(geometry)
327
- else:
328
- raise ValueError("GeoJSON format is invalid.")
329
- except Exception as e:
330
- raise ValueError(f"Error parsing geometry: {e}")
331
  else:
332
- raise ValueError("Unsupported geometry input type. Supported types are Shapely, GeoJSON, and KML.")
333
-
334
  # Date Input for Start and End Dates
335
  start_date = st.date_input("Start Date", value=pd.to_datetime('2024-11-01'))
336
  end_date = st.date_input("End Date", value=pd.to_datetime('2024-12-01'))
@@ -521,7 +530,6 @@ if file_upload is not None:
521
  # Parse KML file for polygon data
522
  kml_string = file_upload.read().decode('utf-8')
523
  try:
524
- # Use xml.etree.ElementTree with unique alias
525
  root = XET.fromstring(kml_string)
526
  ns = {'kml': 'http://www.opengis.net/kml/2.2'}
527
  polygons = []
 
300
 
301
  # Function to convert geometry to Earth Engine format
302
  def convert_to_ee_geometry(geometry):
303
+ st.write(f"Debug: convert_to_ee_geometry called with type - {type(geometry)}") # Debug input type
304
  if isinstance(geometry, base.BaseGeometry):
305
  if geometry.is_valid:
306
  geojson = geometry.__geo_interface__
307
+ st.write(f"Debug: Converting Shapely geometry to GeoJSON - {geojson}") # Debug GeoJSON
308
  return ee.Geometry(geojson)
309
  else:
310
  raise ValueError("Invalid geometry: The polygon geometry is not valid.")
311
+ elif isinstance(geometry, dict):
312
+ if 'type' in geometry and 'coordinates' in geometry:
313
+ return ee.Geometry(geometry)
314
+ else:
315
+ raise ValueError("GeoJSON format is invalid.")
316
+ elif isinstance(geometry, str):
317
  try:
318
+ # If it’s a JSON string, parse it
319
+ parsed = json.loads(geometry)
320
+ if 'type' in parsed and 'coordinates' in parsed:
321
+ return ee.Geometry(parsed)
322
+ else:
323
+ raise ValueError("GeoJSON string format is invalid.")
324
+ except json.JSONDecodeError:
325
+ # If it’s a KML string (not a file path)
326
+ try:
327
  root = XET.fromstring(geometry)
328
  ns = {'kml': 'http://www.opengis.net/kml/2.2'}
329
  coords_elem = root.find('.//kml:Polygon//kml:coordinates', ns)
330
  if coords_elem is not None:
331
  coords_text = ' '.join(coords_elem.text.split())
332
+ st.write(f"Debug: KML string coordinates - {coords_text}") # Debug KML parsing
333
  coords = [tuple(map(float, coord.split(','))) for coord in coords_text.split()]
334
  geojson = {"type": "Polygon", "coordinates": [coords]}
335
  return ee.Geometry(geojson)
336
  else:
337
+ raise ValueError("KML string does not contain valid coordinates.")
338
+ except Exception as e:
339
+ raise ValueError(f"Error parsing KML string: {e}")
 
 
 
 
 
 
340
  else:
341
+ raise ValueError(f"Unsupported geometry input type: {type(geometry)}. Supported types are Shapely, GeoJSON, and KML string.")
342
+
343
  # Date Input for Start and End Dates
344
  start_date = st.date_input("Start Date", value=pd.to_datetime('2024-11-01'))
345
  end_date = st.date_input("End Date", value=pd.to_datetime('2024-12-01'))
 
530
  # Parse KML file for polygon data
531
  kml_string = file_upload.read().decode('utf-8')
532
  try:
 
533
  root = XET.fromstring(kml_string)
534
  ns = {'kml': 'http://www.opengis.net/kml/2.2'}
535
  polygons = []