cboettig commited on
Commit
865760d
·
1 Parent(s): 3ccbccc

tweak tooltip

Browse files
Files changed (1) hide show
  1. app.py +69 -57
app.py CHANGED
@@ -18,6 +18,16 @@ import ibis
18
  from ibis import _
19
  import ibis.selectors as s
20
 
 
 
 
 
 
 
 
 
 
 
21
  # defaults, consider user palette via st.color_picker()
22
  private_color = "#DE881E" # orange #"#850101" # red
23
  tribal_color = "#BF40BF" # purple
@@ -32,20 +42,66 @@ style_choice = "Manager Type"
32
  us_lower_48_area_m2 = 7.8e+12
33
 
34
 
35
- st.set_page_config(layout="wide", page_title="Protected Areas Explorer", page_icon=":globe:")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  '''
38
  # US Protected Area Database Explorer
39
 
40
  '''
41
 
42
- #pad_pmtiles = "https://data.source.coop/cboettig/pad-us-3/pad-stats.pmtiles"
43
- #parquet = "https://data.source.coop/cboettig/pad-us-3/pad-stats.parquet"
44
- pad_pmtiles = "https://huggingface.co/spaces/boettiger-lab/pad-us/resolve/main/pad-stats.pmtiles"
45
- parquet = "https://huggingface.co/spaces/boettiger-lab/pad-us/resolve/main/pad-stats.parquet"
46
- pad_pmtiles = "https://huggingface.co/datasets/boettiger-lab/pad-us-3/resolve/main/pad-stats.pmtiles"
47
- parquet = "https://huggingface.co/datasets/boettiger-lab/pad-us-3/resolve/main/pad-stats.parquet"
48
-
49
 
50
  m = leafmap.Map(center=[35, -100], zoom=4, layers_control=True)
51
 
@@ -66,14 +122,6 @@ mutate(percent_protected = _.percent_protected.round())
66
  )
