cassiebuhler commited on
Commit
2314890
·
1 Parent(s): 575a450

adding data filters

Browse files
Files changed (1) hide show
  1. app.py +101 -15
app.py CHANGED
@@ -48,7 +48,11 @@ def to_streamlit(
48
  except Exception as e:
49
  raise Exception(e)
50
 
51
-
 
 
 
 
52
 
53
 
54
 
@@ -56,7 +60,8 @@ def to_streamlit(
56
  #pad_pmtiles = "https://data.source.coop/cboettig/pad-us-3/pad-stats.pmtiles"
57
  #parquet = "https://data.source.coop/cboettig/pad-us-3/pad-stats.parquet"
58
  pad_pmtiles = "https://huggingface.co/datasets/boettiger-lab/pad-us-3/resolve/main/pad-stats.pmtiles"
59
- parquet = "https://huggingface.co/datasets/boettiger-lab/pad-us-3/resolve/main/pad-stats.parquet"
 
60
 
61
  # adding this to test out git
62
 
@@ -70,7 +75,7 @@ public_color = "#3388ff" # blue
70
  low = 2
71
  high = 3
72
  alpha = .5
73
- style_choice = "Manager Type"
74
  us_lower_48_area_m2 = 7.8e+12
75
 
76
 
@@ -147,9 +152,58 @@ def pad_style(paint, alpha):
147
  }]}
148
 
149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
  # +
152
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  custom_style = '''
154
  "blue"
155
  '''
@@ -303,7 +357,14 @@ st.set_page_config(layout="wide", page_title="Protected Areas Explorer", page_ic
303
  '''
304
  # US Conservation Atlas Prototype
305
 
 
 
 
 
 
306
  '''
 
 
307
 
308
  m = leafmap.Map(style="positron")
309
 
@@ -319,6 +380,12 @@ with st.sidebar:
319
  m.add_basemap("Esri.WorldImagery")
320
 
321
 
 
 
 
 
 
 
322
  with st.expander("🦜 Biodiversity"):
323
  show_richness = st.toggle("Species Richness", False)
324
  if show_richness:
@@ -369,14 +436,7 @@ with st.sidebar:
369
  m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/natcrop_fii_100m_cog.tif",
370
  palette="reds", name="forest integrity loss", transparent_bg=True, opacity = 0.8, fit_bounds=False)
371
 
372
- if st.toggle("Protected Areas", True):
373
-
374
- style_choice = st.radio("Color by:", style_options)
375
- alpha = st.slider("transparency", 0.0, 1.0, 0.5)
376
- style = pad_style(style_options[style_choice], alpha)
377
- m.add_pmtiles(pad_pmtiles, style=style, visible=True, opacity=alpha, tooltip=True)
378
-
379
-
380
  with st.expander("💰 Conservation Investment"):
381
  if st.toggle("Bipartisan Infrastructure Law"):
382
  m.add_geojson(bil_url, layer_type="fill-extrusion", paint=bil_fill, name="BIL", fit_bounds=False)
@@ -388,6 +448,32 @@ with st.sidebar:
388
  value = code_ex,
389
  height = 100)
