Spaces:
Running
Running
File size: 6,541 Bytes
ba4e718 |
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 |
import streamlit as st
import streamlit.components.v1 as components
import base64
import leafmap.maplibregl as leafmap
import altair as alt
import ibis
from ibis import _
import ibis.selectors as s
from typing import Optional
def to_streamlit(
self,
width: Optional[int] = None,
height: Optional[int] = 600,
scrolling: Optional[bool] = False,
**kwargs,
):
try:
import streamlit.components.v1 as components
import base64
raw_html = self.to_html().encode("utf-8")
raw_html = base64.b64encode(raw_html).decode()
return components.iframe(
f"data:text/html;base64,{raw_html}",
width=width,
height=height,
scrolling=scrolling,
**kwargs,
)
except Exception as e:
raise Exception(e)
ca_pmtiles = "https://data.source.coop/cboettig/ca30x30/ca_areas.pmtiles"
# ca_pmtiles = "ca_areas.pmtiles"
# ca_parquet = "https://data.source.coop/cboettig/ca30x30/ca_areas.parquet"
ca_parquet = "ca_areas.parquet"
ca_area_acres = 1.014e8 #acres
style_choice = "GAP Status Code"
# style_choice = "Easement"
# style_choice = "Year"
con = ibis.duckdb.connect()
ca = (con
.read_parquet(ca_parquet)
.cast({"SHAPE": "geometry"})
.mutate(
area=_.SHAPE.area())
# .head()
)
# print(ca.schema())
# ca = ca.filter(ca.Release_Year == 2024)
private_color = "#DE881E" # orange #"#850101" # red
tribal_color = "#BF40BF" # purple
mixed_color = "#005a00" # green
public_color = "#3388ff" # blue
year2023_color = "#26542C" # green
year2024_color = "#F3AB3D" # orange
def summary_table(column, colors):
df = (ca
# .rename(area = "area_square_meters")
.group_by(column)
.aggregate(hectares_protected = (_.area.sum() / 10000).round(),
percent_protected = 100 * _.Acres.sum() / ca_area_acres
)
.mutate(percent_protected = _.percent_protected.round(1),
)
.inner_join(colors, column)
)
df = df.to_pandas()
df[column] = df[column].astype(str)
return df
def area_plot(df, column):
base = alt.Chart(df).encode(
alt.Theta("percent_protected:Q").stack(True),
)
pie = ( base
.mark_arc(innerRadius= 40, outerRadius=100)
.encode(alt.Color("color:N").scale(None).legend(None),
tooltip=['percent_protected', 'hectares_protected', column])
)
text = ( base
.mark_text(radius=80, size=14, color="white")
.encode(text = column + ":N")
)
plot = pie # pie + text
return plot.properties(width="container", height=300)
def pad_style(paint, alpha):
return {
"version": 8,
"sources": {
"ca": {
"type": "vector",
"url": "pmtiles://" + ca_pmtiles,
}},
"layers": [{
"id": "layer1",
"source": "ca",
"source-layer": "CA_Cons_Areas_parentlyr_Merge_ecofix",
"type": "fill",
"paint": {
"fill-color": paint,
"fill-opacity": alpha
}
}]}
manager = {
'property': 'cpad_MNG_AG_LEV',
'type': 'categorical',
'stops': [
['Federal', "darkblue"],
['State', public_color],
['Non Profit', "lightblue"],
['Special District', "darkgreen"],
['', "grey"],
['Joint', "green"],
['Tribal', tribal_color],
['Private', "darkred"],
['City', "pink"],
['County', "blue"],
['Home Owners Association', "lightgreen"]
]
}
easement = {
'property': 'Easement',
'type': 'categorical',
'stops': [
[0, public_color],
[1, private_color]
]
}
year = {
'property': 'Release_Year',
'type': 'categorical',
'stops': [
[2023, year2023_color],
[2024, year2024_color]
]
}
access = {
'property': 'ACCESS_TYP',
'type': 'categorical',
'stops': [
['Open Access', public_color],
['No Public Access', private_color],
['Unknown Access', "grey"],
['Restricted Access', tribal_color]
]
}
gap = {
'property': 'reGAP',
'type': 'categorical',
'stops': [
[1, "#26633d"],
[2, "#879647"],
[3, "#BBBBBB"],
[4, "#F8F8F8"]
]
}
style_options = {
"GAP Status Code": gap,
"Management Agency": manager,
"Easement": easement,
"Public Access": access,
"Year": year
}
st.set_page_config(layout="wide", page_title="CA Protected Areas Explorer", page_icon=":globe:")
'''
# CA 30X30 Prototype
'''
m = leafmap.Map(style="positron")
with st.sidebar:
if st.toggle("Protected Areas", True):
style_choice = st.radio("Color by:", style_options)
alpha = st.slider("transparency", 0.0, 1.0, 0.5)
style = pad_style(style_options[style_choice], alpha)
m.add_pmtiles(ca_pmtiles, style=style, visible=True, opacity=alpha, tooltip=True)
select_column = {
"GAP Status Code": "reGAP",
"Management Agency": "cpad_MNG_AG_LEV",
"Easement": "Easement",
"Year": "Release_Year",
"Public Access": "ACCESS_TYP"}
column = select_column[style_choice]
select_colors = {
"Management Agency": manager["stops"],
"Easement": easement["stops"],
"Public Access": access["stops"],
"Year": year["stops"],
"GAP Status Code": gap["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:
to_streamlit(m, height=700)
df = summary_table(column, colors)
total_percent = df.percent_protected.sum().round(1)
with stats_col:
with st.container():
f"{total_percent}% CA Covered"
st.altair_chart(area_plot(df, column), use_container_width=True)
st.divider()
footer = st.container()
|