Spaces:
Sleeping
Sleeping
cassiebuhler
commited on
Commit
·
9d71920
1
Parent(s):
e6dbe80
adding type
Browse files
app.py
CHANGED
@@ -39,6 +39,7 @@ ca_pmtiles = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/mai
|
|
39 |
ca_parquet = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/main/ca2024-30m.parquet"
|
40 |
|
41 |
|
|
|
42 |
ca_area_acres = 1.014e8 #acres
|
43 |
style_choice = "GAP Status Code"
|
44 |
|
@@ -48,8 +49,10 @@ ca = (con
|
|
48 |
.read_parquet(ca_parquet)
|
49 |
.cast({"geom": "geometry"})
|
50 |
)
|
|
|
|
|
51 |
|
52 |
-
private_access_color = "#DE881E" # orange
|
53 |
public_access_color = "#3388ff" # blue
|
54 |
tribal_color = "#BF40BF" # purple
|
55 |
mixed_color = "#005a00" # green
|
@@ -67,19 +70,21 @@ city_color = "#BDC368" #green
|
|
67 |
hoa_color = "#A89BBC" # purple
|
68 |
nonprofit_color = "#D77031" #orange
|
69 |
|
|
|
70 |
from functools import reduce
|
|
|
71 |
def summary_table(column, colors, filter_cols, filter_vals):
|
72 |
-
filters = [_.reGAP < 3]
|
73 |
|
74 |
-
if filter_cols and filter_vals:
|
75 |
for filter_col, filter_val in zip(filter_cols, filter_vals):
|
76 |
if len(filter_val) > 1:
|
77 |
filters.append(getattr(_, filter_col).isin(filter_val))
|
78 |
else:
|
79 |
filters.append(getattr(_, filter_col) == filter_val[0])
|
80 |
-
|
81 |
combined_filter = reduce(lambda x, y: x & y, filters)
|
82 |
-
|
83 |
df = (ca
|
84 |
.filter(combined_filter)
|
85 |
.group_by(column)
|
@@ -87,12 +92,12 @@ def summary_table(column, colors, filter_cols, filter_vals):
|
|
87 |
.mutate(percent_protected=_.percent_protected.round(1))
|
88 |
.inner_join(colors, column)
|
89 |
)
|
90 |
-
|
91 |
df = df.to_pandas()
|
92 |
df[column] = df[column].astype(str)
|
93 |
-
|
94 |
return df
|
95 |
|
|
|
|
|
96 |
def area_plot(df, column):
|
97 |
base = alt.Chart(df).encode(
|
98 |
alt.Theta("percent_protected:Q").stack(True),
|
@@ -110,28 +115,8 @@ def area_plot(df, column):
|
|
110 |
return plot.properties(width="container", height=300)
|
111 |
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
# return {
|
116 |
-
# "version": 8,
|
117 |
-
# "sources": {
|
118 |
-
# "ca": {
|
119 |
-
# "type": "vector",
|
120 |
-
# "url": "pmtiles://" + ca_pmtiles,
|
121 |
-
# }},
|
122 |
-
# "layers": [{
|
123 |
-
# "id": "ca30x30",
|
124 |
-
# "source": "ca",
|
125 |
-
# "source-layer": "ca202430m",
|
126 |
-
# "type": "fill",
|
127 |
-
# "paint": {
|
128 |
-
# "fill-color": paint,
|
129 |
-
# "fill-opacity": alpha
|
130 |
-
# }
|
131 |
-
# }]}
|
132 |
-
|
133 |
-
|
134 |
-
def filter_pmtiles(paint, alpha, cols, values):
|
135 |
filters = []
|
136 |
|
137 |
for col, val in zip(cols, values):
|
@@ -151,6 +136,7 @@ def filter_pmtiles(paint, alpha, cols, values):
|
|
151 |
"id": "ca30x30",
|
152 |
"source": "ca",
|
153 |
"source-layer": "ca202430m",
|
|
|
154 |
"type": "fill",
|
155 |
"filter": combined_filter, # Use the combined filter
|
156 |
"paint": {
|
@@ -161,27 +147,15 @@ def filter_pmtiles(paint, alpha, cols, values):
|
|
161 |
}
|
162 |
|
163 |
|
164 |
-
# def getButtons(style_options, style_choice):
|
165 |
-
# column = style_options[style_choice]['property']
|
166 |
-
# opts = [style[0] for style in style_options[style_choice]['stops']]
|
167 |
-
# buttons = {name: st.checkbox(f"{name}", value = False, key = column+str(name)) for name in opts}
|
168 |
-
# filter_choice = [key for key, value in buttons.items() if value] #return only the options selected
|
169 |
-
# d = {}
|
170 |
-
# d[column] = filter_choice
|
171 |
-
# return d
|
172 |
-
|
173 |
def getButtons(style_options, style_choice, default_gap=None):
|
174 |
column = style_options[style_choice]['property']
|
175 |
-
opts = [style[0] for style in style_options[style_choice]['stops']]
|
176 |
-
|
177 |
-
default_gap = default_gap or {}
|
178 |
-
|
179 |
buttons = {
|
180 |
name: st.checkbox(f"{name}", value=default_gap.get(name, False), key=column + str(name))
|
181 |
for name in opts
|
182 |
}
|
183 |
-
|
184 |
-
filter_choice = [key for key, value in buttons.items() if value] # return only the options selected
|
185 |
d = {}
|
186 |
d[column] = filter_choice
|
187 |
return d
|
@@ -256,12 +230,23 @@ gap = {
|
|
256 |
}
|
257 |
|
258 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
259 |
style_options = {
|
260 |
"GAP Status Code": gap,
|
261 |
"Manager Type": manager,
|
262 |
"Easement": easement,
|
263 |
"Public Access": access,
|
264 |
-
"Year": year
|
|
|
265 |
}
|
266 |
|
267 |
|
@@ -278,26 +263,26 @@ filters = {}
|
|
278 |
with st.sidebar:
|
279 |
color_choice = st.radio("Color by:", style_options)
|
280 |
alpha = st.slider("transparency", 0.0, 1.0, 0.5)
|
281 |
-
st.text("Filters:")
|
282 |
-
|
283 |
for label in style_options:
|
284 |
with st.expander(label):
|
285 |
-
|
286 |
-
if label == "GAP Status Code"
|
287 |
opts = getButtons(style_options, label, default_gap)
|
288 |
else:
|
289 |
opts = getButtons(style_options, label)
|
290 |
filters.update(opts)
|
291 |
|
292 |
-
selected = {k: v for k, v in filters.items() if v}
|
293 |
-
if selected:
|
294 |
filter_cols = list(selected.keys())
|
295 |
filter_vals = list(selected.values())
|
296 |
-
else:
|
297 |
filter_cols = []
|
298 |
filter_vals = []
|
299 |
-
|
300 |
-
style =
|
301 |
m.add_pmtiles(ca_pmtiles, style=style, visible=True, name="CA", opacity=alpha, tooltip=True)
|
302 |
|
303 |
|
@@ -306,7 +291,8 @@ select_column = {
|
|
306 |
"Manager Type": "manager_type",
|
307 |
"Easement": "Easement",
|
308 |
"Year": "established",
|
309 |
-
"Public Access": "access_type"
|
|
|
310 |
|
311 |
|
312 |
column = select_column[color_choice]
|
@@ -316,7 +302,8 @@ select_colors = {
|
|
316 |
"Easement": easement["stops"],
|
317 |
"Public Access": access["stops"],
|
318 |
"Year": year["stops"],
|
319 |
-
"GAP Status Code": gap["stops"]
|
|
|
320 |
|
321 |
colors = (ibis
|
322 |
.memtable(select_colors[color_choice], columns = [column, "color"])
|
@@ -327,7 +314,6 @@ main = st.container()
|
|
327 |
|
328 |
with main:
|
329 |
map_col, stats_col = st.columns([2,1])
|
330 |
-
|
331 |
with map_col:
|
332 |
to_streamlit(m, height=700)
|
333 |
df = summary_table(column, colors, filter_cols, filter_vals)
|
|
|
39 |
ca_parquet = "https://huggingface.co/datasets/boettiger-lab/ca-30x30/resolve/main/ca2024-30m.parquet"
|
40 |
|
41 |
|
42 |
+
|
43 |
ca_area_acres = 1.014e8 #acres
|
44 |
style_choice = "GAP Status Code"
|
45 |
|
|
|
49 |
.read_parquet(ca_parquet)
|
50 |
.cast({"geom": "geometry"})
|
51 |
)
|
52 |
+
|
53 |
+
|
54 |
|
55 |
+
private_access_color = "#DE881E" # orange
|
56 |
public_access_color = "#3388ff" # blue
|
57 |
tribal_color = "#BF40BF" # purple
|
58 |
mixed_color = "#005a00" # green
|
|
|
70 |
hoa_color = "#A89BBC" # purple
|
71 |
nonprofit_color = "#D77031" #orange
|
72 |
|
73 |
+
|
74 |
from functools import reduce
|
75 |
+
|
76 |
def summary_table(column, colors, filter_cols, filter_vals):
|
77 |
+
filters = [_.reGAP < 3] #only compute with gap codes 1 and 2
|
78 |
|
79 |
+
if filter_cols and filter_vals: #if a filter is selected, add to list of filters
|
80 |
for filter_col, filter_val in zip(filter_cols, filter_vals):
|
81 |
if len(filter_val) > 1:
|
82 |
filters.append(getattr(_, filter_col).isin(filter_val))
|
83 |
else:
|
84 |
filters.append(getattr(_, filter_col) == filter_val[0])
|
85 |
+
|
86 |
combined_filter = reduce(lambda x, y: x & y, filters)
|
87 |
+
|
88 |
df = (ca
|
89 |
.filter(combined_filter)
|
90 |
.group_by(column)
|
|
|
92 |
.mutate(percent_protected=_.percent_protected.round(1))
|
93 |
.inner_join(colors, column)
|
94 |
)
|
|
|
95 |
df = df.to_pandas()
|
96 |
df[column] = df[column].astype(str)
|
|
|
97 |
return df
|
98 |
|
99 |
+
|
100 |
+
|
101 |
def area_plot(df, column):
|
102 |
base = alt.Chart(df).encode(
|
103 |
alt.Theta("percent_protected:Q").stack(True),
|
|
|
115 |
return plot.properties(width="container", height=300)
|
116 |
|
117 |
|
118 |
+
|
119 |
+
def get_pmtiles_style(paint, alpha, cols, values):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
filters = []
|
121 |
|
122 |
for col, val in zip(cols, values):
|
|
|
136 |
"id": "ca30x30",
|
137 |
"source": "ca",
|
138 |
"source-layer": "ca202430m",
|
139 |
+
# "source-layer": "ca2024",
|
140 |
"type": "fill",
|
141 |
"filter": combined_filter, # Use the combined filter
|
142 |
"paint": {
|
|
|
147 |
}
|
148 |
|
149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
def getButtons(style_options, style_choice, default_gap=None):
|
151 |
column = style_options[style_choice]['property']
|
152 |
+
opts = [style[0] for style in style_options[style_choice]['stops']]
|
153 |
+
default_gap = default_gap or {}
|
|
|
|
|
154 |
buttons = {
|
155 |
name: st.checkbox(f"{name}", value=default_gap.get(name, False), key=column + str(name))
|
156 |
for name in opts
|
157 |
}
|
158 |
+
filter_choice = [key for key, value in buttons.items() if value] # return only selected
|
|
|
159 |
d = {}
|
160 |
d[column] = filter_choice
|
161 |
return d
|
|
|
230 |
}
|
231 |
|
232 |
|
233 |
+
area_type = {
|
234 |
+
'property': 'type',
|
235 |
+
'type': 'categorical',
|
236 |
+
'stops': [
|
237 |
+
["Land", "green"],
|
238 |
+
["Water", "blue"]
|
239 |
+
]
|
240 |
+
}
|
241 |
+
|
242 |
+
|
243 |
style_options = {
|
244 |
"GAP Status Code": gap,
|
245 |
"Manager Type": manager,
|
246 |
"Easement": easement,
|
247 |
"Public Access": access,
|
248 |
+
"Year": year,
|
249 |
+
"Type": area_type
|
250 |
}
|
251 |
|
252 |
|
|
|
263 |
with st.sidebar:
|
264 |
color_choice = st.radio("Color by:", style_options)
|
265 |
alpha = st.slider("transparency", 0.0, 1.0, 0.5)
|
266 |
+
# st.text("Filters:")
|
267 |
+
"Filters:"
|
268 |
for label in style_options:
|
269 |
with st.expander(label):
|
270 |
+
|
271 |
+
if label == "GAP Status Code": # gap code 1 and 2 are on by default
|
272 |
opts = getButtons(style_options, label, default_gap)
|
273 |
else:
|
274 |
opts = getButtons(style_options, label)
|
275 |
filters.update(opts)
|
276 |
|
277 |
+
selected = {k: v for k, v in filters.items() if v} #get selected filters
|
278 |
+
if selected:
|
279 |
filter_cols = list(selected.keys())
|
280 |
filter_vals = list(selected.values())
|
281 |
+
else:
|
282 |
filter_cols = []
|
283 |
filter_vals = []
|
284 |
+
|
285 |
+
style = get_pmtiles_style(style_options[color_choice], alpha, filter_cols, filter_vals)
|
286 |
m.add_pmtiles(ca_pmtiles, style=style, visible=True, name="CA", opacity=alpha, tooltip=True)
|
287 |
|
288 |
|
|
|
291 |
"Manager Type": "manager_type",
|
292 |
"Easement": "Easement",
|
293 |
"Year": "established",
|
294 |
+
"Public Access": "access_type",
|
295 |
+
"Type": "type"}
|
296 |
|
297 |
|
298 |
column = select_column[color_choice]
|
|
|
302 |
"Easement": easement["stops"],
|
303 |
"Public Access": access["stops"],
|
304 |
"Year": year["stops"],
|
305 |
+
"GAP Status Code": gap["stops"],
|
306 |
+
"Type": area_type["stops"]}
|
307 |
|
308 |
colors = (ibis
|
309 |
.memtable(select_colors[color_choice], columns = [column, "color"])
|
|
|
314 |
|
315 |
with main:
|
316 |
map_col, stats_col = st.columns([2,1])
|
|
|
317 |
with map_col:
|
318 |
to_streamlit(m, height=700)
|
319 |
df = summary_table(column, colors, filter_cols, filter_vals)
|