cboettig commited on
Commit
779004a
Β·
1 Parent(s): 865760d

add drop down menus for data layers

Browse files
Files changed (1) hide show
  1. app.py +149 -134
app.py CHANGED
@@ -28,7 +28,7 @@ pad_pmtiles = "https://huggingface.co/datasets/boettiger-lab/pad-us-3/resolve/ma
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
34
  mixed_color = "#005a00" # green
@@ -42,6 +42,8 @@ style_choice = "Manager Type"
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)
@@ -92,7 +94,24 @@ def area_plot(df, column):
92
  plot = pie # pie + text
93
  return plot.properties(width=180, height=180)
94
 
95
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
 
98
 
@@ -122,6 +141,8 @@ mutate(percent_protected = _.percent_protected.round())
122
  )
123
  '''
124
 
 
 
125
  manager = {
126
  'property': 'manager_type',
127
  'type': 'categorical',
@@ -186,136 +207,142 @@ iucn = {
186
  ]
187
  }
188
 
189
- thresholds = ['case',
190
- ['<', ['get', 'richness'], low],
191
- private_color,
192
- ['>=', ['get', 'richness'], high],
193
- mixed_color,
194
- public_color # default
195
- ]
196
-
197
- richness = ["interpolate",
198
- ["linear"],
199
- ["get", "richness"],
200
- 0, "#FFE6EE",
201
- 4.8, "#850101"
202
- ]
203
-
204
- rsr = ["interpolate",
205
- ["linear"],
206
- ["get", "rsr"],
207
- 0, "#FFE6EE",
208
- 0.006, "#850101"
209
- ]
210
 
211
- def pad_style(paint, alpha):
212
- return {
 
 
 
 
 
 
 
 
 
 
 
 
 
213
  "version": 8,
214
  "sources": {
215
- "pad": {
216
  "type": "vector",
217
- "url": "pmtiles://" + pad_pmtiles,
218
- "attribution": "US PAD v3"}},
 
219
  "layers": [{
220
- "id": "public",
221
- "source": "pad",
222
- "source-layer": "pad-stats",
223
  "type": "fill",
224
- "paint": {
225
- "fill-color": paint,
226
- "fill-opacity": alpha
227
- }
228
- }]}
229
 
230
- code_ex='''
231
- m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/natcrop_expansion_100m_cog.tif",
232
- palette="oranges", name="Cropland Expansion", transparent_bg=True, opacity = 0.7, zoom_to_layer=False)
233
- '''
234
  # +
235
  ## Map controls sidebar
236
 
237
  with st.sidebar:
238
 
239
- "## Protected Areas"
240
-
241
- if st.toggle("PAD US-3", True):
242
  alpha = st.slider("transparency", 0.0, 1.0, 0.5)
243
-
244
- with st.expander("custom style"):
245
- custom = st.text_area(
246
- label = "Define a custom mapbox fill-color rule",
247
- value = custom_style,
248
- height = 100)
249
-
250
- style_options = {
251
- "GAP Status Code": gap,
252
- "IUCN Status Code": iucn,
253
- "Manager Type": manager,
254
- "Fee/Easement": easement,
255
- "Public Access": access,
256
- "Mean Richness": richness,
257
- "Mean RSR": rsr,
258
- "custom": eval(custom)}
259
-
260
- style_choice = st.radio("Color protected Areas by", style_options)
261
  style = pad_style(style_options[style_choice], alpha)
262
  m.add_pmtiles(pad_pmtiles, name="Protected Areas (PAD-US-3)", style=style, overlay=True, show=True, zoom_to_layer=False)
263
  ## Add legend based on selected style?
264
  # m.add_legend(legend_dict=legend_dict)
265
 
266
- "## Data layers"
267
- if st.toggle("Species Richness", True):
268
- m.add_tile_layer(url="https://data.source.coop/cboettig/mobi/tiles/red/species-richness-all/{z}/{x}/{y}.png",
269
- name="MOBI Species Richness",
270
- attribution="NatureServe",
271
- opacity=0.9
272
- )
273
-
274
- if st.toggle("Range-Size Rarity"):
275
- m.add_tile_layer(url="https://data.source.coop/cboettig/mobi/tiles/green/range-size-rarity-all/{z}/{x}/{y}.png",
276
- name="MOBI Range-Size Rarity",
277
- attribution="NatureServe",
278
- opacity=0.9
279
- )
280
- #m.add_cog_layer("https://data.source.coop/cboettig/mobi/range-size-rarity-all/RSR_All.tif",
281
- # palette="greens", name="Range-Size Rarity", transparent_bg=True, opacity = 0.9, zoom_to_layer=False)
282
-
283
- if st.toggle("Carbon Lost (2002-2022)"):
284
- m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/deforest_carbon_100m_cog.tif",
285
- palette="reds", name="Carbon Lost (2002-2022)", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
286
-
287
- if st.toggle("Irrecoverable Carbon"):
288
- m.add_cog_layer("https://data.source.coop/cboettig/carbon/cogs/irrecoverable_c_total_2018.tif",
289
- palette="purples", name="Irrecoverable Carbon", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
290
-
291
- if st.toggle("Manageable Carbon"):
292
- m.add_cog_layer("https://data.source.coop/cboettig/carbon/cogs/manageable_c_total_2018.tif",
293
- palette="greens", name="Manageable Carbon", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
294
-
295
- if st.toggle("Human Impact"):
296
- hi="https://data.source.coop/vizzuality/hfp-100/hfp_2021_100m_v1-2_cog.tif"
297
- m.add_cog_layer(hi, palette="purples", name="Human Impact", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
298
 
299
- if st.toggle("cropland expansion"):
300
- m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/natcrop_expansion_100m_cog.tif",
301
- palette="greens", name="cropland expansion", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
302
 
303
- if st.toggle("Biodiversity Intactness Loss"):
304
- m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/natcrop_bii_100m_cog.tif",
305
- palette="reds", name="biodiversity intactness loss", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
306
-
307
- if st.toggle("Forest Integrity Loss"):
308
- m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/natcrop_fii_100m_cog.tif",
309
- palette="reds", name="forest integrity loss", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
310
 
 
 
 
311
 
 
 
 
312
 
313
- if st.toggle("Custom map layers"):
314
-
315
- code = st.text_area(label = "leafmap code:",
316
- value = code_ex,
317
- height = 100)
318
- eval(compile(code, "<string>", "exec"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
319
 
320
  # "## Boundaries"
321
  # boundaries = st.radio("Boundaries:",
@@ -326,29 +353,15 @@ with st.sidebar:
326
  # "custom"]
327
  # )
328
 
329
- "## Basemaps"
330
- if st.toggle("Shaded Relief Topo"):
331
- m.add_basemap("Esri.WorldShadedRelief")
 
 
 
 
332
 
333
- "## Additional elements"
334
- # Fire Polygons, USGS
335
- if st.toggle("Fire boundaries"):
336
- usgs = "https://data.source.coop/cboettig/fire/usgs-mtbs.pmtiles"
337
- combined_style = {
338
- "version": 8,
339
- "sources": {
340
- "source1": {
341
- "type": "vector",
342
- "url": "pmtiles://" + usgs,
343
- "attribution": "USGS"}},
344
- "layers": [{
345
- "id": "usgs",
346
- "source": "source1",
347
- "source-layer": "mtbs_perims_DD",
348
- "type": "fill",
349
- "paint": {"fill-color": "#FFA500", "fill-opacity": 0.4}}]}
350
- m.add_pmtiles(usgs, name="Fire", style=combined_style, overlay=True, show=True, zoom_to_layer=False)
351
-
352
  # Map radio buttons to corresponding column:
353
  select_column = {
354
  "GAP Status Code": "gap_code",
@@ -462,11 +475,13 @@ with footer:
462
 
463
  ### Data sources
464
 
465
- - US Protected Areas Database v3 by USGS, data hosted on https://beta.source.coop/cboettig/us-pad-3. Citation: https://doi.org/10.5066/P9Q9LQ4B, License: Public Domain
466
- - 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
 
467
  - 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
468
- - Fire polygons by USGS, reprocessed to PMTiles on https://beta.source.coop/cboettig/fire/. License: Public Domain.
469
  - 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
 
470
 
471
  ### Software
472
 
 
28
  parquet = "https://huggingface.co/datasets/boettiger-lab/pad-us-3/resolve/main/pad-stats.parquet"
29
 
30
 
31
+ # some default color variables, consider user palette via st.color_picker()
32
  private_color = "#DE881E" # orange #"#850101" # red
33
  tribal_color = "#BF40BF" # purple
34
  mixed_color = "#005a00" # green
 
42
  us_lower_48_area_m2 = 7.8e+12
43
 
44
 
45
+ ## Helper functions
46
+
47
  @st.cache_resource
48
  def ibis_connection(parquet):
49
  return ibis.read_parquet(parquet)
 
94
  plot = pie # pie + text
95
  return plot.properties(width=180, height=180)
96
 
97
+ def pad_style(paint, alpha):
98
+ return {
99
+ "version": 8,
100
+ "sources": {
101
+ "pad": {
102
+ "type": "vector",
103
+ "url": "pmtiles://" + pad_pmtiles,
104
+ "attribution": "US PAD v3"}},
105
+ "layers": [{
106
+ "id": "public",
107
+ "source": "pad",
108
+ "source-layer": "pad-stats",
109
+ "type": "fill",
110
+ "paint": {
111
+ "fill-color": paint,
112
+ "fill-opacity": alpha
113
+ }
114
+ }]}
115
 
116
 
117
 
 
141
  )
142
  '''
