cassiebuhler commited on
Commit
40f21cc
·
1 Parent(s): 44008a3

added multiple filters

Browse files
Files changed (1) hide show
  1. app.py +91 -40
app.py CHANGED
@@ -34,10 +34,12 @@ def to_streamlit(
34
  except Exception as e:
35
  raise Exception(e)
36
 
 
 
37
 
38
  ca_pmtiles = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/main/ca2024-tippe.pmtiles"
39
- # ca_parquet = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/main/ca2024.parquet"
40
- ca_parquet = "ca2024.parquet"
41
 
42
  ca_area_acres = 1.014e8 #acres
43
  style_choice = "GAP Status Code"
@@ -49,17 +51,29 @@ ca = (con
49
  .cast({"geom": "geometry"})
50
  )
51
 
52
- private_color = "#DE881E" # orange #"#850101" # red
 
53
  tribal_color = "#BF40BF" # purple
54
  mixed_color = "#005a00" # green
55
- public_color = "#3388ff" # blue
56
  year2023_color = "#26542C" # green
57
  year2024_color = "#F3AB3D" # orange
58
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  def summary_table(column, colors):
61
 
62
  df = (ca
 
63
  .group_by(column)
64
  .aggregate(percent_protected = 100 * _.Acres.sum() / ca_area_acres)
65
  .mutate(percent_protected = _.percent_protected.round(1),
@@ -128,28 +142,63 @@ def filter_pmtiles(paint, alpha, column, items):
128
  }
129
  }]}
130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  def getButtons(style_options, style_choice):
132
  column = style_options[style_choice]['property']
133
  opts = [style[0] for style in style_options[style_choice]['stops']]
134
- buttons = {name: st.checkbox(f"{name}", value = True) for name in opts}
135
- return column, buttons
136
-
 
 
 
137
 
138
  manager = {
139
  'property': 'manager_type',
140
  'type': 'categorical',
141
  'stops': [
142
- ['Federal', "darkblue"],
143
- ['State', public_color],
144
- ['Non Profit', "orange"],
145
- ['Special District', "darkgreen"],
146
  ['Unknown', "grey"],
147
- ['County', "lightblue"],
148
- ['City', "pink"],
149
- ['Joint', "green"],
150
  ['Tribal', tribal_color],
151
- ['Private', "darkred"],
152
- ['Home Owners Association', "lightgreen"]
153
  ]
154
  }
155
 
@@ -158,8 +207,8 @@ easement = {
158
  'property': 'Easement',
159
  'type': 'categorical',
160
  'stops': [
161
- [0, public_color],
162
- [1, private_color]
163
  ]
164
  }
165
 
