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
local SCBnykod = require('Modul:Sandlådan/Larske/Testmodul20').SCBnykod

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 form = args.form or 'Ort'
	if mw.ustring.lower(form) == 'tätort' then
		form = '[[Tätort]]'
	elseif mw.ustring.lower(form) == 'småort' then
		form = '[[Småort]]'
	end
	if not form or form == '' then
		form = "Ort"
	end
	table.insert(a, "\n| category = " .. form)
	
	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
	else
		local c = {['Uppsala'] = true,	['Kalmar'] = true, ['Blekinge'] = true, ['Skåne'] = true, ['Örebro'] = true}
		local lan = lankod2lan(lankod(entity))
		ill_kat = '[[Kategori:Illustrationsbehov-'..lan
		if not c[lan] then ill_kat = ill_kat .. 's' end 
		ill_kat = ill_kat .. ' län]]'
	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       = Sverige")
	table.insert(a, "\n| country_flag  = Flag of Sweden.svg")

	local landskap = args.landskap or args.landskap1
	local landskap2 = args.landskap2 or ''
	local landskap3 = args.landskap3 or ''
	if not landskap then
		local v = harva(entity, 'Q127448', 'Q193556', 99)
		landskap =  indexera(v, 1)
		landskap2 = indexera(v, 2)
		landskap3 = indexera(v, 3)
	end
	if landskap and landskap ~= '' then
		table.insert(a, "\n| state  = " .. landskap)
		if landskap2 and landskap2 ~= '' then
			table.insert(a, "\n| state1 = " .. landskap2)
			if landskap3 and landskap3 ~= '' then
				table.insert(a, "\n| state2 = " .. landskap3)
			end
		end
		table.insert(a, "\n| state_type            = [[Landskap i Sverige|Landskap]]")
		table.insert(a, "\n| state_label           = Landskap orten ligger i")
	end
	local lan = args['län'] or args['län1'] 
	local lan2 = args['län2'] 
	local lan3 = args['län3'] 
	if not lan then
		local v = harva(entity, 'Q127448', 'Q200547', 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            = [[Sveriges län|Län]]")
		table.insert(a, "\n| region_label           = Län 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, 'Q127448', 'Q127448', 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 = [[Sveriges kommuner|Kommuner]]")
			table.insert(a, "\n| municipality_label    = Kommuner orten ligger i")
		else
			table.insert(a, "\n| municipality_type = [[Sveriges kommuner|Kommun]]")
			table.insert(a, "\n| municipality_label    = Kommun orten ligger i")
		end
	end	

	
	
	local distrikt = args.distrikt or args.distrikt1
	local distrikt2 = args.distrikt2
	local distrikt3 = args.distrikt3
	local distrikt4 = args.distrikt4
	local distrikt5 = args.distrikt5
	local distrikt6 = args.distrikt6
	local distrikt7 = args.distrikt7
	if not distrikt then
		local v = harva(entity, 'Q18333556', 'Q18333556', 99)
		distrikt = indexera(v, 1)
		distrikt2 = indexera(v, 2)
		distrikt3 = indexera(v, 3)
		distrikt4 = indexera(v, 4)
		distrikt5 = indexera(v, 5)
		distrikt6 = indexera(v, 6)
		distrikt7 = indexera(v, 7)
		distrikt8 = indexera(v, 8)
		distrikt9 = indexera(v, 9)
		distrikt10 = indexera(v,10)
	end
	if distrikt and distrikt ~= '' then
		table.insert(a, "\n| parish = " .. distrikt)
		if distrikt2 and distrikt2 ~= '' then
			table.insert(a, "\n| parish1 = " .. distrikt2)
			if distrikt3 and distrikt3 ~= '' then
				table.insert(a, "\n| parish2 = " .. distrikt3)
				if distrikt4 and distrikt4 ~= '' then
					table.insert(a, "\n| parish3 = " .. distrikt4)
					if distrikt5 and distrikt5 ~= '' then
						table.insert(a, "\n| parish4 = " .. distrikt5)
						if distrikt6 and distrikt6 ~= '' then
							table.insert(a, "\n| parish5 = " .. distrikt6)
							if distrikt7 and distrikt7 ~= '' then
								table.insert(a, "\n| parish6 = " .. distrikt7)
								if distrikt8 and distrikt8 ~= '' then
									table.insert(a, "\n| parish7 = " .. distrikt8)
									if distrikt9 and distrikt9 ~= '' then
										table.insert(a, "\n| parish8 = " .. distrikt9)
										if distrikt10 and distrikt10 ~= '' then
											table.insert(a, "\n| parish9 = " .. distrikt10)
										end
									end
								end
							end
						end
					end
				end
			end
		end
		table.insert(a, "\n| parish_type           = [[Sveriges distrikt|Distrikt]]")
		table.insert(a, "\n| parish_label          = Distrikt orten ligger i")
	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, "Sverige")
	
	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 i Sverige|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 i Sverige|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 = 'P775', 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['småortskod'] or args['småortskod1'] then
		table.insert(a, '\n| code1_type = [[Småortskod]]')
		if args['småortskod'] then
			table.insert(a, '\n| code1      = ' .. args['småortskod'])
		end
		if args['småortskod1'] then
			table.insert(a, '\n| code1a      = ' .. args['småortskod1'])
			table.insert(a, '\n| code1a_type = ' .. (args['småortskod1_text'] or ''))
			if args['småortskod2'] then
				table.insert(a, '\n| code1b      = ' .. args['småortskod2'])
				table.insert(a, '\n| code1b_type = ' .. args['småortskod2_text'])
				if args['småortskod3'] then
					table.insert(a, '\n| code1c      = ' .. args['småortskod3'])
					table.insert(a, '\n| code1c_type = ' .. args['småortskod3_text'])
				end
			end
		end
	else
		local v = wikidata({entity = entity, property = 'P776', withintervall = 'yes', modifyqualifiertime = 'Y', separator = '<br/>', conjunction = '<br/>', rank = 'valid' })
		if v then
			table.insert(a, '\n| code1_type = [[Småortskod]]')
			table.insert(a, '\n| code1      = ' .. v)
		end
	end
	if args['fritidshusområdeskod'] then
		table.insert(a, '\n| code2       = ' .. args['fritidshusområdeskod'])
		table.insert(a, '\n| code2_type  = [[Fritidshusområde|Fritidshusomr.kod]]')
	else
		local v = wikidata({entity = entity, property = 'P980', withintervall = 'yes', modifyqualifiertime = 'Y', separator = '<br/>', conjunction = '<br/>' })
		if v then 
			table.insert(a, '\n| code2       = ' .. v)
			table.insert(a, '\n| code2_type  = [[Fritidshusområde|Fritidshusomr.kod]]')
		end
	end
	local v = wikidata({entity = entity, property = 'P10708', withintervall = 'yes', sortbytime = 'inverted', modifyqualifiertime = 'Y', separator = '<br/>', conjunction = '<br/>' })
	if v then
		table.insert(a, '\n| code3 = ' .. v)
		table.insert(a, '\n| code3_type = [[Bebyggelseområdeskod|Beb.områdeskod]]')
	end
	if args.statistikkod then
		table.insert(a, '\n| code4      = ' .. args.statistikkod)
		table.insert(a, '\n| code4_type = Statistikkod')
	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           = Sverigekarta infobox.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)
		table.insert(a, '\n| map           = ' .. s1)
		local c = {['Uppsala'] = true,	['Kalmar'] = true, ['Blekinge'] = true, ['Skåne'] = true, ['Örebro'] = true}
		
		if s2 == '' then
			table.insert(a, '\n| map_caption   = Ortens läge i Sverige')
			s2 = 'Sverige'
		elseif c[s2] then
			table.insert(a, '\n| map_caption   = Ortens läge i ' .. s2 .. ' län')
		else
			table.insert(a, '\n| map_caption   = Ortens läge i ' .. s2 .. 's län')
		end
		table.insert(a, '\n| map_locator   = ' .. s2)
	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'})