143
 
144
+
145
+ ## Protected Area polygon color codes
146
  manager = {
147
  'property': 'manager_type',
148
  'type': 'categorical',
 
207
  ]
208
  }
209
 
210
+ style_options = {
211
+ "GAP Status Code": gap,
212
+ "IUCN Status Code": iucn,
213
+ "Manager Type": manager,
214
+ "Fee/Easement": easement,
215
+ "Public Access": access,
216
+ # "Mean Richness": richness,
217
+ # "Mean RSR": rsr,
218
+ # "custom": eval(custom)
219
+ }
 
 
 
 
 
 
 
 
 
 
 
220
 
221
+ code_ex='''
222
+ m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/natcrop_expansion_100m_cog.tif",
223
+ palette="oranges", name="Cropland Expansion", transparent_bg=True, opacity = 0.7, zoom_to_layer=False)
224
+ '''
225
+
226
+
227
+
228
+ justice40 = "https://data.source.coop/cboettig/justice40/disadvantaged-communities.pmtiles"
229
+ justice40_fill = {
230
+ 'property': 'Disadvan',
231
+ 'type': 'categorical',
232
+ 'stops': [
233
+ [0, "rgba(255, 255, 255, 0)"],
234
+ [1, "rgba(0, 0, 139, 1)"]]}
235
+ justice40_style = {
236
  "version": 8,
237
  "sources": {
238
+ "source1": {
239
  "type": "vector",
240
+ "url": "pmtiles://" + justice40,
241
+ "attribution": "Justice40"}
242
+ },
243
  "layers": [{
244
+ "id": "layer1",
245
+ "source": "source1",
246
+ "source-layer": "DisadvantagedCommunitiesCEJST",
247
  "type": "fill",
248
+ "paint": {"fill-color": justice40_fill, "fill-opacity": 0.6}}]
249
+ }
250
+
251
+
252
+
253
 
 
 
 
 
