sunset_simulator/app.py

194 lines
7.9 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()
# 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-11-02 14:04:20 +00:00
def printer():
print("button")
environment_tk = tkinter.Tk()
2024-10-16 02:47:20 +00:00
2024-10-18 01:46:41 +00:00
class renderedImageZoom:
2024-11-02 14:04:20 +00:00
def __init__(self,root,image_window,aerosol_window):
self.root = root
self.root.geometry(f"{1000}x{700}")
self.root.title("map_view_simple_example.py")
self.aerosol_window = aerosol_window
# create map widget
self.map_widget = tkintermapview.TkinterMapView(self.root, width=1000, height=700, corner_radius=0)
self.map_widget.pack(fill="both", expand=True)
self.map_widget.add_right_click_menu_command(label="Set as observer location",
command=self.add_marker_event,
pass_coords=True)
self.map_widget.add_right_click_menu_command(label="get altitude",
command=self.get_altitude,
pass_coords=True)
#self.map_widget.add_right_click_menu_command(label="open_window",
# command=self.show_image,
# pass_coords=True)
self.aerosol_window.title("options selector")
environemnt_info_text = tkinter.Label(self.aerosol_window, text="Please choose in which environment the sunset is occuring")
environemnt_info_text.pack(side='top',pady=20,padx=10)
#Continental Clean - 26e-6 g = 0.709
#Continental Average - 75e-6 g = 0.703
#Continental Polluted - 175e-6 g = 0.698
#
#Urban - 353e-6 g = 0.689
#
#Desert - 145e-6 g = 0.729
#
#Maritime Clean - 90e-6 g = 0.772
#Maritime Polluted - 115e-6 g = 0.756
#Maritime Tropic - 43e-6 g = 0.774
#
#Arctic - 23e-6 g = 0.721
#
#Antarctic - 11e-6 g = 0.784
buton_cont_clean = tkinter.Button(self.aerosol_window,text="Continental Clean",command=lambda: self.env_setter(26e-6,0.709));
buton_cont_clean.pack(side='top',padx=10,pady=5)
buton_cont_avr = tkinter.Button(self.aerosol_window,text="Continental Average ",command=lambda: self.env_setter(75e-6,0.793));
buton_cont_avr.pack(side='top',padx=10,pady=5)
buton_cont_poll = tkinter.Button(self.aerosol_window,text="Continental Polluted ",command=lambda: self.env_setter(175e-6,0.698));
buton_cont_poll.pack(side='top',padx=10,pady=5)
buton_urban = tkinter.Button(self.aerosol_window,text="Urban ",command=lambda: self.env_setter(353e-6,0.689));
buton_urban.pack(side='top',padx=10,pady=5)
buton_desert = tkinter.Button(self.aerosol_window,text="Desert ",command=lambda: self.env_setter(145e-6,0.729));
buton_desert.pack(side='top',padx=10,pady=5)
buton_mar_clean = tkinter.Button(self.aerosol_window,text="Maritime Clean ",command=lambda: self.env_setter(90e-6,0.772));
buton_mar_clean.pack(side='top',padx=10,pady=5)
buton_mar_poll = tkinter.Button(self.aerosol_window,text="Maritime Polluted ",command=lambda: self.env_setter(115e-6,0.756));
buton_mar_poll.pack(side='top',padx=10,pady=5)
buton_mar_tro = tkinter.Button(self.aerosol_window,text="Maritime Tropic ",command=lambda: self.env_setter(43e-6,0.774));
buton_mar_tro.pack(side='top',padx=10,pady=5)
buton_arctic = tkinter.Button(self.aerosol_window,text="Arctic ",command=lambda: self.env_setter(23e-6,0.721));
buton_arctic.pack(side='top',padx=10,pady=5)
buton_antarctic = tkinter.Button(self.aerosol_window,text="Antarctic ",command=lambda: self.env_setter(11e-6,0.784));
buton_antarctic.pack(side='top',padx=10,pady=5)
self.image_window = image_window
2024-10-18 01:46:41 +00:00
self.image_window.title("Simulated Sunset")
self.image_window.config(width=256,height=256)
2024-11-02 14:04:20 +00:00
#image = skydome.renderFromCamera(coords)
2024-10-18 01:46:41 +00:00
self.canvas = tkinter.Canvas(self.image_window,bg="white")
self.canvas.pack(fill=tkinter.BOTH,expand=True)
2024-11-02 14:04:20 +00:00
#self.img = Image.fromarray(image,mode="RGB")
#self.tk_image = ImageTk.PhotoImage(width=256,height=256,image=self.img)
2024-10-18 01:46:41 +00:00
#img = ImageTk.PhotoImage(Image.open("highpress_camera.png"))
2024-11-02 14:04:20 +00:00
#self.canvas.create_image(0,0, anchor="center", image=self.tk_image)
2024-10-18 01:46:41 +00:00
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)
2024-11-02 14:04:20 +00:00
render_button = tkinter.Button(self.image_window,text="render",command=self.render)
2024-10-18 01:46:41 +00:00
zoom_in_button.pack(side=tkinter.LEFT)
zoom_out_button.pack(side=tkinter.LEFT)
2024-11-02 14:04:20 +00:00
render_button.pack(side=tkinter.RIGHT)
2024-10-18 01:46:41 +00:00
self.canvas.bind("<Button-4>",self.zoom_in)
self.canvas.bind("<Button-5>", self.zoom_out)
2024-11-02 14:04:20 +00:00
def env_setter(self,environment_constant,g):
self.betaM = skydome.Vec3(environment_constant,environment_constant,environment_constant)
self.g = g
def render(self):
image = skydome.renderFromCamera(self.coords,self.betaM,self.g,self.altitude)
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)
2024-10-18 01:46:41 +00:00
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")
2024-11-02 14:04:20 +00:00
self.canvas.create_image(0, 0, anchor="nw", image=self.tk_image)
2024-10-18 01:46:41 +00:00
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")
2024-11-02 14:04:20 +00:00
self.canvas.create_image(0, 0, anchor="nw", image=self.tk_image)
2024-10-18 01:46:41 +00:00
2024-11-02 14:04:20 +00:00
# def show_image(self,coords):
# self.coords = coords
#image_shower = renderedImageZoom(image_window,coords)
# self.image_window.mainloop()
2024-10-16 02:47:20 +00:00
2024-11-02 14:04:20 +00:00
def add_marker_event(self,coords):
self.coords = coords
print("marker added to {}".format(self.coords))
self.map_widget.delete_all_marker()
new_marker = self.map_widget.set_marker(self.coords[0], self.coords[1], text="sunset observer")
self.get_altitude(coords)
2024-10-18 01:46:41 +00:00
2024-11-02 14:04:20 +00:00
def marker_click(self,marker):
print(f"marker clicked - text: {marker.text} position: {marker.position}")
2024-10-16 02:47:20 +00:00
2024-11-02 14:04:20 +00:00
def get_altitude(self,coords):
self.coords = coords
print("getting altitude for {}\n".format(self.coords))
params = get_api_params(self.coords)
r = requests.get(URL,params)
data = r.json()
print(data['results'][0]['elevation'])
self.altitude = data['results'][0]['elevation']
2024-10-16 02:47:20 +00:00
2024-11-02 14:04:20 +00:00
image_window = tkinter.Toplevel()
#image_shower = renderedImageZoom(image_window,coords)
image_shower = renderedImageZoom(root_tk,image_window,environment_tk)
2024-10-16 02:47:20 +00:00
root_tk.mainloop()
2024-11-02 14:04:20 +00:00