Spaces:
Sleeping
Sleeping
| 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() | |