Spaces:
Sleeping
Sleeping
File size: 4,174 Bytes
e869503 6c6695e c6f4947 e869503 2a55e4a e869503 91489eb e869503 ac1b392 f6b3687 e2f05e5 2a55e4a b0d97c4 e2f05e5 f6b3687 e2f05e5 b0c8ea2 4b509d4 f6b3687 91489eb e2f05e5 22433a9 e2f05e5 b0c8ea2 f6b3687 e869503 9a29af3 d9fb147 e869503 d9fb147 e869503 d9fb147 5747b99 d9fb147 e869503 0105c7c e869503 91489eb e869503 1fe16a2 91489eb e2f05e5 91489eb e869503 c6f4947 91489eb e869503 2a55e4a c6f4947 2a55e4a c6f4947 2a55e4a 864523e e2f05e5 91489eb e2f05e5 b0c8ea2 91489eb e2f05e5 91489eb e2f05e5 91489eb e2f05e5 |
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 |
# -*- 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.
"""An example of showing species richness from GBIF data."""
import pydeck as pdk
import streamlit as st
import ibis
from ibis import _
# SETTING PAGE CONFIG TO WIDE MODE AND ADDING A TITLE AND FAVICON
st.set_page_config(layout="wide", page_title="GBIF Biodiversity Demo", page_icon=":globe:")
# +
@st.cache_data
def load_data(zoom=7):
con = ibis.duckdb.connect()
path = "gbif-vert-gb-h3z" + str(zoom) + ".csv"
df_all = (
con.
read_csv(path).
group_by(_.h3).
aggregate(n = _.n.sum()).
mutate(color = 255 * _.n / _.n.max()).
to_pandas()
)
return df_all
@st.cache_data
def load_class(taxa, zoom=7):
con = ibis.duckdb.connect()
path = "gbif-vert-gb-h3z" + str(zoom) + ".csv"
df = (con.
read_csv(path).
filter(_['class']==taxa).
mutate(color = 255 * _.n / _.n.max()).
to_pandas()
)
return df
# -
def map(data, lat, lon, zoom):
st.write(
pdk.Deck(
map_style="mapbox://styles/mapbox/light-v9",
initial_view_state={
"latitude": lat,
"longitude": lon,
"zoom": zoom,
"pitch": 50,
},
layers=[
pdk.Layer(
"H3HexagonLayer",
data,
pickable=True,
stroked=True,
filled=True,
extruded=True,
elevation_scale=200,
get_elevation='color',
get_hexagon="h3", # set by zoom
get_fill_color="[color, 30, 255 - color, 160]",
get_line_color=[255, 255, 255],
line_width_min_pixels=2,
),
],
)
)
# LAYING OUT THE TOP SECTION OF THE APP
row1_1, row1_2 = st.columns((2, 3))
## Interactive elements, see https://docs.streamlit.io/library/api-reference/widgets
with row1_1:
st.title("GBIF Species Richness")
taxa = st.text_input('taxonomic class (Chordates only)', value = "Amphibia")
zoom = st.slider("Select hex resolution", 4, 7, key="zoom", value = 5)
with row1_2:
st.write(
"""
## GBIF Streamlit demo
This shows a simple interactive demonstration using a series of [pydeck maps](https://deckgl.readthedocs.io/) overlaid with
[h3 hexagons](https://h3geo.org/) whose height and color indicate species richness of the group.
Adjust the [h3 hexagon size](https://h3geo.org/docs/core-library/restable/) to increase the geographic area over which richness coordinates are aggregated. Select a focal taxonomic group to compare, zoom in and out of maps.
Data derived from GBIF 2024-02-01 and h3 aggregations are pre-computed for cell sizes 1-7 across the UK using [duckdb-hs](https://github.com/isaacbrodsky/h3-duckdb)
"""
)
# +
# LAYING OUT THE MIDDLE SECTION OF THE APP WITH THE MAPS
# -
# SETTING THE ZOOM LOCATIONS
midpoint = (52.0, -1.0) #mpoint(data["lat"], data["lon"])
# STREAMLIT APP LAYOUT
# +
row2_1, row2_2, row2_3, row2_4 = st.columns((2, 1, 1, 1))
with row2_1:
st.write(
f"""**All**"""
)
map(load_data(zoom=zoom), midpoint[0], midpoint[1], 3)
with row2_2:
st.write(f"Selected class is {taxa}")
map(load_class(taxa=taxa, zoom=zoom), midpoint[0], midpoint[1], 3)
with row2_3:
st.write("**Mammals**")
map(load_class("Mammalia", zoom=zoom), midpoint[0], midpoint[1], 4)
with row2_4:
st.write("**Birds**")
map(load_class("Aves", zoom=zoom), midpoint[0], midpoint[1], 4)
|