TCJ21 commited on
Commit
8b07284
·
1 Parent(s): 44d7bee

See Areas now linked to GFM API

Browse files
Files changed (2) hide show
  1. app/pages/0_AOIs.py +24 -15
  2. app/src/gfm.py +24 -0
app/pages/0_AOIs.py CHANGED
@@ -1,13 +1,12 @@
1
  import json
2
  import os
3
- from pathlib import Path
4
 
5
  import folium
6
  import requests
7
  import streamlit as st
8
  from folium.plugins import Draw
9
  from src.config_parameters import params
10
- from src.gfm import download_gfm_geojson, get_existing_flood_geojson, sync_cached_data
11
  from src.utils import (
12
  add_about,
13
  set_tool_page_style,
@@ -34,8 +33,18 @@ set_tool_page_style()
34
  row1 = st.container()
35
  save_area = False
36
 
 
 
 
 
 
 
 
 
 
37
  with row1:
38
- action_type = st.radio(
 
39
  label="Action Type",
40
  options=["See Areas", "Create New Area", "Delete Area"],
41
  label_visibility="hidden",
@@ -43,18 +52,19 @@ with row1:
43
 
44
  # call to render Folium map in Streamlit
45
  folium_map = folium.Map([39, 0], zoom_start=8)
46
- feat_group_selected_area = folium.FeatureGroup(name="selected_area")
47
 
48
- if action_type == "See Areas":
49
- with open("./bboxes/bboxes.json", "r") as f:
50
- bboxes = json.load(f)
51
- for area_name in bboxes.keys():
52
- bbox = bboxes[area_name]["bounding_box"]
 
53
  feat_group_selected_area.add_child(folium.GeoJson(bbox))
54
 
55
  folium_map.fit_bounds(feat_group_selected_area.get_bounds())
 
56
 
57
- elif action_type == "Create New Area":
58
  Draw(
59
  export=False,
60
  draw_options={
@@ -70,7 +80,9 @@ with row1:
70
  new_area_name = st.text_input("Area name")
71
  save_area = st.button("Save Area")
72
 
73
- elif action_type == "Delete Area":
 
 
74
  # Load existing bboxes
75
  with open("./bboxes/bboxes.json", "r") as f:
76
  bboxes = json.load(f)
@@ -90,14 +102,11 @@ with row1:
90
  with open("./bboxes/bboxes.json", "w") as f:
91
  json.dump(bboxes, f)
92
  st.toast("Area successfully deleted")
 
93
 
94
  with open("./bboxes/bboxes.json", "r") as f:
95
  bboxes = json.load(f)
96
 
97
- # geojson_catania = get_existing_flood_geojson("Catania")
98
- # print(geojson_catania)
99
- # geojson_selected_area = folium.GeoJson(geojson_catania)
100
-
101
  # feat_group_selected_area.add_child(geojson_selected_area)
102
  m = st_folium(
103
  folium_map,
 
1
  import json
2
  import os
 
3
 
4
  import folium
5
  import requests
6
  import streamlit as st
7
  from folium.plugins import Draw
8
  from src.config_parameters import params
9
+ from src.gfm import retrieve_all_aois
10
  from src.utils import (
11
  add_about,
12
  set_tool_page_style,
 
33
  row1 = st.container()
34
  save_area = False
35
 
36
+ # To track whether radio selector was changed or something else on the page
37
+ # To prevent reloading on each map change
38
+ if "prev_radio_selection" not in st.session_state:
39
+ st.session_state["prev_radio_selection"] = ""
40
+
41
+ if "all_aois" not in st.session_state:
42
+ st.session_state["all_aois"] = retrieve_all_aois()
43
+
44
+
45
  with row1:
46
+ feat_group_selected_area = folium.FeatureGroup(name="selected_area")
47
+ radio_selection = st.radio(
48
  label="Action Type",
49
  options=["See Areas", "Create New Area", "Delete Area"],
50
  label_visibility="hidden",
 
52
 
53
  # call to render Folium map in Streamlit
54
  folium_map = folium.Map([39, 0], zoom_start=8)
 
55
 
56
+ if radio_selection == "See Areas":
57
+ if st.session_state["prev_radio_selection"] != "See Areas":
58
+ st.session_state["all_aois"] = retrieve_all_aois()
59
+
60
+ for aoi in st.session_state["all_aois"]:
61
+ bbox = aoi["geoJSON"]
62
  feat_group_selected_area.add_child(folium.GeoJson(bbox))
63
 
64
  folium_map.fit_bounds(feat_group_selected_area.get_bounds())
65
+ st.session_state["prev_radio_selection"] = "See Areas"
66
 
67
+ elif radio_selection == "Create New Area":
68
  Draw(
69
  export=False,
70
  draw_options={
 
80
  new_area_name = st.text_input("Area name")
81
  save_area = st.button("Save Area")
82
 
83
+ st.session_state["prev_radio_selection"] = "Create New Area"
84
+
85
+ elif radio_selection == "Delete Area":
86
  # Load existing bboxes
87
  with open("./bboxes/bboxes.json", "r") as f:
88
  bboxes = json.load(f)
 
102
  with open("./bboxes/bboxes.json", "w") as f:
103
  json.dump(bboxes, f)
104
  st.toast("Area successfully deleted")
105
+ st.session_state["prev_radio_selection"] = "Delete Area"
106
 
107
  with open("./bboxes/bboxes.json", "r") as f:
108
  bboxes = json.load(f)
109
 
 
 
 
 
110
  # feat_group_selected_area.add_child(geojson_selected_area)
111
  m = st_folium(
112
  folium_map,
app/src/gfm.py CHANGED
@@ -122,6 +122,30 @@ def download_gfm_geojson(
122
  print("Done!")
123
 
124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  def get_existing_flood_geojson(area_name, product_time, output_file_path=None):
126
  """
127
  Getting a saved GFM flood geojson in an output folder of GFM files. Merge in one feature group if multiple.
 
122
  print("Done!")
123
 
124
 
125
+ def retrieve_all_aois():
126
+ print("Retrieving all AOIs from GFM API")
127
+ username = os.environ["gfm_username"]
128
+ password = os.environ["gfm_password"]
129
+ base_url = "https://api.gfm.eodc.eu/v1"
130
+
131
+ # Get token, setup header
132
+ token_url = f"{base_url}/auth/login"
133
+
134
+ payload = {"email": username, "password": password}
135
+
136
+ response = requests.post(token_url, json=payload)
137
+ user_id = response.json()["client_id"]
138
+ access_token = response.json()["access_token"]
139
+ header = {"Authorization": f"bearer {access_token}"}
140
+
141
+ aoi_url = f"{base_url}/aoi/user/{user_id}"
142
+ response = requests.get(aoi_url, headers=header)
143
+
144
+ aois = response.json()["aois"]
145
+
146
+ return aois
147
+
148
+
149
  def get_existing_flood_geojson(area_name, product_time, output_file_path=None):
150
  """
151
  Getting a saved GFM flood geojson in an output folder of GFM files. Merge in one feature group if multiple.