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


local wikidata = require('Modul:Wikidata2').formatStatementsFromLua
local Ortsfaktarat = require('Modul:Ortsfakta').rat
local HjalpSverige = require('Modul:Ortsfakta Sverige WD2').hjalp
local namnversalisering = require('Modul:Textformatering').namnversalisering

p = {}

function p.ort( entity, args )
	local ill_kat = ''
	local a = {}
	table.insert(a, "{{Geobox\n| Settlement")
	
	local ortnamn = args.ortnamn 
	if not ortnamn or ortnamn == '' then
		if entity.labels and entity.labels.sv then
			ortnamn = entity.labels.sv.value
		else
			ortnamn = mw.title.getCurrentTitle().text
		end
	end
	table.insert(a, "\n| name = " .. ortnamn)
	if args.alternativnamn then
		table.insert(a, "\n| other_name = " .. (args.alternativnamn or ''))
	end
	
	local typ = wikidata({entity = entity, property = 'P31', avoidqualifier='P582', raw = 'true', noref = 'true'})
	if typ and #typ>0 and typ.item== 'Q15092344' then
		table.insert(a, "\n| category = [[Tätort]]")
	elseif args.form and args ~= '' then
		if mw.ustring.lower(form) == 'tätort' then
			table.insert(a, "\n| category = [[Tätort]]")
		else
			table.insert(a, "\n| category = " .. form)
		end
	else
		table.insert(a, "\n| category = Ort")
	end
	
	if args.smeknamn then
		table.insert(a, "\n| nickname      = " .. args.smeknamn)
		table.insert(a, "\n| nickname_type = Smeknamn")
	else
		v = wikidata({entity = entity, property = 'P1449', raw = 'true'})
		if v and #v > 0 then
			local s = {}
			for i, j in pairs(v) do
				table.insert(s, j.value .. j.ref)
			end
			table.insert(a, "\n| nickname      = " .. table.concat(s, ", "))
			table.insert(a, "\n| nickname_type = Smeknamn")
		end
	end	
	if args.slogan then
		table.insert(a, "\n| motto      = " .. args.slogan)
		table.insert(a, "\n| motto_type = Slogan")
	end

	local bild = args.bild
	local bildtext = args.bildtext
	local bildstorlek = args.bildstorlek
	if not bild then
		local data = wikidata({entity = entity, property = 'P18', raw = 'true', noref='true', enbarten = 'true', langpref = 'sv'})
		if data and #data >0 then
			bild = data[1].value
			bildtext = data[1].bildtext or data[1].motiv
		end
	end
	if bild and bild ~= '' then
		table.insert(a, "\n| image         = " .. (bild or ''))
		if bildtext then
			table.insert(a, "\n| image_caption = " .. (bildtext or ''))
		end
		if bildstorlek then
			table.insert(a, "\n| image_size    = " .. bildstorlek)
		end
	end
	
	if ortnamn == "Lödöse" or ortnamn == "Hinken och spaden" or ortnamn == 'Fjällbacka' then
		if args.vapen then
			table.insert(a, "\n| symbol      = " .. args.vapen)
			if args.vapennamn then
				table.insert(a, "\n| symbol_type = " .. args.vapennamn)
			else
				table.insert(a, "\n| symbol_type = Vapen")
			end
		else
			local data = wikidata({entity = entity, property = 'P94', raw = 'true', noref='true', enbarten = 'true'})
			local data2 = wikidata({entity = entity, property = 'P237', raw = 'true', noref='true', enbarten = 'true', label = 'Vapen'})
			if data and #data > 0 then
				table.insert(a, "\n| symbol = " .. data[1].value)
				if data2 and #data2 > 0 then
					table.insert(a, "\n| symbol_type = " .. data2[1].value)
				else
					table.insert(a, "\n| symbol_type = Vapen")
				end
			end
		end
	end

	table.insert(a, "\n| country       = Norge")
	table.insert(a, "\n| country_flag  = Flag of Norway.svg")

	local lan = args['fylke'] or args['fylke1'] 
	local lan2 = args['fylke2'] 
	local lan3 = args['fylke3'] 
	if not lan then
		local v = harva(entity, 'Q755707', 'Q192299', 99)
		lan = indexera(v, 1) 
		lan2 = indexera(v, 2)
		lan3 = indexera(v, 3)
	end
	if lan and lan ~= ''  then
		table.insert(a, "\n| region  = " .. lan)
		if lan2 and lan2 ~= ''  then
			table.insert(a, "\n| region1 = " .. lan2)
			if lan3 and lan3 ~= ''  then
				table.insert(a, "\n| region2 = " .. lan3)
			end
		end
		table.insert(a, "\n| region_type            = [[Norges fylken|Fylke]]")
		table.insert(a, "\n| region_label           = Fylke orten ligger i")
	end

	local comiqs = {}
	local antalkommuner = 0
	local kommun = args['kommun'] or args['kommun1']
	local kommun2 = args['kommun2']
	local kommun3 = args['kommun3']
	local kommun4 = args['kommun4']
	local kommun5 = args['kommun5']
	local kommun6 = args['kommun6']
	local kommun7 = args['kommun7']
	local kommun8 = args['kommun8']
	local kommun9 = args['kommun9']
	local kommun10 = args['kommun10']
	local kommun11 = args['kommun11']
	local kommun12 = args['kommun12']
	local kommun13 = args['kommun13']
	local kommun14 = args['kommun14']
	local kommun15 = args['kommun15']
	if not kommun then
		local v = harva(entity, 'Q755707', 'Q755707', 99)
		kommun   = indexera(v, 1)
		kommun2  = indexera(v, 2)
		kommun3  = indexera(v, 3)
		kommun4  = indexera(v, 4)
		kommun5  = indexera(v, 5)
		kommun6  = indexera(v, 6)
		kommun7  = indexera(v, 7)
		kommun8  = indexera(v, 8)
		kommun9  = indexera(v, 9)
		kommun10 = indexera(v, 10)
		kommun11 = indexera(v, 11)
		kommun12 = indexera(v, 12)
		kommun13 = indexera(v, 13)
		kommun14 = indexera(v, 14)
		kommun15 = indexera(v, 15)
	end
	
	if kommun and kommun ~= '' then
		table.insert(a, "\n| municipality = " .. kommun)
		antalkommuner = 1
		if kommun2 and kommun2 ~= '' then
			table.insert(a, "\n| municipality1 = " .. kommun2)
			antalkommuner = 2
			if kommun3 and kommun3 ~= '' then
				table.insert(a, "\n| municipality2 = " .. kommun3)
				antalkommuner = 3
				if kommun4 and kommun4 ~= '' then
					table.insert(a, "\n| municipality3 = " .. kommun4)
					antalkommuner = 4
					if kommun5 and kommun5 ~= '' then
						table.insert(a, "\n| municipality4 = " .. kommun5)
						antalkommuner = 5
						if kommun6 and kommun6 ~= '' then
							table.insert(a, "\n| municipality5 = " .. kommun6)
							antalkommuner = 6
							if kommun7 and kommun7 ~= '' then
								table.insert(a, "\n| municipality6 = " .. kommun7)
								antalkommuner = 7
								if kommun8 and kommun8 ~= '' then
									table.insert(a, "\n| municipality7 = " .. kommun8)
									antalkommuner = 8
									if kommun9 and kommun9 ~= '' then
										table.insert(a, "\n| municipality8 = " .. kommun9)
										antalkommuner = 9
										if kommun10 and kommun10 ~= '' then
											table.insert(a, "\n| municipality9 = " .. kommun10)
											antalkommuner = 10
											if kommun11 and kommun11 ~= '' then
												table.insert(a, "\n| municipality10 = " .. kommun11)
												antalkommuner = 11
												if kommun12 and kommun12 ~= '' then
													table.insert(a, "\n| municipality11 = " .. kommun12)
													antalkommuner = 12
													if kommun13 and kommun13 ~= '' then
														table.insert(a, "\n| municipality12 = " .. kommun13)
														antalkommuner = 13
														if kommun14 and kommun14 ~= '' then
															table.insert(a, "\n| municipality13 = " .. kommun14)
															antalkommuner = 14
															if kommun15 and kommun15 ~= '' then
																table.insert(a, "\n| municipality14 = " .. kommun15)
																antalkommuner = 15
															end
														end
													end
												end
											end
										end
									end
								end
							end
						end
					end
				end							
			end
		end
		if kommun2 and kommun2 ~= '' then
			table.insert(a, "\n| municipality_type = [[Norges kommuner|Kommuner]]")
			table.insert(a, "\n| municipality_label    = Kommuner orten ligger i")
		else
			table.insert(a, "\n| municipality_type = [[Norges kommuner|Kommun]]")
			table.insert(a, "\n| municipality_label    = Kommun orten ligger i")
		end
	end	

	
	if args.koordinat then
		table.insert(a, "\n| location = " .. args.koordinat)
	end
	
	local elevation = args['höjdläge'] 
	if elevation then
		table.insert(a, "\n| elevation = " .. elevation)
	else
		local v = wikidata({entity=entity, property='P2044', enbarten = 'true', raw = 'true', convertunit='Q11573', noref='noimport'})
		if v and #v > 0 then
			v = v[1]
			if tonumber(v.amount) then
				table.insert(a, "\n| elevation = " .. math.abs( tonumber(v.amount)) )
				if tonumber(v.amount) < 0 then
					table.insert(a, "\n| elevation_unit = muh" )
				else
					table.insert(a, "\n| elevation_unit = möh" )
				end
				if v.ref and v.ref ~= '' then
					table.insert(a, "\n| elevation_note = " .. v.ref)
				end
			end
		end
	end

	local highest = args['högsta punkt namn'] 

	if highest then
		table.insert(a, "\n| highest = " .. highest )
		local highest_elevation = args['högsta punkt meter'] 
		if highest_elevation then
			table.insert(a, "\n| highest_elevation = " .. highest_elevation )
		end
	else
		local v = wikidata({entity=entity, property='P610', enbarten = 'true', raw = 'true'})
		if v and #v > 0 then
			table.insert(a, "\n| highest = " .. v[1].value .. v[1].ref)
			local v2 = wikidata({entityId=v[1].item, property='P2044', enbarten = 'true', raw = 'true', convertunit='Q11573', noref='noimport'})
			if v2 and #v2 > 0 then
				if tonumber(v2[1].amount) then
					table.insert(a, "\n| highest_elevation = " .. math.abs( tonumber(v2[1].amount)) )
					if tonumber(v2[1].amount) < 0 then
						table.insert(a, "\n| highest_elevation_unit = muh" )
					else
						table.insert(a, "\n| highest_elevation_unit = möh" )
					end
					table.insert(a, "\n| highest_elevation_note = " .. v2[1].ref)
				end
			end
			v2 = wikidata({entityId=v[1].item, property='P625', enbarten = 'true', raw = 'true', noref = 'true', nogeodataparser = 'true'})
			if v2 and #v2 > 0 then
				v2 = Ortsfaktarat(v2[1], 'sekund')
				table.insert(a, "\n| highest_lat_d   = " .. (v2.lat_g or ''))
				table.insert(a, "\n| highest_lat_m   = " .. (v2.lat_m or ''))
				table.insert(a, "\n| highest_lat_s   = " .. (v2.lat_s or ''))
				table.insert(a, "\n| highest_lat_NS  = " .. (v2.lat_NS or ''))
				table.insert(a, "\n| highest_long_d  = " .. (v2.long_g or ''))
				table.insert(a, "\n| highest_long_m  = " .. (v2.long_m or ''))
				table.insert(a, "\n| highest_long_s  = " .. (v2.long_s or ''))
				table.insert(a, "\n| highest_long_EW = " .. (v2.long_EW or ''))
			end
		end
	end


	local lowest = args['lägsta punkt namn'] 
	if lowest then
		table.insert(a, "\n| lowest = " .. lowest )
		local lowest_elevation = args['lägsta punkt meter'] 
		if lowest_elevation then
			table.insert(a, "\n| lowest_elevation = " .. lowest_elevation )
		end
	else
		local v = wikidata({entity=entity, property='P1589', enbarten = 'true', raw = 'true'})
		if v and #v > 0 then
			table.insert(a, "\n| lowest = " .. v[1].value .. v[1].ref)
			local v2 = wikidata({entityId=v[1].item, property='P2044', enbarten = 'true', raw = 'true', convertunit='Q11573', noref='noimport'})
			if v2 and #v2 > 0 then
				if tonumber(v2[1].amount) then
					table.insert(a, "\n| lowest_elevation = " .. math.abs( tonumber(v2[1].amount)) )
					if tonumber(v2[1].amount) < 0 then
						table.insert(a, "\n| lowest_elevation_unit = muh" )
					else
						table.insert(a, "\n| lowest_elevation_unit = möh" )
					end
					table.insert(a, "\n| lowest_elevation_note = " .. v2[1].ref)
				end
			end
			v2 = wikidata({entityId=v[1].item, property='P625', enbarten = 'true', raw = 'true', noref = 'true', nogeodataparser = 'true'})
			if v2 and #v2 > 0 then
				v2 = Ortsfaktarat(v2[1], 'sekund')
				table.insert(a, "\n| lowest_lat_d   = " .. (v2.lat_g or ''))
				table.insert(a, "\n| lowest_lat_m   = " .. (v2.lat_m or ''))
				table.insert(a, "\n| lowest_lat_s   = " .. (v2.lat_s or ''))
				table.insert(a, "\n| lowest_lat_NS  = " .. (v2.lat_NS or ''))
				table.insert(a, "\n| lowest_long_d  = " .. (v2.long_g or ''))
				table.insert(a, "\n| lowest_long_m  = " .. (v2.long_m or ''))
				table.insert(a, "\n| lowest_long_s  = " .. (v2.long_s or ''))
				table.insert(a, "\n| lowest_long_EW = " .. (v2.long_EW or ''))
			end
		end
	
	end

	local nordligt = nil
	if args.lat_dec or args.lat_g then
		nordligt = tonumber(args.lat_dec or args.lat_g)
		table.insert(a, "\n| lat_d   = " .. (args.lat_dec or args.lat_g))
		table.insert(a, "\n| lat_m   = " .. (args.lat_m or ''))
		table.insert(a, "\n| lat_s   = " .. (args.lat_s or ''))
		table.insert(a, "\n| lat_NS  = " .. (args.lat_NS or ''))
		table.insert(a, "\n| long_d  = " .. (args.long_dec or args.long_g or ''))
		table.insert(a, "\n| long_m  = " .. (args.long_m or ''))
		table.insert(a, "\n| long_s  = " .. (args.long_s or ''))
		table.insert(a, "\n| long_EW = " .. (args.long_EW or ''))
	elseif not args.koordinat then
		local v = wikidata({entity = entity, property = 'P625', raw = 'true', noref='true', enbarten = 'true', nogeodataparser = 'true'})
		if v and #v > 0 then
			v = Ortsfaktarat(v[1], 'sekund')
			if v then
				nordligt = tonumber(v.lat_g)
				table.insert(a, "\n| lat_d   = " .. (v.lat_g or ''))
				table.insert(a, "\n| lat_m   = " .. (v.lat_m or ''))
				table.insert(a, "\n| lat_s   = " .. (v.lat_s or ''))
				table.insert(a, "\n| lat_NS  = " .. (v.lat_NS or ''))
				table.insert(a, "\n| long_d  = " .. (v.long_g or ''))
				table.insert(a, "\n| long_m  = " .. (v.long_m or ''))
				table.insert(a, "\n| long_s  = " .. (v.long_s or ''))
				table.insert(a, "\n| long_EW = " .. (v.long_EW or ''))
			end
		end
	end
	a = HjalpSverige(entity, a, args, antalkommuner, ortnamn)
	
	if args.grundades then
		table.insert(a, "\n| established = " .. args.grundades)
	else
		local v = wikidata({entity = entity, property = 'P571', conjunction = '<br/>', separator = '<br/>', som = 'true'})
		if v and v ~= '' then
			table.insert(a, "\n| established = " .. v)
		end
	end
	
	table.insert(a, '\n| timezone     = [[Centraleuropeisk tid|CET]] | utc_offset = +1 | timezone_type = [[Tidszon]]')
	table.insert(a, '\n| timezone_DST = [[Centraleuropeisk tid|CEST]]| utc_offset_DST = +2')
	
	if args.postort then
		table.insert(a, '\n| postal_town = ' .. namnversalisering(args.postort))
		table.insert(a, '\n| postal_town_type = [[Postort]]')
		if args.postort_not then
			table.insert(a, '\n| postal_town_note = ' .. args.postort_not)
		end
	end
	
	if args.postnummer then
		table.insert(a, '\n| postal_code = ' .. args.postnummer)
		table.insert(a, '\n| postal_code_type = Postnummer')
	else
		local v = wikidata({entity = entity, property = 'P281'})
		if v and v ~= '' then
			table.insert(a, '\n| postal_code = ' .. v)
			table.insert(a, '\n| postal_code_type = Postnummer')
		end	
	end

	if args.riktnummer then
		table.insert(a, '\n| area_code = ' .. args.riktnummer)
		table.insert(a, '\n| area_code_type = [[Riktnummer]]')
	else
		local v = wikidata({entity = entity, property = 'P473'})
		if v and v ~= '' then
			table.insert(a, '\n| area_code = ' .. v)
			table.insert(a, '\n| area_code_type = [[Riktnummer]]')
		end	
	end
	
	if args['tätortskod'] or args['tätortskod1'] then
		table.insert(a, '\n| code_type = [[Tätortskod]]')
		if args['tätortskod'] then
			table.insert(a, '\n| code      = ' .. args['tätortskod'])
		end
		if args['tätortskod1'] then
			table.insert(a, '\n| codea      = ' .. args['tätortskod1'])
			table.insert(a, '\n| codea_type = ' .. args['tätortskod1_text'])
			if args['tätortskod2'] then
				table.insert(a, '\n| codeb      = ' .. args['tätortskod2'])
				table.insert(a, '\n| codeb_type = ' .. args['tätortskod2_text'])
				if args['tätortskod3'] then
					table.insert(a, '\n| codec      = ' .. args['tätortskod3'])
					table.insert(a, '\n| codec_type = ' .. args['tätortskod3_text'])
				end
			end
		end
	else
		local v = wikidata({entity = entity, property = 'P3992', withintervall = 'yes', modifyqualifiertime = 'Y', separator = '<br/>', conjunction = '<br/>', rank = 'valid' })
		if v then
			table.insert(a, '\n| code_type = [[Tätortskod]]')
			table.insert(a, '\n| code      = ' .. v)
		end
	end
	
	if args.geonames then
		table.insert(a, '\n| geonames = ' .. args.geonames)
	end

	if args.positionskarta then
		table.insert(a, '\n| map           = ' .. (args.positionskartnamn or ''))
		table.insert(a, '\n| map_caption   = ' .. (args.karttext or ''))
		table.insert(a, '\n| map_size      = ' .. (args.kartstorlek or ''))
		if args['prickpos x'] then
			table.insert(a, '\n| map_locator_x = ' .. (args['prickpos x'] or ''))
			table.insert(a, '\n| map_locator_y = ' .. (args['prickpos y'] or ''))
		elseif args.positionskarta then
			table.insert(a, '\n| map_locator   = ' .. args.positionskarta)
		end
	elseif args['prickpos x'] then
		table.insert(a, '\n| map           = Norway location map.svg')
		table.insert(a, '\n| map_caption   = ' .. (args.karttext or ''))
		table.insert(a, '\n| map_size      = ' .. (args.kartstorlek or ''))
		table.insert(a, '\n| map_locator_x = ' .. (args['prickpos x'] or ''))
		table.insert(a, '\n| map_locator_y = ' .. (args['prickpos y'] or ''))
	else
		local s1 = positionskartnamn(entity)
		local s2 = positionskarta(entity)
		local s3 = positionskarta2(entity)
		table.insert(a, '\n| map           = ' .. s1)
		local c = {['Akershus'] = true,	['Oslo'] = true, ['Troms'] = true}
		
		if s2 == '' then
			table.insert(a, '\n| map_caption   = Ortens läge i Norge')
			s2 = 'Norge'
		else
			table.insert(a, '\n| map_caption   = Ortens läge i ' .. s2 .. ' fylke')
		end
		table.insert(a, '\n| map_locator   = ' .. s2)
		if s3 then
			table.insert(a, '\n| map2            = ' .. s3)
			table.insert(a, '\n| map2_caption = ' .. s2 .. ' fylke i Norge')
		end
	end

	if args.karta then
		table.insert(a, '\n| | map2            = ' .. args.karta)
		if args.karttext2 then
			table.insert(a, '\n| map2_caption  = ' .. args.karttext2)
		end
		if args.kartstorlek2 then
			table.insert(a, '\n| map2_size     = ' .. args.kartstorlek2)
		end
	end
	local kmlfaktaruta = require('Modul:KML faktaruta').main
	local k = kmlfaktaruta(entity)
	if k then
		table.insert(a, '\n| KML                   = ' .. k)
	end
	
	if args.fotnot and args.fotnot ~= '' then
		table.insert(a, '\n| footnotes         = ' .. args.fotnot .. '<br />' .. reginalink() .. '<br />[[d:'.. entity.id .. '|Redigera Wikidata]]')
	else
		table.insert(a, '\n| footnotes         = ' .. reginalink() .. '<br />[[d:'.. entity.id .. '|Redigera Wikidata]]')
	end

	if args.commons then
		table.insert(a, '\n| commons               = ' .. args.commons)
	else
		table.insert(a, '\n| commons               = ' .. Comcat(entity))
	end

	table.insert(a, '\n}}')
	return mw.getCurrentFrame():preprocess(table.concat(a)) --..ill_kat (deaktiverar tillsviddare)