254
  # +
255
  ## Map controls sidebar
256
 
257
  with st.sidebar:
258
 
259
+ if st.toggle("Protected Areas", True):
260
+
261
+ style_choice = st.radio("Color by:", style_options)
262
  alpha = st.slider("transparency", 0.0, 1.0, 0.5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
  style = pad_style(style_options[style_choice], alpha)
264
  m.add_pmtiles(pad_pmtiles, name="Protected Areas (PAD-US-3)", style=style, overlay=True, show=True, zoom_to_layer=False)
265
  ## Add legend based on selected style?
266
  # m.add_legend(legend_dict=legend_dict)
267
 
268
+ with st.expander("🦜 Biodiversity"):
269
+ if st.toggle("Species Richness", False):
270
+ m.add_tile_layer(url="https://data.source.coop/cboettig/mobi/tiles/red/species-richness-all/{z}/{x}/{y}.png",
271
+ name="MOBI Species Richness",
272
+ attribution="NatureServe",
273
+ opacity=0.9
274
+ )
275
+
276
+ if st.toggle("Range-Size Rarity"):
277
+ m.add_tile_layer(url="https://data.source.coop/cboettig/mobi/tiles/green/range-size-rarity-all/{z}/{x}/{y}.png",
278
+ name="MOBI Range-Size Rarity",
279
+ attribution="NatureServe",
280
+ opacity=0.9
281
+ )
282
+ #m.add_cog_layer("https://data.source.coop/cboettig/mobi/range-size-rarity-all/RSR_All.tif",
283
+ # palette="greens", name="Range-Size Rarity", transparent_bg=True, opacity = 0.9, zoom_to_layer=False)
284
+ with st.expander("β›… Carbon & Climate"):
285
+ if st.toggle("Carbon Lost (2002-2022)"):
286
+ m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/deforest_carbon_100m_cog.tif",
287
+ palette="reds", name="Carbon Lost (2002-2022)", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
288
+
289
+ if st.toggle("Irrecoverable Carbon"):
290
+ m.add_cog_layer("https://data.source.coop/cboettig/carbon/cogs/irrecoverable_c_total_2018.tif",
291
+ palette="purples", name="Irrecoverable Carbon", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
 
 
 
 
 
 
 
 
292
 
293
+ if st.toggle("Manageable Carbon"):
294
+ m.add_cog_layer("https://data.source.coop/cboettig/carbon/cogs/manageable_c_total_2018.tif",
295
+ palette="greens", name="Manageable Carbon", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
296
 
297
+ with st.expander("🚜 Human Impacts"):
 
 
 
 
 
 
298
 
299
+ if st.toggle("Human Impact"):
300
+ hi="https://data.source.coop/vizzuality/hfp-100/hfp_2021_100m_v1-2_cog.tif"
301
+ m.add_cog_layer(hi, palette="purples", name="Human Impact", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
302
 
303
+ if st.toggle("cropland expansion"):
304
+ m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/natcrop_expansion_100m_cog.tif",
305
+ palette="greens", name="cropland expansion", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
306
 
307
+ if st.toggle("Biodiversity Intactness Loss"):
308
+ m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/natcrop_bii_100m_cog.tif",
309
+ palette="reds", name="biodiversity intactness loss", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
310
+
311
+ if st.toggle("Forest Integrity Loss"):
312
+ m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/natcrop_fii_100m_cog.tif",
313
+ palette="reds", name="forest integrity loss", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
314
+
315
+ with st.expander("βš– Equity and Justice"):
316
+ if st.toggle("Climate & Economic Justice"):
317
+ m.add_pmtiles(justice40, name="Climate & Economic Justice", style = justice40_style, overlay=True, show=True, zoom_to_layer=False)
318
+
319
+ with st.expander("πŸ”₯ Wildfire"):
320
+ # Fire Polygons, USGS
321
+ if st.toggle("Fire boundaries"):
322
+ usgs = "https://data.source.coop/cboettig/fire/usgs-mtbs.pmtiles"
323
+ fire_style = {
324
+ "version": 8,
325
+ "sources": {
326
+ "source1": {
327
+ "type": "vector",
328
+ "url": "pmtiles://" + usgs,
329
+ "attribution": "USGS"}},
330
+ "layers": [{
331
+ "id": "usgs",
332
+ "source": "source1",
333
+ "source-layer": "mtbs_perims_DD",
334
+ "type": "fill",
335
+ "paint": {"fill-color": "#FFA500", "fill-opacity": 0.4}}]}
336
+ m.add_pmtiles(usgs, name="Fire", style=fire_style, overlay=True, show=True, zoom_to_layer=False)
337
+
338
+
339
+ with st.expander("πŸ’» Custom code"):
340
+ if st.toggle("Custom map layers"):
341
+
342
+ code = st.text_area(label = "leafmap code:",
343
+ value = code_ex,
344
+ height = 100)
345
+ eval(compile(code, "<string>", "exec"))
346
 
347
  # "## Boundaries"
348
  # boundaries = st.radio("Boundaries:",
 
353
  # "custom"]
354
  # )
355
 
356
+ with st.expander("πŸ—Ί Basemaps"):
357
+ # radio selector would make more sense
358
+ if st.toggle("Topography"):
359
+ m.add_basemap("Esri.WorldShadedRelief")
360
+ if st.toggle("Satellite"):
361
+ m.add_basemap("Esri.WorldImagery")
362
+
363
 
364
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
365
  # Map radio buttons to corresponding column:
366
  select_column = {
367
  "GAP Status Code": "gap_code",
 
475
 
476
  ### Data sources
477
 
478
+ - US Protected Areas Database v3 by USGS. Data: https://beta.source.coop/cboettig/us-pad-3. Citation: https://doi.org/10.5066/P9Q9LQ4B, License: Public Domain
479
+ - Imperiled Species Richness and Range-Size-Rarity from NatureServe (2022). Data: https://beta.source.coop/repositories/cboettig/mobi. License CC-BY-NC-ND
480
+ - Carbon-loss and farming impact 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
481
  - 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
482
+ - Fire polygons by USGS, reprocessed to PMTiles on https://beta.source.coop/cboettig/fire/. License: Public Domain
483
  - 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
484
+ - Climate and Economic Justice Screening Tool, US Council on Environmental Quality, Justice40, data: https://beta.source.coop/repositories/cboettig/justice40/description/, License: Public Domain
485
 
486
  ### Software
487