updated to utils th plot and convert to image solution

This commit is contained in:
2025-06-06 16:38:52 +03:00
parent 5627c790f5
commit 6240042901
11 changed files with 311 additions and 97 deletions

View File

@@ -208,4 +208,70 @@ def html_to_image(html_path, img_path, width=800, height=600, delay=2, driver_pa
print(f"Image saved to: {img_path}")
except Exception as e:
print(f"Error converting HTML to image: {e}")
driver.quit()
driver.quit()
def process_preview_util(
project_name,
RESOURCES_FOLDER,
label,
progress,
popup,
preview_image_widget,
set_preview_image_path,
Clock,
width=800,
height=600
):
import folium
import os
import json
from utils import html_to_image
try:
project_folder = os.path.join(RESOURCES_FOLDER, "projects", project_name)
positions_path = os.path.join(project_folder, "positions.json")
html_path = os.path.join(project_folder, "preview.html")
img_path = os.path.join(project_folder, "preview.png")
if not os.path.exists(positions_path):
label.text = "positions.json not found!"
progress.value = 100
return
with open(positions_path, "r") as f:
positions = json.load(f)
if not positions:
label.text = "No positions to preview."
progress.value = 100
return
coords = [(pos['latitude'], pos['longitude']) for pos in positions]
m = folium.Map(
location=coords[0],
width=width,
height=height
)
folium.PolyLine(coords, color="blue", weight=4.5, opacity=1).add_to(m)
folium.Marker(coords[0], tooltip="Start", icon=folium.Icon(color="green")).add_to(m)
folium.Marker(coords[-1], tooltip="End", icon=folium.Icon(color="red")).add_to(m)
m.fit_bounds(coords)
m.save(html_path)
html_to_image(html_path, img_path, width=width, height=height)
set_preview_image_path(img_path)
preview_image_widget.reload()
label.text = "Preview ready!"
progress.value = 100
def close_popup(dt):
popup.dismiss()
Clock.schedule_once(close_popup, 1)
except Exception as e:
label.text = f"Error: {e}"
progress.value = 100
def close_popup(dt):
popup.dismiss()
Clock.schedule_once(close_popup, 2)