end		

function reginalink()
-- länk till ReginaWeb (geodata.scb.se) för den tätort eller småort som är angiven. 
-- funktionen klarar i nuvarande form bara av att hantera en tätortskod och en småortskod.
-- länken går till alla generationer av den aktuella orten, skulle kunna förfinas genom att analysera startdatum och slutdatum för alla P31-uttalanden och bara länka till den senaste
    local res = ''
	local tkod = wikidata({entity = entity, property = 'P775', enbarten = 'ja', noref = 'true', avoidstringpattern = 'X'})
	local skod = wikidata({entity = entity, property = 'P776', enbarten = 'ja', noref = 'true'})
    if tkod then
		res = '[http://geodata.scb.se/reginawebmap/main/webapp/?typ=tatort&t=' .. tkod .. ' SCB:s tätortsavgränsning] (långsam)'	
    end
	if skod then 
		if tkod then
			res = res .. '<br />'
		end 
		res = res .. '[http://geodata.scb.se/reginawebmap/main/webapp/?typ=smaort&s=' .. skod .. ' SCB:s småortsavgränsning] (långsam)'
	end
	return res
end

function indexera(v, i)
	if v and #v >= i then
		return v[i].value
	end
	return ''
end

function kommunqids(v)
	local s = {}
	for i, j in pairs(v) do
		table.insert(s, v.item)
	end
	return s
