PVPlant: utm → pyproj (adaptador con sys.modules patch en ImportGrid, eliminado de requirements)

This commit is contained in:
Javier Braña
2026-05-03 00:32:36 +02:00
parent 7c81beb1ba
commit 74aedf6122
2 changed files with 45 additions and 7 deletions
+45 -6
View File
@@ -39,6 +39,51 @@ import os
from PVPlantResources import DirIcons as DirIcons from PVPlantResources import DirIcons as DirIcons
import PVPlantSite import PVPlantSite
# ---------------------------------------------------------------------------
# Adaptador UTM: emula la API de la librería 'utm' usando pyproj
# La librería 'utm' dejó de usarse en favor de pyproj (más completa y mantenida).
# from_latlon(lat, lon) -> (easting, northing, zone_number, zone_letter)
# to_latlon(easting, northing, zone_number, zone_letter) -> (lat, lon)
# ---------------------------------------------------------------------------
_utm_cache = {}
def _get_transformer(lat, lon):
"""Obtiene o crea un transformador UTM para las coordenadas dadas."""
from pyproj import Transformer
zone = int((lon + 180) / 6) + 1
hem = 'S' if lat < 0 else 'N'
key = (zone, hem)
if key not in _utm_cache:
crs_utm = f'+proj=utm +zone={zone} +{hem.lower()} +ellps=WGS84 +datum=WGS84 +units=m +no_defs'
_utm_cache[key] = Transformer.from_crs('EPSG:4326', crs_utm, always_xy=True)
return _utm_cache[key], zone, hem
def from_latlon(lat, lon):
"""Convierte (lat, lon) a UTM. Retorna (easting, northing, zone_number, zone_letter)."""
transformer, zone, hem = _get_transformer(lat, lon)
easting, northing = transformer.transform(lon, lat)
return (easting, northing, zone, hem)
def to_latlon(easting, northing, zone_number, zone_letter):
"""Convierte UTM a (lat, lon)."""
from pyproj import Transformer
hem = zone_letter.upper()
key = (zone_number, hem)
if key not in _utm_cache:
crs_utm = f'+proj=utm +zone={zone_number} +{hem.lower()} +ellps=WGS84 +datum=WGS84 +units=m +no_defs'
_utm_cache[key] = Transformer.from_crs(crs_utm, 'EPSG:4326', always_xy=True)
lon, lat = _utm_cache[key].transform(easting, northing)
return (lat, lon)
# Parche: reemplazar el módulo 'utm' por nuestro adaptador
import sys
class _UTMWrapper:
"""Wrapper para que 'import utm' devuelva nuestras funciones."""
from_latlon = staticmethod(from_latlon)
to_latlon = staticmethod(to_latlon)
sys.modules['utm'] = _UTMWrapper
# ---------------------------------------------------------------------------
def get_elevation_from_oe(coordinates): # v1 deepseek def get_elevation_from_oe(coordinates): # v1 deepseek
"""Obtiene elevaciones de Open-Elevation API y devuelve vectores FreeCAD en coordenadas UTM. """Obtiene elevaciones de Open-Elevation API y devuelve vectores FreeCAD en coordenadas UTM.
@@ -52,7 +97,6 @@ def get_elevation_from_oe(coordinates): # v1 deepseek
return [] return []
import requests import requests
import utm
from requests.exceptions import RequestException from requests.exceptions import RequestException
# Construcción más eficiente de parámetros # Construcción más eficiente de parámetros
@@ -110,7 +154,6 @@ def getElevationFromOE(coordinates):
return None return None
from requests import get from requests import get
import utm
locations_str="" locations_str=""
total = len(coordinates) - 1 total = len(coordinates) - 1
@@ -141,7 +184,6 @@ def getElevationFromOE(coordinates):
def getSinglePointElevationFromBing(lat, lng): def getSinglePointElevationFromBing(lat, lng):
#http://dev.virtualearth.net/REST/v1/Elevation/List?points={lat1,long1,lat2,long2,latN,longnN}&heights={heights}&key={BingMapsAPIKey} #http://dev.virtualearth.net/REST/v1/Elevation/List?points={lat1,long1,lat2,long2,latN,longnN}&heights={heights}&key={BingMapsAPIKey}
import utm
source = "http://dev.virtualearth.net/REST/v1/Elevation/List?points=" source = "http://dev.virtualearth.net/REST/v1/Elevation/List?points="
source += str(lat) + "," + str(lng) source += str(lat) + "," + str(lng)
@@ -166,7 +208,6 @@ def getSinglePointElevationFromBing(lat, lng):
def getGridElevationFromBing(polygon, lat, lng, resolution = 1000): def getGridElevationFromBing(polygon, lat, lng, resolution = 1000):
#http://dev.virtualearth.net/REST/v1/Elevation/Polyline?points=35.89431,-110.72522,35.89393,-110.72578,35.89374,-110.72606,35.89337,-110.72662 #http://dev.virtualearth.net/REST/v1/Elevation/Polyline?points=35.89431,-110.72522,35.89393,-110.72578,35.89374,-110.72606,35.89337,-110.72662
# &heights=ellipsoid&samples=10&key={BingMapsAPIKey} # &heights=ellipsoid&samples=10&key={BingMapsAPIKey}
import utm
import math import math
import requests import requests
@@ -311,7 +352,6 @@ def getSinglePointElevationUtm(lat, lon):
res = s['results'] res = s['results']
print (res) print (res)
import utm
for r in res: for r in res:
c = utm.from_latlon(r['location']['lat'], r['location']['lng']) c = utm.from_latlon(r['location']['lat'], r['location']['lng'])
v = FreeCAD.Vector( v = FreeCAD.Vector(
@@ -323,7 +363,6 @@ def getSinglePointElevationUtm(lat, lon):
def getElevationUTM(polygon, lat, lng, resolution = 10000): def getElevationUTM(polygon, lat, lng, resolution = 10000):
import utm
geo = utm.from_latlon(lat, lng) geo = utm.from_latlon(lat, lng)
# result = (679434.3578335291, 4294023.585627955, 30, 'S') # result = (679434.3578335291, 4294023.585627955, 30, 'S')
# EASTING, NORTHING, ZONE NUMBER, ZONE LETTER # EASTING, NORTHING, ZONE NUMBER, ZONE LETTER
-1
View File
@@ -2,7 +2,6 @@ numpy~=1.26.2
opencv-python~=4.8.1 opencv-python~=4.8.1
matplotlib~=3.8.2 matplotlib~=3.8.2
openpyxl~=3.1.2 openpyxl~=3.1.2
utm~=0.7.0
requests~=2.31.0 requests~=2.31.0
setuptools~=68.2.2 setuptools~=68.2.2
laspy~=2.5.3 laspy~=2.5.3