refactor: migrar utm→pyproj, limpiar código muerto, reestructurar en PVPlant/core/

This commit is contained in:
Javier Braña
2026-05-02 01:02:26 +02:00
parent 3bcdc95978
commit d9b39ac17b
11 changed files with 1067 additions and 1365 deletions
+39 -57
View File
@@ -40,7 +40,7 @@ from PVPlantResources import DirIcons as DirIcons
import PVPlantSite
def get_elevation_from_oe(coordinates): # v1 deepseek
def get_elevation_from_oe(coordinates):
"""Obtiene elevaciones de Open-Elevation API y devuelve vectores FreeCAD en coordenadas UTM.
Args:
coordinates (list): Lista de tuplas con coordenadas (latitud, longitud)
@@ -52,10 +52,9 @@ def get_elevation_from_oe(coordinates): # v1 deepseek
return []
import requests
import utm
from lib.projection import latlon_to_utm
from requests.exceptions import RequestException
# Construcción más eficiente de parámetros
locations = "|".join([f"{lat:.6f},{lon:.6f}" for lat, lon in coordinates])
try:
@@ -65,7 +64,7 @@ def get_elevation_from_oe(coordinates): # v1 deepseek
timeout=20,
verify=True
)
response.raise_for_status() # Lanza excepción para códigos 4xx/5xx
response.raise_for_status()
except RequestException as e:
print(f"Error en la solicitud: {str(e)}")
@@ -84,13 +83,12 @@ def get_elevation_from_oe(coordinates): # v1 deepseek
points = []
for result in data["results"]:
try:
# Conversión UTM con manejo de errores
easting, northing, _, _ = utm.from_latlon(
easting, northing, _, _ = latlon_to_utm(
result["latitude"],
result["longitude"]
)
points.append(FreeCAD.Vector(round(easting), # Convertir metros a milímetros
points.append(FreeCAD.Vector(round(easting),
round(northing),
round(result["elevation"])) * 1000)
@@ -110,7 +108,7 @@ def getElevationFromOE(coordinates):
return None
from requests import get
import utm
from lib.projection import latlon_to_utm
locations_str=""
total = len(coordinates) - 1
@@ -121,34 +119,32 @@ def getElevationFromOE(coordinates):
query = 'https://api.open-elevation.com/api/v1/lookup?locations=' + locations_str
points = []
try:
r = get(query, timeout=20, verify=certifi.where()) # <-- Corrección aquí
r = get(query, timeout=20, verify=certifi.where())
results = r.json()
for point in results["results"]:
c = utm.from_latlon(point["latitude"], point["longitude"])
v = FreeCAD.Vector(round(c[0], 0),
round(c[1], 0),
easting, northing, _, _ = latlon_to_utm(point["latitude"], point["longitude"])
v = FreeCAD.Vector(round(easting, 0),
round(northing, 0),
round(point["elevation"], 0)) * 1000
points.append(v)
except RequestException as e:
# print(f"Error en la solicitud: {str(e)}")
for i, point in enumerate(coordinates):
c = utm.from_latlon(point[0], point[1])
points.append(FreeCAD.Vector(round(c[0], 0),
round(c[1], 0),
for point in coordinates:
easting, northing, _, _ = latlon_to_utm(point[0], point[1])
points.append(FreeCAD.Vector(round(easting, 0),
round(northing, 0),
0) * 1000)
return points
def getSinglePointElevationFromBing(lat, lng):
#http://dev.virtualearth.net/REST/v1/Elevation/List?points={lat1,long1,lat2,long2,latN,longnN}&heights={heights}&key={BingMapsAPIKey}
import utm
import requests
from lib.projection import latlon_to_utm
source = "http://dev.virtualearth.net/REST/v1/Elevation/List?points="
source += str(lat) + "," + str(lng)
source += "&heights=sealevel"
source += "&key=AmsPZA-zRt2iuIdQgvXZIxme2gWcgLaz7igOUy7VPB8OKjjEd373eCnj1KFv2CqX"
import requests
response = requests.get(source)
ans = response.text
@@ -156,26 +152,20 @@ def getSinglePointElevationFromBing(lat, lng):
print(s)
res = s['resourceSets'][0]['resources'][0]['elevations']
for elevation in res:
c = utm.from_latlon(lat, lng)
easting, northing, _, _ = latlon_to_utm(lat, lng)
v = FreeCAD.Vector(
round(c[0] * 1000, 0),
round(c[1] * 1000, 0),
round(easting * 1000, 0),
round(northing * 1000, 0),
round(elevation * 1000, 0))
return v
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
# &heights=ellipsoid&samples=10&key={BingMapsAPIKey}
import utm
import math
import requests
from lib.projection import latlon_to_utm, utm_to_latlon
_, _, zone_number, zone_letter = latlon_to_utm(lat, lng)
geo = utm.from_latlon(lat, lng)
# result = (679434.3578335291, 4294023.585627955, 30, 'S')
# EASTING, NORTHING, ZONE NUMBER, ZONE LETTER
#StepsXX = int((polygon.Shape.BoundBox.XMax - polygon.Shape.BoundBox.XMin) / (resolution*1000))
points = []
yy = polygon.Shape.BoundBox.YMax
while yy > polygon.Shape.BoundBox.YMin:
@@ -189,8 +179,8 @@ def getGridElevationFromBing(polygon, lat, lng, resolution = 1000):
else:
xx1 = xx + StepsXX * resolution
point1 = utm.to_latlon(xx / 1000, yy / 1000, geo[2], geo[3])
point2 = utm.to_latlon(xx1 / 1000, yy / 1000, geo[2], geo[3])
point1 = utm_to_latlon(xx / 1000, yy / 1000, zone_number, zone_letter)
point2 = utm_to_latlon(xx1 / 1000, yy / 1000, zone_number, zone_letter)
source = "http://dev.virtualearth.net/REST/v1/Elevation/Polyline?points="
source += "{lat1},{lng1}".format(lat1=point1[0], lng1=point1[1])
@@ -203,7 +193,6 @@ def getGridElevationFromBing(polygon, lat, lng, resolution = 1000):
response = requests.get(source)
ans = response.text
# +# to do: error handling - wait and try again
s = json.loads(ans)
res = s['resourceSets'][0]['resources'][0]['elevations']
@@ -212,7 +201,7 @@ def getGridElevationFromBing(polygon, lat, lng, resolution = 1000):
v = FreeCAD.Vector(xx + resolution * i, yy, round(elevation * 1000, 4))
points.append(v)
i += 1
xx = xx1 + resolution # para no repetir un mismo punto
xx = xx1 + resolution
yy -= resolution
return points
@@ -295,47 +284,41 @@ def getSinglePointElevation1(lat, lon):
return v
def getSinglePointElevationUtm(lat, lon):
import requests
from lib.projection import latlon_to_utm
source = "https://maps.googleapis.com/maps/api/elevation/json?locations="
source += str(lat) + "," + str(lon)
source += "&key=AIzaSyB07X6lowYJ-iqyPmaFJvr-6zp1J63db8U"
print(source)
#response = urllib.request.urlopen(source)
#ans = response.read()
import requests
response = requests.get(source)
ans = response.text
# +# to do: error handling - wait and try again
s = json.loads(ans)
res = s['results']
print (res)
print(res)
import utm
for r in res:
c = utm.from_latlon(r['location']['lat'], r['location']['lng'])
easting, northing, _, _ = latlon_to_utm(r['location']['lat'], r['location']['lng'])
v = FreeCAD.Vector(
round(c[0] * 1000, 4),
round(c[1] * 1000, 4),
round(easting * 1000, 4),
round(northing * 1000, 4),
round(r['elevation'] * 1000, 2))
print (v)
print(v)
return v
def getElevationUTM(polygon, lat, lng, resolution = 10000):
from lib.projection import latlon_to_utm, utm_to_latlon
import utm
geo = utm.from_latlon(lat, lng)
# result = (679434.3578335291, 4294023.585627955, 30, 'S')
# EASTING, NORTHING, ZONE NUMBER, ZONE LETTER
_, _, zone_number, zone_letter = latlon_to_utm(lat, lng)
StepsXX = int((polygon.Shape.BoundBox.XMax - polygon.Shape.BoundBox.XMin) / (resolution*1000))
points = []
yy = polygon.Shape.BoundBox.YMax
while yy > polygon.Shape.BoundBox.YMin:
# utm.to_latlon(EASTING, NORTHING, ZONE NUMBER, ZONE LETTER).
# result = (LATITUDE, LONGITUDE)
point1 = utm.to_latlon(polygon.Shape.BoundBox.XMin / 1000, yy / 1000, geo[2], geo[3])
point2 = utm.to_latlon(polygon.Shape.BoundBox.XMax / 1000, yy / 1000, geo[2], geo[3])
point1 = utm_to_latlon(polygon.Shape.BoundBox.XMin / 1000, yy / 1000, zone_number, zone_letter)
point2 = utm_to_latlon(polygon.Shape.BoundBox.XMax / 1000, yy / 1000, zone_number, zone_letter)
source = "https://maps.googleapis.com/maps/api/elevation/json?path="
source += "{a},{b}".format(a = point1[0], b = point1[1])
@@ -348,15 +331,14 @@ def getElevationUTM(polygon, lat, lng, resolution = 10000):
response = requests.get(source)
ans = response.text
# +# to do: error handling - wait and try again
s = json.loads(ans)
res = s['results']
for r in res:
c = utm.from_latlon(r['location']['lat'], r['location']['lng'])
easting, northing, _, _ = latlon_to_utm(r['location']['lat'], r['location']['lng'])
v = FreeCAD.Vector(
round(c[0] * 1000, 2),
round(c[1] * 1000, 2),
round(easting * 1000, 2),
round(northing * 1000, 2),
round(r['elevation'] * 1000, 2)
)
points.append(v)