Spaces:
Runtime error
Runtime error
File size: 4,259 Bytes
6f8828f 39b11dc d703a85 91cb88f d703a85 3c0eaed 39b11dc 3c0eaed d703a85 98d2d22 3c0eaed 91cb88f 3c0eaed d703a85 91cb88f d703a85 91cb88f d703a85 91cb88f d703a85 91cb88f d703a85 91cb88f d703a85 91cb88f d703a85 0bd11ce d703a85 91cb88f d703a85 91cb88f d703a85 0bd11ce d703a85 98d2d22 91cb88f 98d2d22 91cb88f 98d2d22 91cb88f 98d2d22 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 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 143 144 145 146 147 148 149 150 151 152 |
import leafmap.foliumap as leafmap
from leafmap import pmtiles_style
import rioxarray
import geopandas as gpd
import streamlit as st
import streamlit_folium
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 = pmtiles_style(eurocrops_pmtiles)
m = leafmap.Map(center=[41, -69], zoom=5)
# Moved the cog layers into optional scripts to make the pmtile information easier to see.
# 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,
)
# TODO: automate getting the polygon from the figrue
# TODO: Calculate some metrics given the area.
# TODO: add a raster to the toy
# TODO: Change calculation depending on what layers are present
# TODO: Dynamically check the center of the map and change what's displayed depending on that.
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]]]}}'
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
# NOTE: It is not possible to access user_roi_bounds from streamlit per: https://github.com/opengeos/leafmap/discussions/152#discussioncomment-1870416
# bounds = m.user_roi_bounds()
# if bounds is not None:
# bounds.set_crs("epsg:4326")
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"))
st_data = m.to_streamlit(height=700)
polygon = st_data["last_active_drawing"]
if polygon is not None:
"### The Polygon is: "
st.write(f"{polygon}")
"## Explore Further"
st.write("""
To modify the map, add code to the `code` section in the sidebar to the left. Some examples of code to add are below.
""")
st.code("""
# Show deforested areas:
m.add_cog_layer(
deforest,
palette="reds",
name="deforested",
transparent_bg=True,
opacity=0.5,
zoom_to_layer=False,
)
# Show human impacted areas
m.add_cog_layer(
hi,
palette="purples",
name="human impact",
transparent_bg=True,
opacity=0.5,
zoom_to_layer=False,
)
""")
"""
## 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
"""
|