refactor: migrar utm→pyproj, limpiar código muerto, reestructurar en PVPlant/core/
This commit is contained in:
+12
-133
@@ -21,7 +21,6 @@
|
||||
# ***********************************************************************
|
||||
|
||||
import FreeCAD
|
||||
import utm
|
||||
|
||||
if FreeCAD.GuiUp:
|
||||
import FreeCADGui
|
||||
@@ -109,13 +108,11 @@ class MapWindow(QtGui.QWidget):
|
||||
self.view.page().loadFinished.connect(self.onLoadFinished)
|
||||
self.view.page().load(QtCore.QUrl.fromLocalFile(file))
|
||||
LeftLayout.addWidget(self.view)
|
||||
# self.layout.addWidget(self.view, 1, 0, 1, 3)
|
||||
|
||||
# -- Latitud y longitud:
|
||||
self.labelCoordinates = QtGui.QLabel()
|
||||
self.labelCoordinates.setFixedHeight(21)
|
||||
LeftLayout.addWidget(self.labelCoordinates)
|
||||
# self.layout.addWidget(self.labelCoordinates, 2, 0, 1, 3)
|
||||
|
||||
# Right Widgets:
|
||||
labelKMZ = QtGui.QLabel()
|
||||
@@ -139,9 +136,6 @@ class MapWindow(QtGui.QWidget):
|
||||
radio3 = QtGui.QRadioButton("Datos GPS")
|
||||
radio1.setChecked(True)
|
||||
|
||||
# buttonDialog = QtGui.QPushButton('...')
|
||||
# buttonDialog.setEnabled(False)
|
||||
|
||||
vbox = QtGui.QVBoxLayout(self)
|
||||
vbox.addWidget(radio1)
|
||||
vbox.addWidget(radio2)
|
||||
@@ -202,12 +196,14 @@ class MapWindow(QtGui.QWidget):
|
||||
|
||||
@QtCore.Slot(float, float)
|
||||
def onMapMove(self, lat, lng):
|
||||
from lib.projection import latlon_to_utm
|
||||
|
||||
self.lat = lat
|
||||
self.lon = lng
|
||||
x, y, zone_number, zone_letter = utm.from_latlon(lat, lng)
|
||||
easting, northing, zone_number, zone_letter = latlon_to_utm(lat, lng)
|
||||
self.labelCoordinates.setText('Longitud: {:.5f}, Latitud: {:.5f}'.format(lng, lat) +
|
||||
' | UTM: ' + str(zone_number) + zone_letter +
|
||||
', {:.5f}m E, {:.5f}m N'.format(x, y))
|
||||
', {:.5f}m E, {:.5f}m N'.format(easting, northing))
|
||||
|
||||
@QtCore.Slot(float, float, float, float, int)
|
||||
def onMapZoom(self, minLat, minLon, maxLat, maxLon, zoom):
|
||||
@@ -245,7 +241,7 @@ class MapWindow(QtGui.QWidget):
|
||||
if location.raw["address"].get("state"):
|
||||
if Site.Region != "":
|
||||
Site.Region += " - "
|
||||
Site.Region += '{0}'.format(location.raw["address"]["state"]) # province - state
|
||||
Site.Region += '{0}'.format(location.raw["address"]["state"])
|
||||
Site.Country = location.raw["address"]["country"]
|
||||
|
||||
@QtCore.Slot(str)
|
||||
@@ -273,7 +269,6 @@ class MapWindow(QtGui.QWidget):
|
||||
p.Base = c
|
||||
obj = Draft.makeCircle(r, placement=p, face=False)
|
||||
else:
|
||||
''' do something '''
|
||||
obj = Draft.make_point(c * 1000, color=(0.5, 0.3, 0.6), point_size=10)
|
||||
else: # 2. if the feature is a Polygon or Line:
|
||||
cw = False
|
||||
@@ -289,7 +284,6 @@ class MapWindow(QtGui.QWidget):
|
||||
pts = [p.sub(offset) for p in tmp]
|
||||
|
||||
obj = Draft.makeWire(pts, closed=cw, face=False)
|
||||
#obj.Placement.Base = Site.Origin
|
||||
obj.Label = name
|
||||
Draft.autogroup(obj)
|
||||
|
||||
@@ -300,77 +294,8 @@ class MapWindow(QtGui.QWidget):
|
||||
self.getDataFromOSM(self.minLat, self.minLon, self.maxLat, self.maxLon)
|
||||
|
||||
if self.checkboxImportSatelitalImagen.isChecked():
|
||||
# Usar los límites reales del terreno (rectangular)
|
||||
'''s_lat = self.minLat
|
||||
s_lon = self.minLon
|
||||
n_lat = self.maxLat
|
||||
n_lon = self.maxLon
|
||||
from lib.projection import latlon_to_utm
|
||||
|
||||
# Obtener puntos UTM para las esquinas
|
||||
corners = ImportElevation.getElevationFromOE([
|
||||
[s_lat, s_lon], # Esquina suroeste
|
||||
[n_lat, s_lon], # Esquina sureste
|
||||
[n_lat, n_lon], # Esquina noreste
|
||||
[s_lat, n_lon] # Esquina noroeste
|
||||
])
|
||||
|
||||
if not corners or len(corners) < 4:
|
||||
FreeCAD.Console.PrintError("Error obteniendo elevaciones para las esquinas\n")
|
||||
return
|
||||
|
||||
# Descargar imagen satelital
|
||||
from lib.GoogleSatelitalImageDownload import GoogleMapDownloader
|
||||
downloader = GoogleMapDownloader(
|
||||
zoom= 18, #self.zoom,
|
||||
layer='raw_satellite'
|
||||
)
|
||||
img = downloader.generateImage(
|
||||
sw_lat=s_lat,
|
||||
sw_lng=s_lon,
|
||||
ne_lat=n_lat,
|
||||
ne_lng=n_lon
|
||||
)
|
||||
|
||||
# Guardar imagen en el directorio del documento
|
||||
doc_path = os.path.dirname(FreeCAD.ActiveDocument.FileName) if FreeCAD.ActiveDocument.FileName else ""
|
||||
if not doc_path:
|
||||
doc_path = FreeCAD.ConfigGet("UserAppData")
|
||||
|
||||
filename = os.path.join(doc_path, "background.jpeg")
|
||||
img.save(filename)
|
||||
|
||||
ancho, alto = img.size
|
||||
|
||||
# Crear objeto de imagen en FreeCAD
|
||||
doc = FreeCAD.ActiveDocument
|
||||
img_obj = doc.addObject('Image::ImagePlane', 'Background')
|
||||
img_obj.ImageFile = filename
|
||||
img_obj.Label = 'Background'
|
||||
|
||||
# Calcular dimensiones en metros usando las coordenadas UTM
|
||||
# Extraer las coordenadas de las esquinas
|
||||
sw = corners[0] # Suroeste
|
||||
se = corners[1] # Sureste
|
||||
ne = corners[2] # Noreste
|
||||
nw = corners[3] # Noroeste
|
||||
|
||||
# Calcular ancho (promedio de los lados superior e inferior)
|
||||
width_bottom = se.x - sw.x
|
||||
width_top = ne.x - nw.x
|
||||
width_m = (width_bottom + width_top) / 2
|
||||
|
||||
# Calcular alto (promedio de los lados izquierdo y derecho)
|
||||
height_left = nw.y - sw.y
|
||||
height_right = ne.y - se.y
|
||||
height_m = (height_left + height_right) / 2
|
||||
|
||||
img_obj.XSize = width_m
|
||||
img_obj.YSize = height_m
|
||||
|
||||
# Posicionar el centro de la imagen en (0,0,0)
|
||||
img_obj.Placement.Base = FreeCAD.Vector(-width_m / 2, -height_m / 2, 0)'''
|
||||
|
||||
# Definir área rectangular
|
||||
s_lat = self.minLat
|
||||
s_lon = self.minLon
|
||||
n_lat = self.maxLat
|
||||
@@ -412,8 +337,8 @@ class MapWindow(QtGui.QWidget):
|
||||
img.save(filename)
|
||||
|
||||
# Calcular dimensiones reales en metros
|
||||
width_m = ne_utm.x - sw_utm.x # Ancho en metros (este-oeste)
|
||||
height_m = ne_utm.y - sw_utm.y # Alto en metros (norte-sur)
|
||||
width_m = ne_utm.x - sw_utm.x
|
||||
height_m = ne_utm.y - sw_utm.y
|
||||
|
||||
# Calcular posición relativa del punto de referencia dentro de la imagen
|
||||
rel_x = (ref_utm.x - sw_utm.x) / width_m if width_m != 0 else 0.5
|
||||
@@ -425,44 +350,19 @@ class MapWindow(QtGui.QWidget):
|
||||
img_obj.ImageFile = filename
|
||||
img_obj.Label = 'Background'
|
||||
|
||||
# Convertir dimensiones a milímetros (FreeCAD trabaja en mm)
|
||||
# FreeCAD trabaja en mm
|
||||
img_obj.XSize = width_m * 1000
|
||||
img_obj.YSize = height_m * 1000
|
||||
|
||||
# Posicionar para que el punto de referencia esté en (0,0,0)
|
||||
# La esquina inferior izquierda debe estar en:
|
||||
# x = -rel_x * ancho_total
|
||||
# y = -rel_y * alto_total
|
||||
img_obj.Placement.Base = FreeCAD.Vector(
|
||||
-rel_x * width_m * 1000,
|
||||
-rel_y * height_m * 1000,
|
||||
0
|
||||
)
|
||||
|
||||
# Refrescar el documento
|
||||
doc.recompute()
|
||||
|
||||
def calculate_texture_transform(self, mesh_obj, width_m, height_m):
|
||||
"""Calcula la transformación precisa para la textura"""
|
||||
try:
|
||||
# Obtener coordenadas reales de las esquinas
|
||||
import utm
|
||||
sw = utm.from_latlon(self.minLat, self.minLon)
|
||||
ne = utm.from_latlon(self.maxLat, self.maxLon)
|
||||
|
||||
# Crear matriz de transformación
|
||||
scale_x = (ne[0] - sw[0]) / width_m
|
||||
scale_y = (ne[1] - sw[1]) / height_m
|
||||
|
||||
# Aplicar transformación (solo si se usa textura avanzada)
|
||||
if hasattr(mesh_obj.ViewObject, "TextureMapping"):
|
||||
mesh_obj.ViewObject.TextureMapping = "PLANE"
|
||||
mesh_obj.ViewObject.TextureScale = (scale_x, scale_y)
|
||||
mesh_obj.ViewObject.TextureOffset = (sw[0], sw[1])
|
||||
|
||||
except Exception as e:
|
||||
FreeCAD.Console.PrintWarning(f"No se pudo calcular transformación: {str(e)}\n")
|
||||
|
||||
def getDataFromOSM(self, min_lat, min_lon, max_lat, max_lon):
|
||||
import Importer.importOSM as importOSM
|
||||
import PVPlantSite
|
||||
@@ -476,32 +376,15 @@ class MapWindow(QtGui.QWidget):
|
||||
osm_data = importer.get_osm_data(f"{min_lat},{min_lon},{max_lat},{max_lon}")
|
||||
importer.process_osm_data(osm_data)
|
||||
|
||||
'''FreeCAD.activeDocument().recompute()
|
||||
FreeCADGui.updateGui()
|
||||
FreeCADGui.SendMsgToActiveView("ViewFit")'''
|
||||
|
||||
def panMap_old(self, lng, lat, geometry=""):
|
||||
frame = self.view.page()
|
||||
bbox = "[{0}, {1}], [{2}, {3}]".format(float(geometry[0]), float(geometry[2]),
|
||||
float(geometry[1]), float(geometry[3]))
|
||||
command = 'map.panTo(L.latLng({lt}, {lg}));'.format(lt=lat, lg=lng)
|
||||
command += 'map.fitBounds([{box}]);'.format(box=bbox)
|
||||
frame.runJavaScript(command)
|
||||
|
||||
# deepseek
|
||||
def panMap(self, lng, lat, geometry=None):
|
||||
frame = self.view.page()
|
||||
|
||||
# 1. Validación del parámetro geometry
|
||||
if not geometry or len(geometry) < 4:
|
||||
# Pan básico sin ajuste de bounds
|
||||
command = f'map.panTo(L.latLng({lat}, {lng}));'
|
||||
else:
|
||||
try:
|
||||
# 2. Mejor manejo de coordenadas (Leaflet usa [lat, lng])
|
||||
# Asumiendo que geometry es [min_lng, min_lat, max_lng, max_lat]
|
||||
southwest = f"{float(geometry[1])}, {float(geometry[0])}" # min_lat, min_lng
|
||||
northeast = f"{float(geometry[3])}, {float(geometry[2])}" # max_lat, max_lng
|
||||
southwest = f"{float(geometry[1])}, {float(geometry[0])}"
|
||||
northeast = f"{float(geometry[3])}, {float(geometry[2])}"
|
||||
command = f'map.panTo(L.latLng({lat}, {lng}));'
|
||||
command += f'map.fitBounds(L.latLngBounds([{southwest}], [{northeast}]));'
|
||||
except (IndexError, ValueError, TypeError) as e:
|
||||
@@ -536,8 +419,4 @@ class CommandPVPlantGeoreferencing:
|
||||
if FreeCAD.ActiveDocument:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
'''if FreeCAD.GuiUp:
|
||||
FreeCADGui.addCommand('PVPlantGeoreferencing',_CommandPVPlantGeoreferencing())
|
||||
'''
|
||||
return False
|
||||
Reference in New Issue
Block a user