khushidhar1210 commited on
Commit
8a43c34
·
verified ·
1 Parent(s): 1aacf9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -12,9 +12,9 @@ import streamlit as st
12
 
13
  # Function to parse KML
14
  def parse_kml(kml_file):
15
- with open(kml_file, 'rt') as f:
16
- doc = f.read()
17
- k = ET.ElementTree(ET.fromstring(doc))
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
- return parse_kml(os.path.join('temp_kml', kml_file))
 
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(uploaded_file)
53
  else:
54
- kml_shapes = parse_kml(uploaded_file)
 
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("K_FLD_HAZ_AR.shp")
66
- shapefile_2 = gpd.read_file("N_FLD_HAZ_AR.shp")
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))