File size: 2,982 Bytes
6f8828f
d703a85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c0eaed
 
 
 
d703a85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c0eaed
 
 
d703a85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import leafmap.foliumap as leafmap
import rioxarray
import geopandas as gpd
import streamlit as st


st.set_page_config(layout="wide", page_title="Huggingface Toy", page_icon="❦")

"""
# Huggingface Streamlit Toy

This is a toy to play around with Huggingface, Leafmap, and Streamlit

I'm looking to implement at least these things:
* Visualize raster data from an S3 bucket
* Let the user (you!) draw polygons and calculate some stats (mean, median, etc.)
* Let user load pre-defined geometries (admin levels? country borders?)
    * Load boundaries based on the center of the map
"""


hi = "https://data.source.coop/vizzuality/hfp-100/hfp_2021_100m_v1-2_cog.tif"
deforest = "https://data.source.coop/vizzuality/lg-land-carbon-data/deforest_carbon_100m_cog.tif"

# Euromap info
eurocrops_pmtiles = "https://s3.us-west-2.amazonaws.com/us-west-2.opendata.source.coop/cholmes/eurocrops/eurocrops-all.pmtiles"
ec_style = leafmap.pmtiles_style(eurocrops_pmtiles)

m = leafmap.Map(center=[41, -69], zoom=5)
m.add_cog_layer(
    deforest,
    palette="reds",
    name="deforested",
    transparent_bg=True,
    opacity=0.5,
    zoom_to_layer=False,
)
m.add_cog_layer(
    hi,
    palette="purples",
    name="human impact",
    transparent_bg=True,
    opacity=0.5,
    zoom_to_layer=False,
)
m.add_pmtiles(
    eurocrops_pmtiles, name="euro crops", style=ec_style, overlay=True, show=True, zoom_to_layer=False
)


polygon_ex ='{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-124.628906,34.741612],[-124.628906,42.423457],[-117.246094,42.423457],[-117.246094,34.741612],[-124.628906,34.741612]]]}}'
code_ex = '''
m.add_gdf(geo, layer_name="selection")
'''


## Map controls sidebar
with st.sidebar:
    '''
        # Map controls
            
        ###  Polygon selector
    '''
    
    polygon = st.text_area(
        label = "geometry",
        value = polygon_ex, 
        height = 400)
    '''
    ### python code for map layer:
    adjust options or add additional layers using leafmap
    '''
    code = st.text_area(
        label = "code:",
        value = code_ex, 
        height = 400)

# Here we actually compute the total carbon in the requested polygon
geo = gpd.read_file(polygon, driver='GeoJSON')
geo.set_crs('epsg:4326')
x = (
        rioxarray.
        open_rasterio('/vsicurl/'+deforest, masked=True).
        rio.clip(geo.geometry.values, geo.crs, from_disk=True).
        mean()
)
value = x


"### Average of tons of carbon lost:"
st.write(f'{value:,}')
st.divider()

# run whatever python code is in the python box, just for fun
eval(compile(code, "<string>", "exec"))
m.to_streamlit(height=700)


"""
## Credits

This toy was inspired by boettiger-lab's Carbon Calculator Demo: https://huggingface.co/spaces/boettiger-lab/leafmap

### Data Sources

- 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
"""