Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,9 +12,9 @@ import streamlit as st
|
|
| 12 |
|
| 13 |
# Function to parse KML
|
| 14 |
def parse_kml(kml_file):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
k = ET.ElementTree(ET.fromstring(
|
| 18 |
root = k.getroot()
|
| 19 |
ns = {'kml': 'http://www.opengis.net/kml/2.2'}
|
| 20 |
|
|
@@ -44,14 +44,20 @@ def extract_kmz(kmz_file):
|
|
| 44 |
with zipfile.ZipFile(kmz_file, 'r') as zip_ref:
|
| 45 |
zip_ref.extractall('temp_kml')
|
| 46 |
kml_file = [f for f in os.listdir('temp_kml') if f.endswith('.kml')][0]
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
# Function to handle KML/KMZ file input
|
| 50 |
def handle_kml_upload(uploaded_file):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
if uploaded_file.name.endswith('.kmz'):
|
| 52 |
-
kml_shapes = extract_kmz(
|
| 53 |
else:
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
return kml_shapes
|
| 57 |
|
|
@@ -62,9 +68,9 @@ uploaded_file = st.file_uploader("Upload KML/KMZ file", type=['kml', 'kmz'])
|
|
| 62 |
|
| 63 |
if uploaded_file is not None:
|
| 64 |
# Step 1: Read in the shapefiles
|
| 65 |
-
shapefile_1 = gpd.read_file("
|
| 66 |
-
shapefile_2 = gpd.read_file("
|
| 67 |
-
shapefile_3 = gpd.read_file("S_FLD_HAZ_AR.shp")
|
| 68 |
|
| 69 |
# Step 2: Concatenate them into a single GeoDataFrame
|
| 70 |
merged_gdf = gpd.GeoDataFrame(pd.concat([shapefile_1, shapefile_2, shapefile_3], ignore_index=True))
|
|
|
|
| 12 |
|
| 13 |
# Function to parse KML
|
| 14 |
def parse_kml(kml_file):
|
| 15 |
+
# kml_file will be in bytes format, so we decode it to a string
|
| 16 |
+
kml_content = kml_file.decode('utf-8') # Decode bytes to string
|
| 17 |
+
k = ET.ElementTree(ET.fromstring(kml_content))
|
| 18 |
root = k.getroot()
|
| 19 |
ns = {'kml': 'http://www.opengis.net/kml/2.2'}
|
| 20 |
|
|
|
|
| 44 |
with zipfile.ZipFile(kmz_file, 'r') as zip_ref:
|
| 45 |
zip_ref.extractall('temp_kml')
|
| 46 |
kml_file = [f for f in os.listdir('temp_kml') if f.endswith('.kml')][0]
|
| 47 |
+
with open(os.path.join('temp_kml', kml_file), 'rb') as f:
|
| 48 |
+
return parse_kml(f.read())
|
| 49 |
|
| 50 |
# Function to handle KML/KMZ file input
|
| 51 |
def handle_kml_upload(uploaded_file):
|
| 52 |
+
# Read the uploaded file as bytes
|
| 53 |
+
file_bytes = uploaded_file.read()
|
| 54 |
+
|
| 55 |
+
# If the file is a KMZ, first extract the KML file from the KMZ archive
|
| 56 |
if uploaded_file.name.endswith('.kmz'):
|
| 57 |
+
kml_shapes = extract_kmz(file_bytes)
|
| 58 |
else:
|
| 59 |
+
# Otherwise, it's a direct KML file
|
| 60 |
+
kml_shapes = parse_kml(file_bytes)
|
| 61 |
|
| 62 |
return kml_shapes
|
| 63 |
|
|
|
|
| 68 |
|
| 69 |
if uploaded_file is not None:
|
| 70 |
# Step 1: Read in the shapefiles
|
| 71 |
+
shapefile_1 = gpd.read_file("Kent/S_FLD_HAZ_AR.shp")
|
| 72 |
+
shapefile_2 = gpd.read_file("NC/S_FLD_HAZ_AR.shp")
|
| 73 |
+
shapefile_3 = gpd.read_file("Sus/S_FLD_HAZ_AR.shp")
|
| 74 |
|
| 75 |
# Step 2: Concatenate them into a single GeoDataFrame
|
| 76 |
merged_gdf = gpd.GeoDataFrame(pd.concat([shapefile_1, shapefile_2, shapefile_3], ignore_index=True))
|