Module:Infobox mapframe

विकिपीडिया से
Jump to navigation Jump to search


Module that automatically makes a mapframe suitable for an infobox automatically, if a page's linked Wikidata item has coordinates (coordinate location (P625)). Otherwise, an empty string is returned.

Can be used from {{Infobox mapframe}}, or invoked directly by a template, or imported to another Lua module.

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

function setCleanArgs(argsTable)
	local cleanArgs = {}
	for key, val in pairs(argsTable) do
		if type(val) == 'string' then
			val = val:match('^%s*(.-)%s*$')
			if val ~= '' then
				cleanArgs[key] = val
			cleanArgs[key] = val
	return cleanArgs

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

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))

local p = {}

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

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

	-- 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"
	-- Calculate zoom from length or area (converted to km or km2)
	if config.length_km then
		args.zoom = getZoom(tonumber(config.length_km))
	elseif config.length_mi then
		args.zoom = getZoom(tonumber(config.length_mi)*1.609344)
	elseif config.area_km2 then
		args.zoom = getZoom(math.sqrt(tonumber(config.area_km2)))
	elseif config.area_mi2 then
		args.zoom = getZoom(math.sqrt(tonumber(config.area_mi2))*1.609344)
		args.zoom = config.zoom or 10

	-- Shape
	args.type = "shape"
	if then = 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 then args.id2 = 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 then args.id3 = end
	if config.marker then args.marker3 = config.marker end
	args["marker-color"] = config["marker-color"] or config["marker-colour"] or "#5E74F3"

	local mapframe = mf._main(args)
	return mapframe

return p