67
  '''
68
 
69
- def bar_chart(df, x, y):
70
- chart = alt.Chart(df).mark_bar().encode(
71
- x=x,
72
- y=y,
73
- color=alt.Color('color').scale(None)
74
- ).properties(width="container", height=300)
75
- return chart
76
-
77
  manager = {
78
  'property': 'manager_type',
79
  'type': 'categorical',
@@ -320,9 +368,10 @@ select_colors = {
320
  "Manager Type": manager["stops"],
321
  "Fee/Easement": easement["stops"],
322
  "Public Access": access["stops"],
323
- "Mean Richness": manager["stops"],
324
- "Mean RSR": manager["stops"],
325
- "custom": manager["stops"]}
 
326
  colors = (ibis
327
  .memtable(select_colors[style_choice], columns = [column, "color"])
328
  .to_pandas()
@@ -337,45 +386,8 @@ with main:
337
  with map_col:
338
  m.to_streamlit(height=700)
339
 
340
-
341
- @st.cache_resource
342
- def ibis_connection(parquet):
343
- return ibis.read_parquet(parquet)
344
- pad_data = ibis_connection(parquet)
345
-
346
- @st.cache_data()
347
- def summary_table(column = column, colors = colors):
348
- df = (pad_data
349
- .rename(area = "area_square_meters")
350
- .group_by(_[column])
351
- .aggregate(percent_protected = 100 * _.area.sum() / us_lower_48_area_m2,
352
- mean_richness = (_.richness * _.area).sum() / _.area.sum(),
353
- mean_rsr = (_.rsr * _.area).sum() / _.area.sum(),
354
- carbon_lost = (_.deforest_carbon * _.area).sum() / _.area.sum(),
355
- crop_expansion = (_.crop_expansion * _.area).sum() / _.area.sum(),
356
- human_impact = (_.human_impact * _.area).sum() / _.area.sum(),
357
- )
358
- .mutate(percent_protected = _.percent_protected.round())
359
- .inner_join(colors, column)
360
- )
361
- df = df.to_pandas()
362
- df[column] = df[column].astype(str)
363
- return df
364
-
365
  df = summary_table(column, colors)
366
  total_percent = df.percent_protected.sum()
367
-
368
-
369
- base = alt.Chart(df).encode(
370
- alt.Theta("percent_protected:Q").stack(True),
371
- alt.Color("color:N").scale(None).legend(None)
372
- )
373
-
374
- area_chart = (
375
- base.mark_arc(innerRadius=40, outerRadius=70)
376
- ).properties(width=180, height=180)
377
-
378
-
379
  richness_chart = bar_chart(df, column, 'mean_richness')
380
  rsr_chart = bar_chart(df, column, 'mean_rsr')
381
  carbon_lost = bar_chart(df, column, 'carbon_lost')
@@ -387,7 +399,7 @@ with main:
387
  col1, col2, col3 = st.columns(3)
388
  with col1:
389
  f"{total_percent}% Continental US Covered"
390
- st.altair_chart(area_chart, use_container_width=False)
391
 
392
  with col2:
393
  "Species Richness"
 
18
  from ibis import _
19
  import ibis.selectors as s
20
 
21
+ st.set_page_config(layout="wide", page_title="Protected Areas Explorer", page_icon=":globe:")
22
+
23
+ #pad_pmtiles = "https://data.source.coop/cboettig/pad-us-3/pad-stats.pmtiles"
24
+ #parquet = "https://data.source.coop/cboettig/pad-us-3/pad-stats.parquet"
25
+ #pad_pmtiles = "https://huggingface.co/spaces/boettiger-lab/pad-us/resolve/main/pad-stats.pmtiles"
26
+ #parquet = "https://huggingface.co/spaces/boettiger-lab/pad-us/resolve/main/pad-stats.parquet"
27
+ pad_pmtiles = "https://huggingface.co/datasets/boettiger-lab/pad-us-3/resolve/main/pad-stats.pmtiles"
28
+ parquet = "https://huggingface.co/datasets/boettiger-lab/pad-us-3/resolve/main/pad-stats.parquet"
29
+
30
+
31
  # defaults, consider user palette via st.color_picker()
32
  private_color = "#DE881E" # orange #"#850101" # red
33
  tribal_color = "#BF40BF" # purple
 
42
  us_lower_48_area_m2 = 7.8e+12
43
 
44
 
45
+ @st.cache_resource
46
+ def ibis_connection(parquet):
47
+ return ibis.read_parquet(parquet)
48
+ pad_data = ibis_connection(parquet)
49
+
50
+ @st.cache_data()
51
+ def summary_table(column, colors):
52
+ df = (pad_data
53
+ .rename(area = "area_square_meters")
54
+ .group_by(_[column])
55
+ .aggregate(hectares_protected = (_.area.sum() / 10000).round(),
56
+ percent_protected = 100 * _.area.sum() / us_lower_48_area_m2,
57
+ mean_richness = (_.richness * _.area).sum() / _.area.sum(),
58
+ mean_rsr = (_.rsr * _.area).sum() / _.area.sum(),
59
+ carbon_lost = (_.deforest_carbon * _.area).sum() / _.area.sum(),
60
+ crop_expansion = (_.crop_expansion * _.area).sum() / _.area.sum(),
61
+ human_impact = (_.human_impact * _.area).sum() / _.area.sum(),
62
+ )
63
+ .mutate(percent_protected = _.percent_protected.round(1))
64
+ .inner_join(colors, column)
65
+ )
66
+ df = df.to_pandas()
67
+ df[column] = df[column].astype(str)
68
+ return df
69
+
70
+ def bar_chart(df, x, y):
71
+ chart = alt.Chart(df).mark_bar().encode(
72
+ x=x,
73
+ y=y,
74
+ color=alt.Color('color').scale(None)
75
+ ).properties(width="container", height=300)
76
+ return chart
77
+
78
+
79
+ def area_plot(df, column):
80
+ base = alt.Chart(df).encode(
81
+ alt.Theta("percent_protected:Q").stack(True),
82
+ )
83
+ pie = ( base
84
+ .mark_arc(innerRadius= 40, outerRadius=70)
85
+ .encode(alt.Color("color:N").scale(None).legend(None),
86
+ tooltip=['percent_protected', 'hectares_protected', column])
87
+ )
88
+ text = ( base
89
+ .mark_text(radius=60, size=12, color="white")
90
+ .encode(text = column + ":N")
91
+ )
92
+ plot = pie # pie + text
93
+ return plot.properties(width=180, height=180)
94
+
95
+
96
+
97
+
98
+
99
 
100
  '''
101
  # US Protected Area Database Explorer
102
 
103
  '''
104
 
 
 
 
 
 
 
 
105
 
106
  m = leafmap.Map(center=[35, -100], zoom=4, layers_control=True)
107
 
 
122
  )
123
  '''
124
 
 
 
 
 
 
 
 
 
125
  manager = {
126
  'property': 'manager_type',
127
  'type': 'categorical',
 
368
  "Manager Type": manager["stops"],
369
  "Fee/Easement": easement["stops"],
370
  "Public Access": access["stops"],
371
+ "Mean Richness": gap["stops"],
372
+ "Mean RSR": gap["stops"],
373
+ "custom": gap["stops"]}
374
+
375
  colors = (ibis
376
  .memtable(select_colors[style_choice], columns = [column, "color"])
377
  .to_pandas()
 
386
  with map_col:
387
  m.to_streamlit(height=700)
388
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389
  df = summary_table(column, colors)
390
  total_percent = df.percent_protected.sum()
 
 
 
 
 
 
 
 
 
 
 
 
391
  richness_chart = bar_chart(df, column, 'mean_richness')
392
  rsr_chart = bar_chart(df, column, 'mean_rsr')
393
  carbon_lost = bar_chart(df, column, 'carbon_lost')
 
399
  col1, col2, col3 = st.columns(3)
400
  with col1:
401
  f"{total_percent}% Continental US Covered"
402
+ st.altair_chart(area_plot(df, column), use_container_width=False)
403
 
404
  with col2:
405
  "Species Richness"