390
  eval(compile(code, "<string>", "exec"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391
 
392
  # "## Boundaries"
393
  # boundaries = st.radio("Boundaries:",
@@ -411,7 +497,7 @@ select_column = {
411
  "Mean Richness": "gap_code",
412
  "Mean RSR": "gap_code",
413
  "custom": "gap_code"}
414
- column = select_column[style_choice]
415
 
416
  # Map radio buttons to corresponding color-scheme:
417
  select_colors = {
@@ -425,7 +511,7 @@ select_colors = {
425
  "custom": gap["stops"]}
426
 
427
  colors = (ibis
428
- .memtable(select_colors[style_choice], columns = [column, "color"])
429
  .to_pandas()
430
  )
431
 
@@ -507,7 +593,7 @@ with footer:
507
  '''
508
  ## Credits
509
 
510
- Author: Carl Boettiger, UC Berkeley
511
  License: BSD-2-clause
512
 
513
  ### Data sources
 
48
  except Exception as e:
49
  raise Exception(e)
50
 
51
+ # gap codes 3 and 4 are off by default.
52
+ default_gap = {
53
+ 3: False,
54
+ 4: False,
55
+ }
56
 
57
 
58
 
 
60
  #pad_pmtiles = "https://data.source.coop/cboettig/pad-us-3/pad-stats.pmtiles"
61
  #parquet = "https://data.source.coop/cboettig/pad-us-3/pad-stats.parquet"
62
  pad_pmtiles = "https://huggingface.co/datasets/boettiger-lab/pad-us-3/resolve/main/pad-stats.pmtiles"
63
+ # parquet = "https://huggingface.co/datasets/boettiger-lab/pad-us-3/resolve/main/pad-stats.parquet"
64
+ parquet = "pad-stats.parquet"
65
 
66
  # adding this to test out git
67
 
 
75
  low = 2
76
  high = 3
77
  alpha = .5
78
+ color_choice = "Manager Type"
79
  us_lower_48_area_m2 = 7.8e+12
80
 
81
 
 
152
  }]}
153
 
154
 
155
+ def get_pmtiles_style(paint, alpha, cols, values): #style depends on the filters selected.
156
+ filters = []
157
+ for col, val in zip(cols, values):
158
+ filter_condition = ["match", ["get", col], val, True, False]
159
+ filters.append(filter_condition)
160
+ combined_filter = ["all"] + filters
161
+ return {
162
+ "version": 8,
163
+ "sources": {
164
+ "pad": {
165
+ "type": "vector",
166
+ "url": "pmtiles://" + pad_pmtiles,
167
+ "attribution": "US PAD v3",
168
+ }
169
+ },
170
+ "layers": [{
171
+ "id": "public",
172
+ "source": "pad",
173
+ "source-layer": "pad-stats",
174
+ "type": "fill",
175
+ "filter": combined_filter, # Use the combined filter
176
+ "paint": {
177
+ "fill-color": paint,
178
+ "fill-opacity": alpha
179
+ }
180
+ }]
181
+ }
182
+
183
 
184
  # +
185
+ def getButtons(style_options, color_choice, default_gap=None): #finding the buttons selected to use as filters
186
+ column = style_options[color_choice]['property']
187
+ opts = [style[0] for style in style_options[color_choice]['stops']]
188
+ default_gap = default_gap or {}
189
+ buttons = {
190
+ name: st.checkbox(f"{name}", value=default_gap.get(name, True), key=column + str(name))
191
+ for name in opts
192
+ }
193
+ filter_choice = [key for key, value in buttons.items() if value] # return only selected
194
+ d = {}
195
+ d[column] = filter_choice
196
+ return d
197
+
198
+
199
+ def getColorVals(style_options, color_choice):
200
+ #df_tab only includes filters selected, we need to manually add "color_by" column (if it's not already a filter).
201
+ column = style_options[color_choice]['property']
202
+ opts = [style[0] for style in style_options[color_choice]['stops']]
203
+ d = {}
204
+ d[column] = opts
205
+ return d
206
+
207
  custom_style = '''
208
  "blue"
209
  '''
 
357
  '''
358
  # US Conservation Atlas Prototype
359
 
360
+ An interactive cloud-native geospatial tool for exploring and visualizing the United States' protected lands with open data.
361
+ - ❌ Safari/iOS not yet supported
362
+ - ⬅️ Use the left sidebar to color-code the map by different attributes, toggle on data layers and view summary charts, or filter data.
363
+
364
+
365
  '''
366
+ st.divider()
367
+ filters = {}
368
 
369
  m = leafmap.Map(style="positron")
370
 
 
380
  m.add_basemap("Esri.WorldImagery")
381
 
382
 
383
+ # if st.toggle("Protected Areas", True):
384
+
385
+ color_choice = st.radio("Color by:", style_options)
386
+ alpha = st.slider("transparency", 0.0, 1.0, 0.5)
387
+
388
+ "Data layers:"
389
  with st.expander("🦜 Biodiversity"):
390
  show_richness = st.toggle("Species Richness", False)
391
  if show_richness:
 
436
  m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/natcrop_fii_100m_cog.tif",
437
  palette="reds", name="forest integrity loss", transparent_bg=True, opacity = 0.8, fit_bounds=False)
438
 
439
+
 
 
 
 
 
 
 
440
  with st.expander("💰 Conservation Investment"):
441
  if st.toggle("Bipartisan Infrastructure Law"):
442
  m.add_geojson(bil_url, layer_type="fill-extrusion", paint=bil_fill, name="BIL", fit_bounds=False)
 
448
  value = code_ex,
449
  height = 100)
450
  eval(compile(code, "<string>", "exec"))
451
+ st.divider()
452
+
453
+ "Filters:"
454
+ for label in style_options: # get selected filters (based on the buttons selected)
455
+ with st.expander(label):
456
+ if label == "GAP Status Code": # gap code 1 and 2 are on by default
457
+ opts = getButtons(style_options, label, default_gap)
458
+ else: # other buttons are not on by default.
459
+ opts = getButtons(style_options, label)
460
+ filters.update(opts)
461
+
462
+ selected = {k: v for k, v in filters.items() if v}
463
+ if selected:
464
+ filter_cols = list(selected.keys())
465
+ filter_vals = list(selected.values())
466
+ else:
467
+ filter_cols = []
468
+ filter_vals = []
469
+
470
+ style = get_pmtiles_style(style_options[color_choice], alpha, filter_cols, filter_vals)
471
+ legend_d = {cat: color for cat, color in style_options[color_choice]['stops']}
472
+ m.add_legend(legend_dict = legend_d, position = 'bottom-left')
473
+ m.add_pmtiles(pad_pmtiles, style=style, name="PAD", opacity=alpha, tooltip=True)
474
+
475
+ # style = get_pmtiles_style(style_options[color_choice], alpha)
476
+ # m.add_pmtiles(pad_pmtiles, style=style, visible=True, opacity=alpha, tooltip=True)
477
 
478
  # "## Boundaries"
479
  # boundaries = st.radio("Boundaries:",
 
497
  "Mean Richness": "gap_code",
498
  "Mean RSR": "gap_code",
499
  "custom": "gap_code"}
500
+ column = select_column[color_choice]
501
 
502
  # Map radio buttons to corresponding color-scheme:
503
  select_colors = {
 
511
  "custom": gap["stops"]}
512
 
513
  colors = (ibis
514
+ .memtable(select_colors[color_choice], columns = [column, "color"])
515
  .to_pandas()
516
  )
517
 
 
593
  '''
594
  ## Credits
595
 
596
+ Author: Cassie Buhler & Carl Boettiger, UC Berkeley
597
  License: BSD-2-clause
598
 
599
  ### Data sources