Spaces:
Sleeping
Sleeping
less whitespace?
Browse files
app.py
CHANGED
|
@@ -34,45 +34,27 @@ Map will zoom in on selected area and display the tons of carbon lost between 20
|
|
| 34 |
Data comes from Vizzuality repo on [source.coop](https://beta.source.coop/repositories/vizzuality/lg-land-carbon-data/description/).
|
| 35 |
'''
|
| 36 |
|
| 37 |
-
|
| 38 |
deforest = "https://data.source.coop/vizzuality/lg-land-carbon-data/deforest_carbon_100m_cog.tif"
|
| 39 |
irrecoverable = "https://data.source.coop/cboettig/carbon/cogs/irrecoverable_c_total_2018.tif"
|
| 40 |
vulnerable = "https://data.source.coop/cboettig/carbon/cogs/vulnerable_c_total_2018.tif"
|
| 41 |
|
| 42 |
m = leafmap.Map(center=[35, -100], zoom=5)
|
| 43 |
m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/deforest_carbon_100m_cog.tif", palette="reds", name="deforested", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
|
| 44 |
-
|
| 45 |
-
# m.add_cog_layer(deforest, palette="reds", name="deforested", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
|
| 46 |
-
# m.add_cog_layer("https://data.source.coop/cboettig/carbon/cogs/irrecoverable_c_total_2018.tif", palette="reds", name="irrecoverable carbon", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
|
| 47 |
-
# m.add_cog_layer("https://data.source.coop/cboettig/carbon/cogs/vulnerable_c_total_2018.tif", palette="purples", name="vulnerable carbon", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
|
| 48 |
-
# m.add_cog_layer("https://data.source.coop/cboettig/carbon/cogs/irrecoverable_c_total_2018.tif", palette="greens", name="irreplacable carbon", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
|
| 49 |
# -
|
| 50 |
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# +
|
| 56 |
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]]]}}'
|
| 57 |
-
|
| 58 |
-
|
| 59 |
code_ex = '''
|
| 60 |
m.add_gdf(geo, layer_name="selection")
|
| 61 |
'''
|
| 62 |
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
# +
|
| 67 |
## Map controls sidebar
|
| 68 |
-
|
| 69 |
with st.sidebar:
|
| 70 |
-
|
| 71 |
-
"# Map controls"
|
| 72 |
-
|
| 73 |
'''
|
| 74 |
-
#
|
| 75 |
|
|
|
|
| 76 |
'''
|
| 77 |
|
| 78 |
polygon = st.text_area(
|
|
@@ -80,7 +62,6 @@ with st.sidebar:
|
|
| 80 |
value = polygon_ex,
|
| 81 |
height = 400)
|
| 82 |
|
| 83 |
-
|
| 84 |
"### python code for map layer:"
|
| 85 |
"adjust options or add additional layers using leafmap"
|
| 86 |
code = st.text_area(
|
|
@@ -89,11 +70,9 @@ with st.sidebar:
|
|
| 89 |
height = 400)
|
| 90 |
|
| 91 |
|
| 92 |
-
|
| 93 |
-
# +
|
| 94 |
geo = gpd.read_file(polygon, driver='GeoJSON')
|
| 95 |
geo.set_crs('epsg:4326')
|
| 96 |
-
|
| 97 |
x = (rioxarray.
|
| 98 |
open_rasterio('/vsicurl/'+deforest, masked=True).
|
| 99 |
rio.clip(geo.geometry.values, geo.crs, from_disk=True).
|
|
@@ -102,22 +81,15 @@ x = (rioxarray.
|
|
| 102 |
value = float(x)
|
| 103 |
|
| 104 |
|
| 105 |
-
# +
|
| 106 |
-
|
| 107 |
"### Tons of carbon lost:"
|
| 108 |
-
|
| 109 |
st.write(f'{value:,}')
|
| 110 |
-
|
| 111 |
st.divider()
|
| 112 |
-
# -
|
| 113 |
|
|
|
|
| 114 |
eval(compile(code, "<string>", "exec"))
|
| 115 |
m.to_streamlit(height=700)
|
| 116 |
|
| 117 |
-
|
| 118 |
-
# +
|
| 119 |
"## Explore further"
|
| 120 |
-
|
| 121 |
st.write('''
|
| 122 |
Try adding additional options to the map. Some suggested examples are shown below.
|
| 123 |
(Of course any self-respecting streamlit app would make these into toggle buttons,
|
|
@@ -127,13 +99,7 @@ To explore further, simply modify the Streamlit app.py file from the Files menu
|
|
| 127 |
'''
|
| 128 |
)
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
# +
|
| 133 |
-
|
| 134 |
-
|
| 135 |
st.code('''
|
| 136 |
-
|
| 137 |
# irrecoverable carbon (Conservation International):
|
| 138 |
carbon = "https://data.source.coop/cboettig/carbon/cogs/irrecoverable_c_total_2018.tif"
|
| 139 |
m.add_cog_layer(carbon, palette="reds", name="irrecoverable carbon", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)')
|
|
@@ -150,24 +116,16 @@ combined_style = {
|
|
| 150 |
"source1": {
|
| 151 |
"type": "vector",
|
| 152 |
"url": "pmtiles://" + usgs,
|
| 153 |
-
"attribution": "USGS",
|
| 154 |
-
|
| 155 |
-
},
|
| 156 |
-
"layers": [
|
| 157 |
-
{
|
| 158 |
"id": "usgs",
|
| 159 |
"source": "source1",
|
| 160 |
"source-layer": "mtbs_perims_DD",
|
| 161 |
"type": "fill",
|
| 162 |
-
"paint": {"fill-color": "#FFA500", "fill-opacity": 0.2}
|
| 163 |
-
},
|
| 164 |
-
]
|
| 165 |
-
}
|
| 166 |
|
| 167 |
m.add_pmtiles(usgs, name="Fire", style=combined_style, overlay=True, show=True, zoom_to_layer=False)
|
| 168 |
-
''')
|
| 169 |
-
|
| 170 |
-
|
| 171 |
|
| 172 |
|
| 173 |
# +
|
|
@@ -184,6 +142,4 @@ st.divider()
|
|
| 184 |
- Irrecoverable Carbon from Conservation International, reprocessed to COG on https://beta.source.coop/cboettig/carbon, citation: https://doi.org/10.1038/s41893-021-00803-6, License: CC-BY-NC
|
| 185 |
|
| 186 |
Software stack: Streamlit (python) app hosted on free-tier HuggingFace spaces. Mapping with Leafmap.
|
| 187 |
-
|
| 188 |
-
|
| 189 |
'''
|
|
|
|
| 34 |
Data comes from Vizzuality repo on [source.coop](https://beta.source.coop/repositories/vizzuality/lg-land-carbon-data/description/).
|
| 35 |
'''
|
| 36 |
|
|
|
|
| 37 |
deforest = "https://data.source.coop/vizzuality/lg-land-carbon-data/deforest_carbon_100m_cog.tif"
|
| 38 |
irrecoverable = "https://data.source.coop/cboettig/carbon/cogs/irrecoverable_c_total_2018.tif"
|
| 39 |
vulnerable = "https://data.source.coop/cboettig/carbon/cogs/vulnerable_c_total_2018.tif"
|
| 40 |
|
| 41 |
m = leafmap.Map(center=[35, -100], zoom=5)
|
| 42 |
m.add_cog_layer("https://data.source.coop/vizzuality/lg-land-carbon-data/deforest_carbon_100m_cog.tif", palette="reds", name="deforested", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
# -
|
| 44 |
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
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]]]}}'
|
|
|
|
|
|
|
| 47 |
code_ex = '''
|
| 48 |
m.add_gdf(geo, layer_name="selection")
|
| 49 |
'''
|
| 50 |
|
| 51 |
|
|
|
|
|
|
|
|
|
|
| 52 |
## Map controls sidebar
|
|
|
|
| 53 |
with st.sidebar:
|
|
|
|
|
|
|
|
|
|
| 54 |
'''
|
| 55 |
+
# Map controls
|
| 56 |
|
| 57 |
+
### Polygon selector
|
| 58 |
'''
|
| 59 |
|
| 60 |
polygon = st.text_area(
|
|
|
|
| 62 |
value = polygon_ex,
|
| 63 |
height = 400)
|
| 64 |
|
|
|
|
| 65 |
"### python code for map layer:"
|
| 66 |
"adjust options or add additional layers using leafmap"
|
| 67 |
code = st.text_area(
|
|
|
|
| 70 |
height = 400)
|
| 71 |
|
| 72 |
|
| 73 |
+
# Here we actually compute the total carbon in the requested polygon
|
|
|
|
| 74 |
geo = gpd.read_file(polygon, driver='GeoJSON')
|
| 75 |
geo.set_crs('epsg:4326')
|
|
|
|
| 76 |
x = (rioxarray.
|
| 77 |
open_rasterio('/vsicurl/'+deforest, masked=True).
|
| 78 |
rio.clip(geo.geometry.values, geo.crs, from_disk=True).
|
|
|
|
| 81 |
value = float(x)
|
| 82 |
|
| 83 |
|
|
|
|
|
|
|
| 84 |
"### Tons of carbon lost:"
|
|
|
|
| 85 |
st.write(f'{value:,}')
|
|
|
|
| 86 |
st.divider()
|
|
|
|
| 87 |
|
| 88 |
+
# run whatever python code is in the python box, just for fun
|
| 89 |
eval(compile(code, "<string>", "exec"))
|
| 90 |
m.to_streamlit(height=700)
|
| 91 |
|
|
|
|
|
|
|
| 92 |
"## Explore further"
|
|
|
|
| 93 |
st.write('''
|
| 94 |
Try adding additional options to the map. Some suggested examples are shown below.
|
| 95 |
(Of course any self-respecting streamlit app would make these into toggle buttons,
|
|
|
|
| 99 |
'''
|
| 100 |
)
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
st.code('''
|
|
|
|
| 103 |
# irrecoverable carbon (Conservation International):
|
| 104 |
carbon = "https://data.source.coop/cboettig/carbon/cogs/irrecoverable_c_total_2018.tif"
|
| 105 |
m.add_cog_layer(carbon, palette="reds", name="irrecoverable carbon", transparent_bg=True, opacity = 0.8, zoom_to_layer=False)')
|
|
|
|
| 116 |
"source1": {
|
| 117 |
"type": "vector",
|
| 118 |
"url": "pmtiles://" + usgs,
|
| 119 |
+
"attribution": "USGS"}},
|
| 120 |
+
"layers": [{
|
|
|
|
|
|
|
|
|
|
| 121 |
"id": "usgs",
|
| 122 |
"source": "source1",
|
| 123 |
"source-layer": "mtbs_perims_DD",
|
| 124 |
"type": "fill",
|
| 125 |
+
"paint": {"fill-color": "#FFA500", "fill-opacity": 0.2}}]}
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
m.add_pmtiles(usgs, name="Fire", style=combined_style, overlay=True, show=True, zoom_to_layer=False)
|
| 128 |
+
''')
|
|
|
|
|
|
|
| 129 |
|
| 130 |
|
| 131 |
# +
|
|
|
|
| 142 |
- Irrecoverable Carbon from Conservation International, reprocessed to COG on https://beta.source.coop/cboettig/carbon, citation: https://doi.org/10.1038/s41893-021-00803-6, License: CC-BY-NC
|
| 143 |
|
| 144 |
Software stack: Streamlit (python) app hosted on free-tier HuggingFace spaces. Mapping with Leafmap.
|
|
|
|
|
|
|
| 145 |
'''
|