Module:MapLocations

From ATLAS Wiki
Jump to: navigation, search
Template-info.png Documentation

Displays a map with multiple markers at given coordinates

  • NOTE: Maps must be square

Parameters[edit source]

all are optionally

Parameter default description
map Region A1.jpg Filename of the map
mapsize 300 mapsize in px
markersize 10 markersize in px
markercolor #f30
markericon Filename of the default-icon
opacity 1 opacity of markers
text is displayed under the map
float where the map floats (left / right)
borderCoordT 7.2 Coords of Top
borderCoordR 92.8 Coords of Right
borderCoordB 92.8 Coords of Bottom
borderCoordL 7.2 Coords of Left
tableArg class="wikitable" Optional arguments for the returned table

Locations are given as comma-separated-values and have to be in this order (only lat and lon are mandatory) lat,lon,markersize,markercolor,markertooltip,markericon[,markericon2[,markericon3...]]. Leave parameters empty for the default. E.g. write 20,50,,green for a green marker at 20-50 with the default size.

  • Commas are not allowed in the title-text (it breaks the format)
  • An equal-sign has to be written like {{=}}, a vertical bar / pipe has to be written like {{!}}.

Example[edit source]

{{MapLocations|map=Region A1.jpg|30,50|20,80,20}}
Region A1.jpg
{{MapLocations|map=Region A2.jpg|40,50|30,80|text=so many spots|80,50,30,yellow,I'm a yellow marker!|opacity=0.7|50,50,5|markercolor=green|mapsize=200|10,10,5}}
Region A2.jpg
so many spots

Other methods[edit source]

Can get just the map without the table with {{#invoke MapLocations|generateMapDivmap=Region A1.jpg|30,50|20,80,20}}

Region A1.jpg


_maplocations and _generateMapDiv can be called from other modules


local p = {}
local args
local map
local tableArg
local borderCoords
local mapSize
local defaultMarkerSize
local defaultMarkerColor
local defaulatMarkerIcon
local defaultMarkerOpacity
local text
local float
local markerNameTable = {}
local areArgsParsed = false

function isEmpty(x)
  return x == nil or x == ''
end
-- copies args or assigns default
function p.parseArgs (f)
  if areArgsParsed then
      return
  end
  args = require('Module:ProcessArgs').merge(true)
  map = args.map or 'Map The Island.jpg'
  tableArg = 'class="wikitable"'
  borderCoords = {  t = args.borderCoordT or 100,
                          r = args.borderCoordR or 100,
                          b = args.borderCoordB or 0,
                          l = args.borderCoordL or 0}
  mapSize = args.mapsize or 300
  defaultMarkerSize = args.markersize or 10
  defaultMarkerColor = args.markercolor or '#f40'
  defaulatMarkerIcon = args.markericon or ''
  defaultMarkerOpacity = args.opacity or 1
  text = args.text or ''
  float  = args.float or ''
  areArgsParsed = true
end

function p.maplocations(f)
  p.parseArgs(f)
  return p.drawMap(f)
end

function p._maplocations(_map
                        , _tableArg 
                        ,_borderCoordT 
                        ,_borderCoordR
                        ,_borderCoordB
                        ,_borderCoordL
                        ,_mapsize
                        ,_markersize
                        ,_markercolor
                        ,_markericon
                        ,_opacity
                        ,_text
                        ,_float
                        ,...
)
  args = {...}
  map = _map or "map.jpg"
  tableArg = _tableArg or 'class="wikitable"'
  borderCoords = {  t = _borderCoordT or 100,
                    r = _borderCoordR or 100,
                    b = _borderCoordB or 0,
                    l = _borderCoordL or 0
                 }
  mapSize = _mapsize or 300
  defaultMarkerSize = _markersize or 10
  defaultMarkerColor = _markercolor or '#f40'
  defaulatMarkerIcon = _markericon or ''
  defaultMarkerOpacity = _opacity or 1
  text = _text or ''
  float  = _float or ''
  areArgsParsed = true
  return p.printArgs() .. p.drawMap()
end
--
function p.drawMap(f)
    local subtitle = ''
    if text ~= '' then
      subtitle = '\n|-\n| align="middle" | '..text
    end
    return '{| ' .. tableArg.. ' style="float:' .. float .. '"\n|-\n|'..p.generateMapDiv(f)..subtitle..'\n|}'
end
function p.generateMapDiv (f)
  p.parseArgs(f)
  return p._generateMapDiv( map,
                            borderCoords.t,
                            borderCoords.r,
                            borderCoords.b,
                            borderCoords.l,
                            mapSize,
                            defaultMarkerSize,
                            defaultMarkerColor,
                            defaulatMarkerIcon,
                            defaultMarkerOpacity)
end
function p._generateMapDiv( _map
                          ,_borderCoordT 
                          ,_borderCoordR
                          ,_borderCoordB
                          ,_borderCoordL
                          ,_mapsize
                          ,_markersize
                          ,_markercolor
                          ,_markericon
                          ,_opacity
                        )
  map = _map or "map.jpg"
  borderCoords = {  t = _borderCoordT or 100,
                    r = _borderCoordR or 100,
                    b = _borderCoordB or 0,
                    l = _borderCoordL or 0
                }
  mapSize = _mapsize or 300
  defaultMarkerSize = _markersize or 10
  defaultMarkerColor = _markercolor or '#f40'
  defaulatMarkerIcon = _markericon or ''
  defaultMarkerOpacity = _opacity or 1
  areArgsParsed = true
  local locations = p.generateLocationTable(args)
  return '<div class="noviewer" style="position: relative;' ..
                                      'width:' .. _mapsize .. 'px;' ..
                                      'height:' .. _mapsize .. 'px">'
                .. table.concat(locations) 
                .. '[[File:'.._map..'|'..mapSize..'px]]' .. 
         '</div>'
end

function p.generateLocationTable ()
    local locations = {}
    for _,l in ipairs(args) do
      local markerSize = defaultMarkerSize
      local markerColor = defaultMarkerColor
      local markerIcons = {}
      markerIcons[1] = defaulatMarkerIcon
      local parts,i = {},0
      -- We are assuming the only arguments with commas locations
      for part in string.gmatch(l..',', "([^,]*),") do
        table.insert(parts,part:match "^%s*(.-)%s*$")
      end

      if #parts > 1 then
        -- lat/long are manditory
        local latitude = parts[1] or 0
        local longitude = parts[2] or 0
        local markerName = 'lat '..latitude..', lon '..longitude
        if isEmpty(parts[3]) then 
          markerSize = defaultMarkerSize
        else
          markerSize = parts[3]
        end
        if isEmpty(parts[4]) then
          markerColor = defaultMarkerColor
        else
          markerColor = parts[4]
        end
        if not isEmpty(parts[5]) then
          table.insert(markerNameTable,parts[5])
          markerName = parts[5]..'&#010;'..markerName
        end
        i=5
        while #parts > i do
          markerIcons[i-4] = parts[i+1]
          i = i + 1
        end
        --Construct a colored div or icon marker
        local markerDivStart = '<div style="position: absolute;' ..
                                            'line-height:0;' ..
                                            'padding:0;' ..
                                            'opacity:'..defaultMarkerOpacity..';'
        local markerDivPosition =          'left:'.. p.calculateLeftSide(longitude,markerSize) .. '%;' ..
                                           'top:' .. p.calculateTopSide(latitude,markerSize) ..'%;'
        local markerSizeAndColor =         'width:'..markerSize..'px;' ..
                                           'height:'..markerSize..'px;' ..
                                           'border-radius:50%;' ..
                                           'background-color:'..markerColor..';' ..
                                           'border:1px solid black'
        local markerDivEnd =      '" title="'..markerName..'">'
        local divClose =      '</div>'
        if #markerIcons > 0 and string.len(markerIcons[1]) > 0 then
            local iconDelimeter = '|'..markerSize..'px]]</div><div style="position:absolute">[[File:'
            local iconDiv = '<div style="position:absolute">[[File:'..table.concat(markerIcons,iconDelimeter)..'|'..markerSize..'px]]</div>'
            table.insert(locations,markerDivStart..markerDivPosition..markerDivEnd..iconDiv..divClose)
        else
            table.insert(locations,markerDivStart..markerDivPosition..markerSizeAndColor..markerDivEnd..divClose)
        end
      end
    end
    return locations
end

-- Calculates the top left side of the marker
function p.calculateLeftSide(longitude, markerSize)
    local markerRadiusPercentage = p.calculateMarkerRadiusAsPercentage(markerSize)
    local width = (borderCoords.r-borderCoords.l)
    local xOffset = (longitude-borderCoords.l)
    return 100*(xOffset/width - markerRadiusPercentage)
end
-- Calculates the top edge of the marker
function p.calculateTopSide(latitude, markerSize)
    local markerRadiusPercentage = p.calculateMarkerRadiusAsPercentage(markerSize)
    local height = (borderCoords.b-borderCoords.t)
    local yOffset = (latitude-borderCoords.t)
    return 100*(yOffset/height - markerRadiusPercentage)
end
-- Calculates marker radius as percentage of map width
function p.calculateMarkerRadiusAsPercentage (markerSize)
    return markerSize/(2*mapSize)
end
-- Useful for debugging your input
function p.printArgs(f)
  p.parseArgs(f)
  return '\n<br>map = ' .. map
  .. '<br>tableArg = ' .. tableArg
  .. '<br>borderCoords = ' .. borderCoords.t.. ','.. borderCoords.r..'  '.. borderCoords.b..','.. borderCoords.l
  .. '<br>mapsize = ' .. mapSize
  .. '<br>defaultMarkerSize = ' .. defaultMarkerSize
  .. '<br>defaultMarkerColor = ' .. defaultMarkerColor
  .. '<br>markericon = ' .. defaulatMarkerIcon
  .. '<br>defaultMarkerOpacity = ' .. defaultMarkerOpacity
  .. '<br>text = ' .. text
  .. '<br>float = ' .. float .. '\n'
end

return p