cboettig commited on
Commit
52e577b
·
1 Parent(s): 69ae112
Files changed (4) hide show
  1. app/.gitignore +4 -0
  2. app/README.md +11 -0
  3. app/app.py +1142 -0
  4. app/requirements.txt +19 -0
app/.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ *.parquet
2
+ *.pmtiles
3
+ *.db
4
+
app/README.md ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: CA 30x30
3
+ emoji: 🏞️
4
+ colorFrom: green
5
+ colorTo: yellow
6
+ sdk: streamlit
7
+ pinned: false
8
+ license: bsd
9
+ ---
10
+
11
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app/app.py ADDED
@@ -0,0 +1,1142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import streamlit.components.v1 as components
3
+ import base64
4
+ import leafmap.maplibregl as leafmap
5
+ import altair as alt
6
+ import ibis
7
+ from ibis import _
8
+ import ibis.selectors as s
9
+ import os
10
+ import pandas as pd
11
+ import geopandas as gpd
12
+ from shapely import wkb
13
+ import sqlalchemy
14
+ import pathlib
15
+ from typing import Optional
16
+
17
+ # urls for main layer
18
+ ca_pmtiles = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/main/cpad-stats.pmtiles"
19
+ ca_parquet = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/main/cpad-stats.parquet"
20
+ #ca_parquet = "cpad-stats.parquet" #local copy is faster
21
+
22
+ ca_area_acres = 1.014e8 #acres
23
+ style_choice = "GAP Status Code"
24
+
25
+
26
+
27
+
28
+ ## Create the engine
29
+ cwd = pathlib.Path.cwd()
30
+ connect_args = {'preload_extensions':['spatial']}
31
+ eng = sqlalchemy.create_engine(f"duckdb:///{cwd}/duck.db",connect_args = connect_args)
32
+
33
+ # Create the duckdb connection directly from the sqlalchemy engine instead.
34
+ # Not as elegant as `ibis.duckdb.connect()` but shares connection with sqlalchmey.
35
+ con = ibis.duckdb.from_connection(eng.raw_connection())
36
+
37
+ ## Create the table from remote parquet only if it doesn't already exist on disk
38
+ current_tables = con.list_tables()
39
+ if "mydata" not in set(current_tables):
40
+ tbl = con.read_parquet(ca_parquet)
41
+ con.create_table("mydata", tbl)
42
+
43
+ ca = con.table("mydata")
44
+
45
+ # urls for additional data layers
46
+ url_sr = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/main/species-richness-ca/{z}/{x}/{y}.png"
47
+ url_rsr = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/main/range-size-rarity/{z}/{x}/{y}.png"
48
+ url_irr_carbon = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/main/ca_irrecoverable_c_2018_cog.tif"
49
+ url_man_carbon = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/main/ca_manageable_c_2018_cog.tif"
50
+ url_svi = "https://data.source.coop/cboettig/social-vulnerability/svi2020_us_county.pmtiles"
51
+ url_justice40 = "https://data.source.coop/cboettig/justice40/disadvantaged-communities.pmtiles"
52
+ url_loss_carbon = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/main/deforest-carbon-ca/{z}/{x}/{y}.png"
53
+ url_hi = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/main/ca_human_impact_cog.tif"
54
+ url_calfire = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/main/cal_fire_2022.pmtiles"
55
+ url_rxburn = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/main/cal_rxburn_2022.pmtiles"
56
+
57
+ # colors for plotting
58
+ private_access_color = "#DE881E" # orange
59
+ public_access_color = "#3388ff" # blue
60
+ tribal_color = "#BF40BF" # purple
61
+ mixed_color = "#005a00" # green
62
+ year2023_color = "#26542C" # green
63
+ year2024_color = "#F3AB3D" # orange
64
+ federal_color = "#529642" # green
65
+ state_color = "#A1B03D" # light green
66
+ local_color = "#365591" # blue
67
+ special_color = "#0096FF" # blue
68
+ private_color = "#7A3F1A" # brown
69
+ joint_color = "#DAB0AE" # light pink
70
+ county_color = "#DE3163" # magenta
71
+ city_color = "#ADD8E6" #light blue
72
+ hoa_color = "#A89BBC" # purple
73
+ nonprofit_color = "#D77031" #orange
74
+ justice40_color = "#00008B" #purple
75
+ svi_color = "#1bc7c3" #cyan
76
+ white = "#FFFFFF"
77
+
78
+
79
+ # gap codes 3 and 4 are off by default.
80
+ default_gap = {
81
+ 3: False,
82
+ 4: False,
83
+ }
84
+
85
+
86
+ for key in [
87
+ 'richness', 'rsr', 'irrecoverable_carbon', 'manageable_carbon',
88
+ 'percent_fire_10yr', 'percent_rxburn_10yr', 'percent_disadvantaged',
89
+ 'svi', 'svi_socioeconomic_status', 'svi_household_char',
90
+ 'svi_racial_ethnic_minority', 'svi_housing_transit',
91
+ 'deforest_carbon', 'human_impact'
92
+ ]:
93
+ if key not in st.session_state:
94
+ st.session_state[key] = False
95
+
96
+
97
+
98
+ from functools import reduce
99
+
100
+ def get_summary(ca, combined_filter, column, colors=None): #summary stats, based on filtered data
101
+ df = ca.filter(combined_filter)
102
+ df = (df
103
+ .group_by(*column) # unpack the list for grouping
104
+ .aggregate(percent_protected=100 * _.acres.sum() / ca_area_acres,
105
+ mean_richness = (_.richness * _.acres).sum() / _.acres.sum(),
106
+ mean_rsr = (_.rsr * _.acres).sum() / _.acres.sum(),
107
+ mean_irrecoverable_carbon = (_.irrecoverable_carbon * _.acres).sum() / _.acres.sum(),
108
+ mean_manageable_carbon = (_.manageable_carbon * _.acres).sum() / _.acres.sum(),
109
+ mean_percent_fire_10yr = (_.percent_fire_10yr *_.acres).sum()/_.acres.sum(),
110
+ mean_percent_rxburn_10yr = (_.percent_rxburn_10yr *_.acres).sum()/_.acres.sum(),
111
+ mean_percent_disadvantaged = (_.percent_disadvantaged * _.acres).sum() / _.acres.sum(),
112
+ mean_svi = (_.svi * _.acres).sum() / _.acres.sum(),
113
+ mean_svi_socioeconomic_status = (_.svi_socioeconomic_status * _.acres).sum() / _.acres.sum(),
114
+ mean_svi_household_char = (_.svi_household_char * _.acres).sum() / _.acres.sum(),
115
+ mean_svi_racial_ethnic_minority = (_.svi_racial_ethnic_minority * _.acres).sum() / _.acres.sum(),
116
+ mean_svi_housing_transit = (_.svi_housing_transit * _.acres).sum() / _.acres.sum(),
117
+ mean_carbon_lost = (_.deforest_carbon * _.acres).sum() / _.acres.sum(),
118
+ mean_human_impact = (_.human_impact * _.acres).sum() / _.acres.sum(),
119
+ )
120
+ .mutate(percent_protected=_.percent_protected.round(1))
121
+ )
122
+ if colors is not None and not colors.empty: #only the df will have colors, df_tab doesn't since we are printing it.
123
+ df = df.inner_join(colors, column)
124
+ df = df.cast({col: "string" for col in column})
125
+ df = df.to_pandas()
126
+ return df
127
+
128
+
129
+ def summary_table(column, colors, filter_cols, filter_vals,colorby_vals): # get df for charts + df_tab for printed table
130
+ filters = []
131
+ if filter_cols and filter_vals: #if a filter is selected, add to list of filters
132
+ for filter_col, filter_val in zip(filter_cols, filter_vals):
133
+ if len(filter_val) > 1:
134
+ filters.append(getattr(_, filter_col).isin(filter_val))
135
+ else:
136
+ filters.append(getattr(_, filter_col) == filter_val[0])
137
+ if column not in filter_cols: #show color_by column in table by adding it as a filter (if it's not already a filter)
138
+ filter_cols.append(column)
139
+ filters.append(getattr(_, column).isin(colorby_vals[column]))
140
+ combined_filter = reduce(lambda x, y: x & y, filters) #combining all the filters into ibis filter expression
141
+ df = get_summary(ca, combined_filter, [column], colors) # df used for charts
142
+ df_tab = get_summary(ca, combined_filter, filter_cols, colors = None) #df used for printed table
143
+ return df, df_tab
144
+
145
+
146
+
147
+ def area_plot(df, column): #percent protected pie chart
148
+ base = alt.Chart(df).encode(
149
+ alt.Theta("percent_protected:Q").stack(True),
150
+ )
151
+ pie = ( base
152
+ .mark_arc(innerRadius= 40, outerRadius=100)
153
+ .encode(alt.Color("color:N").scale(None).legend(None),
154
+ tooltip=['percent_protected', column])
155
+ )
156
+ text = ( base
157
+ .mark_text(radius=80, size=14, color="white")
158
+ .encode(text = column + ":N")
159
+ )
160
+ plot = pie # pie + text
161
+ return plot.properties(width="container", height=290)
162
+
163
+
164
+ def bar_chart(df, x, y, title): #display summary stats for color_by column
165
+
166
+ #axis label angles / chart size
167
+ if x == "manager_type": #labels are too long, making vertical
168
+ angle = 270
169
+ height = 373
170
+ else: #other labels are horizontal
171
+ angle = 0
172
+ height = 310
173
+
174
+ # order of bars
175
+ if x == "established": # order labels in chronological order, not alphabetic.
176
+ sort = '-x'
177
+ elif x == "access_type": #order based on levels of openness
178
+ sort=['Open', 'Restricted', 'No Public', "Unknown"]
179
+ elif x == "manager_type":
180
+ sort = ["Federal","Tribal","State","Special District", "County", "City", "HOA","Joint","Non Profit","Private","Unknown"]
181
+ else:
182
+ sort = 'x'
183
+
184
+ x_title = next(key for key, value in select_column.items() if value == x)
185
+ chart = alt.Chart(df).mark_bar().transform_calculate(
186
+ access_label=f"replace(datum.{x}, ' Access', '')" #omit access from access_type labels so it fits in frame
187
+ ).encode(
188
+ x=alt.X("access_label:N",
189
+ axis=alt.Axis(labelAngle=angle, title=x_title),
190
+ sort=sort),
191
+ y=alt.Y(y, axis=alt.Axis()),
192
+ color=alt.Color('color').scale(None)
193
+ ).properties(width="container", height=height, title = title
194
+ )
195
+ # sizing for poster
196
+ # ).configure_title(
197
+ # fontSize=40
198
+ # ).configure_axis(
199
+ # labelFontSize=24,
200
+ # titleFontSize=34
201
+ # )
202
+ return chart
203
+
204
+
205
+
206
+ def getButtons(style_options, style_choice, default_gap=None): #finding the buttons selected to use as filters
207
+ column = style_options[style_choice]['property']
208
+ opts = [style[0] for style in style_options[style_choice]['stops']]
209
+ default_gap = default_gap or {}
210
+ buttons = {
211
+ name: st.checkbox(f"{name}", value=default_gap.get(name, True), key=column + str(name))
212
+ for name in opts
213
+ }
214
+ filter_choice = [key for key, value in buttons.items() if value] # return only selected
215
+ d = {}
216
+ d[column] = filter_choice
217
+ return d
218
+
219
+
220
+
221
+ def getColorVals(style_options, style_choice):
222
+ #df_tab only includes filters selected, we need to manually add "color_by" column (if it's not already a filter).
223
+ column = style_options[style_choice]['property']
224
+ opts = [style[0] for style in style_options[style_choice]['stops']]
225
+ d = {}
226
+ d[column] = opts
227
+ return d
228
+
229
+ manager = {
230
+ 'property': 'manager_type',
231
+ 'type': 'categorical',
232
+ 'stops': [
233
+ ['Federal', federal_color],
234
+ ['State', state_color],
235
+ ['Non Profit', nonprofit_color],
236
+ ['Special District', special_color],
237
+ ['Unknown', "#bbbbbb"],
238
+ ['County', county_color],
239
+ ['City', city_color],
240
+ ['Joint', joint_color],
241
+ ['Tribal', tribal_color],
242
+ ['Private', private_color],
243
+ ['HOA', hoa_color]
244
+ ]
245
+ }
246
+
247
+ easement = {
248
+ 'property': 'easement',
249
+ 'type': 'categorical',
250
+ 'stops': [
251
+ ['True', private_access_color],
252
+ ['False', public_access_color]
253
+ ]
254
+ }
255
+
256
+ year = {
257
+ 'property': 'established',
258
+ 'type': 'categorical',
259
+ 'stops': [
260
+ ['pre-2024', year2023_color],
261
+ ['2024', year2024_color]
262
+ ]
263
+ }
264
+
265
+ access = {
266
+ 'property': 'access_type',
267
+ 'type': 'categorical',
268
+ 'stops': [
269
+ ['Open Access', public_access_color],
270
+ ['No Public Access', private_access_color],
271
+ ['Unknown Access', "#bbbbbb"],
272
+ ['Restricted Access', tribal_color]
273
+ ]
274
+ }
275
+
276
+ gap = {
277
+ 'property': 'reGAP',
278
+ 'type': 'categorical',
279
+ 'stops': [
280
+ [1, "#26633d"],
281
+ [2, "#879647"],
282
+ [3, "#EE4B2B"],
283
+ [4, "#BF40BF"]
284
+ ]
285
+ }
286
+
287
+ style_options = {
288
+ "Year": year,
289
+ "GAP Status Code": gap,
290
+ "Manager Type": manager,
291
+ "Easement": easement,
292
+ "Access Type": access,
293
+ }
294
+
295
+ justice40_fill = {
296
+ 'property': 'Disadvan',
297
+ 'type': 'categorical',
298
+ 'stops': [
299
+ [0, white],
300
+ [1, justice40_color]
301
+ ]
302
+ }
303
+
304
+ justice40_style = {
305
+ "version": 8,
306
+ "sources": {
307
+ "source1": {
308
+ "type": "vector",
309
+ "url": "pmtiles://" + url_justice40,
310
+ "attribution": "Justice40"
311
+ }
312
+ },
313
+ "layers": [
314
+ {
315
+ "id": "layer1",
316
+ "source": "source1",
317
+ "source-layer": "DisadvantagedCommunitiesCEJST",
318
+ "filter": ["match", ["get", "StateName"], "California", True, False],
319
+ "type": "fill",
320
+ "paint": {
321
+ "fill-color": justice40_fill,
322
+ }
323
+ }
324
+ ]
325
+ }
326
+
327
+ def fire_style(layer):
328
+ return {"version": 8,
329
+ "sources": {
330
+ "source1": {
331
+ "type": "vector",
332
+ "url": "pmtiles://" + url_calfire,
333
+ "attribution": "CAL FIRE"
334
+ }
335
+ },
336
+ "layers": [
337
+ {
338
+ "id": "fire",
339
+ "source": "source1",
340
+ "source-layer": layer,
341
+ "type": "fill",
342
+ "paint": {
343
+ "fill-color": "#D22B2B",
344
+ }
345
+ }
346
+ ]
347
+ }
348
+ def rx_style(layer):
349
+ return{
350
+ "version": 8,
351
+ "sources": {
352
+ "source2": {
353
+ "type": "vector",
354
+ "url": "pmtiles://" + url_rxburn,
355
+ "attribution": "CAL FIRE"
356
+ }
357
+ },
358
+ "layers": [
359
+ {
360
+ "id": "fire",
361
+ "source": "source2",
362
+ "source-layer": layer,
363
+ # "filter": [">=", ["get", "YEAR_"], year],
364
+ "type": "fill",
365
+ "paint": {
366
+ "fill-color": "#702963",
367
+ }
368
+ }
369
+ ]
370
+ }
371
+
372
+ def get_sv_style(column):
373
+ return {
374
+ "layers": [
375
+ {
376
+ "id": "SVI",
377
+ "source": column, #need different "source" for multiple pmtiles layers w/ same file
378
+ "source-layer": "SVI2020_US_county",
379
+ "filter": ["match", ["get", "STATE"], "California", True, False],
380
+ "type": "fill",
381
+ "paint": {
382
+ "fill-color": [
383
+ "interpolate", ["linear"], ["get", column],
384
+ 0, white,
385
+ 1, svi_color
386
+ ]
387
+ }
388
+ }
389
+ ]
390
+ }
391
+
392
+
393
+ def get_pmtiles_style(paint, alpha, filter_cols, filter_vals):
394
+ filters = []
395
+ for col, val in zip(filter_cols, filter_vals):
396
+ filters.append(["match", ["get", col], val, True, False])
397
+ combined_filters = ["all"] + filters
398
+ style = {
399
+ "version": 8,
400
+ "sources": {
401
+ "ca": {
402
+ "type": "vector",
403
+ "url": "pmtiles://" + ca_pmtiles,
404
+ }
405
+ },
406
+ "layers": [
407
+ {
408
+ "id": "ca30x30",
409
+ "source": "ca",
410
+ "source-layer": "layer",
411
+ "type": "fill",
412
+ "filter": combined_filters,
413
+ "paint": {
414
+ "fill-color": paint,
415
+ "fill-opacity": alpha
416
+ }
417
+ }
418
+ ]
419
+ }
420
+ return style
421
+
422
+ st.set_page_config(layout="wide", page_title="CA Protected Areas Explorer", page_icon=":globe:")
423
+
424
+ #customizing style with CSS
425
+ st.markdown(
426
+ """
427
+ <style>
428
+ /* Customizing font size for radio text */
429
+ div[class*="stRadio"] > label > div[data-testid="stMarkdownContainer"] > p {
430
+ font-size: 18px;
431
+ }
432
+ /* Reduce margin below the header */
433
+ h2 {
434
+ margin-top: 0rem !important;
435
+ margin-bottom: 0rem !important; /* Reduce space below headers */
436
+
437
+ }
438
+ /* Customizing font size for medium-font class */
439
+ .medium-font {
440
+ font-size: 18px !important;
441
+ margin-top: 0rem !important;
442
+ margin-bottom: 0.25rem !important; /* Reduce space below */
443
+ }
444
+ .medium-font-sidebar {
445
+ font-size: 18px;
446
+ }
447
+ /* Customizing layout and divider */
448
+ hr {
449
+ margin-top: 0rem !important; /* Adjust to reduce top margin */
450
+ margin-bottom: 0.5rem !important; /* Adjust to reduce bottom margin */
451
+ }
452
+ .stAppHeader {
453
+ background-color: rgba(255, 255, 255, 0.0); /* Transparent background */
454
+ visibility: visible; /* Ensure the header is visible */
455
+ }
456
+ .block-container {
457
+ padding-top: 0.5rem;
458
+ padding-bottom: 0rem;
459
+ padding-left: 5rem;
460
+ padding-right: 5rem;
461
+ }
462
+ /* Reduce whitespace for the overall expander container */
463
+ .st-expander {
464
+ margin-top: 0rem; /* Space above the expander */
465
+ margin-bottom: 0rem; /* Space below the expander */
466
+ }
467
+ /* Adjust padding for the content inside the expander */
468
+ .st-expander-content {
469
+ padding: 0rem 0rem; /* Reduce padding inside */
470
+ }
471
+ /* Optional: Adjust the expander header if needed */
472
+ .st-expander-header {
473
+ margin-top: 0rem;
474
+ margin-bottom: 0rem;
475
+ }
476
+ </style>
477
+ """,
478
+ unsafe_allow_html=True,
479
+ )
480
+
481
+ st.markdown(
482
+ """
483
+ <style>
484
+ /* Remove or reduce whitespace at the top of the sidebar */
485
+ [data-testid="stSidebar"] > div:first-child {
486
+ padding-top: 0rem !important;
487
+ }
488
+ </style>
489
+ """,
490
+ unsafe_allow_html=True,
491
+ )
492
+
493
+ # st.header("CA 30x30 Planning & Assessment Prototype")
494
+ st.markdown("<h2>CA 30x30 Planning & Assessment Prototype</h2>", unsafe_allow_html=True)
495
+
496
+ st.markdown('<p class = "medium-font"> An interactive cloud-native geospatial tool for exploring and visualizing California\'s protected lands with open data and generative AI. </p>', unsafe_allow_html = True)
497
+
498
+
499
+ '''
500
+ - ❌ Safari/iOS not yet supported. For Safari/iOS users, try [this version](https://huggingface.co/spaces/boettiger-lab/ca-30x30-folium) with similar functionality.
501
+ - 📊 Use the left sidebar to color-code the map by different attributes **(Group by)**, toggle on data layers and view summary charts **(Data Layers)**, or filter data **(Filters)**.
502
+ - 💬 For a more tailored experience, query our dataset of protected areas and their precomputed mean values for each of the displayed layers, using the experimental chatbot below.
503
+ '''
504
+
505
+ st.divider()
506
+
507
+
508
+ m = leafmap.Map(style="positron")
509
+ #############
510
+
511
+
512
+ def get_pmtiles_style_llm(paint, ids):
513
+ combined_filters = ["all", ["match", ["get", "id"], ids, True, False]]
514
+ style = {
515
+ "version": 8,
516
+ "sources": {
517
+ "ca": {
518
+ "type": "vector",
519
+ "url": "pmtiles://" + ca_pmtiles,
520
+ }
521
+ },
522
+ "layers": [
523
+ {
524
+ "id": "ca30x30",
525
+ "source": "ca",
526
+ "source-layer": "layer",
527
+ "type": "fill",
528
+ "filter": combined_filters,
529
+ "paint": {
530
+ "fill-color": paint,
531
+ "fill-opacity": 1,
532
+ # "fill-extrusion-height": 1000
533
+ }
534
+ }
535
+ ]
536
+ }
537
+ return style
538
+
539
+ ##### Chatbot stuff
540
+
541
+
542
+ # create sharable low-level connection, see: https://github.com/Mause/duckdb_engine
543
+ import sqlalchemy
544
+
545
+ connect_args = {'preload_extensions':['spatial']}
546
+ eng = sqlalchemy.create_engine("duckdb:///:memory:",connect_args = connect_args)
547
+
548
+ # ibis can talk to this connection and create the VIEW
549
+ con = ibis.duckdb.from_connection(eng.raw_connection())
550
+ tbl = con.read_parquet(ca_parquet, "mydata")
551
+
552
+ # langchain can also talk to this connection and see the table:
553
+ from langchain_community.utilities import SQLDatabase
554
+ db = SQLDatabase(eng, view_support=True)
555
+
556
+
557
+ from pydantic import BaseModel, Field
558
+ class SQLResponse(BaseModel):
559
+ """Defines the structure for SQL response."""
560
+ sql_query: str = Field(description="The SQL query generated by the assistant.")
561
+ explanation: str = Field(description="A detailed explanation of how the SQL query answers the input question.")
562
+
563
+
564
+ from langchain.chains import create_sql_query_chain
565
+ template = '''You are an expert in SQL and an assistant for mapping and analyzing California land data. Given an input question, create a syntactically correct {dialect} query to run, and then provide an explanation of how you answered the input question.
566
+
567
+ For example:
568
+ {{
569
+ "sql_query": "SELECT * FROM my_table WHERE condition = 'value';",
570
+ "explanation": "This query retrieves all rows from my_table where the condition column equals 'value'."
571
+ }}
572
+
573
+ Ensure the response contains only this JSON object, with no additional text, formatting, or commentary.
574
+
575
+ # Important Details
576
+
577
+ - For map-related queries (e.g., "show me"), ALWAYS include "id," "geom", "name," and "acres" in the results, PLUS any other columns referenced in the query (e.g., in conditions, calculations, or subqueries). This output structure is MANDATORY for all map-related queries.
578
+ - ONLY use LIMIT in your SQL queries if the user specifies a quantity (e.g., 'show me 5'). Otherwise, return all matching data without a limit.
579
+ - Wrap each column name in double quotes (") to denote them as delimited identifiers.
580
+ - Pay attention to use only the column names you can see in the tables below. DO NOT query for columns that do not exist.
581
+ If the query mentions "biodiversity" without specifying a column, default to using "richness" (species richness). Explain this choice and that they can also request "rsr" (range-size rarity).
582
+ - If the query mentions carbon without specifying a column, use "irrecoverable carbon". Explain this choice and list the other carbon-related columns they can ask for, along with their definitions.
583
+ - If the query asks about the manager, use the "manager" column. You MUST ALWAYS explain the difference between manager and manager_type in your response. Clarify that "manager" refers to the name of the managing entity (e.g., an agency), while "manager_type" specifies the type of jurisdiction (e.g., Federal, State, Non Profit). Also, let the user know they can include "manager_type" in their query if they want to refine their results.
584
+ - If the user's query is unclear, DO NOT make assumptions. Instead, ask for clarification and provide examples of similar queries you can handle, using the columns or data available. You MUST ONLY deliver accurate results.
585
+ - If you are mapping the data, explicitly state that the data is being visualized on a map. ALWAYS include a statement encouraging the user to examine the queried data below the map, as some areas may be too small at the current zoom level.
586
+ - Users may not be familiar with this data, so your explanation should be short, clear, and easily understandable. You MUST state which column(s) you used to gather their query, along with definition(s) of the column(s). Do NOT explain SQL commands.
587
+ - If the prompt is unrelated to the California dataset, provide examples of relevant queries that you can answer.
588
+
589
+ # Example Questions and How to Approach Them
590
+
591
+ ## Example:
592
+ example_user: "Show me all non-profit land."
593
+ example_assistant: {{"sql_query":
594
+ SELECT id, geom, name, acres
595
+ FROM mydata
596
+ WHERE "manager_type" = "Non Profit";
597
+ "explanation":"I selected all data where `manager_type` is 'Non Profit'."
598
+ }}
599
+
600
+ ## Example:
601
+ example_user: "Which gap code has been impacted the most by fire?"
602
+ example_assistant: {{"sql_query":
603
+ SELECT "reGAP", SUM("percent_fire_10yr") AS temp
604
+ FROM mydata
605
+ GROUP BY "reGAP"
606
+ ORDER BY temp ASC
607
+ LIMIT 1;
608
+ "explanation":"I used the `percent_fire_10yr` column, which shows the percentage of each area burned over the past 10 years (2013–2022), summing it for each GAP code to find the one with the highest total fire impact."
609
+ }}
610
+
611
+ ## Example:
612
+ example_user: "Who manages the land with the worst biodiversity and highest SVI?"
613
+ example_assistant: {{"sql_query":
614
+ SELECT manager,richness, svi
615
+ FROM mydata
616
+ GROUP BY "manager"
617
+ ORDER BY richness ASC, svi DESC
618
+ LIMIT 1;
619
+ "explanation": "I identified the land manager with the worst biodiversity and highest Social Vulnerability Index (SVI) by analyzing the columns: `richness`, which measures species richness, and `svi`, which represents social vulnerability based on factors like socioeconomic status, household characteristics, racial & ethnic minority status, and housing & transportation.
620
+
621
+ I sorted the data by richness in ascending order (worst biodiversity first) and svi in descending order (highest vulnerability). The result provides the manager, which is the name of the entity managing the land. Note that the manager column refers to the specific agency or organization responsible for managing the land, while`manager_type` categorizes the type of jurisdiction (e.g., Federal, State, Non Profit)."
622
+ }}
623
+
624
+
625
+ ## Example:
626
+ example_user: "Show me the biggest protected area"
627
+ example_assistant: {{"sql_query":
628
+ SELECT "id", "geom", "name", "acres", "manager", "manager_type", "acres"
629
+ FROM mydata
630
+ ORDER BY "acres" DESC
631
+ LIMIT 1;
632
+ "explanation": "I identified the biggest protected area by sorting the data in descending order based on the `acres` column, which represents the size of each area."
633
+
634
+ ## Example:
635
+ example_user: "Show me the 50 most biodiverse areas found in disadvantaged communities."
636
+ example_assistant: {{"sql_query":
637
+ SELECT "id", "geom", "name", "acres", "richness", "percent_disadvantaged" FROM mydata
638
+ WHERE "percent_disadvantaged" > 0
639
+ ORDER BY "richness" DESC
640
+ LIMIT 50;
641
+ "explanation": "I used the `richness` column to measure biodiversity and the `percent_disadvantaged` column to identify areas located in disadvantaged communities. The `percent_disadvantaged` value is derived from the Justice40 initiative, which identifies communities burdened by systemic inequities and vulnerabilities across multiple domains, including climate resilience, energy access, health disparities, housing affordability, pollution exposure, transportation infrastructure, water quality, and workforce opportunities.
642
+
643
+ The results are sorted in descending order by biodiversity richness (highest biodiversity first), and only areas with a `percent_disadvantaged` value greater than 0 (indicating some portion of the area overlaps with a disadvantaged community) are included."
644
+ }}
645
+
646
+
647
+ ## Example:
648
+ example_user: "Show me federally managed gap 3 lands that are in the top 5% of biodiversity richness and have experienced forest fire over at least 50% of their area"
649
+ sql_query:
650
+ WITH temp_tab AS (
651
+ SELECT PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY "richness") AS temp
652
+ FROM mydata
653
+ )
654
+ SELECT "id", "geom", "name", "acres","richness", "reGAP"
655
+ FROM mydata
656
+ WHERE "reGAP" = 3
657
+ AND "percent_fire_10yr" >= 0.5
658
+ and "manager_type" = "Federal"
659
+ AND "richness" > (SELECT temp FROM temp_tab);
660
+
661
+
662
+ ## Example:
663
+ example_user: "What is the total acreage of areas designated as easements?
664
+ sql_query:
665
+ SELECT SUM("acres") AS total_acres
666
+ FROM mydata
667
+ WHERE "easement" = "True";
668
+
669
+
670
+ # Detailed Explanation of the Columns in the California Dataset
671
+ - "established": The time range which the land was acquired, either "2024" or "pre-2024".
672
+ - "reGAP": The GAP status code; corresponds to the level of protection the area has. There are 4 gap codes and are defined as the following.
673
+ Status 1: Permanently protected to maintain a natural state, allowing natural disturbances or mimicking them through management.
674
+ Status 2: Permanently protected but may allow some uses or management practices that degrade natural communities or suppress natural disturbances.
675
+ Status 3: Permanently protected from major land cover conversion but allows some extractive uses (e.g., logging, mining) and protects federally listed species.
676
+ Status 4: No protection mandates; land may be converted to unnatural habitat types or its management intent is unknown.
677
+
678
+ - "name": The name of a protected area. The user may use a shortened name and/or not capitalize it. For example, "redwoods" may refer to "Redwood National Park", or "klamath" refers to "Klamath National Forest". Another example, "san diego wildlife refuge" could refer to multiple areas, so you would use "WHERE LOWER("name") LIKE '%san diego%' AND LOWER("name") LIKE '%wildlife%' AND LOWER("name") LIKE '%refuge%';" in your SQL query, to ensure that it is case-insensitive and matches any record that includes our phrases, because we don't want to overlook a match. If the name isn't capitalized, you MUST ensure the search is case-insensitive by converting "name" to lowercase.
679
+ The names of the largest parks are {names}.
680
+ - "access_type": Level of access to the land: "Unknown Access","Restricted Access","No Public Access" and "Open Access".
681
+ - "manager": The name of land manager for the area. Also referred to as the agency name. These are the manager names: {managers}. Users might use acronyms or could omit "United States" in the agency name, make sure to use the name used in the table. Some examples: "BLM" or "Bureau of Land Management" refers to the "United States Bureau of Land Management" or "CDFW" is "California Department of Fish and Wildlife". Similar to the "name" field, you can search for managers using "LIKE" in the SQL query.
682
+ - "manager_type": The jurisdiction of the land manager: "Federal","State","Non Profit","Special District","Unknown","County","City","Joint","Tribal","Private","HOA". If the user says "non-profit", do not use a hyphen in your query.
683
+ - "easement": Boolean value; whether or not the land is an easement.
684
+ - "acres": Land acreage; measures the size of the area.
685
+ - "id": unique id for each area. This is necessary for displaying queried results on a map.
686
+ - "type": Physical type of area, either "Land" or "Water".
687
+ - "richness": Species richness; higher values indicate better biodiversity.
688
+ - "rsr": Range-size rarity; higher values indicate better rarity metrics.
689
+ - "svi": Social Vulnerability Index based on 4 themes: socioeconomic status, household characteristics, racial & ethnic minority status, and housing & transportation. Higher values indicate greater vulnerability.
690
+ - Themes:
691
+ - "svi_socioeconomic_status": Poverty, unemployment, housing cost burden, education, and health insurance.
692
+ - "svi_household_char": Age, disability, single-parent households, and language proficiency.
693
+ - "svi_racial_ethnic_minority": Race and ethnicity variables.
694
+ - "svi_housing_transit": Housing type, crowding, vehicles, and group quarters.
695
+ - "percent_disadvantaged": Justice40-defined disadvantaged communities overburdened by climate, energy, health, housing, pollution, transportation, water, and workforce factors. Higher values indicate more disadvantage. Range is between 0 and 1.
696
+ - "deforest_carbon": Carbon emissions due to deforestation.
697
+ - "human_impact": A score representing the human footprint: cumulative anthropogenic impacts such as land cover change, population density, and infrastructure.
698
+ - "percent_fire_10yr": The percentage of the area burned by fires from (2013-2022). Range is between 0 and 1.
699
+ - "percent_rxburn_10yr": The percentage of the area affected by prescribed burns from (2013-2022). Range is between 0 and 1.
700
+
701
+ Only use the following tables:
702
+ {table_info}.
703
+
704
+ Question: {input}'''
705
+
706
+ from langchain_openai import ChatOpenAI
707
+ # os.environ["OPENAI_API_KEY"] = st.secrets["LITELLM_KEY"]
708
+ # llm = ChatOpenAI(model="gorilla", temperature=0, base_url="https://llm.nrp-nautilus.io/")
709
+ # llm = ChatOpenAI(model = "llama3", api_key=st.secrets["LITELLM_KEY"], base_url = "https://llm.nrp-nautilus.io", temperature=0)
710
+
711
+ llm = ChatOpenAI(model="gpt-4", temperature=0)
712
+
713
+ managers = tbl.sql("SELECT DISTINCT manager FROM mydata;").execute()
714
+ names = tbl.sql("SELECT name FROM mydata GROUP BY name HAVING SUM(acres) >10000;").execute()
715
+
716
+ from langchain_core.prompts import ChatPromptTemplate
717
+ prompt = ChatPromptTemplate.from_messages([
718
+ ("system", template),
719
+ ("human", "{input}")
720
+ ]).partial(dialect="duckdb", table_info = ca.schema(), managers = managers, names = names)
721
+
722
+ structured_llm = llm.with_structured_output(SQLResponse)
723
+ few_shot_structured_llm = prompt | structured_llm
724
+
725
+ # @st.cache_data(ttl=600) # Cache expires every 10 minutes
726
+ def run_sql(query,color_choice):
727
+ """
728
+ Filter data based on an LLM-generated SQL query and return matching IDs.
729
+
730
+ Args:
731
+ query (str): The natural language query to filter the data.
732
+ color_choice (str): The column used for plotting.
733
+ """
734
+ output = few_shot_structured_llm.invoke(query)
735
+ sql_query = output.sql_query
736
+ explanation =output.explanation
737
+
738
+ if not sql_query: # if the chatbot can't generate a SQL query.
739
+ st.success(explanation)
740
+ return pd.DataFrame({'id' : []})
741
+
742
+
743
+ result = tbl.sql(sql_query).execute()
744
+ if result.empty :
745
+ explanation = "This query did not return any results. Please try again with a different query."
746
+ st.warning(explanation, icon="⚠️")
747
+ st.caption("SQL Query:")
748
+ st.code(sql_query,language = "sql")
749
+ if 'geom' in result.columns:
750
+ return result.drop('geom',axis = 1)
751
+ else:
752
+ return result
753
+
754
+ elif ("id" and "geom" in result.columns):
755
+ style = get_pmtiles_style_llm(style_options[color_choice], result["id"].tolist())
756
+ legend_d = {cat: color for cat, color in style_options[color_choice]['stops']}
757
+ m.add_legend(legend_dict=legend_d, position='bottom-left')
758
+ m.add_pmtiles(ca_pmtiles, style=style, opacity=alpha, tooltip=True, fit_bounds=True)
759
+ m.fit_bounds(result.total_bounds.tolist())
760
+ result = result.drop('geom',axis = 1) #printing to streamlit so I need to drop geom
761
+ else:
762
+ st.write(result) # if we aren't mapping, just print out the data
763
+
764
+ with st.popover("Explanation"):
765
+ st.write(explanation)
766
+ st.caption("SQL Query:")
767
+ st.code(sql_query,language = "sql")
768
+
769
+ return result
770
+
771
+
772
+
773
+ def summary_table_sql(column, colors, ids): # get df for charts + df_tab for printed table
774
+ filters = [_.id.isin(ids)]
775
+ combined_filter = reduce(lambda x, y: x & y, filters) #combining all the filters into ibis filter expression
776
+ df = get_summary(ca, combined_filter, [column], colors) # df used for charts
777
+ return df
778
+
779
+
780
+
781
+
782
+ chatbot_toggles = {key: False for key in [
783
+ 'richness', 'rsr', 'irrecoverable_carbon', 'manageable_carbon',
784
+ 'percent_fire_10yr', 'percent_rxburn_10yr', 'percent_disadvantaged',
785
+ 'svi', 'svi_socioeconomic_status', 'svi_household_char',
786
+ 'svi_racial_ethnic_minority', 'svi_housing_transit',
787
+ 'deforest_carbon', 'human_impact'
788
+ ]}
789
+
790
+
791
+ #############
792
+
793
+
794
+ filters = {}
795
+
796
+ with st.sidebar:
797
+
798
+ color_choice = st.radio("Group by:", style_options, key = "color", help = "Select a category to change map colors and chart groupings.")
799
+ colorby_vals = getColorVals(style_options, color_choice) #get options for selected color_by column
800
+ # alpha = st.slider("transparency", 0.0, 1.0, 0.7)
801
+ alpha = 0.8
802
+ st.divider()
803
+
804
+
805
+
806
+ ##### Chatbot
807
+ with st.container():
808
+
809
+ with st.popover("ℹ️ Example Queries"):
810
+ '''
811
+ Mapping queries:
812
+ - Show me areas open to the public that are in the top 10% of species richness.
813
+ - Show me all GAP 1 and 2 lands managed by The Nature Conservancy.
814
+ - Show me state land smaller than 1000 acres, with a social vulnerability index in the 90th percentile.
815
+ - Show me GAP 3 and 4 lands managed by BLM in the top 5% of range-size rarity.
816
+ - Show me Joshua Tree National Park.
817
+ - Show me all protected lands that have experienced forest fire over at least 50% of their area.
818
+ - Show me the biggest protected area in California.
819
+ - Show me all land managed by the United States Forest Service.
820
+ '''
821
+
822
+ '''
823
+ Exploratory data queries:
824
+ - What is a GAP code?
825
+ - What is the total acreage of areas designated as easements?
826
+ - Which GAP code has been impacted the most by fire?
827
+ - Who manages the land with the highest amount of irrecoverable carbon and highest social vulnerability index?
828
+ '''
829
+
830
+ st.info('If the map appears blank, queried data may be too small to see at the default zoom level. Check the table below the map, as query results will also be displayed there.', icon="ℹ️")
831
+
832
+
833
+ example_query = "👋 Input query here"
834
+ if prompt := st.chat_input(example_query, key="chain", max_chars = 300):
835
+ st.chat_message("user").write(prompt)
836
+
837
+ try:
838
+ with st.chat_message("assistant"):
839
+ with st.spinner("Invoking query..."):
840
+
841
+ out = run_sql(prompt,color_choice)
842
+ if ("id" in out.columns) and (not out.empty):
843
+ ids = out['id'].tolist()
844
+ cols = out.columns.tolist()
845
+ chatbot_toggles = {
846
+ key: (True if key in cols else value)
847
+ for key, value in chatbot_toggles.items()
848
+ }
849
+ else:
850
+ ids = []
851
+ except Exception as e:
852
+ error_message = f"ERROR: An unexpected error has occured with the following query:\n\n*{prompt}*\n\n which raised the following error:\n\n{type(e)}: {e}\n"
853
+ st.warning("Please try again with a different query", icon="⚠️")
854
+ st.write(error_message)
855
+ st.stop()
856
+
857
+
858
+
859
+ #### Data layers
860
+ with st.sidebar:
861
+ st.markdown('<p class = "medium-font-sidebar"> Data Layers:</p>', help = "Select data layers to visualize on the map. Summary charts will update based on the displayed layers.", unsafe_allow_html= True)
862
+ # Biodiversity Section
863
+ with st.expander("🦜 Biodiversity"):
864
+ a_bio = st.slider("transparency", 0.0, 1.0, 0.1, key = "biodiversity")
865
+ show_richness = st.toggle("Species Richness", key = "richness", value=chatbot_toggles['richness'])
866
+ show_rsr = st.toggle("Range-Size Rarity", key = "rsr", value=chatbot_toggles['rsr'])
867
+
868
+ if show_richness:
869
+ m.add_tile_layer(url_sr, name="MOBI Species Richness",opacity=a_bio)
870
+
871
+ if show_rsr:
872
+ m.add_tile_layer(url_rsr, name="MOBI Range-Size Rarity", opacity=a_bio)
873
+
874
+ #Carbon Section
875
+ with st.expander("⛅ Carbon & Climate"):
876
+ a_climate = st.slider("transparency", 0.0, 1.0, 0.15, key = "climate")
877
+ show_irrecoverable_carbon = st.toggle("Irrecoverable Carbon", key = "irrecoverable_carbon", value=chatbot_toggles['irrecoverable_carbon'])
878
+ show_manageable_carbon = st.toggle("Manageable Carbon", key = "manageable_carbon", value=chatbot_toggles['manageable_carbon'])
879
+
880
+ if show_irrecoverable_carbon:
881
+ m.add_cog_layer(url_irr_carbon, palette="reds", name="Irrecoverable Carbon", opacity = a_climate, fit_bounds=False)
882
+
883
+ if show_manageable_carbon:
884
+ m.add_cog_layer(url_man_carbon, palette="purples", name="Manageable Carbon", opacity = a_climate, fit_bounds=False)
885
+
886
+
887
+ # Justice40 Section
888
+ with st.expander("🌱 Climate & Economic Justice"):
889
+ a_justice = st.slider("transparency", 0.0, 1.0, 0.07, key = "social justice")
890
+ show_justice40 = st.toggle("Disadvantaged Communities (Justice40)", key = "percent_disadvantaged", value=chatbot_toggles['percent_disadvantaged'])
891
+
892
+ if show_justice40:
893
+ m.add_pmtiles(url_justice40, style=justice40_style, name="Justice40", opacity=a_justice, tooltip=False, fit_bounds = False)
894
+
895
+ # SVI Section
896
+ with st.expander("🏡 Social Vulnerability"):
897
+ a_svi = st.slider("transparency", 0.0, 1.0, 0.1, key = "SVI")
898
+ show_sv = st.toggle("Social Vulnerability Index (SVI)", key = "svi", value=chatbot_toggles['svi'])
899
+ show_sv_socio = st.toggle("Socioeconomic Status", key = "svi_socioeconomic_status", value=chatbot_toggles['svi_socioeconomic_status'])
900
+ show_sv_household = st.toggle("Household Characteristics", key = "svi_household_char", value=chatbot_toggles['svi_household_char'])
901
+ show_sv_minority = st.toggle("Racial & Ethnic Minority Status", key = "svi_racial_ethnic_minority", value=chatbot_toggles['svi_racial_ethnic_minority'])
902
+ show_sv_housing = st.toggle("Housing Type & Transportation", key = "svi_housing_transit", value=chatbot_toggles['svi_housing_transit'])
903
+
904
+ if show_sv:
905
+ m.add_pmtiles(url_svi, style = get_sv_style("RPL_THEMES"), opacity=a_svi, tooltip=False, fit_bounds = False)
906
+
907
+ if show_sv_socio:
908
+ m.add_pmtiles(url_svi, style = get_sv_style("RPL_THEME1"), opacity=a_svi, tooltip=False, fit_bounds = False)
909
+
910
+ if show_sv_household:
911
+ m.add_pmtiles(url_svi, style = get_sv_style("RPL_THEME2"), opacity=a_svi, tooltip=False, fit_bounds = False)
912
+
913
+ if show_sv_minority:
914
+ m.add_pmtiles(url_svi, style = get_sv_style("RPL_THEME3"), opacity=a_svi, tooltip=False, fit_bounds = False)
915
+
916
+ if show_sv_housing:
917
+ m.add_pmtiles(url_svi, style = get_sv_style("RPL_THEME4"), opacity=a_svi, tooltip=False, fit_bounds = False)
918
+
919
+ # Fire Section
920
+ with st.expander("🔥 Fire"):
921
+ a_fire = st.slider("transparency", 0.0, 1.0, 0.15, key = "fire")
922
+ show_fire_10 = st.toggle("Fires (2013-2022)", key = "percent_fire_10yr", value=chatbot_toggles['percent_fire_10yr'])
923
+
924
+ show_rx_10 = st.toggle("Prescribed Burns (2013-2022)", key = "percent_rxburn_10yr", value=chatbot_toggles['percent_rxburn_10yr'])
925
+
926
+
927
+ if show_fire_10:
928
+ m.add_pmtiles(url_calfire, style=fire_style("layer2"), name="CALFIRE Fire Polygons (2013-2022)", opacity=a_fire, tooltip=False, fit_bounds = True)
929
+
930
+ if show_rx_10:
931
+ m.add_pmtiles(url_rxburn, style=rx_style("layer2"), name="CAL FIRE Prescribed Burns (2013-2022)", opacity=a_fire, tooltip=False, fit_bounds = True)
932
+
933
+
934
+ # HI Section
935
+ with st.expander("🚜 Human Impacts"):
936
+ a_hi = st.slider("transparency", 0.0, 1.0, 0.1, key = "hi")
937
+ show_carbon_lost = st.toggle("Deforested Carbon", key = "deforest_carbon", value=chatbot_toggles['deforest_carbon'])
938
+ show_human_impact = st.toggle("Human Footprint", key = "human_impact", value=chatbot_toggles['human_impact'])
939
+
940
+ if show_carbon_lost:
941
+ m.add_tile_layer(url_loss_carbon, name="Deforested Carbon (2002-2022)", opacity = a_hi)
942
+
943
+ if show_human_impact:
944
+ m.add_cog_layer(url_hi, name="Human Footprint (2017-2021)", opacity = a_hi, fit_bounds=False)
945
+
946
+ st.divider()
947
+ st.markdown('<p class = "medium-font-sidebar"> Filters:</p>', help = "Apply filters to adjust what data is shown on the map.", unsafe_allow_html= True)
948
+ for label in style_options: # get selected filters (based on the buttons selected)
949
+ with st.expander(label):
950
+ if label == "GAP Status Code": # gap code 1 and 2 are on by default
951
+ opts = getButtons(style_options, label, default_gap)
952
+ else: # other buttons are not on by default.
953
+ opts = getButtons(style_options, label)
954
+ filters.update(opts)
955
+
956
+ selected = {k: v for k, v in filters.items() if v}
957
+ if selected:
958
+ filter_cols = list(selected.keys())
959
+ filter_vals = list(selected.values())
960
+ else:
961
+ filter_cols = []
962
+ filter_vals = []
963
+
964
+
965
+ # Display CA 30x30 Data
966
+ if 'out' not in locals():
967
+ style = get_pmtiles_style(style_options[color_choice], alpha, filter_cols, filter_vals)
968
+ legend_d = {cat: color for cat, color in style_options[color_choice]['stops']}
969
+ m.add_legend(legend_dict = legend_d, position = 'bottom-left')
970
+ m.add_pmtiles(ca_pmtiles, style=style, name="CA", opacity=alpha, tooltip=True, fit_bounds = True)
971
+
972
+
973
+ select_column = {
974
+ "Year": "established",
975
+ "GAP Status Code": "reGAP",
976
+ "Manager Type": "manager_type",
977
+ "Easement": "easement",
978
+ "Access Type": "access_type",
979
+ }
980
+
981
+ column = select_column[color_choice]
982
+
983
+ select_colors = {
984
+ "Year": year["stops"],
985
+ "GAP Status Code": gap["stops"],
986
+ "Manager Type": manager["stops"],
987
+ "Easement": easement["stops"],
988
+ "Access Type": access["stops"],
989
+ }
990
+
991
+ colors = (
992
+ ibis
993
+ .memtable(select_colors[color_choice], columns=[column, "color"])
994
+ .to_pandas()
995
+ )
996
+
997
+ # get summary tables used for charts + printed table
998
+ # df - charts; df_tab - printed table (omits colors)
999
+ if 'out' not in locals():
1000
+ df,df_tab = summary_table(column, colors, filter_cols, filter_vals, colorby_vals)
1001
+ else:
1002
+ df = summary_table_sql(column, colors, ids)
1003
+
1004
+ total_percent = df.percent_protected.sum().round(2)
1005
+
1006
+
1007
+ # charts displayed based on color_by variable
1008
+ richness_chart = bar_chart(df, column, 'mean_richness', "Species Richness")
1009
+ rsr_chart = bar_chart(df, column, 'mean_rsr', "Range-Size Rarity")
1010
+ irr_carbon_chart = bar_chart(df, column, 'mean_irrecoverable_carbon', "Irrecoverable Carbon")
1011
+ man_carbon_chart = bar_chart(df, column, 'mean_manageable_carbon', "Manageable Carbon")
1012
+ fire_10_chart = bar_chart(df, column, 'mean_percent_fire_10yr', "Fires (2013-2022)")
1013
+ rx_10_chart = bar_chart(df, column, 'mean_percent_rxburn_10yr',"Prescribed Burns (2013-2022)")
1014
+ justice40_chart = bar_chart(df, column, 'mean_percent_disadvantaged', "Disadvantaged Communities (Justice40)")
1015
+ svi_chart = bar_chart(df, column, 'mean_svi', "Social Vulnerability Index")
1016
+ svi_socio_chart = bar_chart(df, column, 'mean_svi_socioeconomic_status', "SVI - Socioeconomic Status")
1017
+ svi_house_chart = bar_chart(df, column, 'mean_svi_household_char', "SVI - Household Characteristics")
1018
+ svi_minority_chart = bar_chart(df, column, 'mean_svi_racial_ethnic_minority', "SVI - Racial and Ethnic Minority")
1019
+ svi_transit_chart = bar_chart(df, column, 'mean_svi_housing_transit', "SVI - Housing Type and Transit")
1020
+ carbon_loss_chart = bar_chart(df, column, 'mean_carbon_lost', "Deforested Carbon (2002-2022)")
1021
+ hi_chart = bar_chart(df, column, 'mean_human_impact', "Human Footprint (2017-2021)")
1022
+
1023
+
1024
+ main = st.container()
1025
+
1026
+ with main:
1027
+ map_col, stats_col = st.columns([2,1])
1028
+
1029
+ with map_col:
1030
+ m.to_streamlit(height=650)
1031
+ if 'out' not in locals():
1032
+ st.dataframe(df_tab, use_container_width = True)
1033
+ else:
1034
+ st.dataframe(out, use_container_width = True)
1035
+
1036
+ with stats_col:
1037
+ with st.container():
1038
+
1039
+ st.markdown(f"{total_percent}% CA Covered", help = "Updates based on displayed data")
1040
+ st.altair_chart(area_plot(df, column), use_container_width=True)
1041
+
1042
+ if show_richness:
1043
+ # "Species Richness"
1044
+ st.altair_chart(richness_chart, use_container_width=True)
1045
+
1046
+ if show_rsr:
1047
+ # "Range-Size Rarity"
1048
+ st.altair_chart(rsr_chart, use_container_width=True)
1049
+
1050
+ if show_irrecoverable_carbon:
1051
+ # "Irrecoverable Carbon"
1052
+ st.altair_chart(irr_carbon_chart, use_container_width=True)
1053
+
1054
+ if show_manageable_carbon:
1055
+ # "Manageable Carbon"
1056
+ st.altair_chart(man_carbon_chart, use_container_width=True)
1057
+
1058
+ if show_fire_10:
1059
+ # "Fires (2013-2022)"
1060
+ st.altair_chart(fire_10_chart, use_container_width=True)
1061
+
1062
+ if show_rx_10:
1063
+ # "Prescribed Burns (2013-2022)"
1064
+ st.altair_chart(rx_10_chart, use_container_width=True)
1065
+
1066
+ if show_justice40:
1067
+ # "Disadvantaged Communities (Justice40)"
1068
+ st.altair_chart(justice40_chart, use_container_width=True)
1069
+
1070
+ if show_sv:
1071
+ # "Social Vulnerability Index"
1072
+ st.altair_chart(svi_chart, use_container_width=True)
1073
+
1074
+ if show_sv_socio:
1075
+ # "SVI - Socioeconomic Status"
1076
+ st.altair_chart(svi_socio_chart, use_container_width=True)
1077
+
1078
+ if show_sv_household:
1079
+ # "SVI - Household Characteristics"
1080
+ st.altair_chart(svi_house_chart, use_container_width=True)
1081
+
1082
+ if show_sv_minority:
1083
+ # "SVI - Racial and Ethnic Minority"
1084
+ st.altair_chart(svi_minority_chart, use_container_width=True)
1085
+
1086
+ if show_sv_housing:
1087
+ # "SVI - Housing Type and Transit"
1088
+ st.altair_chart(svi_transit_chart, use_container_width=True)
1089
+
1090
+ if show_carbon_lost:
1091
+ # "Deforested Carbon (2002-2022)"
1092
+ st.altair_chart(carbon_loss_chart, use_container_width=True)
1093
+
1094
+ if show_human_impact:
1095
+ # "Human Footprint (2017-2021)"
1096
+ st.altair_chart(hi_chart, use_container_width=True)
1097
+
1098
+
1099
+
1100
+ #########
1101
+
1102
+
1103
+ footer = st.container()
1104
+
1105
+
1106
+
1107
+ st.caption("***The label 'established' is inferred from the California Protected Areas Database, which may introduce artifacts. For details on our methodology, please refer to our code: https://github.com/boettiger-lab/ca-30x30.")
1108
+
1109
+ st.caption("***Under California’s 30x30 framework, only GAP codes 1 and 2 are counted toward the conservation goal.")
1110
+
1111
+
1112
+
1113
+ st.divider()
1114
+
1115
+
1116
+
1117
+ '''
1118
+ ## Credits
1119
+ Authors: Cassie Buhler & Carl Boettiger, UC Berkeley
1120
+ License: BSD-2-clause
1121
+
1122
+ Data: https://huggingface.co/datasets/boettiger-lab/ca-30x30
1123
+
1124
+ ### Data sources
1125
+ - CA Nature Terrestrial 30x30 Conserved Areas map layer by CA Nature. Data: https://www.californianature.ca.gov/datasets/CAnature::30x30-conserved-areas-terrestrial-2024/about. License: Public Domain
1126
+
1127
+ - Imperiled Species Richness and Range-Size-Rarity from NatureServe (2022). Data: https://beta.source.coop/repositories/cboettig/mobi. License CC-BY-NC-ND
1128
+
1129
+ - Irrecoverable Carbon from Conservation International, reprocessed to COG on https://beta.source.coop/cboettig/carbon, citation: https://doi.org/10.1038/s41893-021-00803-6, License: CC-BY-NC
1130
+
1131
+ - Fire polygons by CAL FIRE (2022), reprocessed to PMTiles on https://beta.source.coop/cboettig/fire/. License: Public Domain
1132
+
1133
+ - Climate and Economic Justice Screening Tool, US Council on Environmental Quality, Justice40. Description: https://screeningtool.geoplatform.gov/en/methodology#3/33.47/-97.5. Data: https://beta.source.coop/repositories/cboettig/justice40/description/, License: Public Domain
1134
+
1135
+ - CDC 2020 Social Vulnerability Index by US Census Tract. Description: https://www.atsdr.cdc.gov/place-health/php/svi/index.html. Data: https://source.coop/repositories/cboettig/social-vulnerability/description. License: Public Domain
1136
+
1137
+ - Carbon-loss by Vizzuality, on https://beta.source.coop/repositories/vizzuality/lg-land-carbon-data. Citation: https://doi.org/10.1101/2023.11.01.565036, License: CC-BY
1138
+
1139
+ - Human Footprint by Vizzuality, on https://beta.source.coop/repositories/vizzuality/hfp-100. Citation: https://doi.org/10.3389/frsen.2023.1130896, License: Public Domain
1140
+
1141
+ '''
1142
+
app/requirements.txt ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pandas==2.2.2
2
+ leafmap[maplibre]
3
+ ibis-framework[duckdb]
4
+ streamlit==1.35.0
5
+ streamlit_folium==0.20.0
6
+ altair==5.3.0
7
+ referencing==0.35.1
8
+ rasterio==1.3.10
9
+ shapely==2.0.4
10
+ shiny==0.10.2
11
+ geoarrow-types
12
+ geoarrow-pandas
13
+ leafmap==0.38.12
14
+ SQLAlchemy==2.0.35
15
+ duckdb_engine
16
+ langchain
17
+ langchain-community
18
+ langchain-core
19
+ langchain-openai