Flachdachs (talk | contribs) (maybe this?) |
Tag: Undo |
||
(16 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
+ | local args |
||
− | function p.maplocations( f ) |
||
+ | local map |
||
− | local args = f:getParent().args |
||
+ | local tableArg |
||
− | -- set default values (borderCoords are for top, right, bottom, left) |
||
+ | local borderCoords |
||
− | local map, borderCoords, mapsize, markersize, markercolor, markericon, opacity, text, float = 'Map The Island.jpg', {t=7.2,r=92.8,b=92.8,l=7.2}, 300, 10, '#f40','', 1, '', '' |
||
+ | local mapSize |
||
+ | local defaultMarkerSize |
||
+ | local defaultMarkerColor |
||
+ | local defaulatMarkerIcon |
||
+ | local defaultMarkerOpacity |
||
+ | local text |
||
+ | local float |
||
+ | local markerNameTable = {} |
||
+ | local areArgsParsed = false |
||
+ | function isEmpty(x) |
||
− | -- get values from parameters |
||
− | + | return x == nil or x == '' |
|
+ | end |
||
− | map = args.map |
||
− | + | -- copies args or assigns default |
|
+ | function p.parseArgs (f) |
||
− | if map == 'The Center Map.jpg' then |
||
+ | if areArgsParsed then |
||
− | borderCoords = {t=-2.0,r=100.0,b=100.0,l=0.0} |
||
− | + | return |
|
− | if map == 'Scorched Earth Map.jpg' then |
||
− | borderCoords = {t=0.0,r=100.0,b=100.0,l=0.0} |
||
− | end |
||
− | if map == 'Ragnarok_Topographic_Map.jpg' then |
||
− | borderCoords = {t=0.0,r=100.0,b=100.0,l=0.0} |
||
− | end |
||
− | end |
||
− | if args.mapsize ~= nil then |
||
− | mapsize = args.mapsize |
||
− | end |
||
− | if args.markersize ~= nil then |
||
− | markersize = args.markersize |
||
− | end |
||
− | if args.markercolor ~= nil then |
||
− | markercolor = args.markercolor |
||
− | end |
||
− | if args.markericon ~= nil then |
||
− | markericon = args.markericon |
||
− | end |
||
− | if args.opacity ~= nil then |
||
− | opacity = args.opacity |
||
− | end |
||
− | if args.text ~= nil then |
||
− | text = args.text |
||
− | end |
||
− | if args.float ~= nil then |
||
− | float = args.float |
||
− | end |
||
− | if args.borderCoordT ~= nil then |
||
− | borderCoords.t = args.borderCoordT |
||
− | end |
||
− | if args.borderCoordR ~= nil then |
||
− | borderCoords.r = args.borderCoordR |
||
− | end |
||
− | if args.borderCoordB ~= nil then |
||
− | borderCoords.b = args.borderCoordB |
||
− | end |
||
− | if args.borderCoordL ~= nil then |
||
− | borderCoords.l = args.borderCoordL |
||
end |
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 |
||
− | -- variables for a single marker: lat, lon, ms (markersize), mc (markercolor), mt (markertext/title/tooltip), mis (markericons) |
||
− | + | , _tableArg |
|
+ | ,_borderCoordT |
||
− | for _,l in ipairs(args) do |
||
+ | ,_borderCoordR |
||
− | ms = markersize |
||
+ | ,_borderCoordB |
||
− | mc = markercolor |
||
+ | ,_borderCoordL |
||
− | mis = {} |
||
+ | ,_mapsize |
||
− | mis[1] = markericon |
||
+ | ,_markersize |
||
− | local parts,i = {},0 |
||
+ | ,_markercolor |
||
− | for part in string.gmatch(l..',', "([^,]*),") do |
||
+ | ,_markericon |
||
− | table.insert(parts,part:match "^%s*(.-)%s*$") |
||
+ | ,_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 |
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 () |
||
− | if #parts > 1 then |
||
− | + | local locations = {} |
|
− | + | for _,l in ipairs(args) do |
|
+ | local markerSize = defaultMarkerSize |
||
− | mt = 'lat '..lat..', lon '..lon |
||
+ | local markerColor = defaultMarkerColor |
||
− | if #parts > 2 and string.len(parts[3])>0 then |
||
− | + | 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 |
end |
||
+ | |||
− | if #parts > 3 and string.len(parts[4])>0 then |
||
− | + | if #parts > 1 then |
|
+ | -- lat/long are manditory |
||
− | end |
||
− | + | local latitude = parts[1] or 0 |
|
− | + | local longitude = parts[2] or 0 |
|
+ | local markerName = 'lat '..latitude..', lon '..longitude |
||
− | end |
||
+ | if isEmpty(parts[3]) then |
||
− | i=5 |
||
+ | markerSize = defaultMarkerSize |
||
− | while #parts > i do |
||
− | + | else |
|
− | + | markerSize = parts[3] |
|
− | end |
+ | end |
− | + | if isEmpty(parts[4]) then |
|
+ | markerColor = defaultMarkerColor |
||
− | table.insert(locations,'<div style="position:absolute;line-height:0;left:'.. 100*((lon-borderCoords.l)/(borderCoords.r-borderCoords.l) - ms/(2*mapsize)) ..'%;top:'.. 100*((lat-borderCoords.t)/(borderCoords.b-borderCoords.t) - ms/(2*mapsize)) ..'%;padding:0;opacity:'..opacity..'" title="'..mt..'"><div style="position:absolute">[[File:'..table.concat(mis,'|'..ms..'px]]</div><div style="position:absolute">[[File:')..'|'..ms..'px]]</div></div>') |
||
− | else |
+ | else |
+ | markerColor = parts[4] |
||
− | table.insert(locations,'<div style="position:absolute;line-height:0;left:'.. 100*((lon-borderCoords.l)/(borderCoords.r-borderCoords.l) - ms/(2*mapsize)) ..'%;top:'.. 100*((lat-borderCoords.t)/(borderCoords.b-borderCoords.t) - ms/(2*mapsize)) ..'%;padding:0;width:'..ms..'px;height:'..ms..'px;border-radius:50%;background-color:'..mc..';border:1px solid black;opacity:'..opacity..'" title="'..mt..'"></div>') |
||
+ | end |
||
+ | if not isEmpty(parts[5]) then |
||
+ | table.insert(markerNameTable,parts[5]) |
||
+ | markerName = parts[5]..'
'..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 |
||
end |
end |
||
+ | return locations |
||
− | end |
||
− | local subtitle = '' |
||
− | if text ~= '' then |
||
− | subtitle = '\n|-\n| align="middle" | '..text |
||
− | end |
||
− | return '{| class="wikitable" style="float:'..float..'"\n|-\n|<div class="noviewer" style="position: relative;width:'..mapsize..'px;height:'..mapsize..'px">'..table.concat(locations)..'[[File:'..map..'|'..mapsize..'px]]</div>'..subtitle..'\n|}' |
||
end |
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 |
return p |
Latest revision as of 02:59, 27 July 2020
Documentation
[purge]
Displays a map with multiple markers at given coordinates
- NOTE: Maps must be square
Parameters[]
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[]
{{MapLocations|map=Region A1.jpg|30,50|20,80,20}}
{{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}}
so many spots |
Other methods[]
Can get just the map without the table with {{#invoke MapLocations|generateMapDivmap=Region A1.jpg|30,50|20,80,20}}
_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]..'
'..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