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