updated to utils th plot and convert to image solution
This commit is contained in:
@@ -8,7 +8,8 @@ from kivy.properties import StringProperty, ListProperty, AliasProperty
|
||||
from utils import (
|
||||
generate_key, load_key, encrypt_data, decrypt_data,
|
||||
check_server_settings, save_server_settings,
|
||||
test_connection, get_devices_from_server, save_route_to_file, fetch_positions_for_selected_day
|
||||
test_connection, get_devices_from_server, save_route_to_file, fetch_positions_for_selected_day,
|
||||
process_preview_util
|
||||
)
|
||||
from datetime import date
|
||||
from kivy.uix.popup import Popup
|
||||
@@ -32,7 +33,6 @@ from selenium.webdriver.chrome.options import Options
|
||||
from PIL import Image
|
||||
import time
|
||||
import os
|
||||
from utils import html_to_image
|
||||
|
||||
class CreateAnimationScreen(Screen):
|
||||
project_name = StringProperty("")
|
||||
@@ -43,8 +43,7 @@ class CreateAnimationScreen(Screen):
|
||||
project_folder = os.path.join(RESOURCES_FOLDER, "projects", self.project_name)
|
||||
img_path = os.path.join(project_folder, "preview.png")
|
||||
if os.path.exists(img_path):
|
||||
# Add a dummy query string to force reload
|
||||
return f"{img_path}?{int(time.time())}"
|
||||
return img_path
|
||||
return "resources/images/track.png"
|
||||
|
||||
preview_image_source = AliasProperty(get_preview_image_source, None, bind=['project_name'])
|
||||
@@ -211,54 +210,22 @@ class CreateAnimationScreen(Screen):
|
||||
)
|
||||
popup.open()
|
||||
|
||||
def process_preview(dt):
|
||||
try:
|
||||
import folium
|
||||
|
||||
project_folder = os.path.join(RESOURCES_FOLDER, "projects", self.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], zoom_start=14)
|
||||
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.save(html_path)
|
||||
|
||||
# Convert HTML to image
|
||||
html_to_image(html_path, img_path)
|
||||
self.property('preview_image_source').dispatch(self)
|
||||
# Set the image path for Kivy Image widget
|
||||
self.preview_image_path = img_path
|
||||
|
||||
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)
|
||||
|
||||
Clock.schedule_once(process_preview, 0.5)
|
||||
def set_preview_image_path(path):
|
||||
self.preview_image_path = path
|
||||
self.property('preview_image_source').dispatch(self)
|
||||
self.ids.preview_image.reload()
|
||||
# Schedule the processing function
|
||||
Clock.schedule_once(
|
||||
lambda dt: process_preview_util(
|
||||
self.project_name,
|
||||
RESOURCES_FOLDER,
|
||||
label,
|
||||
progress,
|
||||
popup,
|
||||
self.ids.preview_image,
|
||||
set_preview_image_path,
|
||||
Clock
|
||||
),
|
||||
0.5
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user