-- från oktober 2021 har SCB inför nya koder för tätorter och småorter. 
-- kartfunktionen klarar för tillfället inte de gamla koderna för tätorter och småorter varför det krävs att gamla koder översätt till nya koder innan reginawebmap anropas för att man ska komma till rätt karta.
	tkod = SCBnykod({args = {gammalkod=tkod}})
	local skod = wikidata({entity = entity, property = 'P776', enbarten = 'ja', noref = 'true'})
	skod = SCBnykod({args = {gammalkod=skod}})
	local okod = wikidata({entity = entity, property = 'P10708', enbarten = 'ja', sortbytime = 'chronological', noref = 'true'})
    if tkod and tkod ~= '' and okod == nil then
		res = '[http://geodata.scb.se/reginawebmap/main/webapp/?typ=tatort&t=' .. tkod .. ' SCB:s tätortsavgränsning]'	
    end
	if skod and skod ~= '' and okod == nil then 
		if tkod and tkod ~= '' then
			res = res .. '<br />'
		end 
		res = res .. '[http://geodata.scb.se/reginawebmap/main/webapp/?typ=smaort&s=' .. skod .. ' SCB:s småortsavgränsning]'
	end
	if okod and okod ~= '' then
		if skod and skod ~= '' or tkod and tkod ~= '' then
