Favicon Vikidia.png
¡Vikidia te necesita!Face-wink.svg
Corazón.svg

Actualmente tenemos 6625 artículos. ¡Anímate! Face-smile.svg a crear los artículos solicitados

Módulo:Mapa

De Vikidia
Ir a la navegación Ir a la búsqueda
Test Template Info-Icon - Version (2).svg Documentación de módulo
No hay documentación para este módulo. Pulsa aquí para crearla.
local z = {}
local modUtilidades = require('Módulo:Utilidades')

function tipoPunto(v)
  local objeto = {
    ['type'] = 'Feature',
    ['geometry'] = {
      ['type'] = 'Point',
      ['coordinates'] = v.coordenadas,
    },
    ['properties'] = {
      ['title'] = v['título'],
      ['description'] = v['descripción'],
      ['marker-color'] = v.color,
      ['marker-size'] = v['tamaño'],
      ['marker-symbol'] = v['símbolo'],
    }
  }
  return objeto
end

function tipoLinea(v)
  local objeto = {
    ['type'] = 'Feature',
    ['geometry'] = {
      ['type'] = 'LineString',
      ['coordinates'] = v.coordenadas,
    },
    ['properties'] = {
      ['title'] = v['título'],
      ['description'] = v['descripción'],
      ['stroke'] = v.color,
      ['stroke-width'] = v.ancho,
      ['stroke-opacity'] = v.opacidad,
    }
  }
  return objeto
end

function tipoPoligono(v)
  local objeto = {
    ['type'] = 'Feature',
    ['geometry'] = {
      ['type'] = 'Polygon',
      ['coordinates'] = {v.coordenadas},
    },
    ['properties'] = {
      ['title'] = v['título'],
      ['description'] = v['descripción'],
      ['stroke'] = v.color,
      ['stroke-width'] = v.ancho,
      ['fill'] = v.relleno,
      ['fill-opacity'] = v.opacidad,
    }
  }
  return objeto
end

function tipoExterno(v, n)
  error('Mapa: se requiere conexión a Wikidata.')
end

function z.Mapa(args, objetos)
  -- Parámetros para etiqueta
  local tagParams = {}
  tagParams.text = args['título']
  tagParams.width = args.anchura
  tagParams.height = args.altura
  tagParams.zoom = args.zoom
  tagParams.latitude = args.latitud
  tagParams.longitude = args.longitud
  tagParams.frameless = (sinmarco and '' or nil)
  tagParams.align = args['alineación']
  -- Sintaxis geoJSON
  local geojson = {}
  for i, v in ipairs(objetos) do
    if v.tipo == 'Point' then
      table.insert(geojson, tipoPunto(v))
    elseif v.tipo == 'LineString' then
      table.insert(geojson, tipoLinea(v))
    elseif v.tipo == 'Polygon' then
      table.insert(geojson, tipoPoligono(v))
    elseif v.tipo == 'geoshape' then
      table.insert(geojson, tipoExterno(v, 1))
    elseif v.tipo == 'geomask' then
      table.insert(geojson, tipoExterno(v, 2))
    elseif v.tipo == 'geoline' then
      table.insert(geojson, tipoExterno(v, 3))
    elseif v.tipo == 'page' then
      table.insert(geojson, tipoExterno(v, 4))
    elseif v.tipo then
      error('Objeto '..tostring(i)..': Tipo inválido.')
    end
  end
  -- Generar debug o producción
  local frm = mw.getCurrentFrame()
  if args['debug'] then
    return frm:extensionTag{name='source', content=mw.text.tag(args.tipo, tagParams, mw.text.jsonEncode(geojson, mw.text.JSON_PRETTY)), args={['lang']='json'}}
  else
    return frm:extensionTag{name=args.tipo, content=mw.text.jsonEncode(geojson, mw.text.JSON_PRETTY), args=tagParams}
  end
end

