Jonathan Marokhovsky commited on
Commit
d703a85
1 Parent(s): 1492e6f

First pass at the toy, starting from code present in the Carbon Calculator demo

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import leafmap
2
+ import rioxarray
3
+ import geopandas as gpd
4
+ import streamlit as st
5
+
6
+
7
+ st.set_page_config(layout="wide", page_title="Huggingface Toy", page_icon="❦")
8
+
9
+ """
10
+ # Huggingface Streamlit Toy
11
+
12
+ This is a toy to play around with Huggingface, Leafmap, and Streamlit
13
+
14
+ I'm looking to implement at least these things:
15
+ * Visualize raster data from an S3 bucket
16
+ * Let the user (you!) draw polygons and calculate some stats (mean, median, etc.)
17
+ * Let user load pre-defined geometries (admin levels? country borders?)
18
+ * Load boundaries based on the center of the map
19
+ """
20
+
21
+
22
+ hi = "https://data.source.coop/vizzuality/hfp-100/hfp_2021_100m_v1-2_cog.tif"
23
+ deforest = "https://data.source.coop/vizzuality/lg-land-carbon-data/deforest_carbon_100m_cog.tif"
24
+
25
+ m = leafmap.Map(center=[41, -69], zoom=5)
26
+ m.add_cog_layer(
27
+ deforest,
28
+ palette="reds",
29
+ name="deforested",
30
+ transparent_bg=True,
31
+ opacity=0.5,
32
+ zoom_to_layer=False,
33
+ )
34
+ m.add_cog_layer(
35
+ hi,
36
+ palette="purples",
37
+ name="human impact",
38
+ transparent_bg=True,
39
+ opacity=0.5,
40
+ zoom_to_layer=False,
41
+ )
42
+
43
+
44
+ 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]]]}}'
45
+ code_ex = '''
46
+ m.add_gdf(geo, layer_name="selection")
47
+ '''
48
+
49
+
50
+ ## Map controls sidebar
51
+ with st.sidebar:
52
+ '''
53
+ # Map controls
54
+
55
+ ### Polygon selector
56
+ '''
57
+
58
+ polygon = st.text_area(
59
+ label = "geometry",
60
+ value = polygon_ex,
61
+ height = 400)
62
+ '''
63
+ ### python code for map layer:
64
+ adjust options or add additional layers using leafmap
65
+ '''
66
+ code = st.text_area(
67
+ label = "code:",
68
+ value = code_ex,
69
+ height = 400)
70
+
71
+ # Here we actually compute the total carbon in the requested polygon
72
+ geo = gpd.read_file(polygon, driver='GeoJSON')
73
+ geo.set_crs('epsg:4326')
74
+ x = (
75
+ rioxarray.
76
+ open_rasterio('/vsicurl/'+deforest, masked=True).
77
+ rio.clip(geo.geometry.values, geo.crs, from_disk=True).
78
+ mean()
79
+ )
80
+ value = x
81
+
82
+
83
+ "### Average of tons of carbon lost:"
84
+ st.write(f'{value:,}')
85
+ st.divider()
86
+
87
+ # run whatever python code is in the python box, just for fun
88
+ eval(compile(code, "<string>", "exec"))
89
+ m.to_streamlit(height=700)
90
+
91
+
92
+ """
93
+ ## Credits
94
+
95
+ This toy was inspired by boettiger-lab's Carbon Calculator Demo: https://huggingface.co/spaces/boettiger-lab/leafmap
96
+
97
+ ### Data Sources
98
+
99
+ - 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
100
+ """