diff --git a/iface.py b/iface.py index 45731fd..ce0f881 100644 --- a/iface.py +++ b/iface.py @@ -7,6 +7,7 @@ import os import platform import subprocess import configparser +from math import log, tan, radians, pi, degrees, atan, sinh from downloader import CopernicusDownloader @@ -220,39 +221,58 @@ class MapSelector: pass def xy_from_latlon(self, lat, lon): - lat_min, lat_max = self.config.getfloat('map','map_lat_start'), self.config.getfloat('map','map_lat_end') - lon_min, lon_max = self.config.getfloat('map','map_lon_start'), self.config.getfloat('map','map_lon_end') - # print(f"xy_from_latlon dbg coord = {lat_min}-{lat_max}-{lon_min}-{lon_max}") - + lat_min = self.config.getfloat('map', 'map_lat_start') + lat_max = self.config.getfloat('map', 'map_lat_end') + lon_min = self.config.getfloat('map', 'map_lon_start') + lon_max = self.config.getfloat('map', 'map_lon_end') + canvas_w = self.canvas.winfo_width() canvas_h = self.canvas.winfo_height() - - print(f"canvas = {canvas_w} {canvas_h} scale = {self.scale} offset = {self.offset_x} {self.offset_y} ") + + print(f"canvas = {canvas_w} {canvas_h} scale = {self.scale} offset = {self.offset_x} {self.offset_y}") + + # Longitude (linéaire) + x_norm = (lon - lon_min) / (lon_max - lon_min) + x = x_norm * self.scale * self.original_img.width + self.offset_x - x = (lon - lon_min) / (lon_max - lon_min) * self.scale * self.original_img.width + self.offset_x - y = (lat_max - lat) / (lat_max - lat_min) * self.scale * self.original_img.height + self.offset_y - - print(f" xy_from_latlon : {x} - {y}") + # Latitude (projection Mercator) + mercator_min = log(tan(pi / 4 + radians(lat_min) / 2)) # Mercator + mercator_max = log(tan(pi / 4 + radians(lat_max) / 2)) # Mercator + mercator_lat = log(tan(pi / 4 + radians(lat) / 2)) # Mercator + + y_norm = (mercator_max - mercator_lat) / (mercator_max - mercator_min) + y = y_norm * self.scale * self.original_img.height + self.offset_y + + print(f"xy_from_latlon : {x} - {y}") return x, y def latlon_from_xy(self, x, y): # Coordonnées géographiques de la carte - lat_min, lat_max = self.config.getfloat('map','map_lat_start'), self.config.getfloat('map','map_lat_end') - lon_min, lon_max = self.config.getfloat('map','map_lon_start'), self.config.getfloat('map','map_lon_end') + lat_min = self.config.getfloat('map', 'map_lat_start') + lat_max = self.config.getfloat('map', 'map_lat_end') + lon_min = self.config.getfloat('map', 'map_lon_start') + lon_max = self.config.getfloat('map', 'map_lon_end') + print(f"latlon_from_xy dbg coord = {lat_min}-{lat_max}-{lon_min}-{lon_max}") - - # Calculer la position relative sur l'image + # Calculer la position relative dans l’image (sans offset/zoom) img_x = (x - self.offset_x) / self.scale img_y = (y - self.offset_y) / self.scale - # Vérifier si le clic est dans l'image affichée if 0 <= img_x <= self.original_img.width and 0 <= img_y <= self.original_img.height: rel_x = img_x / self.original_img.width rel_y = img_y / self.original_img.height - lat = lat_max - rel_y * (lat_max - lat_min) + + # Longitude : linéaire lon = lon_min + rel_x * (lon_max - lon_min) - print(f" latlon_from_xy : {lat} - {lon}") + + # Latitude : projection inverse de Mercator + merc_min = log(tan(pi / 4 + radians(lat_min) / 2)) # Mercator + merc_max = log(tan(pi / 4 + radians(lat_max) / 2)) # Mercator + merc_y = merc_max - rel_y * (merc_max - merc_min) + lat = degrees(atan(sinh(merc_y))) # Mercator Inverse + + print(f"latlon_from_xy : {lat} - {lon}") return lat, lon else: return None, None # En dehors de l'image diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c6e1cc9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +pillow +copernicusmarine