Diferencia entre revisiones de «Módulo:Prop latilong»
Apariencia
Contenido eliminado Contenido añadido
Sin resumen de edición |
Optimizo reemplazando bucle por expresión regular y unificando la lectura de datos en una función común que además arregla los problemas de precisión |
||
Línea 1: | Línea 1: | ||
-- Módulo creado en marzo de |
-- Módulo creado en marzo de 2024 para generar latitud y longitud a partir de las coordenadas contenidas en la propiedad p625 |
||
-- Se utiliza en la plantilla ficha de cráter lunar, debido a que latitud y longitud han desaparecido de Wikidata |
-- Se utiliza en la plantilla ficha de cráter lunar, debido a que latitud y longitud han desaparecido de Wikidata |
||
-- Se invoca de la siguiente manera: |
-- Se invoca de la siguiente manera: |
||
-- * {{#invoke:Prop_latilong|PROLATI|1={{#property: |
-- * {{#invoke:Prop_latilong|PROLATI|1={{#property:P625}}}} (genera la LATITUD) |
||
-- * {{#invoke:Prop_latilong|NOS|1={{#property: |
-- * {{#invoke:Prop_latilong|NOS |1={{#property:P625}}}} (genera la posición Norte o Sur [N o S]) |
||
-- * {{#invoke:Prop_latilong|PROLONG|1={{#property: |
-- * {{#invoke:Prop_latilong|PROLONG|1={{#property:P625}}}} (genera la LONGITUD) |
||
-- * {{#invoke:Prop_latilong|EOW|1={{#property: |
-- * {{#invoke:Prop_latilong|EOW |1={{#property:P625}}}} (genera la posición Este u Oeste [E o O]) |
||
-- El parámetro 1 siempre serán las coordenadas recibidas de la llamada al módulo desde la propiedad P625 |
|||
local Prop_latilong = {} |
|||
-- El valor de la propiedad P625 tiene este aspecto cuando se carga: "7°33'36"S, 87°40'12"E" |
|||
--Declaración de variables globales |
|||
-- aunque puede tener menor precisión ("7°33'S, 87°40'E" o "7°S, 87°E") |
|||
--FFRR = {} --Matriz en la que se vuelcan los datos del frame |
|||
--vv = {} --Matriz de los 7 parámetros de unidad leídos de Convert/ud (separados por !) |
|||
SALIDA="" --Valor que devolverá la rutina a través del comando return |
|||
NOS="" |
|||
EOW="" |
|||
local p = {} |
|||
function Prop_latilong.NOS(frame) |
|||
Prop_latilong.PROLATI(frame) |
|||
function CoordsFromString(s) |
|||
SALIDA=NOS |
|||
local lat_deg, lat_min, lat_sec, lat_pos, |
|||
return SALIDA |
|||
lon_deg, lon_min, lon_sec, lon_pos |
|||
lat_deg, lat_min, lat_sec, lat_pos, lon_deg, lon_min, lon_sec, lon_pos = |
|||
mw.ustring.match(s, "([0-9]+)°([0-9]+)'([0-9]+)"([NS]), ([0-9]+)°([0-9]+)'([0-9]+)"([EW])", 0) |
|||
if not lon_pos then |
|||
lat_sec, lon_sec = 0, 0 |
|||
lat_deg, lat_min, lat_pos, lon_deg, lon_min, lon_pos = |
|||
mw.ustring.match(s, "([0-9]+)°([0-9]+)'([NS]), ([0-9]+)°([0-9]+)'([EW])", 0) |
|||
if not lon_pos then |
|||
lat_min, lon_min = 0, 0 |
|||
lat_deg, lat_pos, lon_deg, lon_pos = |
|||
mw.ustring.match(s, "([0-9]+)°([NS]), ([0-9]+)°([EW])", 0) |
|||
if not lon_pos then |
|||
-- lat_deg, lon_deg = 0, 0 |
|||
return nil, nil -- invalid coordinates |
|||
end |
|||
end |
|||
end |
|||
local lat, lon |
|||
lat = tonumber(lat_deg) + tonumber(lat_min)/60 + tonumber(lat_sec)/3600 |
|||
if lat_pos == "S" then lat = -lat end |
|||
lon = tonumber(lon_deg) + tonumber(lon_min)/60 + tonumber(lon_sec)/3600 |
|||
if lon_pos == "W" then lon = -lon end |
|||
return lat, lon |
|||
end |
end |
||
function |
function p.NOS(frame) |
||
local s = frame.args[1] |
|||
Prop_latilong.PROLONG(frame) |
|||
if s then |
|||
local lat, lon = CoordsFromString(s) |
|||
SALIDA=EOW |
|||
if lat then |
|||
return SALIDA |
|||
if lat < 0 then |
|||
return 'S' |
|||
else |
|||
return 'N' |
|||
end |
|||
end |
|||
end |
|||
end |
end |
||
function p.EOW(frame) |
|||
local s = frame.args[1] |
|||
function Prop_latilong.PROLATI(frame) |
|||
if s then |
|||
-- La propiedad p625 tiene este aspecto cuando se carga: 7°33'36"S, 87°40'12"E |
|||
local lat, lon = CoordsFromString(s) |
|||
-- Para obtener la latitud, se barre caracter a caracter dese el principio |
|||
if lon then |
|||
-- Cuando aparece ° se cargan los grados GRA |
|||
if lon < 0 then |
|||
-- Cuando aparece ; se cargan los minutos MIN |
|||
return 'W' |
|||
-- Y cuando aparece de nuevo ; se cargan los segundos SEG |
|||
else |
|||
-- Finalmente, se pasa a fracción de grado, y se le añade la letra de la latitud (N o S) |
|||
return 'E' |
|||
end |
|||
--Volcado del frame |
|||
end |
|||
s=frame.args[1] or '' --Coordenadas (p625) recibidas de la llamada al módulo |
|||
local n = 0 |
|||
local cont=1 |
|||
local valor=0 |
|||
local NUMERO="" |
|||
local GRA="" |
|||
local MIN="" |
|||
local SEG="" |
|||
local LETRA="" |
|||
local LATITUD=0 |
|||
local BASE="" |
|||
local l = mw.ustring.len( s ) or 0 |
|||
for i = 1, l do |
|||
n = n + 1 |
|||
local c = mw.ustring.sub(s, i, i) |
|||
BASE=BASE .. "*" .. c |
|||
valor=tonumber(c) |
|||
if valor~= nil then |
|||
if c~= " " then |
|||
NUMERO=NUMERO .. c |
|||
end |
|||
else |
|||
if c == "," then |
|||
cont=5 |
|||
else |
|||
if cont == 4 then |
|||
LETRA=c |
|||
end |
|||
end |
|||
end |
|||
if c == "°" then |
|||
if cont == 1 then |
|||
GRA=NUMERO |
|||
NUMERO="" |
|||
cont=2 |
|||
end |
|||
end |
|||
if c == "&" then |
|||
if cont == 2 then |
|||
MIN=NUMERO |
|||
NUMERO="" |
|||
cont=3 |
|||
else |
|||
if cont == 3 then |
|||
SEG=NUMERO |
|||
NUMERO="" |
|||
cont=4 |
|||
end |
|||
end |
|||
end |
|||
if c == ";" then |
|||
NUMERO="" |
|||
end |
|||
end |
end |
||
--LATITUD=tonumber(GRA) + tonumber(MIN)/60 + tonumber(SEG)/3600 |
|||
NOS=LETRA |
|||
--SALIDA=BASE .. "(BASE)" |
|||
SALIDA=tonumber(GRA) + tonumber(MIN)/60 + tonumber(SEG)/3600 |
|||
-- SALIDA=GRA .. " grad " .. MIN .. " min " .. SEG .. " seg " .. LETRA .. BASE .. " Longitud=" .. LONGITUD |
|||
return SALIDA |
|||
end |
end |
||
function |
function p.PROLATI(frame) |
||
local s = frame.args[1] |
|||
-- La propiedad p625 tiene este aspecto cuando se carga: 7°33'36"S, 87°40'12"E |
|||
if s then |
|||
-- Para obtener la longitud, se barre caracter a caracter hasta encontrar la coma, y se hace cont=1 |
|||
local lat, lon = CoordsFromString(s) |
|||
-- Cuando aparece ° se cargan los grados GRA |
|||
if lat then |
|||
-- Cuando aparece ; se cargan los minutos MIN |
|||
return math.abs(lat) |
|||
-- Y cuando aparece de nuevo ; se cargan los segundos SEG |
|||
end |
|||
-- Finalmente, se pasa a fracción de grado, y se le añade la letra de la longitud (E o W) |
|||
--Volcado del frame |
|||
s=frame.args[1] or '' --Coordenadas (p625) recibidas de la llamada al módulo |
|||
local n = 0 |
|||
local cont=0 |
|||
local valor=0 |
|||
local NUMERO="" |
|||
local GRA="" |
|||
local MIN="" |
|||
local SEG="" |
|||
local LETRA="" |
|||
local LONGITUD=0 |
|||
local BASE="" |
|||
local l = mw.ustring.len( s ) or 0 |
|||
for i = 1, l do |
|||
n = n + 1 |
|||
local c = mw.ustring.sub(s, i, i) |
|||
BASE=BASE .. "*" .. c |
|||
valor=tonumber(c) |
|||
if valor~= nil then |
|||
if c~= " " then |
|||
NUMERO=NUMERO .. c |
|||
end |
|||
else |
|||
LETRA=c |
|||
end |
|||
if c == "," then |
|||
cont=1 |
|||
end |
|||
if c == "°" then |
|||
if cont == 1 then |
|||
GRA=NUMERO |
|||
NUMERO="" |
|||
cont=2 |
|||
end |
|||
end |
|||
if c == "&" then |
|||
if cont == 2 then |
|||
MIN=NUMERO |
|||
NUMERO="" |
|||
cont=3 |
|||
else |
|||
if cont == 3 then |
|||
SEG=NUMERO |
|||
NUMERO="" |
|||
cont=4 |
|||
end |
|||
end |
|||
end |
|||
if c == ";" then |
|||
NUMERO="" |
|||
end |
|||
end |
end |
||
LONGITUD=tonumber(GRA) + tonumber(MIN)/60 + tonumber(SEG)/3600 |
|||
EOW=LETRA |
|||
SALIDA= tonumber(GRA) + tonumber(MIN)/60 + tonumber(SEG)/3600 |
|||
-- SALIDA=GRA .. " grad " .. MIN .. " min " .. SEG .. " seg " .. LETRA .. BASE .. " Longitud=" .. LONGITUD |
|||
return SALIDA |
|||
end |
end |
||
function p.PROLONG(frame) |
|||
local s = frame.args[1] |
|||
function nowrap( value ) |
|||
if s then |
|||
return tostring( |
|||
local lat, lon = CoordsFromString(s) |
|||
mw.html.create('span') |
|||
if lon then |
|||
:css('white-space', 'nowrap') |
|||
return math.abs(lon) |
|||
:wikitext( value ) |
|||
end |
|||
:done() |
|||
end |
|||
) |
|||
end |
end |
||
return p |
|||
return Prop_latilong |
Revisión actual - 14:36 17 mar 2024
Este módulo no tiene página de documentación[crear]
-- Módulo creado en marzo de 2024 para generar latitud y longitud a partir de las coordenadas contenidas en la propiedad p625
-- Se utiliza en la plantilla ficha de cráter lunar, debido a que latitud y longitud han desaparecido de Wikidata
-- Se invoca de la siguiente manera:
-- * {{#invoke:Prop_latilong|PROLATI|1={{#property:P625}}}} (genera la LATITUD)
-- * {{#invoke:Prop_latilong|NOS |1={{#property:P625}}}} (genera la posición Norte o Sur [N o S])
-- * {{#invoke:Prop_latilong|PROLONG|1={{#property:P625}}}} (genera la LONGITUD)
-- * {{#invoke:Prop_latilong|EOW |1={{#property:P625}}}} (genera la posición Este u Oeste [E o O])
-- El parámetro 1 siempre serán las coordenadas recibidas de la llamada al módulo desde la propiedad P625
-- El valor de la propiedad P625 tiene este aspecto cuando se carga: "7°33'36"S, 87°40'12"E"
-- aunque puede tener menor precisión ("7°33'S, 87°40'E" o "7°S, 87°E")
local p = {}
function CoordsFromString(s)
local lat_deg, lat_min, lat_sec, lat_pos,
lon_deg, lon_min, lon_sec, lon_pos
lat_deg, lat_min, lat_sec, lat_pos, lon_deg, lon_min, lon_sec, lon_pos =
mw.ustring.match(s, "([0-9]+)°([0-9]+)'([0-9]+)"([NS]), ([0-9]+)°([0-9]+)'([0-9]+)"([EW])", 0)
if not lon_pos then
lat_sec, lon_sec = 0, 0
lat_deg, lat_min, lat_pos, lon_deg, lon_min, lon_pos =
mw.ustring.match(s, "([0-9]+)°([0-9]+)'([NS]), ([0-9]+)°([0-9]+)'([EW])", 0)
if not lon_pos then
lat_min, lon_min = 0, 0
lat_deg, lat_pos, lon_deg, lon_pos =
mw.ustring.match(s, "([0-9]+)°([NS]), ([0-9]+)°([EW])", 0)
if not lon_pos then
-- lat_deg, lon_deg = 0, 0
return nil, nil -- invalid coordinates
end
end
end
local lat, lon
lat = tonumber(lat_deg) + tonumber(lat_min)/60 + tonumber(lat_sec)/3600
if lat_pos == "S" then lat = -lat end
lon = tonumber(lon_deg) + tonumber(lon_min)/60 + tonumber(lon_sec)/3600
if lon_pos == "W" then lon = -lon end
return lat, lon
end
function p.NOS(frame)
local s = frame.args[1]
if s then
local lat, lon = CoordsFromString(s)
if lat then
if lat < 0 then
return 'S'
else
return 'N'
end
end
end
end
function p.EOW(frame)
local s = frame.args[1]
if s then
local lat, lon = CoordsFromString(s)
if lon then
if lon < 0 then
return 'W'
else
return 'E'
end
end
end
end
function p.PROLATI(frame)
local s = frame.args[1]
if s then
local lat, lon = CoordsFromString(s)
if lat then
return math.abs(lat)
end
end
end
function p.PROLONG(frame)
local s = frame.args[1]
if s then
local lat, lon = CoordsFromString(s)
if lon then
return math.abs(lon)
end
end
end
return p