Spaces:
Running
Running
File size: 3,903 Bytes
db9dca3 |
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 |
import branca
import folium
import geopandas as gpd
import streamlit as st
from folium.plugins import Draw
def get_map_crop():
m = folium.Map(
location=[50, 32],
zoom_start=4,
tiles="Esri.WorldImagery",
attributionControl=False,
prefer_canvas=True,
)
Draw(
export=False,
show_geometry_on_click=False,
draw_options={
"polyline": False,
"polygon": False,
"circle": False,
"circlemarker": False,
"marker": False,
},
).add_to(m)
legend_html = """
{% macro html(this, kwargs) %}
<div style="
position: fixed;
bottom: 35px;
left: 30px;
width: 130px;
line-height: 3px;
height: 120px;
z-index:9999;
font-size:14px;
">
<p><a style="color:#00ff00;font-size:150%;margin-left:20px;">◼</a> Grassland</p>
<p><a style="color:#8b4513;font-size:150%;margin-left:20px;">◼</a> Cereals</p>
<p><a style="color:#ffffff;font-size:150%;margin-left:20px;">◼</a> Other</p>
<p><a style="color:#00ffff;font-size:150%;margin-left:20px;">◼</a> Flowers</p>
<p><a style="color:#800080;font-size:150%;margin-left:20px;">◼</a> Vegetables</p>
<p><a style="color:#006400;font-size:150%;margin-left:20px;">◼</a> Fruit trees</p>
</div>
<div style="
position: fixed;
bottom: 50px;
left: 30px;
width: 130px;
line-height: 3px;
height: 120px;
z-index:9998;
font-size:14px;
background-color: #ffffff;
opacity: 0.85;
">
</div>
{% endmacro %}
"""
legend = branca.element.MacroElement()
legend._template = branca.element.Template(legend_html)
m.get_root().add_child(legend)
return m
def get_parquet(tile_path, id_classes):
data = gpd.read_parquet(tile_path).to_crs(epsg="4326")
data["class_name"] = [id_classes[i] for i in data["class"]]
data["geometry"] = data["geometry"].simplify(0.0001)
return data
def draw_gdf(
_map,
tile_path,
name,
_style_call,
id_classes,
):
if tile_path not in st.session_state.keys():
tile_gdf = get_parquet(tile_path, id_classes)
else:
tile_gdf = st.session_state[tile_path]
feature_group = folium.FeatureGroup(f"{name} layer")
tooltip = folium.GeoJsonTooltip(
fields=["class_name", "area"],
aliases=["Crop type: \t", "Area (m²): \t"],
localize=True,
sticky=False,
labels=True,
style="""
background-color: #F0EFEF;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
max_width=800,
)
folium.GeoJson(tile_gdf, style_function=_style_call, tooltip=tooltip).add_to(
feature_group
)
feature_group.add_to(_map)
bound = feature_group.get_bounds()
_map.fit_bounds(bound)
def get_clean_rendering_container(app_state: str):
"""Makes sure we can render from a clean slate on state changes."""
slot_in_use = st.session_state.slot_in_use = st.session_state.get(
"slot_in_use", "a"
)
if app_state != st.session_state.get("previous_state", app_state):
if slot_in_use == "a":
slot_in_use = st.session_state.slot_in_use = "b"
else:
slot_in_use = st.session_state.slot_in_use = "a"
st.session_state.previous_state = app_state
slot = {
"a": st.empty(),
"b": st.empty(),
}[slot_in_use]
return slot.container()
|