local wd = require('Module:Wd')
local p =
local function comma_to_dots(x) if x then res, _ = string.gsub(x, "%,", ".") return tonumber(res) or x end return end
local function find(f, l) for _, v in pairs(l) do if v
local function get_unit(result) local allowed_lang = local lang =
for index, type_unit in pairs(mw.wikibase.getBestStatements(result, 'P5061')) do if find(type_unit["mainsnak"]["datavalue"]["value"]["language"], allowed_lang) then lang[type_unit["mainsnak"]["datavalue"]["value"]["language"]] = type_unit["mainsnak"]["datavalue"]["value"]["text"] end end
if lang["nb"] then return lang["nb"] elseif lang["nn"] then return lang["nn"] elseif lang["en"] then return lang["en"] else return mw.wikibase.getLabel(result) endend
local function round(num, numDecimalPlaces) return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))end
local function find_population_density(popu, area, item_p) local population_density = round(popu/area, 2) local type_SI if item_p then claims = mw.wikibase.getBestStatements(item_p, 'P2046') result = claims[1].mainsnak.datavalue.value.unit result = mw.ustring.sub(result, mw.ustring.find(result, "Q"), -1) type_SI = '' .. get_unit(result) .. '' else type_SI = "km²" end return mw.language.getContentLanguage:formatNum(population_density) .. " innb./" .. type_SIend
local function wd_match(item_p, pid, last_val) if wd._property ~= "" then if pid
"P1082" then return find_population_density(tonumber(wd._property), last_val) end end if wd._property ~= "" and wd._property ~= "" then return find_population_density(tonumber(wd._property), tonumber(wd._property), item_p) end return end
function p.main(frame) local area = comma_to_dots(frame.args[1]) --P2046 local population = comma_to_dots(frame.args[2]) --P1082 local population_density = comma_to_dots(frame.args[3]) local item_p = mw.wikibase.getEntityIdForCurrentPage local item_p = "Q55" if population_density
and population
then return wd_match(item_p) end if population_density ~= then if type(population_density)
"number" and type(population)
"number" and population
"number" and area
return p