end

function lankod2lan(lankod) --fylkeskoder
local lan = ''
if lankod == '01' then lan = 'Østfold' -- Norge
	elseif lankod == '02' then lan = 'Akershus'	
	elseif lankod == '03' then lan = 'Oslo'
	-- elseif lankod == '04' then lan = 'Hedmark'
	-- elseif lankod == '05' then lan = 'Oppland'
	elseif lankod == '06' then lan = 'Buskerud'
	elseif lankod == '07' then lan = 'Vestfold'
	elseif lankod == '08' then lan = 'Telemark'
	-- elseif lankod == '09' then lan = 'Aust-Agder'
	-- elseif lankod == '10' then lan = 'Vest-Agder'
    elseif lankod == '11' then lan = 'Rogaland'
    -- elseif lankod == '12' then lan = 'Hordaland'
    -- elseif lankod == '13' then lan = 'Bergen'
    -- elseif lankod == '14' then lan = 'Sogn og Fjordane'
    elseif lankod == '15' then lan = 'Møre og Romsdal'
	-- elseif lankod == '16' then lan = 'Sør-Trøndelag'
	-- elseif lankod == '17' then lan = 'Nord-Trøndelag'
	elseif lankod == '18' then lan = 'Nordland'
	elseif lankod == '19' then lan = 'Troms'
	elseif lankod == '20' then lan = 'Finnmark'
	-- elseif lankod == '30' then lan = 'Viken'
	elseif lankod == '31' then lan = 'Østfold'
	elseif lankod == '32' then lan = 'Akershus'
	elseif lankod == '33' then lan = 'Buskerud'
	elseif lankod == '34' then lan = 'Innlandet'
	-- elseif lankod == '38' then lan = 'Vestfold og Telemark'
	elseif lankod == '39' then lan = 'Vestfold'
	elseif lankod == '40' then lan = 'Telemark'
	elseif lankod == '42' then lan = 'Agder'
	elseif lankod == '46' then lan = 'Vestland'
	elseif lankod == '50' then lan = 'Trøndelag'					
	-- elseif lankod == '54' then lan = 'Troms og Finnmark'
	elseif lankod == '55' then lan = 'Troms'
	elseif lankod == '56' then lan = 'Finnmark'
	else lan = ''
