sunset_simulator/app.py

102 lines
3.8 KiB
Python
Raw Normal View History

2024-10-16 02:47:20 +00:00
import tkinter
import tkintermapview
import requests
import skydome
from PIL import Image, ImageTk
#api altitude endpoint
URL="https://api.open-elevation.com/api/v1/lookup"
def get_api_params(coords):
return {"locations":"{},{}".format(coords[0],coords[1])}
# create tkinter window
root_tk = tkinter.Tk()
root_tk.geometry(f"{1000}x{700}")
root_tk.title("map_view_simple_example.py")
# create map widget
map_widget = tkintermapview.TkinterMapView(root_tk, width=1000, height=700, corner_radius=0)
map_widget.pack(fill="both", expand=True)
# set other tile server (standard is OpenStreetMap)
# map_widget.set_tile_server("https://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}&s=Ga", max_zoom=22) # google normal
# map_widget.set_tile_server("https://mt0.google.com/vt/lyrs=s&hl=en&x={x}&y={y}&z={z}&s=Ga", max_zoom=22) # google satellite
2024-10-18 01:46:41 +00:00
class renderedImageZoom:
def __init__(self,root,coords):
self.image_window = root
self.image_window.title("Simulated Sunset")
self.image_window.config(width=256,height=256)
image = skydome.renderFromCamera(coords)
self.canvas = tkinter.Canvas(self.image_window,bg="white")
self.canvas.pack(fill=tkinter.BOTH,expand=True)
self.img = Image.fromarray(image,mode="RGB")
self.tk_image = ImageTk.PhotoImage(width=256,height=256,image=self.img)
#img = ImageTk.PhotoImage(Image.open("highpress_camera.png"))
self.canvas.create_image(0,0, anchor="nw", image=self.tk_image)
zoom_in_button = tkinter.Button(self.image_window, text="Zoom In", command=self.zoom_in)
zoom_out_button = tkinter.Button(self.image_window, text="Zoom Out", command=self.zoom_out)
zoom_in_button.pack(side=tkinter.LEFT)
zoom_out_button.pack(side=tkinter.LEFT)
self.canvas.bind("<Button-4>",self.zoom_in)
self.canvas.bind("<Button-5>", self.zoom_out)
def zoom_in(self, event=None):
# Increase the image size by a factor (e.g., 1.2)
self.img = self.img.resize((int(self.img.width * 1.2), int(self.img.height * 1.2)))
self.tk_image = ImageTk.PhotoImage(self.img)
self.canvas.delete("all")
self.canvas.create_image(0, 0, anchor=tkinter.NW, image=self.tk_image)
def zoom_out(self, event=None):
# Decrease the image size by a factor (e.g., 0.8)
self.img = self.img.resize((int(self.img.width * 0.8), int(self.img.height * 0.8)))#,resampling)
self.tk_image = ImageTk.PhotoImage(self.img)
self.canvas.delete("all")
self.canvas.create_image(0, 0, anchor=tkinter.NW, image=self.tk_image)
2024-10-16 02:47:20 +00:00
def show_image(coords):
image_window = tkinter.Toplevel()
2024-10-18 01:46:41 +00:00
image_shower = renderedImageZoom(image_window,coords)
2024-10-16 02:47:20 +00:00
image_window.mainloop()
def marker_click(marker):
print(f"marker clicked - text: {marker.text} position: {marker.position}")
def add_marker_event(coords):
print("marker added to {}".format(coords))
map_widget.delete_all_marker()
new_marker = map_widget.set_marker(coords[0], coords[1], text="sunset observer")
def get_altitude(coords):
print("getting altitude for {}\n".format(coords))
params = get_api_params(coords)
r = requests.get(URL,params)
data = r.json()
print(data['results'][0]['elevation'])
map_widget.add_right_click_menu_command(label="Set as observer location",
command=add_marker_event,
pass_coords=True)
map_widget.add_right_click_menu_command(label="get altitude",
command=get_altitude,
pass_coords=True)
map_widget.add_right_click_menu_command(label="open_window",
command=show_image,
pass_coords=True)
root_tk.mainloop()