Spaces:
Running
Running
castledan
commited on
Commit
Β·
42e7604
1
Parent(s):
62b4495
erase useless app function
Browse files
app/pages/1_π_Flood_extent_analysis.py
CHANGED
@@ -39,264 +39,260 @@ set_tool_page_style()
|
|
39 |
ee_initialize()
|
40 |
|
41 |
|
42 |
-
#
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
# output map should be visualised or not
|
47 |
-
if "output_created" not in st.session_state:
|
48 |
-
st.session_state.output_created = False
|
49 |
|
50 |
-
# Function to be used to hide bottom panel (when setting parameters for a
|
51 |
-
# new analysis)
|
52 |
-
def callback():
|
53 |
-
st.session_state.output_created = False
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
"polygon": True,
|
77 |
-
"circle": False,
|
78 |
-
"marker": False,
|
79 |
-
"circlemarker": False,
|
80 |
-
},
|
81 |
-
).add_to(Map)
|
82 |
-
# Add search bar with geocoder to map
|
83 |
-
Geocoder(add_marker=False).add_to(Map)
|
84 |
-
# Add minimap to map
|
85 |
-
MiniMap().add_to(Map)
|
86 |
-
# Export map to Streamlit
|
87 |
-
output = st_folium(Map, width=800, height=600)
|
88 |
-
with col2:
|
89 |
-
# Add collapsable container for image dates
|
90 |
-
with st.expander("Choose Image Dates"):
|
91 |
-
# Callback is added, so that, every time a parameters is changed,
|
92 |
-
# the bottom panel containing the output map is hidden
|
93 |
-
before_start = st.date_input(
|
94 |
-
"Start date for reference imagery",
|
95 |
-
value=dt.date(year=2022, month=7, day=1),
|
96 |
-
help="It needs to be prior to the flooding event",
|
97 |
-
on_change=callback,
|
98 |
-
)
|
99 |
-
before_end = st.date_input(
|
100 |
-
"End date for reference imagery",
|
101 |
-
value=dt.date(year=2022, month=7, day=30),
|
102 |
-
help=(
|
103 |
-
"It needs to be prior to the flooding event, at least 15 "
|
104 |
-
"days subsequent to the date selected above"
|
105 |
-
),
|
106 |
-
on_change=callback,
|
107 |
-
)
|
108 |
-
after_start = st.date_input(
|
109 |
-
"Start date for flooding imagery",
|
110 |
-
value=dt.date(year=2022, month=9, day=1),
|
111 |
-
help="It needs to be subsequent to the flooding event",
|
112 |
-
on_change=callback,
|
113 |
-
)
|
114 |
-
after_end = st.date_input(
|
115 |
-
"End date for flooding imagery",
|
116 |
-
value=dt.date(year=2022, month=9, day=16),
|
117 |
-
help=(
|
118 |
-
"It needs to be subsequent to the flooding event and at "
|
119 |
-
"least 10 days to the date selected above"
|
120 |
-
),
|
121 |
-
on_change=callback,
|
122 |
-
)
|
123 |
-
# Add collapsable container for parameters
|
124 |
-
with st.expander("Choose Parameters"):
|
125 |
-
# Add slider for threshold
|
126 |
-
add_slider = st.slider(
|
127 |
-
label="Select a threshold",
|
128 |
-
min_value=0.0,
|
129 |
-
max_value=5.0,
|
130 |
-
value=1.25,
|
131 |
-
step=0.25,
|
132 |
-
help="Higher values might reduce overall noise",
|
133 |
-
on_change=callback,
|
134 |
-
)
|
135 |
-
# Add radio buttons for pass direction
|
136 |
-
pass_direction = st.radio(
|
137 |
-
"Set pass direction",
|
138 |
-
["Ascending", "Descending"],
|
139 |
-
on_change=callback,
|
140 |
-
)
|
141 |
-
# Button for computation
|
142 |
-
submitted = st.button("Compute flood extent")
|
143 |
-
# Introduce date validation
|
144 |
-
check_dates = before_start < before_end <= after_start < after_end
|
145 |
-
# Introduce drawing validation (a polygon needs to exist)
|
146 |
-
check_drawing = (
|
147 |
-
output["all_drawings"] != [] and output["all_drawings"] is not None
|
148 |
)
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
)
|
184 |
-
|
185 |
-
|
186 |
-
# basemap="HYBRID",
|
187 |
-
plugin_Draw=False,
|
188 |
-
Draw_export=False,
|
189 |
-
locate_control=False,
|
190 |
-
plugin_LatLngPopup=False,
|
191 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
try:
|
193 |
-
#
|
194 |
-
|
195 |
-
|
196 |
-
|
|
|
|
|
|
|
|
|
197 |
)
|
198 |
-
|
199 |
-
Map2.centerObject(detected_flood_raster)
|
200 |
-
except ee.EEException:
|
201 |
-
# If error contains the sentence below, it means that
|
202 |
-
# an image could not be properly generated
|
203 |
st.error(
|
204 |
"""
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
dates: it is likely that the satellite did not
|
210 |
-
cover the area of interest in the range of
|
211 |
-
dates specified (either before or after the
|
212 |
-
flooding event).
|
213 |
"""
|
214 |
)
|
215 |
else:
|
216 |
-
|
217 |
-
#
|
218 |
-
st.
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
st.session_state.detected_flood_vector = (
|
225 |
-
detected_flood_vector
|
226 |
)
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
of interest (side <~ 150km) and repeat the
|
257 |
-
analysis.
|
258 |
-
"""
|
259 |
-
)
|
260 |
-
else:
|
261 |
-
response_r = requests.get(url_r)
|
262 |
-
# Get download url for raster data
|
263 |
-
vector = st.session_state.detected_flood_vector
|
264 |
-
url_v = vector.getDownloadUrl("GEOJSON")
|
265 |
-
response_v = requests.get(url_v)
|
266 |
-
filename = "flood_extent"
|
267 |
-
timestamp = dt.datetime.now().strftime(
|
268 |
-
"%Y-%m-%d_%H-%M"
|
269 |
-
)
|
270 |
-
with row2:
|
271 |
-
# Create download buttons for raster and vector
|
272 |
-
# data
|
273 |
-
with open("flood_extent.tif", "wb"):
|
274 |
-
ste.download_button(
|
275 |
-
label="Download Raster Extent",
|
276 |
-
data=response_r.content,
|
277 |
-
file_name=(
|
278 |
-
f"{filename}"
|
279 |
-
"_raster_"
|
280 |
-
f"{timestamp}"
|
281 |
-
".tif"
|
282 |
-
),
|
283 |
-
mime="image/tif",
|
284 |
-
)
|
285 |
-
with open("flood_extent.geojson", "wb"):
|
286 |
-
ste.download_button(
|
287 |
-
label="Download Vector Extent",
|
288 |
-
data=response_v.content,
|
289 |
-
file_name=(
|
290 |
-
f"{filename}"
|
291 |
-
"_vector_"
|
292 |
-
f"{timestamp}"
|
293 |
-
".geojson"
|
294 |
-
),
|
295 |
-
mime="text/json",
|
296 |
-
)
|
297 |
-
# Output for computation complete
|
298 |
-
st.success("Computation complete")
|
299 |
-
|
300 |
-
|
301 |
-
# Run app
|
302 |
-
app()
|
|
|
39 |
ee_initialize()
|
40 |
|
41 |
|
42 |
+
# Output_created is useful to decide whether the bottom panel with the
|
43 |
+
# output map should be visualised or not
|
44 |
+
if "output_created" not in st.session_state:
|
45 |
+
st.session_state.output_created = False
|
|
|
|
|
|
|
46 |
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
# Function to be used to hide bottom panel (when setting parameters for a
|
49 |
+
# new analysis)
|
50 |
+
def callback():
|
51 |
+
"""Set output created to zero: reset tool."""
|
52 |
+
st.session_state.output_created = False
|
53 |
+
|
54 |
+
|
55 |
+
# Create two rows: top and bottom panel
|
56 |
+
row1 = st.container()
|
57 |
+
row2 = st.container()
|
58 |
+
# Crate two columns in the top panel: input map and paramters
|
59 |
+
col1, col2 = row1.columns([2, 1])
|
60 |
+
with col1:
|
61 |
+
# Add collapsable container for input map
|
62 |
+
with st.expander("Input map", expanded=True):
|
63 |
+
# Create folium map
|
64 |
+
Map = folium.Map(
|
65 |
+
location=[52.205276, 0.119167],
|
66 |
+
zoom_start=3,
|
67 |
+
control_scale=True,
|
68 |
+
# crs='EPSG4326'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
)
|
70 |
+
# Add drawing tools to map
|
71 |
+
Draw(
|
72 |
+
export=False,
|
73 |
+
draw_options={
|
74 |
+
"circle": False,
|
75 |
+
"polyline": False,
|
76 |
+
"polygon": True,
|
77 |
+
"circle": False,
|
78 |
+
"marker": False,
|
79 |
+
"circlemarker": False,
|
80 |
+
},
|
81 |
+
).add_to(Map)
|
82 |
+
# Add search bar with geocoder to map
|
83 |
+
Geocoder(add_marker=False).add_to(Map)
|
84 |
+
# Add minimap to map
|
85 |
+
MiniMap().add_to(Map)
|
86 |
+
# Export map to Streamlit
|
87 |
+
output = st_folium(Map, width=800, height=600)
|
88 |
+
with col2:
|
89 |
+
# Add collapsable container for image dates
|
90 |
+
with st.expander("Choose Image Dates"):
|
91 |
+
# Callback is added, so that, every time a parameters is changed,
|
92 |
+
# the bottom panel containing the output map is hidden
|
93 |
+
before_start = st.date_input(
|
94 |
+
"Start date for reference imagery",
|
95 |
+
value=dt.date(year=2022, month=7, day=1),
|
96 |
+
help="It needs to be prior to the flooding event",
|
97 |
+
on_change=callback,
|
98 |
+
)
|
99 |
+
before_end = st.date_input(
|
100 |
+
"End date for reference imagery",
|
101 |
+
value=dt.date(year=2022, month=7, day=30),
|
102 |
+
help=(
|
103 |
+
"It needs to be prior to the flooding event, at least 15 "
|
104 |
+
"days subsequent to the date selected above"
|
105 |
+
),
|
106 |
+
on_change=callback,
|
107 |
+
)
|
108 |
+
after_start = st.date_input(
|
109 |
+
"Start date for flooding imagery",
|
110 |
+
value=dt.date(year=2022, month=9, day=1),
|
111 |
+
help="It needs to be subsequent to the flooding event",
|
112 |
+
on_change=callback,
|
113 |
+
)
|
114 |
+
after_end = st.date_input(
|
115 |
+
"End date for flooding imagery",
|
116 |
+
value=dt.date(year=2022, month=9, day=16),
|
117 |
+
help=(
|
118 |
+
"It needs to be subsequent to the flooding event and at "
|
119 |
+
"least 10 days to the date selected above"
|
120 |
+
),
|
121 |
+
on_change=callback,
|
122 |
+
)
|
123 |
+
# Add collapsable container for parameters
|
124 |
+
with st.expander("Choose Parameters"):
|
125 |
+
# Add slider for threshold
|
126 |
+
add_slider = st.slider(
|
127 |
+
label="Select a threshold",
|
128 |
+
min_value=0.0,
|
129 |
+
max_value=5.0,
|
130 |
+
value=1.25,
|
131 |
+
step=0.25,
|
132 |
+
help="Higher values might reduce overall noise",
|
133 |
+
on_change=callback,
|
134 |
+
)
|
135 |
+
# Add radio buttons for pass direction
|
136 |
+
pass_direction = st.radio(
|
137 |
+
"Set pass direction",
|
138 |
+
["Ascending", "Descending"],
|
139 |
+
on_change=callback,
|
140 |
+
)
|
141 |
+
# Button for computation
|
142 |
+
submitted = st.button("Compute flood extent")
|
143 |
+
# Introduce date validation
|
144 |
+
check_dates = before_start < before_end <= after_start < after_end
|
145 |
+
# Introduce drawing validation (a polygon needs to exist)
|
146 |
+
check_drawing = (
|
147 |
+
output["all_drawings"] != [] and output["all_drawings"] is not None
|
148 |
+
)
|
149 |
+
# What happens when button is clicked on?
|
150 |
+
if submitted:
|
151 |
+
with col2:
|
152 |
+
# Output error if dates are not valid
|
153 |
+
if not check_dates:
|
154 |
+
st.error("Make sure that the dates were inserted correctly")
|
155 |
+
# Output error if no polygons were drawn
|
156 |
+
elif not check_drawing:
|
157 |
+
st.error("No region selected.")
|
158 |
+
else:
|
159 |
+
# Add output for computation
|
160 |
+
with st.spinner("Computing... Please wait..."):
|
161 |
+
# Extract coordinates from drawn polygon
|
162 |
+
coords = output["all_drawings"][-1]["geometry"]["coordinates"][
|
163 |
+
0
|
164 |
+
]
|
165 |
+
# Create geometry from coordinates
|
166 |
+
ee_geom_region = ee.Geometry.Polygon(coords)
|
167 |
+
# Crate flood raster and vector
|
168 |
+
(
|
169 |
+
detected_flood_vector,
|
170 |
+
detected_flood_raster,
|
171 |
+
_,
|
172 |
+
_,
|
173 |
+
) = derive_flood_extents(
|
174 |
+
aoi=ee_geom_region,
|
175 |
+
before_start_date=str(before_start),
|
176 |
+
before_end_date=str(before_end),
|
177 |
+
after_start_date=str(after_start),
|
178 |
+
after_end_date=str(after_end),
|
179 |
+
difference_threshold=add_slider,
|
180 |
+
polarization="VH",
|
181 |
+
pass_direction=pass_direction,
|
182 |
+
export=False,
|
183 |
+
)
|
184 |
+
# Create output map
|
185 |
+
Map2 = geemap.Map(
|
186 |
+
# basemap="HYBRID",
|
187 |
+
plugin_Draw=False,
|
188 |
+
Draw_export=False,
|
189 |
+
locate_control=False,
|
190 |
+
plugin_LatLngPopup=False,
|
191 |
+
)
|
192 |
+
try:
|
193 |
+
# Add flood vector layer to map
|
194 |
+
Map2.add_layer(
|
195 |
+
ee_object=detected_flood_vector,
|
196 |
+
name="Flood extent vector",
|
197 |
+
)
|
198 |
+
# Center map on flood raster
|
199 |
+
Map2.centerObject(detected_flood_raster)
|
200 |
+
except ee.EEException:
|
201 |
+
# If error contains the sentence below, it means that
|
202 |
+
# an image could not be properly generated
|
203 |
+
st.error(
|
204 |
+
"""
|
205 |
+
No satellite image found for the selected
|
206 |
+
dates.\n\n
|
207 |
+
Try changing the pass direction.\n\n
|
208 |
+
If this does not work, choose different
|
209 |
+
dates: it is likely that the satellite did not
|
210 |
+
cover the area of interest in the range of
|
211 |
+
dates specified (either before or after the
|
212 |
+
flooding event).
|
213 |
+
"""
|
214 |
+
)
|
215 |
+
else:
|
216 |
+
# If computation was succesfull, save outputs for
|
217 |
+
# output map
|
218 |
+
st.success("Computation complete")
|
219 |
+
st.session_state.output_created = True
|
220 |
+
st.session_state.Map2 = Map2
|
221 |
+
st.session_state.detected_flood_raster = (
|
222 |
+
detected_flood_raster
|
223 |
)
|
224 |
+
st.session_state.detected_flood_vector = (
|
225 |
+
detected_flood_vector
|
|
|
|
|
|
|
|
|
|
|
226 |
)
|
227 |
+
st.session_state.ee_geom_region = ee_geom_region
|
228 |
+
# If computation was successful, create output map in bottom panel
|
229 |
+
if st.session_state.output_created:
|
230 |
+
with row2:
|
231 |
+
# Add collapsable container for output map
|
232 |
+
with st.expander("Output map", expanded=True):
|
233 |
+
# Export Map2 to streamlit
|
234 |
+
st.session_state.Map2.to_streamlit()
|
235 |
+
# Create button to export to file
|
236 |
+
submitted2 = st.button("Export to file")
|
237 |
+
# What happens if button is clicked on?
|
238 |
+
if submitted2:
|
239 |
+
# Add output for computation
|
240 |
+
with st.spinner("Computing... Please wait..."):
|
241 |
try:
|
242 |
+
# Get download url for raster data
|
243 |
+
raster = st.session_state.detected_flood_raster
|
244 |
+
url_r = raster.getDownloadUrl(
|
245 |
+
{
|
246 |
+
"region": st.session_state.ee_geom_region,
|
247 |
+
"scale": 30,
|
248 |
+
"format": "GEO_TIFF",
|
249 |
+
}
|
250 |
)
|
251 |
+
except Exception:
|
|
|
|
|
|
|
|
|
252 |
st.error(
|
253 |
"""
|
254 |
+
The image size is too big for the image to
|
255 |
+
be exported to file. Select a smaller area
|
256 |
+
of interest (side <~ 150km) and repeat the
|
257 |
+
analysis.
|
|
|
|
|
|
|
|
|
258 |
"""
|
259 |
)
|
260 |
else:
|
261 |
+
response_r = requests.get(url_r)
|
262 |
+
# Get download url for raster data
|
263 |
+
vector = st.session_state.detected_flood_vector
|
264 |
+
url_v = vector.getDownloadUrl("GEOJSON")
|
265 |
+
response_v = requests.get(url_v)
|
266 |
+
filename = "flood_extent"
|
267 |
+
timestamp = dt.datetime.now().strftime(
|
268 |
+
"%Y-%m-%d_%H-%M"
|
|
|
|
|
269 |
)
|
270 |
+
with row2:
|
271 |
+
# Create download buttons for raster and vector
|
272 |
+
# data
|
273 |
+
with open("flood_extent.tif", "wb"):
|
274 |
+
ste.download_button(
|
275 |
+
label="Download Raster Extent",
|
276 |
+
data=response_r.content,
|
277 |
+
file_name=(
|
278 |
+
f"{filename}"
|
279 |
+
"_raster_"
|
280 |
+
f"{timestamp}"
|
281 |
+
".tif"
|
282 |
+
),
|
283 |
+
mime="image/tif",
|
284 |
+
)
|
285 |
+
with open("flood_extent.geojson", "wb"):
|
286 |
+
ste.download_button(
|
287 |
+
label="Download Vector Extent",
|
288 |
+
data=response_v.content,
|
289 |
+
file_name=(
|
290 |
+
f"{filename}"
|
291 |
+
"_vector_"
|
292 |
+
f"{timestamp}"
|
293 |
+
".geojson"
|
294 |
+
),
|
295 |
+
mime="text/json",
|
296 |
+
)
|
297 |
+
# Output for computation complete
|
298 |
+
st.success("Computation complete")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|