Module:MapLocations

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 '' .. table.concat(locations) .. '' ..         ' ' 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 =     ' ' if #markerIcons > 0 and string.len(markerIcons[1]) > 0 then local iconDelimeter = '|'..markerSize..'px]] [[File:' local iconDiv = ' '..table.concat(markerIcons,iconDelimeter)..' ' 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 map = ' .. map .. ' tableArg = ' .. tableArg .. ' borderCoords = ' .. borderCoords.t.. ','.. borderCoords.r..' '.. borderCoords.b..','.. borderCoords.l .. ' mapsize = ' .. mapSize .. ' defaultMarkerSize = ' .. defaultMarkerSize .. ' defaultMarkerColor = ' .. defaultMarkerColor .. ' markericon = ' .. defaulatMarkerIcon .. ' defaultMarkerOpacity = ' .. defaultMarkerOpacity .. ' text = ' .. text .. ' float = ' .. float .. '\n' end

return p