--			res = res .. '<br />'	
		end
		if string.find(okod,'T') == 5 then
			typ = 'tatort&t='
		elseif string.find(okod,'S') == 5 then
			typ = 'smaort&s='
		else
			typ = ''  -- hit ska man inte komma sålänge det bara finns P10708 för småorter (S) och tätorter (T) i Wikidata
		end
		if typ ~= '' then
			res = res .. '[http://geodata.scb.se/reginawebmap/main/webapp/?typ=' .. typ .. okod .. ' SCB:s bebyggelseområdesavgränsning]'
		end
	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)
local lan = ''
if lankod == '01' then lan = 'Stockholm'
	elseif lankod == '03' then lan = 'Uppsala'
	elseif lankod == '04' then lan = 'Södermanland'
	elseif lankod == '05' then lan = 'Östergötland'
	elseif lankod == '06' then lan = 'Jönköping'
	elseif lankod == '07' then lan = 'Kronoberg'
	elseif lankod == '08' then lan = 'Kalmar'
	elseif lankod == '09' then lan = 'Gotland'
	elseif lankod == '10' then lan = 'Blekinge'	
    elseif lankod == '12' then lan = 'Skåne'
    elseif lankod == '13' then lan = 'Halland'
	elseif lankod == '14' then lan = 'Västra Götaland'
	elseif lankod == '17' then lan = 'Värmland'
	elseif lankod == '18' then lan = 'Örebro'
	elseif lankod == '19' then lan = 'Västmanland'
	elseif lankod == '20' then lan = 'Dalarna'
	elseif lankod == '21' then lan = 'Gävleborg'
	elseif lankod == '22' then lan = 'Västernorrland'
	elseif lankod == '23' then lan = 'Jämtland'
	elseif lankod == '24' then lan = 'Västerbotten'
	elseif lankod == '25' then lan = 'Norrbotten'
	else lan = ''
end
return lan
end

function lankod(entity) -- hämtar länskod från i första hand det län, 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 = 'Q200547'
	local          SVERIGES_KOMMUNER = 'Q127448'
	local                    LANSKOD = 'P507'
	local        KOMMUNKOD_I_SVERIGE = 'P525'
	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 wikidata({property=LANSKOD,noref='ja',nolink='ja',entityId=t[1].item}) -- returnera länskoden 
	end
	t = wikidata({entity = entity, property=INOM_ADMINISTRATIVT_OMRADE,noref='true',nolink='ja',getonly=SVERIGES_KOMMUNER,raw='ja',avoidqualifier='P582'}) -- 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,avoidqualifier='P582'}),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',avoidqualifier='P582'}) -- 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',avoidqualifier='P582'}) -- 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,avoidqualifier='P582'}),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 Sverige '..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 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', rank = 'valid'})
	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