Skip to content

Commit 93bb417

Browse files
committed
WebUI refactoring WIP.
1 parent f4fb8cf commit 93bb417

File tree

5 files changed

+23
-75
lines changed

5 files changed

+23
-75
lines changed

dev/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ owslib
2222
tqdm
2323
types-tqdm
2424
scipy
25-
schedule
25+
schedule
26+
streamlit-folium

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ pygmdl
1515
owslib
1616
tqdm
1717
scipy
18-
schedule
18+
schedule
19+
streamlit-folium

webui/generator/main_settings.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import config
44
import osmp
55
import streamlit as st
6-
import streamlit.components.v1 as components
76
from generator.base_component import BaseComponent
7+
from streamlit_folium import folium_static
88
from templates import Messages
99

1010
import maps4fs as mfs
@@ -113,10 +113,10 @@ def map_preview(self) -> None:
113113
return
114114

115115
map_size = self.map_size_input
116-
html_file = osmp.get_rotated_preview(lat, lon, map_size, angle=-self.rotation)
116+
map = osmp.get_rotated_preview(lat, lon, map_size, angle=-self.rotation)
117117

118118
with self.html_preview_container:
119-
components.html(open(html_file).read(), height=600)
119+
folium_static(map, height=600, width=600)
120120

121121
def provider_info(self) -> None:
122122
provider_code = self.dtm_provider_code

webui/osmp.py

Lines changed: 12 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import math
2-
import os
32
import random
43

5-
import config
64
import folium
75
import osmnx as ox
86

97

10-
def get_rotated_preview(lat: float, lon: float, distance: int, angle: int):
8+
def get_rotated_preview(lat: float, lon: float, distance: int, angle: int) -> folium.Map:
119
"""Return the path to the HTML file where the OpenStreetMap data is saved.
1210
1311
Arguments:
@@ -17,12 +15,8 @@ def get_rotated_preview(lat: float, lon: float, distance: int, angle: int):
1715
angle (int): Angle of rotation in degrees.
1816
1917
Returns:
20-
str: Path to the HTML file.
18+
folium.Map: Folium map object.
2119
"""
22-
save_path = get_rotated_save_path(lat, lon, distance, angle)
23-
if os.path.isfile(save_path):
24-
return save_path
25-
2620
m = folium.Map(zoom_control=False)
2721

2822
url = "https://mt0.google.com/vt/lyrs=s&hl=en&x={x}&y={y}&z={z}&s=Ga"
@@ -56,15 +50,18 @@ def get_rotated_preview(lat: float, lon: float, distance: int, angle: int):
5650
center = get_center(bbox)
5751
folium.CircleMarker(center, radius=1, color=color, fill=True).add_to(m)
5852

59-
m.save(save_path)
60-
return save_path
53+
return m
54+
6155

56+
def get_preview(bboxes: list[tuple[float, float, float, float]]) -> folium.Map:
57+
"""Returns the folium map object with the bounding boxes.
6258
63-
def get_preview(bboxes: list[tuple[float, float, float, float]]) -> str:
64-
save_path = get_save_path(bboxes)
65-
if os.path.isfile(save_path):
66-
return save_path
59+
Arguments:
60+
bboxes (list[tuple[float, float, float, float]]): List of bounding boxes.
6761
62+
Returns:
63+
folium.Map: Folium map object.
64+
"""
6865
m = folium.Map(zoom_control=False)
6966

7067
for bbox in bboxes:
@@ -86,8 +83,7 @@ def get_preview(bboxes: list[tuple[float, float, float, float]]) -> str:
8683
# Fit bounds to the last bbox in the list.
8784
m.fit_bounds([[south, west], [north, east]])
8885

89-
m.save(save_path)
90-
return save_path
86+
return m
9187

9288

9389
def get_random_color() -> str:
@@ -105,53 +101,3 @@ def get_bbox(center: tuple[float, float], size_meters: int) -> tuple[float, floa
105101
(center_lat, center_lon), size_meters / 2, project_utm=False
106102
)
107103
return north, south, east, west
108-
109-
110-
def get_save_path(bboxes: list[tuple[float, float, float, float]]) -> str:
111-
"""Return the path to the HTML file where the OpenStreetMap data is saved.
112-
113-
Arguments:
114-
lat (float): Latitude of the central point.
115-
lon (float): Longitude of the central point.
116-
size_meters (int): Width of the bounding box in meters.
117-
postfix (str): Optional postfix to add to the filename.
118-
119-
Returns:
120-
str: Path to the HTML file.
121-
"""
122-
file_names = [format_coordinates(bbox) for bbox in bboxes]
123-
filename = "_".join(file_names) + ".html"
124-
return os.path.join(
125-
config.OSMPS_DIRECTORY,
126-
filename,
127-
)
128-
129-
130-
def get_rotated_save_path(lat: float, lon: float, size_meters: int, angle: int) -> str:
131-
"""Return the path to the HTML file where the OpenStreetMap data is saved.
132-
133-
Arguments:
134-
lat (float): Latitude of the central point.
135-
lon (float): Longitude of the central point.
136-
size_meters (int): Width of the bounding box in meters.
137-
angle (int): Angle of rotation in degrees.
138-
139-
Returns:
140-
str: Path to the HTML file.
141-
"""
142-
return os.path.join(
143-
config.OSMPS_DIRECTORY,
144-
f"{lat}_{lon}_{size_meters}_{angle}.html",
145-
)
146-
147-
148-
def format_coordinates(bbox: tuple[float, float, float, float]) -> str:
149-
"""Return a string representation of the coordinates.
150-
151-
Arguments:
152-
bbox (tuple[float, float, float, float]): The bounding box coordinates.
153-
154-
Returns:
155-
str: String representation of the coordinates.
156-
"""
157-
return "_".join(map(str, bbox))

webui/tools/dem.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import os
22

33
import streamlit as st
4-
import streamlit.components.v1 as components
54
from config import INPUT_DIRECTORY, is_on_community_server, is_public
65
from osmp import get_bbox, get_center, get_preview
7-
from tools.tool import Tool
6+
from streamlit_folium import folium_static
87
from templates import Messages
8+
from tools.tool import Tool
99

1010
from maps4fs.toolbox.dem import extract_roi, get_geo_tiff_bbox, read_geo_tiff
1111

@@ -122,10 +122,10 @@ def get_preview(self):
122122
roi_bbox = get_bbox(roi_center, roi_size)
123123
bboxes = [roi_bbox, self.full_bbox]
124124

125-
html_file = get_preview(bboxes)
125+
map = get_preview(bboxes)
126126

127127
with self.html_preview_container:
128-
components.html(open(html_file).read(), height=600)
128+
folium_static(map, height=600, width=600)
129129

130130
@property
131131
def lat_lon(self) -> tuple[float, float]:

0 commit comments

Comments
 (0)