end
return lan
end
function lankarta(lankod)
	local kart = ''
	if lankod == '01' then kart = 'Østfold in Norway 2024.svg'
	elseif lankod == '02' then kart = 'Akershus in Norway 2024.svg'
	elseif lankod == '03' then kart = 'Oslo in Norway 2024.svg'
	elseif lankod == '06' then kart = 'Buskerud in Norway 2024.svg'
	elseif lankod == '07' then kart = 'Vestfold in Norway 2024.svg'
	elseif lankod == '08' then kart = 'Telemark in Norway 2024.svg'
	elseif lankod == '11' then kart = 'Rogaland in Norway 2024.svg'
	elseif lankod == '15' then kart = 'Møre og Romsdal in Norway 2024.svg'
	elseif lankod == '18' then kart = 'Nordland in Norway 2024.svg'
	elseif lankod == '19' then kart = 'Troms in Norway 2024.svg'
	elseif lankod == '20' then kart = 'Finnmark in Norway 2024.svg'
	elseif lankod == '39' then kart = 'Vestfold in Norway 2024.svg'
	elseif lankod == '31' then kart = 'Østfold in Norway 2024.svg'
	elseif lankod == '32' then kart = 'Akershus in Norway 2024.svg'
	elseif lankod == '33' then kart = 'Buskerud in Norway 2024.svg'
	elseif lankod == '34' then kart = 'Innlandet in Norway 2024.svg'
	elseif lankod == '39' then kart = 'Vestfold in Norway 2024.svg'
	elseif lankod == '40' then kart = 'Telemark in Norway 2024.svg'
	elseif lankod == '42' then kart = 'Agder in Norway 2024.svg'
	elseif lankod == '46' then kart = 'Vestland in Norway 2024.svg'
	elseif lankod == '50' then kart = 'Trøndelag in Norway 2024.svg'
	elseif lankod == '55' then kart = 'Troms in Norway 2024.svg'
	elseif lankod == '56' then kart = 'Finnmark in Norway 2024.svg'
	end
	return kart