@@ -178,8 +227,8 @@ access = {
178
  'property': 'access_type',
179
  'type': 'categorical',
180
  'stops': [
181
- ['Open Access', public_color],
182
- ['No Public Access', private_color],
183
  ['Unknown Access', "grey"],
184
  ['Restricted Access', tribal_color]
185
  ]
@@ -192,16 +241,16 @@ gap = {
192
  'type': 'categorical',
193
  'stops': [
194
  [1, "#26633d"],
195
- [2, "#879647"]
196
- # [3, "#BBBBBB"],
197
- # [4, "#F8F8F8"]
198
  ]
199
  }
200
 
201
 
202
  style_options = {
203
  "GAP Status Code": gap,
204
- "Management Agency": manager,
205
  "Easement": easement,
206
  "Public Access": access,
207
  "Year": year
@@ -216,44 +265,46 @@ st.set_page_config(layout="wide", page_title="CA Protected Areas Explorer", page
216
  '''
217
 
218
  m = leafmap.Map(style="positron")
 
219
 
220
  with st.sidebar:
221
-
222
- if st.toggle("Protected Areas", True):
223
- alpha = st.slider("transparency", 0.0, 1.0, 0.5)
224
- style_choice = st.radio("Color by:", style_options)
225
 
 
 
 
 
226
 
227
- with st.expander(style_choice, expanded=True):
228
- column, buttons = getButtons(style_options, style_choice)
229
- option_choice = [key for key, value in buttons.items() if value] #return only the options selected
230
-
231
- # If any options are selected, apply the filter
232
- if option_choice:
233
- style = filter_pmtiles(style_options[style_choice], alpha, column, option_choice)
234
- m.add_pmtiles(ca_pmtiles, style=style, visible=True, name="CA", opacity=alpha, tooltip=True)
235
 
236
 
237
 
238
  select_column = {
239
  "GAP Status Code": "reGAP",
240
- "Management Agency": "manager_type",
241
  "Easement": "Easement",
242
  "Year": "established",
243
  "Public Access": "access_type"}
244
 
245
 
246
- column = select_column[style_choice]
247
 
248
  select_colors = {
249
- "Management Agency": manager["stops"],
250
  "Easement": easement["stops"],
251
  "Public Access": access["stops"],
252
  "Year": year["stops"],
253
  "GAP Status Code": gap["stops"]}
254
 
255
  colors = (ibis
256
- .memtable(select_colors[style_choice], columns = [column, "color"])
257
  .to_pandas()
258
  )
259
 
 
34
  except Exception as e:
35
  raise Exception(e)
36
 
37
+ from huggingface_hub import create_branch, create_tag
38
+ create_branch("boettiger-lab/ca-30x30", repo_type="space", branch="feat")
39
 
40
  ca_pmtiles = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/main/ca2024-tippe.pmtiles"
41
+ ca_parquet = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/main/ca2024.parquet"
42
+ # ca_parquet = "ca2024.parquet"
43
 
44
  ca_area_acres = 1.014e8 #acres
45
  style_choice = "GAP Status Code"
 
51
  .cast({"geom": "geometry"})
52
  )
53
 
54
+ private_access_color = "#DE881E" # orange #"#850101" # red
55
+ public_access_color = "#3388ff" # blue
56
  tribal_color = "#BF40BF" # purple
57
  mixed_color = "#005a00" # green
 
58
  year2023_color = "#26542C" # green
59
  year2024_color = "#F3AB3D" # orange
60
 
61
+ federal_color = "#529642" # green
62
+ state_color = "#A1B03D" # light green
63
+ local_color = "#365591" # blue
64
+ special_color = "#529642" # brown
65
+ private_color = "#7A3F1A" # brown
66
+ joint_color = "#DAB0AE" # pink
67
+ county_color = "#BFD76B" # green
68
+ city_color = "#BDC368" #green
69
+ hoa_color = "#A89BBC" # purple
70
+ nonprofit_color = "#D77031" #orange
71
+
72
 
73
  def summary_table(column, colors):
74
 
75
  df = (ca
76
+ .filter(_.reGAP <3)
77
  .group_by(column)
78
  .aggregate(percent_protected = 100 * _.Acres.sum() / ca_area_acres)
79
  .mutate(percent_protected = _.percent_protected.round(1),
 
142
  }
143
  }]}
144
 
145
+
146
+ def filter_pmtiles(paint, alpha, cols, values):
147
+ filters = []
148
+
149
+ for col, val in zip(cols, values):
150
+ # Create the filter for each column
151
+ filter_condition = ["match", ["get", col], val, True, False]
152
+ filters.append(filter_condition)
153
+ combined_filter = ["all"] + filters
154
+
155
+ return {
156
+ "version": 8,
157
+ "sources": {
158
+ "ca": {
159
+ "type": "vector",
160
+ "url": "pmtiles://" + ca_pmtiles,
161
+ }
162
+ },
163
+ "layers": [{
164
+ "id": "ca30x30",
165
+ "source": "ca",
166
+ "source-layer": "ca2024",
167
+ "type": "fill",
168
+ "filter": combined_filter, # Use the combined filter
169
+ "paint": {
170
+ "fill-color": paint,
171
+ "fill-opacity": alpha
172
+ }
173
+ }]
174
+ }
175
+
176
+
177
  def getButtons(style_options, style_choice):
178
  column = style_options[style_choice]['property']
179
  opts = [style[0] for style in style_options[style_choice]['stops']]
180
+ buttons = {name: st.checkbox(f"{name}", value = False, key = column+str(name)) for name in opts}
181
+ filter_choice = [key for key, value in buttons.items() if value] #return only the options selected
182
+ d = {}
183
+ d[column] = filter_choice
184
+ return d
185
+
186
 
187
  manager = {
188
  'property': 'manager_type',
189
  'type': 'categorical',
190
  'stops': [
191
+ ['Federal', federal_color],
192
+ ['State', state_color],
193
+ ['Non Profit', nonprofit_color],
194
+ ['Special District', special_color],
195
  ['Unknown', "grey"],
196
+ ['County', county_color],
197
+ ['City', city_color],
198
+ ['Joint', joint_color],
199
  ['Tribal', tribal_color],
200
+ ['Private', private_color],
201
+ ['Home Owners Association', hoa_color]
202
  ]
203
  }
204
 
 
207
  'property': 'Easement',
208
  'type': 'categorical',
209
  'stops': [
210
+ [0, public_access_color],
211
+ [1, private_access_color]
212
  ]
213
  }
214
 
 
227
  'property': 'access_type',
228
  'type': 'categorical',
229
  'stops': [
230
+ ['Open Access', public_access_color],
231
+ ['No Public Access', private_access_color],
232
  ['Unknown Access', "grey"],
233
  ['Restricted Access', tribal_color]
234
  ]
 
241
  'type': 'categorical',
242
  'stops': [
243
  [1, "#26633d"],
244
+ [2, "#879647"],
245
+ [3, "#BBBBBB"],
246
+ [4, "#F8F8F8"]
247
  ]
248
  }
249
 
250
 
251
  style_options = {
252
  "GAP Status Code": gap,
253
+ "Manager Type": manager,
254
  "Easement": easement,
255
  "Public Access": access,
256
  "Year": year
 
265
  '''
266
 
267
  m = leafmap.Map(style="positron")
268
+ filters = {}
269
 
270
  with st.sidebar:
271
+ color_choice = st.radio("Color by:", style_options)
272
+ alpha = st.slider("transparency", 0.0, 1.0, 0.5)
273
+ st.text("Filters:")
 
274
 
275
+ for label in style_options:
276
+ with st.expander(label):
277
+ opts = getButtons(style_options, label)
278
+ filters.update(opts)
279
 
280
+ selected = {k: v for k, v in filters.items() if v}
281
+ if selected:
282
+ cols = list(selected.keys())
283
+ vals = list(selected.values())
284
+ style = filter_pmtiles(style_options[color_choice], alpha, cols, vals)
285
+ m.add_pmtiles(ca_pmtiles, style=style, visible=True, name="CA", opacity=alpha, tooltip=True)
 
 
286
 
287
 
288
 
289
  select_column = {
290
  "GAP Status Code": "reGAP",
291
+ "Manager Type": "manager_type",
292
  "Easement": "Easement",
293
  "Year": "established",
294
  "Public Access": "access_type"}
295
 
296
 
297
+ column = select_column[color_choice]
298
 
299
  select_colors = {
300
+ "Manager Type": manager["stops"],
301
  "Easement": easement["stops"],
302
  "Public Access": access["stops"],
303
  "Year": year["stops"],
304
  "GAP Status Code": gap["stops"]}
305
 
306
  colors = (ibis
307
+ .memtable(select_colors[color_choice], columns = [column, "color"])
308
  .to_pandas()
309
  )
310