Jump to content

Module:Pop density/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
No edit summary
Line 19: Line 19:
["acre"] = "acre",
["acre"] = "acre",
["ha"] = "ha",
["ha"] = "ha",
["dunam"] = "dunam",
["m²"] = "m2",
["sqm"] = "m2",
["m2"] = "m2"
["m2"] = "m2"
}
}
Line 26: Line 29:
["acre"] = 4046.8564224,
["acre"] = 4046.8564224,
["ha"] = 10000,
["ha"] = 10000,
["dunam"] = 1000,
["m2"] = 1
["m2"] = 1
}
}
Line 33: Line 37:
["acre"] = 'acre',
["acre"] = 'acre',
["ha"] = 'ha',
["ha"] = 'ha',
["dunam"] = 'dunam',
["m2"] = 'm<sup>2</sup>'
["m2"] = 'm<sup>2</sup>'
}
}
Line 40: Line 45:
local dens2, dens2, prec2 = nil, nil, nil
local dens2, dens2, prec2 = nil, nil, nil
local str1, str2 = '', ''
local str1, str2 = '', ''
local cat = ''
if(pop ~= '' and area1 ~= '') then
if(pop ~= '' and area1 ~= '') then
-- pop and area are defined
-- pop and area are defined
local dens1num = tonumber(pop)/tonumber(area1)
local dens1num = tonumber(pop)/tonumber(area1)
local unit1 = unitnames[areaunit1] or nil
local unit2 = unitnames[areaunit2] or nil
if(unit1 == nil) then
cat = '[[Category:Pop density using unsupported units]]'
else
if(unit2 == nil and areaunit2 ~= '') then
cat = '[[Category:Pop density using unsupported units]]'
end
end
prec1 = (prec ~= '') and tonumber(prec) or (1+math.log10(2*area1/(1/10^precision(pop)+pop/area1/10^precision(area1))))
prec1 = (prec ~= '') and tonumber(prec) or (1+math.log10(2*area1/(1/10^precision(pop)+pop/area1/10^precision(area1))))
dens1 = rnd(dens1num, math.floor(prec1 + 0.5))
dens1 = rnd(dens1num, math.floor(prec1 + 0.5))
if (unit1) then
areaunit1 = unitnames[areaunit1] or nil
str1 = '/' .. unitstr[unit1]
areaunit2 = unitnames[areaunit2] or nil
if (areaunit1) then
if(unit2) then
str1 = '/' .. unitstr[areaunit1]
if(areaunit2) then
-- convert
-- convert
local mult = unitmult[areaunit2]/unitmult[areaunit1]
local mult = unitmult[unit2]/unitmult[unit1]
prec2 = prec1 - math.log10(mult)
prec2 = prec1 - math.log10(mult)
dens2 = rnd(dens1num*mult, math.floor(prec2 + 0.5))
dens2 = rnd(dens1num*mult, math.floor(prec2 + 0.5))
str2 = '/' .. unitstr[areaunit2]
str2 = '/' .. unitstr[unit2]
end
end
end
end
Line 77: Line 91:
if( disp ~= 'num' ) then
if( disp ~= 'num' ) then
-- display input density number with unit
-- display input density number with unit
return dens1 .. str1
return dens1 .. str1 .. cat
else
else
-- display input density number without unit
-- display input density number without unit
return dens1
return dens1 .. cat
end
end
end
end
end
end
return '<span class="error">Error</span>'
return '<span class="error">Error</span>' .. cat
end
end



Revision as of 18:17, 4 January 2014

--
-- This module implements {{Pop density}}
--
local p = {}
local math_module = require( "Module:Math" )
local precision = math_module._precision

local function rnd(num, digits)
	-- This function implements {{rnd}}
	return math_module._precision_format(tostring(num), tostring(digits))
end
local unitnames = {
	["km²"] = "km2",
	["sqkm"] = "km2",
	["km2"] = "km2",
	["mi2"] = "sqmi",
	["sqmi"] = "sqmi",
	["acres"] = "acre",
	["acre"] = "acre",
	["ha"] = "ha",
	["dunam"] = "dunam",
	["m²"] = "m2",
	["sqm"] = "m2",
	["m2"] = "m2"
}
local unitmult = {
	["km2"] = 1000000,
	["sqmi"] = 2589988.110336,
	["acre"] = 4046.8564224,
	["ha"] = 10000,
	["dunam"] = 1000,
	["m2"] = 1
}
local unitstr = {
	["km2"] = 'km<sup>2</sup>',
	["sqmi"] = 'sq&nbsp;mi',
	["acre"] = 'acre',
	["ha"] = 'ha',
	["dunam"] = 'dunam',
	["m2"] = 'm<sup>2</sup>'
}

local function popdensity(pop, area1, areaunit1, areaunit2, prec, disp, flip)
	local dens1, dens1, prec1 = nil, nil, nil
	local dens2, dens2, prec2 = nil, nil, nil
	local str1, str2 = '', ''
	local cat = ''
	
	if(pop ~= '' and area1 ~= '') then
		-- pop and area are defined
		local dens1num = tonumber(pop)/tonumber(area1)
		local unit1 = unitnames[areaunit1] or nil
		local unit2 = unitnames[areaunit2] or nil
		if(unit1 == nil) then
			cat = '[[Category:Pop density using unsupported units]]'
		else
			if(unit2 == nil and areaunit2 ~= '') then
				cat = '[[Category:Pop density using unsupported units]]'
			end
		end
		
		prec1 = (prec ~= '') and tonumber(prec) or (1+math.log10(2*area1/(1/10^precision(pop)+pop/area1/10^precision(area1))))
		dens1 = rnd(dens1num, math.floor(prec1 + 0.5))
		if (unit1) then
			str1 = '/' .. unitstr[unit1]
			if(unit2) then
				-- convert
				local mult = unitmult[unit2]/unitmult[unit1]
				prec2 = prec1 - math.log10(mult)
				dens2 = rnd(dens1num*mult, math.floor(prec2 + 0.5))
				str2 = '/' .. unitstr[unit2]
			end
		end

		-- form output
		if( str2 ~= '' ) then
			-- has a converted unit
			if( disp ~= 'num' ) then
				-- display input and output density numbers with units
				if( flip == 'on' ) then
					return dens2 .. str2 .. ' (' .. dens1 .. str1 .. ')'
				else
					return dens1 .. str1 .. ' (' .. dens2 .. str2 .. ')'
				end
			else
				-- display output density number without unit
				return dens2
			end
		else
			-- no converted unit
			if( disp ~= 'num' ) then
				-- display input density number with unit 
				return dens1 .. str1 .. cat
			else
				-- display input density number without unit
				return dens1 .. cat
			end
		end
	end
	return '<span class="error">Error</span>' .. cat
end

function p.density(frame)
	local args = (frame.args[3] ~= nil) and frame.args or frame:getParent().args
	return popdensity(args[1] or '', args[2] or '', args[3] or '', args[4] or '', args['prec'] or '', args['disp'] or '', args['flip'] or '')
end

return p