end

function lankod(entity) -- hämtar fylkeskod från i första hand det fylke, i andra hand den kommun som objektet ingår i och i tredje hand det distrikt som objektet ingår i
	local INOM_ADMINISTRATIVT_OMRADE = 'P131'
	local               SVERIGES_LAN = 'Q192299'
	local          SVERIGES_KOMMUNER = 'Q755707'
	local                    LANSKOD = 'P300'
	local        KOMMUNKOD_I_SVERIGE = 'P2504'
	local          SVERIGES_DISTRIKT = 'Q18333556'
	local t = {}
	t = wikidata({entity = entity, property=INOM_ADMINISTRATIVT_OMRADE,noref='true',nolink='ja',getonly=SVERIGES_LAN,raw='ja'}) -- hämta län som objektet ingår i 
	if t and t[1] then 
    	return string.sub(wikidata({property=LANSKOD,noref='ja',nolink='ja',entityId=t[1].item}) or '', -2) -- returnera länskoden 
	end
	t = wikidata({entity = entity, property=INOM_ADMINISTRATIVT_OMRADE,noref='true',nolink='ja',getonly=SVERIGES_KOMMUNER,raw='ja'}) -- hämta kommun om län ej var angivet
	if t and t[1] then
		return mw.ustring.sub(wikidata({property=KOMMUNKOD_I_SVERIGE,noref='true',nolink='ja',entityId=t[1].item}),1,2) -- returnera de två första siffrorna i kommunkoden	
	end
	t = wikidata({entity = entity, property=INOM_ADMINISTRATIVT_OMRADE,noref='true',nolink='ja',getonly=SVERIGES_DISTRIKT,raw='ja'}) -- hämta distrikt om varken län eller kommun var angivna
    if t and t[1] then
    	tt = wikidata({entityId = t[1].item,property=INOM_ADMINISTRATIVT_OMRADE,noref='true',nolink='ja',getonly=SVERIGES_KOMMUNER,raw='ja'}) -- hämta kommun från distriktet
    	if tt and tt[1] then
    		return mw.ustring.sub(wikidata({property=KOMMUNKOD_I_SVERIGE,noref='true',nolink='ja',entityId=tt[1].item}),1,2) -- returnera de två första siffrorna i kommunkoden	
    	end
    end
	return ''
