Jonathan Marokhovsky commited on
Commit
91cb88f
1 Parent(s): 6aa0f57

Reformatted the code and added streamlit_folium to the requirements

Browse files
Files changed (2) hide show
  1. app.py +31 -26
  2. requirements.txt +1 -0
app.py CHANGED
@@ -3,6 +3,7 @@ from leafmap import pmtiles_style
3
  import rioxarray
4
  import geopandas as gpd
5
  import streamlit as st
 
6
 
7
 
8
  st.set_page_config(layout="wide", page_title="Huggingface Toy", page_icon="❦")
@@ -46,51 +47,54 @@ m = leafmap.Map(center=[41, -69], zoom=5)
46
  # zoom_to_layer=False,
47
  # )
48
  m.add_pmtiles(
49
- eurocrops_pmtiles, name="euro crops", style=ec_style, overlay=True, show=True, zoom_to_layer=False
 
 
 
 
 
50
  )
51
 
 
 
 
 
 
52
 
53
- polygon_ex ='{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-6.108398,40.780541],[-6.108398,54.342149],[15.908203,54.342149],[15.908203,40.780541],[-6.108398,40.780541]]]}}'
54
- code_ex = '''
55
  m.add_gdf(geo, layer_name="selection")
56
- '''
57
 
58
 
59
  ## Map controls sidebar
60
  with st.sidebar:
61
- '''
62
  # Map controls
63
 
64
  ### Polygon selector
65
- '''
66
-
67
- polygon = st.text_area(
68
- label = "geometry",
69
- value = polygon_ex,
70
- height = 400)
71
- '''
72
  ### python code for map layer:
73
  adjust options or add additional layers using leafmap
74
- '''
75
- code = st.text_area(
76
- label = "code:",
77
- value = code_ex,
78
- height = 400)
79
 
80
  # Here we actually compute the total carbon in the requested polygon
81
  geo = gpd.read_file(polygon, driver='GeoJSON')
82
  geo.set_crs('epsg:4326')
83
  x = (
84
- rioxarray.
85
- open_rasterio('/vsicurl/'+deforest, masked=True).
86
- rio.clip(geo.geometry.values, geo.crs, from_disk=True).
87
- mean()
88
  )
89
  value = x
90
 
91
 
92
  "### Average of tons of carbon lost:"
93
- st.write(f'{value:,}')
94
  st.divider()
95
 
96
  # run whatever python code is in the python box, just for fun
@@ -98,10 +102,11 @@ eval(compile(code, "<string>", "exec"))
98
  m.to_streamlit(height=700)
99
 
100
  "## Explore Further"
101
- st.write('''
102
  To modify the map, add code to the `code` section in the sidebar to the left. Some examples of code to add are below.
103
- ''')
104
- st.code('''
 
105
  # Show deforested areas:
106
 
107
  m.add_cog_layer(
@@ -123,7 +128,7 @@ m.add_cog_layer(
123
  opacity=0.5,
124
  zoom_to_layer=False,
125
  )
126
- ''')
127
 
128
 
129
  """
 
3
  import rioxarray
4
  import geopandas as gpd
5
  import streamlit as st
6
+ import streamlit_folium
7
 
8
 
9
  st.set_page_config(layout="wide", page_title="Huggingface Toy", page_icon="❦")
 
47
  # zoom_to_layer=False,
48
  # )
49
  m.add_pmtiles(
50
+ eurocrops_pmtiles,
51
+ name="euro crops",
52
+ style=ec_style,
53
+ overlay=True,
54
+ show=True,
55
+ zoom_to_layer=False,
56
  )
57
 
58
+ # TODO: automate getting the polygon from the figrue
59
+ # TODO: Calculate some metrics given the area.
60
+ # TODO: add a raster to the toy
61
+ # TODO: Change calculation depending on what layers are present
62
+ # TODO: Dynamically check the center of the map and change what's displayed depending on that.
63
 
64
+ polygon_ex = '{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-6.108398,40.780541],[-6.108398,54.342149],[15.908203,54.342149],[15.908203,40.780541],[-6.108398,40.780541]]]}}'
65
+ code_ex = """
66
  m.add_gdf(geo, layer_name="selection")
67
+ """
68
 
69
 
70
  ## Map controls sidebar
71
  with st.sidebar:
72
+ """
73
  # Map controls
74
 
75
  ### Polygon selector
76
+ """
77
+
78
+ polygon = st.text_area(label="geometry", value=polygon_ex, height=400)
79
+ """
 
 
 
80
  ### python code for map layer:
81
  adjust options or add additional layers using leafmap
82
+ """
83
+ code = st.text_area(label="code:", value=code_ex, height=400)
 
 
 
84
 
85
  # Here we actually compute the total carbon in the requested polygon
86
  geo = gpd.read_file(polygon, driver='GeoJSON')
87
  geo.set_crs('epsg:4326')
88
  x = (
89
+ rioxarray.open_rasterio("/vsicurl/" + deforest, masked=True)
90
+ .rio.clip(geo.geometry.values, geo.crs, from_disk=True)
91
+ .mean()
 
92
  )
93
  value = x
94
 
95
 
96
  "### Average of tons of carbon lost:"
97
+ st.write(f"{value:,}")
98
  st.divider()
99
 
100
  # run whatever python code is in the python box, just for fun
 
102
  m.to_streamlit(height=700)
103
 
104
  "## Explore Further"
105
+ st.write("""
106
  To modify the map, add code to the `code` section in the sidebar to the left. Some examples of code to add are below.
107
+ """)
108
+
109
+ st.code("""
110
  # Show deforested areas:
111
 
112
  m.add_cog_layer(
 
128
  opacity=0.5,
129
  zoom_to_layer=False,
130
  )
131
+ """)
132
 
133
 
134
  """
requirements.txt CHANGED
@@ -4,3 +4,4 @@ leafmap
4
  rioxarray
5
  geopandas
6
  streamlit
 
 
4
  rioxarray
5
  geopandas
6
  streamlit
7
+ streamlit_folium