Module:Infobox mapframe: Difference between revisions

From Random Island Wiki
Jump to navigation Jump to search
>Evad37
(adjust styles)
>Evad37
(module entry point; Require wikidata item with coords, so something will definetly be displayed; adjust and add more config options)
Line 24: Line 24:
local parent = frame.getParent(frame)
local parent = frame.getParent(frame)
local parentArgs = parent.args
local parentArgs = parent.args
local mapframe = p_main(parentArgs)
return frame:preprocess(mapframe)
end


-- convert parent args to standard table
p._main = function(config)
-- Require wikidata item with coords, so something will definetly be displayed
if not hasWikidataCoords(config.id or mw.wikibase.getEntityIdForCurrentPage()) then
return ''
end
 
-- arguments for mapframe module
local args = {}
local args = {}
for k, v in pairs(parentArgs) do
args[k] = v
end


-- Some defaults/overrides for infobox presentation
-- Some defaults/overrides for infobox presentation
Line 35: Line 41:
args.frame = "yes"
args.frame = "yes"
args.plain = "yes"
args.plain = "yes"
args["frame-width"] = args["frame-width"] or "270"
args["frame-width"] = config["frame-width"] or "270"
if args.length_km then
args["frame-height"] = config["frame-height"] or "200"
args.zoom = getZoom(tonumber(args.length_km))
args["frame-align"] = center
elseif args.area_km2 then
if config.length_km then
args.zoom = getZoom(math.sqrt(tonumber(args.area_km2)))
args.zoom = getZoom(tonumber(config.length_km))
elseif config.area_km2 then
args.zoom = getZoom(math.sqrt(tonumber(config.area_km2)))
else
else
args.zoom = 10
args.zoom = config.zoom or 10
end
end
-- Shape
-- Shape
args.type = "shape"
args.type = "shape"
args["stroke-width"] = "3"
if config.id then args.id = config.id end
args["stroke-width"] = config["shape-stroke-width"] or config["stroke-width"] or "3"
args["stroke-color"] = config["shape-stroke-color"] or config["shape-stroke-colour"] or config["stroke-color"] or config["stroke-colour"] or "#FF0000"
 
-- Line
-- Line
args.type2 = "line"
args.type2 = "line"
args["stroke-width2"] = "5"
if config.id then args.id2 = config.id end
if args.id then
args["stroke-width2"] = config["line-stroke-width"] or config["stroke-width"] or "5"
args.id2 = args.id
args["stroke-color2"] = config["line-stroke-color"] or config["line-stroke-colour"] or config["stroke-color"] or config["stroke-colour"] or "#FF0000"
end
 
-- Point
-- Point
if hasWikidataCoords(args.id or mw.wikibase.getEntityIdForCurrentPage()) then
args.type3 = "point"
args.type3 = "point"
if config.id then args.id3 = config.id end
if args.id then
if config.marker then args.marker3 = config.marker end
args.id3 = args.id
args.marker["marker-color"] = config["marker-color"] or config["marker-colour"] or "#5E74F3"
end
end


local mapframe = mf._main(args)
local mapframe = mf._main(args)
return frame:preprocess(mapframe)
return mapframe
end
end


return p
return p

Revision as of 00:59, 11 May 2018

Documentation for this module may be created at Module:Infobox mapframe/doc

local mf = require('Module:Mapframe/sandbox')

function hasWikidataCoords(item_id)
	if not(mw.wikibase.isValidEntityId(item_id)) or not(mw.wikibase.entityExists(item_id)) then
		return false
	end
	local coordStatements = mw.wikibase.getBestStatements(item_id, 'P625')
	if not coordStatements or #coordStatements == 0 then
		return false
	end
	return true
end

function getZoom(length_km)
	-- max for zoom 2 is 6400km, for zoom 3 is 3200km, for zoom 4 is 1600km, etc
	local zoom = math.floor(8 - (math.log10(length_km) - 2)/(math.log10(2)))
	-- limit to values between 1 and 17
	return math.max(1, math.min(17, zoom))
end

local p = {}

p.main = function(frame)
	local parent = frame.getParent(frame)
	local parentArgs = parent.args
	local mapframe = p_main(parentArgs)
	return frame:preprocess(mapframe)
end

p._main = function(config)
	-- Require wikidata item with coords, so something will definetly be displayed
	if not hasWikidataCoords(config.id or mw.wikibase.getEntityIdForCurrentPage()) then
		return ''
	end

	-- arguments for mapframe module
	local args = {}

	-- Some defaults/overrides for infobox presentation
	args.display = "inline"
	args.frame = "yes"
	args.plain = "yes"
	args["frame-width"] = config["frame-width"] or "270"
	args["frame-height"] = config["frame-height"] or "200"
	args["frame-align"] = center
	if config.length_km then
		args.zoom = getZoom(tonumber(config.length_km))
	elseif config.area_km2 then
		args.zoom = getZoom(math.sqrt(tonumber(config.area_km2)))
	else
		args.zoom = config.zoom or 10
	end

	-- Shape
	args.type = "shape"
	if config.id then args.id = config.id end
	args["stroke-width"] = config["shape-stroke-width"] or config["stroke-width"] or "3"
	args["stroke-color"] = config["shape-stroke-color"] or config["shape-stroke-colour"] or config["stroke-color"] or config["stroke-colour"] or "#FF0000"

	-- Line
	args.type2 = "line"
	if config.id then args.id2 = config.id end
	args["stroke-width2"] = config["line-stroke-width"] or config["stroke-width"] or "5"
	args["stroke-color2"] = config["line-stroke-color"] or config["line-stroke-colour"] or config["stroke-color"] or config["stroke-colour"] or "#FF0000"

	-- Point
	args.type3 = "point"
	if config.id then args.id3 = config.id end
	if config.marker then args.marker3 = config.marker end
	args.marker["marker-color"] = config["marker-color"] or config["marker-colour"] or "#5E74F3"

	local mapframe = mf._main(args)
	return mapframe
end

return p