end

function positionskartnamn(frame) -- kartbild för länet som objektet tillhör. används av till exempel mallen "Ortsfakta Sverige WD"
   return mw.getCurrentFrame():preprocess('{{Kartposition Norge '..lankod2lan(lankod(frame))..'|bild}}')
end 

function positionskarta(frame) -- kartlokalisering (Geolocator) för länet som objektet tillhör. används av till exempel mallen "Ortsfakta Sverige WD"
	return lankod2lan(lankod(frame))
end
function positionskarta2(frame) -- kartlokalisering för att visa var i Norge fylket ligger
	return lankarta(lankod(frame))
end


function harva(entity, first, second, limit)
	local s = {}
	local l = wikidata({entity = entity, property = "P131", noref='true', avoidqualifier='P582', getonly=second, raw = 'true', getsimpleproperty = '15px', getproperty = 'P94', getenbarten = 'true'})
	if l and #l > limit then 
		return {}
	end
	if l and #l > 0 then
		return l
	else 
		l = wikidata({entity = entity, property = "P131", noref='true', avoidqualifier='P582', getonly=first, raw = 'true', getsimpleproperty = '15px', getproperty = 'P94', getenbarten = 'true'})
		if l and #l > limit then 
			return {}
		end
		local b = {}
		if l then
			for i3, j3 in pairs(l) do
				l2 = wikidata({entityId = j3.item, property = "P131", noref='true', avoidqualifier='P582', getonly=second, raw = 'true', getsimpleproperty = '15px', getproperty = 'P94', getenbarten = 'true'})
				for i, j in pairs(l2) do
					local f = false
					for i2, j2 in pairs(b) do
						if j.item == j2.item then
							f = true
						end
					end
					if not f then
						table.insert(b, j)
					end
				end
			end
		end
		if #b > limit then 
			return {}
		else
			return b
		end
	end
	return {}
end

function Comcat(entity)
	if not entity or not entity.claims or not entity.claims.P373 then
		return ''
	else
		if entity.claims.P373[1].mainsnak.snaktype ~= 'value' then
			return ''
		else
			return entity.claims.P373[1].mainsnak.datavalue.value
		end
	end
end


return p