Hoppa till innehållet

Modul:Ortsfakta Sverige WD2

Från Wikipedia

Dokumentation [visa] [redigera] [historik] [rensa sidcachen]


local wikidata = require('Modul:Wikidata2').formatStatementsFromLua

p = {}

function p.hjalp(entity, a, args, antalkommuner, ortnamn,land)
	local KOMMUNQID = nil
	--globaliserar den här modulen
	if land == 'Sverige' then
		KOMMUNQID = 'Q127448'
	elseif land == 'Norge' then
		KOMMUNQID = 'Q755707'
	end
	local kommunhuvudort_entity = nil
	if not args.areakommun or not args.invkommun then
		local l = wikidata({entity = entity, property = "P1376", noref='true', avoidqualifier='P582', getonly= KOMMUNQID, raw = 'true', enbarten = 'true'}) -- P1376 = huvudstad i
		if l and #l > 0 then
			kommunhuvudort_entity = mw.wikibase.getEntityObject( l[1].item )
		end
	end
	
	local areakommun, invkommun = nil, nil
	if args.areakommun then
		table.insert(a, 	"\n| area_municipal       = " .. args.areakommun)
		areakommun = tonumber(args.areakommun)
		if args['areakommunår'] then
			table.insert(a, "\n| area_municipal_date  = " .. args['areakommunår'])
		end
		if args.areakommunnot then
			table.insert(a, "\n| area_municipal_note  = " .. args.areakommunnot)
		end
		table.insert(a, 	"\n| area_municipal_label = Kommunens areal")
	elseif kommunhuvudort_entity then
		-- söker först efter fastland och därefter efter utan specifikation
		local v = wikidata({entity = kommunhuvudort_entity, property = "P2046", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', preferqualifier ='P518', rank = 'valid', preferqualifiervalue = 'Q205895', modifyqualifiertime = 'Y', convertunit = 'Q712226', noref='true'})
		if not v or #v == 0 then		
			v = wikidata({entity = kommunhuvudort_entity, property='P2046', sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', modifyqualifiertime = 'Y', noref='noimport'})
		end
		if v and #v > 0 then
			v = v[1]
			table.insert(a, 	"\n| area_municipal       = " .. plus(v.amount))
			areakommun = tonumber(plus(v.amount))
			if v.datum then
				table.insert(a, "\n| area_municipal_date  = " .. v.datum)
			end
			if v.ref then
				table.insert(a, "\n| area_municipal_note  = " .. v.ref)
			end
			table.insert(a, 	"\n| area_municipal_unit  = " .. string.gsub((v.unit or ''),'|kvadratkilometer]]','|km²]]'))
			table.insert(a, 	"\n| area_municipal_label = Kommunens areal")
		end
	end
	if args.invkommun then
		table.insert(a, "\n| population_municipal = " .. args.invkommun)
		invkommun = tonumber(args.invkommun)
		if args['invkommunår'] then
			table.insert(a, "\n| population_municipal_date = " .. args['invkommunår'])
		end
		if args.invkommunnot then
			table.insert(a, "\n| population_municipal_note = " .. args.invkommunnot)
		end
	elseif kommunhuvudort_entity then
		local v = wikidata({entity = kommunhuvudort_entity, property='P1082', sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', modifyqualifiertime = 'Y', noref='noimport'})
		if v and #v > 0 then
			v = v[1]
			table.insert(a, "\n| population_municipal = " .. plus(v.amount))
			invkommun = tonumber(plus(v.amount))
			if v.datum then
				table.insert(a, "\n| population_municipal_date = " .. v.datum)
			end
			if v.ref then
				table.insert(a, "\n| population_municipal_note = " .. v.ref)
			end
		end
	end
	if (args.areakommun or args.invkommun) and args['täthetkommun'] then
		table.insert(a, "\n| population_municipal_density = " .. args['täthetkommun'])
		table.insert(a, "\n| population_municipal_density_round = " .. (args['täthet_decimaler'] or '2'))
	elseif invkommun and areakommun then
		table.insert(a, "\n| population_municipal_density = auto")
		local decimaler = '4'
		if areakommun > 0 then
			if invkommun/areakommun > 2000 then
				decimaler = '0'
			elseif invkommun/areakommun > 200 then
				decimaler = '1'
			elseif invkommun/areakommun > 20 then
				decimaler = '2'
			elseif invkommun/areakommun > 2 then
				decimaler = '3'
			end
		end
		table.insert(a, "\n| population_municipal_round = " .. decimaler)
	end
	
	local metropol_entity = nil
	local metropol_qid = { 	['Stockholm']	= 'Q427543',
							['Malmö']		= 'Q94298',
							['Göteborg']	= 'Q94395'
						}
	if metropol_qid[ortnamn] then
		metropol_entity =  mw.wikibase.getEntityObject( metropol_qid[ortnamn] )
	end	
	
	local invstorstad, areastorstad = nil, nil
	if args.invstorstad then
		table.insert(a, "\n| population_metro = " .. args.invstorstad)
		invstorstad = tonumber(args.invstorstad)
		if args['invstorstadår'] then
			table.insert(a, "\n| population_metro_date = " .. args['invstorstadår'])
		end
		if args.invstorstadnot then
			table.insert(a, "\n| population_metro_not = " .. args.invstorstadnot)
		end
	elseif metropol_entity then
		local v = wikidata({entity = metropol_entity, property='P1082', sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', modifyqualifiertime = 'Y', noref='noimport'})
		if v and #v > 0 then
			v = v[1]
			table.insert(a, "\n| population_metro = " .. plus(v.amount))
			invstorstad = tonumber(plus(v.amount))
			if v.datum then
				table.insert(a, "\n| population_metro_date = " .. v.datum)
			end
			if v.ref then
				table.insert(a, "\n| population_metro_note = " .. v.ref)
			end
		end
	end

	if args.areastorstad then
		table.insert(a, "\n| area_metro           = " .. args.areastorstad)
		areastorstad = tonumber(args.areastorstad)
		if args['areastorstadår'] then
			table.insert(a, "\n| area_metro_date  = " .. args['areastorstadår'])
		end
		if args.areastorstadnot then
			table.insert(a, "\n| area_metro_not   = " .. args.areastorstadnot)
		end
		table.insert(a, "\n| area_metro_unit      = " .. (args.areastorstadenhet or '[[kvadratkilometer|km²]]'))
		table.insert(a, "\n| area_metro_label      = Storstadsområdets areal")
	elseif metropol_entity then
		local v = wikidata({entity = metropol_entity, property='P2046', sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', modifyqualifiertime = 'Y', noref='noimport'})
		if v and #v > 0 then
			v = v[1]
			table.insert(a, "\n| area_metro = " .. plus(v.amount))
			areastorstad = tonumber(plus(v.amount))
			if v.datum then
				table.insert(a, "\n| area_metro_date = " .. v.datum)
			end
			if v.ref then
				table.insert(a, "\n| area_metro_note = " .. v.ref)
			end
			table.insert(a, "\n| area_metro_unit      = " .. string.gsub((v.unit or ''),'|kvadratkilometer]]','|km²]]'))
			table.insert(a, "\n| area_metro_label     = Storstadsområdets areal")
		end
	end

	local extra = ""
	local kollarea = {}
	local kollpop = {}
	
	if invkommun and areakommun then 
		extra = "_urban"	
	end
	local invort = args.invort
	if not invort then
		--hämtar först om berörd del=totalt
		local v = wikidata({entity = entity, property='P1082', sortbytime = 'chronological', enbarten = 'yes', raw = 'true', preferqualifier='P518', preferqualifiervalue='Q16868672', modifyqualifiertime = 'Y', noref='noimport'})
		if not v or #v == 0 then
			v = wikidata({entity = entity, property='P1082', sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', modifyqualifiertime = 'Y', noref='noimport'})
		end
		if v and #v > 0 then
			v = v[1]
			if v.amount then
				invort = tonumber(plus(v.amount))
				table.insert(a, "\n| population" .. extra .." = " .. invort)
				if v.datum then
					table.insert(a, "\n| population" .. extra .."_date = " .. v.datum)
				end
				if v.ref then
					table.insert(a, "\n| population" .. extra .."_note = " .. v.ref)
				end
			end
			if antalkommuner > 1 then
				local i = 1
				local v = wikidata({entity = entity, property = "P131", noref='true', avoidqualifier='P582', getonly = KOMMUNQID, raw ='true', nolink = 'true', noref='true', rank='valid'})
				for uk, uv in pairs(v) do
					local k = "\n| population" .. i
					local v2 = wikidata({entity = entity, property = "P1082", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', preferqualifier ='P518', rank = 'valid', preferqualifiervalue = uv.item, modifyqualifiertime = 'Y', noref='noimport'})
					if v2 and #v2 > 0 and v2[1].amount then
						table.insert(a, k .. " = " .. plus(v2[1].amount))
						table.insert(a, k .. "_type = " .. uv.value)
						if v2[1].datum then
							table.insert(a, k .. "_date = " .. v2[1].datum)
						end
						kollpop[i] = true
						if v2[1].ref then
							table.insert(a, k .. "_note = " .. v2[1].ref)
						end
					end
					i = i + 1
				end
			end
		end
	else
		table.insert(a, "\n| population" .. extra .." = " .. args.invort)
		if args['invår'] then
			table.insert(a, "\n| population" .. extra .."_date = " .. args['invår'])
		end
		if args.invnot then
			table.insert(a, "\n| population" .. extra .."_note = " .. args.invnot)
		end
		local i = 1
		repeat
			v     = 'invort' .. i
			var   = v .. '_år'
			vtext = v .. '_text'
			vnot  = v .. '_not'
			u     = 'population' .. i
			uar   = u .. '_date'
			utext = u .. '_type'
			unot  = u .. '_note'
			if args[v] then
				table.insert(a, "\n| " .. u .. " = " .. args[v])
				if args[var] then
					table.insert(a, "\n| " .. uar .. " = " .. args[var])
				end
				if args[vtext] then
					table.insert(a, "\n| " .. utext .. " = " .. args[vtext])
				end
				if args[vnot] then
					table.insert(a, "\n| " .. unot .. " = " .. args[vnot])
				end
			end
			i = i + 1
		until i > 15
	end
	local areaort = args.areaort
	if not areaort then
		local v = wikidata({entity = entity, property='P2046', sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', modifyqualifiertime = 'Y', noref = 'true', noref='true'})
		if v and #v > 0 then
			v = v[1]
			convertunit = v.unitraw
			if v.amount and ( tonumber(plus(v.amount)) > 3000 or tonumber(plus(v.amount)) > (invort or 3000)) and v.unitraw == 'Q35852' then
				convertunit = 'Q712226'
				v = wikidata({entity = entity, property='P2046', sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', modifyqualifiertime = 'Y', convertunit = convertunit, noref='noimport'})[1]
			else
				v = wikidata({entity = entity, property='P2046', sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', modifyqualifiertime = 'Y', noref='noimport'})[1]
			end
			if v.amount then
				areaort = tonumber(plus(v.amount))
				table.insert(a, "\n| area" .. extra .." = " .. plus(v.amount))
				if v.datum then
					table.insert(a, "\n| area" .. extra .."_date = " .. v.datum)
				end
				if v.ref then
					table.insert(a, "\n| area" .. extra .."_note = " .. v.ref)
				end
				table.insert(a, "\n| area" .. extra .."_unit = " .. string.gsub((v.unit or ''),'|kvadratkilometer]]','|km²]]'))
			end
			if antalkommuner > 1 then
				local i = 1
				local v = wikidata({entity = entity, property = "P131", noref='true', avoidqualifier='P582', getonly = KOMMUNQID, raw ='true', nolink = 'true', noref='true', rank='valid'})
				for uk, uv in pairs(v) do
					local k = "\n| area" .. i
					local v2 = wikidata({entity = entity, property = "P2046", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', preferqualifier ='P518', rank = 'valid', preferqualifiervalue = uv.item, modifyqualifiertime = 'Y', convertunit = convertunit, noref='noimport'})
					if v2 and #v2 > 0 and v2[1].amount then
						table.insert(a, k .. " = " .. plus(v2[1].amount))
						table.insert(a, k .. "_type = " .. uv.value)
						kollarea[i] = true
						if v2[1].datum then
							table.insert(a, k .. "_date = " .. v2[1].datum)
						end
						if v2[1].ref then
							table.insert(a, k .. "_note = " .. v2[1].ref)
						end
						if v2[1].unit then
							table.insert(a, k .. "_unit = " .. string.gsub((v2[1].unit or ''),'|kvadratkilometer]]','|km²]]'))
						end
					end
					i = i + 1
				end
			end
		end
	else
		table.insert(a, "\n| area" .. extra .." = " .. args.areaort)
		if args['areaår'] then
			table.insert(a, "\n| area" .. extra .."_date = " .. args['areaår'])
		end
		if args.areanot then
			table.insert(a, "\n| area" .. extra .."_note = " .. args.areanot)
		end
		if args.areaenhet then
			table.insert(a, "\n| area" .. extra .."_unit = " .. args.areaenhet)
		end
		local i = 1
		repeat
			v     = 'areaort' .. i
			var   = v .. '_år'
			vtext = v .. '_text'
			vnot  = v .. '_not'
			u     = 'area' .. i
			uar   = u .. '_date'
			utext = u .. '_type'
			unot  = u .. '_note'
			if args[v] then
				table.insert(a, "\n| " .. u .. " = " .. args[v])
				if args[var] then
					table.insert(a, "\n| " .. uar   .. " = " .. args[var])
				end
				if args[vtext] then
					table.insert(a, "\n| " .. utext .. " = " .. args[vtext])
				end
				if args[vnot] then
					table.insert(a, "\n| " .. unot  .. " = " .. args[vnot])
				end
			end
			i = i + 1
		until i > 15
	end

	if args.areaort or args.invort then
		if args['täthet'] then
			table.insert(a, "\n| population" .. extra .."_density = " ..  args['täthet'])
			table.insert(a, "\n| population" .. extra .."_density_round = " .. (args['täthet_decimaler'] or '2'))
		end
		local i = 1
		repeat
			local v  = '\n| population' .. i .. '_density = '
			local v2 = '\n| population' .. i .. '_density_round = '
			local u = 'täthet' .. i
			if args[u] then
				table.insert(a, v .. args[u])
				table.insert(a, v2 .. (args['täthet_decimaler'] or '2'))
			end
			i = i + 1
		until i > 15
	elseif areaort and invort then
		local decimaler = '2'
		if areaort > 0 then
			if invort/areaort > 2000 then
				decimaler = '-2'
			elseif invort/areaort > 200 then
				decimaler = '-1'
			elseif invort/areaort > 20 then
				decimaler = '0'
			elseif invort/areaort > 2 then
				decimaler = '1'
			end
		end
		table.insert(a, "\n| population" .. extra .. "_density = auto")
		table.insert(a, "\n| population" .. extra .. "_density_round = " .. decimaler)
		if antalkommuner > 1 then
			local i = 1
			repeat
				if kollpop[i] and kollarea[i] then
					table.insert(a, "\n| population" .. i .. "_density = auto")
					table.insert(a, "\n| population" .. i .. "_density_round = " .. decimaler)
				end
				i = i + 1
			until i > antalkommuner
		end
	end
	
	if args.areastorstad and args.invstorstad and args['täthetstorstad'] then
		table.insert(a, "\n| args.population_metro_density = " .. args['täthetstorstad'])
		table.insert(a, "\n| args.population_metro_density_round = " .. (args['täthet_decimaler'] or '2'))
	elseif metropol_entity and areastorstad and invstorstad then
		table.insert(a, "\n| population_metro_density = auto")
		local decimaler = '4'
		if areastorstad > 0 then
			if invstorstad/areastorstad > 2000 then
				decimaler = '0'
			elseif invstorstad/areastorstad > 200 then
				decimaler = '1'
			elseif invstorstad/areastorstad > 20 then
				decimaler = '2'
			elseif invstorstad/areastorstad > 2 then
				decimaler = '3'
			end
		end
		table.insert(a, "\n| population_metro_density_round = " .. decimaler)
	end


	return a
end
		
		

function plus(a)
	if string.sub(a, 1, 1) == '+' then
		return string.sub(a, 2)
	end
	return a
end

return p