Modul:Infobox mapframe

Iz Wikipedije, proste enciklopedije
Jump to navigation Jump to search
Documentation icon Dokumentacija modula[predogled] [uredi] [zgodovina] [osveži]

Usage[uredi kodo]

Module that automatically makes a mapframe suitable for an infobox automatically, if a page's linked Wikidata item has coordinates (geografske koordinate (P625)). Otherwise, an empty string is returned. The coordinates will be displayed as a point feature unless ID številka OpenStreetMap (P402) is specified on the Wikidata item.

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

local mf = require('Modul:Mapframe')

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 hasWikidataProperty(item_id, property_id)
	if not(item_id) or not(mw.wikibase.isValidEntityId(item_id)) or not(mw.wikibase.entityExists(item_id)) then
		return false
	local statements = mw.wikibase.getBestStatements(item_id, property_id)
	if not statements or #statements == 0 then
		return false
	local hasNoValue = ( statements[1].mainsnak and statements[1].mainsnak.snaktype == 'novalue' )
	if hasNoValue 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)
	local wikidataId = or mw.wikibase.getEntityIdForCurrentPage()
	if not wikidataId then
		return ''

	-- Require wikidata item with coords, so something will be centred somewhere
	local hasCoordinates = hasWikidataProperty(wikidataId, 'P625') -- P625 = coordinate location
	if not hasCoordinates 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"
	args["frame-lat"]    = config["frame-lat"] or config["frame-latitude"] or ""
	args["frame-long"]   = config["frame-long"] or config["frame-longitude"] or ""

	-- 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
	local hasOsmRelationId = hasWikidataProperty(wikidataId, 'P402') -- P402 is OSM relation ID
	local shouldShowPointMarker = not(hasOsmRelationId) or (config.marker and config.marker ~= 'none')
	if shouldShowPointMarker then
		args.type3 = "point"
		if then args.id3 = end
		if config.marker then args.marker3 = config.marker end
		args["marker-color3"] = config["marker-color"] or config["marker-colour"] or "#5E74F3"
	local mapframe = mf._main(args)
	local tracking = hasOsmRelationId and '' or '[[Kategorija:Infopolje mapframe brez OSM relation ID na  Wikipodatkih]]'
	return mapframe .. tracking

return p