Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -409,6 +409,8 @@ elif shape_type.lower() == "polygon":
|
|
409 |
# m.to_streamlit()
|
410 |
# st.session_state.map_data = m
|
411 |
|
|
|
|
|
412 |
# Ask user to upload a file based on shape type
|
413 |
file_upload = st.file_uploader(f"Upload your {shape_type} data (CSV, GeoJSON, KML)", type=["csv", "geojson", "kml"])
|
414 |
|
@@ -428,10 +430,14 @@ if file_upload is not None:
|
|
428 |
points = []
|
429 |
for placemark in root.findall('.//kml:Placemark', ns):
|
430 |
name = placemark.findtext('kml:name', default=f"Point_{len(points)}", namespaces=ns)
|
431 |
-
|
432 |
-
if
|
433 |
-
|
434 |
-
|
|
|
|
|
|
|
|
|
435 |
if not points:
|
436 |
st.error("No valid Point data found in the KML file.")
|
437 |
locations_df = pd.DataFrame()
|
@@ -485,11 +491,14 @@ if file_upload is not None:
|
|
485 |
polygons = []
|
486 |
for placemark in root.findall('.//kml:Placemark', ns):
|
487 |
name = placemark.findtext('kml:name', default=f"Polygon_{len(polygons)}", namespaces=ns)
|
488 |
-
|
489 |
-
if
|
490 |
-
|
491 |
-
|
492 |
-
|
|
|
|
|
|
|
493 |
if not polygons:
|
494 |
st.error("No valid Polygon data found in the KML file.")
|
495 |
locations_df = pd.DataFrame()
|
|
|
409 |
# m.to_streamlit()
|
410 |
# st.session_state.map_data = m
|
411 |
|
412 |
+
# ... (Previous code remains unchanged until the file upload section) ...
|
413 |
+
|
414 |
# Ask user to upload a file based on shape type
|
415 |
file_upload = st.file_uploader(f"Upload your {shape_type} data (CSV, GeoJSON, KML)", type=["csv", "geojson", "kml"])
|
416 |
|
|
|
430 |
points = []
|
431 |
for placemark in root.findall('.//kml:Placemark', ns):
|
432 |
name = placemark.findtext('kml:name', default=f"Point_{len(points)}", namespaces=ns)
|
433 |
+
coords_elem = placemark.find('.//kml:Point/kml:coordinates', ns)
|
434 |
+
if coords_elem is not None:
|
435 |
+
# Split coordinates and handle potential whitespace
|
436 |
+
coords_text = coords_elem.text.strip()
|
437 |
+
coords = [c.strip() for c in coords_text.split(',')]
|
438 |
+
if len(coords) >= 2: # Ensure at least lon, lat
|
439 |
+
lon, lat = float(coords[0]), float(coords[1])
|
440 |
+
points.append({'name': name, 'geometry': f"POINT ({lon} {lat})"})
|
441 |
if not points:
|
442 |
st.error("No valid Point data found in the KML file.")
|
443 |
locations_df = pd.DataFrame()
|
|
|
491 |
polygons = []
|
492 |
for placemark in root.findall('.//kml:Placemark', ns):
|
493 |
name = placemark.findtext('kml:name', default=f"Polygon_{len(polygons)}", namespaces=ns)
|
494 |
+
coords_elem = placemark.find('.//kml:Polygon//kml:coordinates', ns)
|
495 |
+
if coords_elem is not None:
|
496 |
+
# Process coordinates string
|
497 |
+
coords_text = coords_elem.text.strip()
|
498 |
+
coord_pairs = [pair.split(',')[:2] for pair in coords_text.split() if pair] # Take only lon, lat, ignore altitude
|
499 |
+
if len(coord_pairs) >= 4: # Minimum 4 points for a closed polygon
|
500 |
+
coords_str = " ".join([f"{float(lon)} {float(lat)}" for lon, lat in coord_pairs])
|
501 |
+
polygons.append({'name': name, 'geometry': f"POLYGON (({coords_str}))"})
|
502 |
if not polygons:
|
503 |
st.error("No valid Polygon data found in the KML file.")
|
504 |
locations_df = pd.DataFrame()
|