Spaces:
Sleeping
Sleeping
File size: 16,320 Bytes
9a5d6f4 8750848 8bd32d3 918728f 8bd32d3 918728f d827042 5d54e07 8bd32d3 5d54e07 918728f c89fcc1 9a5d6f4 8750848 8bd32d3 8750848 c89fcc1 8bd32d3 9a5d6f4 d827042 fb98f2d 8bd32d3 898bb75 8bd32d3 d827042 8bd32d3 d827042 8bd32d3 d827042 8bd32d3 d827042 8bd32d3 d827042 8bd32d3 d827042 f052b1c 8bd32d3 d827042 8bd32d3 d827042 8bd32d3 d827042 2b3c092 fb98f2d 2b3c092 d827042 fb98f2d 2b3c092 fb98f2d 8bd32d3 2b3c092 d827042 fb98f2d 8750848 5d54e07 d827042 69b7b38 d827042 69b7b38 3aa2741 d827042 69b7b38 f052b1c 69b7b38 8bd32d3 69b7b38 a25711e 69b7b38 d827042 4c44a48 d827042 8bd32d3 5d54e07 69b7b38 d827042 a25711e d827042 69b7b38 d827042 8bd32d3 d827042 8750848 8bd32d3 f052b1c 8bd32d3 f052b1c 8bd32d3 f052b1c 8bd32d3 f052b1c 8bd32d3 8750848 8bd32d3 8750848 8bd32d3 8750848 8bd32d3 9a5d6f4 8750848 8bd32d3 8750848 8bd32d3 898bb75 201a16f 8750848 8bd32d3 8750848 8bd32d3 8750848 8bd32d3 8750848 8bd32d3 8750848 8bd32d3 201a16f 8bd32d3 201a16f 8bd32d3 201a16f 8bd32d3 201a16f 8bd32d3 201a16f 8bd32d3 e8745ad 8bd32d3 e8745ad 8bd32d3 e8745ad 8bd32d3 e8745ad 8bd32d3 e8745ad 8bd32d3 69b7b38 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import leafmap.foliumap as leafmap
import streamlit as st
import altair as alt
import ibis
from ibis import _
import ibis.selectors as s
# defaults, consider user palette via st.color_picker()
private_color = "#DE881E" # orange #"#850101" # red
tribal_color = "#BF40BF" # purple
mixed_color = "#005a00" # green
public_color = "#3388ff" # blue
# default color breaks, consider tool via st.slider()
low = 2
high = 3
alpha = .5
style_choice = "Manager Type"
us_lower_48_area_m2 = 7.8e+12
st.set_page_config(layout="wide", page_title="Protected Areas Explorer", page_icon=":globe:")
'''
# US Protected Area Database Explorer
'''
#pad_pmtiles = "https://data.source.coop/cboettig/pad-us-3/pad-stats.pmtiles"
#parquet = "https://data.source.coop/cboettig/pad-us-3/pad-stats.parquet"
pad_pmtiles = "https://huggingface.co/datasets/boettiger-lab/pad-us-3/resolve/main/pad-stats.pmtiles"
parquet = "https://huggingface.co/datasets/boettiger-lab/pad-us-3/resolve/main/pad-stats.parquet"
m = leafmap.Map(center=[35, -100], zoom=4, layers_control=True)
custom_style = '''
"blue"
'''
sample_q = '''(
ibis.read_parquet('https://minio.carlboettiger.info/public-biodiversity/pad-us-3/pad-mobi.parquet').
group_by(_.bucket).
aggregate(percent_protected = 100 * _.area.sum() / us_lower_48_area_m2,
mean_richness = (_.richness * _.area).sum() / _.area.sum(),
mean_rsr = (_.rsr * _.area).sum() / _.area.sum()
).
mutate(percent_protected = _.percent_protected.round())
)
'''
def bar_chart(df, x, y):
chart = alt.Chart(df).mark_bar().encode(
x=x,
y=y,
color=alt.Color('color').scale(None)
).properties(width="container", height=300)
return chart
manager = {
'property': 'manager_group',
'type': 'categorical',
'stops': [
['public', public_color],
['private', private_color],
['mixed', mixed_color],
['tribal', tribal_color]
]
}
easement = {
'property': 'category',
'type': 'categorical',
'stops': [
['Fee', public_color],
['Easement', private_color],
['Proclamation', tribal_color]
]
}
access = {
'property': 'public_access',
'type': 'categorical',
'stops': [
['Open Access', public_color],
['Closed', private_color],
['Unknown', "grey"],
['Restricted Access', tribal_color]
]
}
gap = {
'property': 'gap_code',
'type': 'categorical',
'stops': [
[1, "#26633d"],
[2, "#879647"],
[3, "#BBBBBB"],
[4, "#F8F8F8"]
]
}
iucn = {
'property': 'iucn_category',
'type': 'categorical',
'stops': [
["Ia: Strict nature reserves", "#4B0082"],
["Ib: Wilderness areas", "#663399"],
["II: National park", "#7B68EE"],
["III: Natural monument or feature", "#9370DB"],
["IV: Habitat / species management", "#8A2BE2"],
["V: Protected landscape / seascape", "#9932CC"],
["VI: Protected area with sustainable use of natural resources", "#9400D3"],
["Other Conservation Area", "#DDA0DD"],
["Unassigned", "#F8F8F8"],
]
}
thresholds = ['case',
['<', ['get', 'richness'], low],
private_color,
['>=', ['get', 'richness'], high],
mixed_color,
public_color # default
]
richness = ["interpolate",
["linear"],
["get", "richness"],
0, "#FFE6EE",
4.8, "#850101"
]
rsr = ["interpolate",
["linear"],
["get", "rsr"],
0, "#FFE6EE",
0.006, "#850101"
]
def pad_style(paint, alpha):
return {
"version": 8,
"sources": {
"pad": {
"type": "vector",
"url": "pmtiles://" + pad_pmtiles,
"attribution": "US PAD v3"}},
"layers": [{
"id": "public",
"source": "pad",
"source-layer": "pad-stats",
"type": "fill",
"paint": {
"fill-color": paint,
"fill-opacity": alpha
}
}]}
code_ex='''
m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/natcrop_expansion_100m_cog.tif",
palette="oranges", name="Cropland Expansion", transparent_bg=True, opacity = 0.7, zoom_to_layer=False)
'''
# +
## Map controls sidebar
with st.sidebar:
"## Protected Areas"
if st.toggle("PAD US-3", True):
alpha = st.slider("transparency", 0.0, 1.0, 0.5)
with st.expander("custom style"):
custom = st.text_area(
label = "Define a custom mapbox fill-color rule",
value = custom_style,
height = 100)
style_options = {
"GAP Status Code": gap,
"IUCN Status Code": iucn,
"Manager Type": manager,
"Fee/Easement": easement,
"Public Access": access,
"Mean Richness": richness,
"Mean RSR": rsr,
"custom": eval(custom)}
style_choice = st.radio("Color protected Areas by", style_options)
style = pad_style(style_options[style_choice], alpha)
m.add_pmtiles(pad_pmtiles, name="Protected Areas (PAD-US-3)", style=style, overlay=True, show=True, zoom_to_layer=False)
## Add legend based on selected style?
# m.add_legend(legend_dict=legend_dict)
"## Data layers"
if st.toggle("Species Richness", True):
m.add_tile_layer(url="https://data.source.coop/cboettig/mobi/tiles/red/species-richness-all/{z}/{x}/{y}.png",
name="MOBI Species Richness",
attribution="NatureServe",
opacity=0.9
)
if st.toggle("Range-Size Rarity"):
m.add_tile_layer(url="https://data.source.coop/cboettig/mobi/tiles/green/range-size-rarity-all/{z}/{x}/{y}.png",
name="MOBI Range-Size Rarity",
attribution="NatureServe",
opacity=0.9
)
#m.add_cog_layer("https://data.source.coop/cboettig/mobi/range-size-rarity-all/RSR_All.tif",
# palette="greens", name="Range-Size Rarity", transparent_bg=True, opacity = 0.9, zoom_to_layer=False)
if st.toggle("Carbon Lost (2002-2022)"):
m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/deforest_carbon_100m_cog.tif",
palette="reds", name="Carbon Lost (2002-2022)", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
if st.toggle("Irrecoverable Carbon"):
m.add_cog_layer("https://data.source.coop/cboettig/carbon/cogs/irrecoverable_c_total_2018.tif",
palette="purples", name="Irrecoverable Carbon", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
if st.toggle("Manageable Carbon"):
m.add_cog_layer("https://data.source.coop/cboettig/carbon/cogs/manageable_c_total_2018.tif",
palette="greens", name="Manageable Carbon", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
if st.toggle("Human Impact"):
hi="https://data.source.coop/vizzuality/hfp-100/hfp_2021_100m_v1-2_cog.tif"
m.add_cog_layer(hi, palette="purples", name="Human Impact", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
if st.toggle("cropland expansion"):
m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/natcrop_expansion_100m_cog.tif",
palette="greens", name="cropland expansion", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
if st.toggle("Biodiversity Intactness Loss"):
m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/natcrop_bii_100m_cog.tif",
palette="reds", name="biodiversity intactness loss", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
if st.toggle("Forest Integrity Loss"):
m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/natcrop_fii_100m_cog.tif",
palette="reds", name="forest integrity loss", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
if st.toggle("Custom map layers"):
code = st.text_area(label = "leafmap code:",
value = code_ex,
height = 100)
eval(compile(code, "<string>", "exec"))
# "## Boundaries"
# boundaries = st.radio("Boundaries:",
# ["None",
# "State Boundaries",
# "County Boundaries",
# "Congressional District",
# "custom"]
# )
"## Basemaps"
if st.toggle("Shaded Relief Topo"):
m.add_basemap("Esri.WorldShadedRelief")
"## Additional elements"
# Fire Polygons, USGS
if st.toggle("Fire boundaries"):
usgs = "https://data.source.coop/cboettig/fire/usgs-mtbs.pmtiles"
combined_style = {
"version": 8,
"sources": {
"source1": {
"type": "vector",
"url": "pmtiles://" + usgs,
"attribution": "USGS"}},
"layers": [{
"id": "usgs",
"source": "source1",
"source-layer": "mtbs_perims_DD",
"type": "fill",
"paint": {"fill-color": "#FFA500", "fill-opacity": 0.4}}]}
m.add_pmtiles(usgs, name="Fire", style=combined_style, overlay=True, show=True, zoom_to_layer=False)
# Map radio buttons to corresponding column:
select_column = {
"GAP Status Code": "gap_code",
"IUCN Status Code": "iucn_category",
"Manager Type": "manager_group",
"Fee/Easement": "category",
"Public Access": "public_access",
"Mean Richness": "manager_group",
"Mean RSR": "manager_group",
"custom": "gap_code"}
column = select_column[style_choice]
# Map radio buttons to corresponding color-scheme:
select_colors = {
"GAP Status Code": gap["stops"],
"IUCN Status Code": iucn["stops"],
"Manager Type": manager["stops"],
"Fee/Easement": easement["stops"],
"Public Access": access["stops"],
"Mean Richness": manager["stops"],
"Mean RSR": manager["stops"],
"custom": manager["stops"]}
colors = (ibis
.memtable(select_colors[style_choice], columns = [column, "color"])
.to_pandas()
)
main = st.container()
with main:
map_col, stats_col = st.columns([2,1])
with map_col:
m.to_streamlit(height=700)
@st.cache_resource
def ibis_connection(parquet):
return ibis.read_parquet(parquet)
pad_data = ibis_connection(parquet)
@st.cache_data()
def summary_table(column = column, colors = colors):
df = (pad_data
.rename(area = "area_square_meters")
.group_by(_[column])
.aggregate(percent_protected = 100 * _.area.sum() / us_lower_48_area_m2,
mean_richness = (_.richness * _.area).sum() / _.area.sum(),
mean_rsr = (_.rsr * _.area).sum() / _.area.sum(),
carbon_lost = (_.deforest_carbon * _.area).sum() / _.area.sum(),
crop_expansion = (_.crop_expansion * _.area).sum() / _.area.sum(),
human_impact = (_.human_impact * _.area).sum() / _.area.sum(),
)
.mutate(percent_protected = _.percent_protected.round())
.inner_join(colors, column)
)
df = df.to_pandas()
df[column] = df[column].astype(str)
return df
df = summary_table(column, colors)
total_percent = df.percent_protected.sum()
base = alt.Chart(df).encode(
alt.Theta("percent_protected:Q").stack(True),
alt.Color("color:N").scale(None).legend(None)
)
area_chart = (
base.mark_arc(innerRadius=40, outerRadius=70)
).properties(width=180, height=180)
richness_chart = bar_chart(df, column, 'mean_richness')
rsr_chart = bar_chart(df, column, 'mean_rsr')
carbon_lost = bar_chart(df, column, 'carbon_lost')
crop_expansion = bar_chart(df, column, 'crop_expansion')
human_impact = bar_chart(df, column, 'human_impact')
with stats_col:
with st.container():
col1, col2, col3 = st.columns(3)
with col1:
f"{total_percent}% Continental US Covered"
st.altair_chart(area_chart, use_container_width=False)
with col2:
"Species Richness"
st.altair_chart(richness_chart, use_container_width=True)
with col3:
"Range-Size Rarity"
st.altair_chart(rsr_chart, use_container_width=True)
with st.container():
col1b, col2b, col3b = st.columns(3)
with col1b:
"Carbon Lost ('02-'22)"
st.altair_chart(carbon_lost, use_container_width=True)
with col2b:
"Crop expansion"
st.altair_chart(crop_expansion, use_container_width=True)
with col3b:
"Human Impact"
st.altair_chart(human_impact, use_container_width=True)
st.divider()
footer = st.container()
with footer:
'''
## Custom queries
Input custom python code below to interactively explore the data.
'''
col2_1, col2_2 = st.columns(2)
with col2_1:
query = st.text_area(
label = "Python code:",
value = sample_q,
height = 300)
with col2_2:
"Output table:"
df = eval(query)
st.write(df.to_pandas())
st.divider()
'''
## Credits
Author: Carl Boettiger, UC Berkeley
License: BSD-2-clause
### Data sources
- 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
- 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
- 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
- Fire polygons by USGS, reprocessed to PMTiles on https://beta.source.coop/cboettig/fire/. License: Public Domain.
- 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
### Software
Proudly built with a free and Open Source software stack: Streamlit (reactive application), HuggingFace (application hosting), Source.Coop (data hosting),
using cloud-native data serializations in COG, PMTiles, and GeoParquet. Coded in pure python using leafmap and duckdb. Map styling with [MapLibre](https://maplibre.org/).
'''
|