function z._Mapa(frame)
  local argumentos = modUtilidades.eliminarArgSinValor(mw.getCurrentFrame():getParent().args)
  -- Parámetros para etiqueta
  local params = {}
  if not (argumentos.latitud and argumentos.longitud and argumentos.tipo) then
    error('Mapa: Faltan argumentos requeridos. Vea [[Plantilla:Mapa|la documentación de la plantilla]] para aclarar dudas.')
  end
  if argumentos.tipo == 'mapa' then
    params.tipo = 'mapframe'
    params.anchura = argumentos.anchura or '300'
    params.altura = argumentos.altura or '300'
    if argumentos['alineación'] then
      if argumentos['alineación'] == 'izquierda' then
        params['alineación'] = 'left'
      elseif argumentos['alineación'] == 'centro' then
        params['alineación'] = 'center'
      elseif argumentos['alineación'] == 'derecha' then
        params['alineación'] = 'right'
      else
        error('Mapa: Argumento <code>alineación</code> inválido: se especificó "'..argumentos['alineación']..'".')
      end
    end
    if argumentos.marco == 'no' then
      params.sinmarco = true;
    end
  elseif argumentos.tipo == 'enlace' then
    params.tipo = 'maplink'
  else
    error('Mapa: Tipo inválido: se especificó "'..argumentos['tipo']..'"')
  end
  params.latitud = argumentos.latitud
  params.longitud = argumentos.longitud
  params.zoom = argumentos.zoom or '9'
  params['título'] = argumentos['título']
  params.debug = (argumentos['debug'] == 'sí')
  -- Sintaxis geoJSON
  local elementos = {}
  local i = 1
  while true do
    local objeto = {}
    local coord
    local tipox = argumentos["tipo" .. tostring(i)]
    if tipox == 'punto' then
      objeto.tipo = 'Point'
      objeto.color = argumentos['color'..tostring(i)] or nil
      objeto['tamaño'] = argumentos['tamaño'..tostring(i)] or nil
      objeto['símbolo'] = argumentos['símbolo'..tostring(i)] or nil
      coord = mw.text.split(argumentos['coordenadas'..tostring(i)], ',')
      objeto.coordenadas = {tonumber(coord[2]), tonumber(coord[1])}
    elseif tipox == 'línea' then
      objeto.tipo = 'LineString'
      objeto.color = argumentos['color'..tostring(i)] or nil
      objeto.ancho = tonumber(argumentos['ancho'..tostring(i)]) or nil
      objeto.opacidad = argumentos['opacidad'..tostring(i)] or nil
      local coordArray
      coordArray = mw.text.split(argumentos['coordenadas'..tostring(i)], ';')
      if next(coordArray) then
        objeto.coordenadas = {}
        for i, v in ipairs(coordArray) do
          coord = mw.text.split(v, ',')
          table.insert(objeto.coordenadas, {tonumber(coord[2]), tonumber(coord[1])})
        end
      else
        error('Mapa: formato de coordenadas invalidas en objeto '..tostring(i))
      end
    elseif tipox == 'polígono' then
      objeto.tipo = 'Polygon'
      objeto.color = argumentos['color'..tostring(i)] or nil
      objeto.ancho = tonumber(argumentos['ancho'..tostring(i)]) or nil
      objeto.relleno = argumentos['relleno'..tostring(i)] or nil
      objeto.opacidad = argumentos['opacidad'..tostring(i)] or nil
      local coordArray
      coordArray = mw.text.split(argumentos['coordenadas'..tostring(i)], ';')
      if next(coordArray) then
        objeto.coordenadas = {}
        for i, v in ipairs(coordArray) do
          coord = mw.text.split(v, ',')
          table.insert(objeto.coordenadas, {tonumber(coord[2]), tonumber(coord[1])})
        end
      else
        error('Mapa: formato de coordenadas invalidas en objeto '..tostring(i))
      end
    elseif tipox == 'geoforma' then
      objeto.tipo = 'geoshape'
    elseif tipox == 'geomáscara' then
      objeto.tipo = 'geomask'
    elseif tipox == 'geolínea' then
      objeto.tipo = 'geoline'
    elseif tipox == 'página' then
      objeto.tipo = 'page'
    elseif tipox then
      error('Objeto '..tostring(i)..': Tipo inválido.')
    else
      break
    end
    objeto['título'] = argumentos['título'..tostring(i)] or nil
    objeto['descripción'] = argumentos['descripción'..tostring(i)] or nil
    table.insert(elementos, objeto)
    i = i + 1
  end
  return z.Mapa(params, elementos)
end

return z