Hoppa till innehållet

Modul:Ortsfakta USA WD

Från Wikipedia

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 tidszonerna = require('Modul:Ortsfakta tidszoner').tidszon

p = {}

local tidzon = tidszonerna['Q30']

local kartor = {
		['Q797']   = { karta = 'USA Alaska location map.svg',         positionskarta = 'Alaska',         karta2 = 'Map of USA AK.svg', karta2text = 'Alaskas läge i USA'},
		['Q816']   = { karta = 'USA Arizona location map.svg',        positionskarta = 'Arizona',        karta2 = 'Map of USA AZ.svg', karta2text = 'Arizonas läge i USA'},
		['Q99']    = { karta = 'USA California location map.svg',     positionskarta = 'Kalifornien',    karta2 = 'Map of USA CA.svg', karta2text = 'Kaliforniens läge i USA'},
		['Q1261']  = { karta = 'USA Colorado location map.svg',       positionskarta = 'Colorado',       karta2 = 'Map of USA CO.svg', karta2text = 'Colorados läge i USA'},
		['Q812']   = { karta = 'USA Florida location map.svg',        positionskarta = 'Florida',        karta2 = 'Map of USA FL.svg', karta2text = 'Floridas läge i USA'},
		['Q1428']  = { karta = 'USA Georgia location map.svg',        positionskarta = 'Georgia',        karta2 = 'Map of USA GA.svg', karta2text = 'Georgias läge i USA'},
		['Q1546']  = { karta = 'USA Iowa location map.svg',           positionskarta = 'Iowa',           karta2 = 'Map of USA IA.svg', karta2text = 'Iowas läge i USA'},
		['Q1204']  = { karta = 'USA Illinois location map.svg',       positionskarta = 'Illinois',       karta2 = 'Map of USA IL.svg', karta2text = 'Illinois läge i USA'},
		['Q1558']  = { karta = 'USA Kansas location map.svg',         positionskarta = 'Kansas',         karta2 = 'Map of USA KS.svg', karta2text = 'Kansas läge i USA'},
		['Q1603']  = { karta = 'USA Kentucky location map.svg',       positionskarta = 'Kentucky',       karta2 = 'Map of USA KY.svg', karta2text = 'Kentuckys läge i USA'},
		['Q1588']  = { karta = 'USA Louisiana location map.svg',      positionskarta = 'Louisiana',      karta2 = 'Map of USA LA.svg', karta2text = 'Louisianas läge i USA'},
		['Q724']   = { karta = 'USA Maine location map.svg',          positionskarta = 'Maine',          karta2 = 'Map of USA ME.svg', karta2text = 'Maines läge i USA'},
		['Q771']   = { karta = 'USA Massachusetts location map.svg',  positionskarta = 'Massachusetts',  karta2 = 'Map of USA MA.svg', karta2text = 'Massachusetts läge i USA'},
		['Q1166']  = { karta = 'USA Michigan location map.svg',       positionskarta = 'Michigan',       karta2 = 'Map of USA MI.svg', karta2text = 'Michigans läge i USA'},
		['Q1527']  = { karta = 'USA Minnesota location map.svg',      positionskarta = 'Minnesota',      karta2 = 'Map of USA MN.svg', karta2text = 'Minnesotas läge i USA'},
		['Q1494']  = { karta = 'USA Mississippi location map.svg',    positionskarta = 'Mississippi',    karta2 = 'Map of USA MS.svg', karta2text = 'Mississippis läge i USA'},
		['Q1581']  = { karta = 'USA Missouri location map.svg',       positionskarta = 'Missouri',       karta2 = 'Map of USA MO.svg', karta2text = 'Missouris läge i USA'},
		['Q759']   = { karta = 'USA New Hampshire location map.svg',  positionskarta = 'New Hampshire',  karta2 = 'Map of USA NH.svg', karta2text = 'New Hampshires läge i USA'},
		['Q1553']  = { karta = 'USA Nebraska location map.svg',       positionskarta = 'Nebraska',       karta2 = 'Map of USA NE.svg', karta2text = 'Nebraskas läge i USA'},
		['Q1227']  = { karta = 'USA Nevada location map.svg',         positionskarta = 'Nevada',         karta2 = 'Map of USA NV.svg', karta2text = 'Nevadas läge i USA'},
		['Q1522']  = { karta = 'USA New Mexico location map.svg',     positionskarta = 'New Mexico',     karta2 = 'Map of USA NM.svg', karta2text = 'New Mexicos läge i USA'},
		['Q1384']  = { karta = 'USA New York location map.svg',       positionskarta = 'New York',       karta2 = 'Map of USA NY.svg', karta2text = 'New Yorks läge i USA'},
		['Q1454']  = { karta = 'USA North Carolina location map.svg', positionskarta = 'North Carolina', karta2 = 'Map of USA NC.svg', karta2text = 'North Carolinas läge i USA'},
		['Q1207']  = { karta = 'USA North Dakota location map.svg',   positionskarta = 'North Dakota',   karta2 = 'Map of USA ND.svg', karta2text = 'North Dakotas läge i USA'},
		['Q1397']  = { karta = 'USA Ohio location map.svg',           positionskarta = 'Ohio',           karta2 = 'Map of USA OH.svg', karta2text = 'Ohios läge i USA'},
		['Q1649']  = { karta = 'USA Oklahoma location map.svg',       positionskarta = 'Oklahoma',       karta2 = 'Map of USA OK.svg', karta2text = 'Oklahomas läge i USA'},
		['Q824']   = { karta = 'USA Oregon location map.svg',         positionskarta = 'Oregon',         karta2 = 'Map of USA OR.svg', karta2text = 'Oregons läge i USA'},
		['Q1400']  = { karta = 'USA Pennsylvania location map.svg',   positionskarta = 'Pennsylvania',   karta2 = 'Map of USA PA.svg', karta2text = 'Pennsylvanias läge i USA'},
		['Q1387']  = { karta = 'USA Rhode Island location map.svg',   positionskarta = 'Rhode Island',   karta2 = 'Map of USA RI.svg', karta2text = 'Rhode Islands läge i USA'},
		['Q1456']  = { karta = 'USA South Carolina location map.svg', positionskarta = 'South Carolina', karta2 = 'Map of USA SC.svg', karta2text = 'South Carolinas läge i USA'},
		['Q1211']  = { karta = 'USA South Dakota location map.svg',   positionskarta = 'South Dakota',   karta2 = 'Map of USA SD.svg', karta2text = 'South Dakotas läge i USA'},
		['Q1509']  = { karta = 'USA Tennessee location map.svg',      positionskarta = 'Tennessee',      karta2 = 'Map of USA TN.svg', karta2text = 'Tennessees läge i USA'},
		['Q1439']  = { karta = 'USA Texas location map.svg',          positionskarta = 'Texas',          karta2 = 'Map of USA TX.svg', karta2text = 'Texas läge i USA'},
		['Q829']   = { karta = 'USA Utah location map.svg',           positionskarta = 'Utah',           karta2 = 'Map of USA UT.svg', karta2text = 'Utahs läge i USA'},
		['Q16551'] = { karta = 'USA Vermont location map.svg',        positionskarta = 'Vermont',        karta2 = 'Map of USA VT.svg', karta2text = 'Vermonts läge i USA'},
		['Q1370']  = { karta = 'USA Virginia location map.svg',       positionskarta = 'Virginia',       karta2 = 'Map_of_USA_VA.svg', karta2text = 'Virginias läge i USA'}, 
		['Q1223']  = { karta = 'USA Washington location map.svg',     positionskarta = 'Washington',     karta2 = 'Map of USA WA.svg', karta2text = 'Washingtons läge i USA'}, 
		['Q1371']  = { karta = 'USA West Virginia location map.svg',  positionskarta = 'West Virginia',  karta2 = 'Map_of_USA_WV.svg', karta2text = 'West Virginias läge i USA'},
		['Q1537']  = { karta = 'USA Wisconsin location map.svg',      positionskarta = 'Wisconsin',      karta2 = 'Map of USA WI.svg', karta2text = 'Wisconsins läge i USA'},
		['Q1214']  = { karta = 'USA Wyoming location map.svg',        positionskarta = 'Wyoming',        karta2 = 'Map of USA WY.svg', karta2text = 'Wyomings läge i USA'},
		['Q11703'] = { karta = 'USA Virgin Islands location map.svg', positionskarta = 'U.S. Virgin Islands', karta2 = 'United States Virgin Islands in United States (US48).svg', karta2text = 'Amerikanska Jungfruöarnas läge relativt USA'},
		['Q16641'] = { karta = 'American Samoa location map.svg', positionskarta = 'Amerikanska Samoa', karta2 = 'American_Samoa_on_the_globe_(Polynesia_centered).svg', karta2text = 'Amerikanska Samoas läge i Stilla havet'},
		['Q3551781'] = {karta = '', positionskarta = '', karta2 = 'Washington D.C. in United States (zoom).svg', karta2text = 'District of Columbias läge i USA'},
		['Q16635'] = { karta = 'USA Guam location map.svg', positionskarta = 'Guam', karta2 = 'Guam on the globe (small islands magnified) (Polynesia centered).svg', karta2text = 'Guams läge i Stilla havet'},
		['Q16644'] = { karta = 'USA_Northern_Mariana_Islands_location_map.svg', positionskarta = 'Nordmarianerna', karta2 = 'Northern Mariana Islands in United States.svg', karta2text = 'Nordmarianernas läge relativt USA'},
		['Q1183']  = { karta = 'USA Puerto Rico location map.svg',    positionskarta = 'Puerto Rico', karta2 = 'Puerto Rico in United States (US48).svg', karta2text = 'Puerto Ricos läge relativt USA'}
	}
local town = {
		['Q1261']  = 'Stad (town)', -- Colorado
		['Q1588']  = 'Stad (town)', -- Louisiana
		['Q724']   = 'Kommun (town)', -- Maine
		['Q771']   = 'Kommun (town)', -- Massachusetts
		['Q1494']  = 'Stad (town)', -- Mississippi
		['Q1581']  = 'Stad (town)', -- Missouri
		['Q759']   = 'Kommun (town)', -- New Hampshire
		['Q1522']  = 'Stad (town)', -- New Mexico
		['Q1384']  = 'Kommun (town)', -- New York
		['Q1454']  = 'Stad (town)', -- North Carolina
		['Q1649']  = 'Stad (town)', -- Oklahoma
		['Q1400']  = 'Stad (town)', -- Pennsylvania
		['Q1387']  = 'Kommun (town)', -- Rhode Island
		['Q1456']  = 'Stad (town)', -- South Carolina
		['Q1211']  = 'Stad (town)', -- South Dakota
		['Q1509']  = 'Stad (town)', -- Tennessee
		['Q829']   = 'Stad (town)', -- Utah
		['Q16551'] = 'Kommun (town)', -- Vermont
		['Q1370']  = 'Stad (town)', -- Virginia
		['Q1223']  = 'Stad (town)', -- Washington
		['Q1371']  = 'Stad (town)', -- West Virginia
		['Q1537']  = 'Kommun (town)', -- Wisconsin
		['Q1214']  = 'Stad (town)' -- Wyoming
}
local storstad = {
		['Q60'] = 'Q683705', --New York city
		['Q100'] = 'Q1190137', -- Boston,
		['Q43788'] = 'Q944103' -- Madison, Wisconsin
	}


function test(item, claim)
	local f = false
	for k, v in pairs(claim) do
		if v.item == item then 
			return true
		end
	end
	return false
end

function test2(item, claim)
	local f = false
	for k, v in pairs(claim) do
		if v == item then 
			return true
		end
	end
	return false
end


function p.ort( entity, args )
	local longitude = 200
	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
		elseif entity.labels.en then
			ortnamn = entity.labels.en.value
		else
			ortnamn = mw.title.getCurrentTitle().text
		end
	end
	table.insert(a, "\n| name = " .. ortnamn)
	if args['övriga namn'] then
		table.insert(a, "\n| other_name = " .. (args['övriga namn'] or ''))
	end

	local inhemskt = args['inhemskt namn']
	if not inhemskt and inhemskt ~= '' then
		-- Går det att använda P1705?
	elseif inhemskt ~= '' then
		table.insert(a, "\n| native_name       = " .. inhemskt )
	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	
	local officiellt = args['officiellt namn']
	if not officiellt and officiellt ~= '' then
		v = wikidata({entity = entity, property = 'P1448', raw = 'true', sources = 'sources'})
		if v and #v > 0 then
			local s = {}
			local s1 = {}
			for i, j in pairs(v) do
				table.insert(s, j.value)
				table.insert(s1, j.ref)
			end
			table.insert(a, "\n| official_name      = " .. table.concat(s, ", "))
			table.insert(a, "\n| official_name_note = " .. table.concat(s1, ""))
		end
	elseif officiellt ~= '' then
		table.insert(a, "\n| official_name      = " .. officiellt )
		if args['officiellt namnnot'] then
			table.insert(a, "\n| official_name_note = " .. args['officiellt namnnot'] )
		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
		else
			data = wikidata({entity = entity, property = 'P2716', 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
	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
	
	local f1 = args.flagga
	if not f1 and f1 ~= '' then
		f1 = wikidata({entity = entity, property = 'P41', noref='true', enbarten = 'true'})
	end
	local f2 = args.sigill
	if not f2 and f2 ~= '' then
		f2 = wikidata({entity = entity, property = 'P158', noref='true', enbarten = 'true'})
	end
	local f3 = args.vapen
	if not f3 and f3 ~= '' then
		f3 = wikidata({entity = entity, property = 'P94', noref='true', enbarten = 'true'})
	end
	
	if f1 and f1 ~= '' and f2 and f2 ~= '' and f3 and f3 ~= '' then -- om alla av flagga, sigill och vapen finns 
		local flagga = args.flagga
		if flagga and flagga ~= '' then
			table.insert(a, "\n| flag = " .. flagga)
			table.insert(a, "\n| flag_type = Flagga" )
		elseif not flagga then
			local data = wikidata({entity = entity, property = 'P41', raw = 'true', noref='true', enbarten = 'true'})
			if data and #data > 0 then
				table.insert(a, "\n| flag = " .. data[1].value)
				local data2 = wikidata({entity = entity, property = 'P163', raw = 'true', noref='true', enbarten = 'true'})
				if data2 and #data2 > 0 then
					table.insert(a, "\n| flag_type = " .. data2[1].value)
				else
					table.insert(a, "\n| flag_type = Flagga" )
				end
			end
		end
		if args.sigill then
			table.insert(a, "\n| flag1      = " .. args.sigill)
			table.insert(a, "\n| flag1_type = [[Sigill]]")
		elseif not args.sigill then
			local data = wikidata({entity = entity, property = 'P158', raw = 'true', noref='true', enbarten = 'true'})
			local data2 = wikidata({entity = entity, property = 'P418', raw = 'true', noref='true', enbarten = 'true', label = 'Sigill'})
			if data and #data > 0 then
				table.insert(a, "\n| flag1 = " .. data[1].value)
				if data2 and #data2 > 0 then
					table.insert(a, "\n| flag1_type = " .. data2[1].value)
				else
					table.insert(a, "\n| flag1_type = [[Sigill]]")
				end
			end
		end
		local vapen = args.vapen
		if vapen and vapen ~= '' then
			table.insert(a, "\n| symbol1      = " .. args.vapen)
			table.insert(a, "\n| symbol1_type = [[Heraldiskt vapen|Vapen]]")
		elseif not vapen then
			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| symbol1 = " .. data[1].value)
				if data2 and #data2 > 0 then
					table.insert(a, "\n| symbol1_type = " .. data2[1].value)
				else
					table.insert(a, "\n| symbol1_type = [[Heraldiskt vapen|Vapen]]")
				end
			end
		end
	elseif f1 and f1 ~= '' and f2 and f2 ~= '' then -- flagga och sigill finns
		local flagga = args.flagga
		if flagga and flagga ~= '' then
			table.insert(a, "\n| flag = " .. flagga)
			table.insert(a, "\n| flag_type = Flagga" )
		elseif not flagga then
			local data = wikidata({entity = entity, property = 'P41', raw = 'true', noref='true', enbarten = 'true'})
			if data and #data > 0 then
				table.insert(a, "\n| flag = " .. data[1].value)
				local data2 = wikidata({entity = entity, property = 'P163', raw = 'true', noref='true', enbarten = 'true'})
				if data2 and #data2 > 0 then
					table.insert(a, "\n| flag_type = " .. data2[1].value)
				else
					table.insert(a, "\n| flag_type = Flagga" )
				end
			end
		end
		if args.sigill then
			table.insert(a, "\n| symbol      = " .. args.sigill)
			table.insert(a, "\n| symbol_type = [[Sigill]]")
		elseif not args.sigill then
			local data = wikidata({entity = entity, property = 'P158', raw = 'true', noref='true', enbarten = 'true'})
			local data2 = wikidata({entity = entity, property = 'P418', raw = 'true', noref='true', enbarten = 'true', label = 'Sigill'})
			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 = [[Sigill]]")
				end
			end
		end
	elseif f1 and f1 ~= '' and f3 and f3 ~= '' then -- flagga och vapen finns
		local flagga = args.flagga
		if flagga and flagga ~= '' then
			table.insert(a, "\n| flag = " .. flagga)
			table.insert(a, "\n| flag_type = Flagga" )
		elseif not flagga then
			local data = wikidata({entity = entity, property = 'P41', raw = 'true', noref='true', enbarten = 'true'})
			if data and #data > 0 then
				table.insert(a, "\n| flag = " .. data[1].value)
				local data2 = wikidata({entity = entity, property = 'P163', raw = 'true', noref='true', enbarten = 'true'})
				if data2 and #data2 > 0 then
					table.insert(a, "\n| flag_type = " .. data2[1].value)
				else
					table.insert(a, "\n| flag_type = Flagga" )
				end
			end
		end
		local vapen = args.vapen
		if vapen and vapen ~= '' then
			table.insert(a, "\n| symbol      = " .. args.vapen)
			table.insert(a, "\n| symbol_type = [[Heraldiskt vapen|Vapen]]")
		elseif not vapen then
			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 = [[Heraldiskt vapen|Vapen]]")
				end
			end
		end
	elseif f2 and f2 ~= '' and f3 and f3 ~= '' then
		if args.sigill then
			table.insert(a, "\n| flag      = " .. args.sigill)
			table.insert(a, "\n| flag_type = [[Sigill]]")
		elseif not args.sigill then
			local data = wikidata({entity = entity, property = 'P158', raw = 'true', noref='true', enbarten = 'true'})
			local data2 = wikidata({entity = entity, property = 'P418', raw = 'true', noref='true', enbarten = 'true', label = 'Sigill'})
			if data and #data > 0 then
				table.insert(a, "\n| flag = " .. data[1].value)
				if data2 and #data2 > 0 then
					table.insert(a, "\n| flag_type = " .. data2[1].value)
				else
					table.insert(a, "\n| flag_type = [[Sigill]]")
				end
			end
		end
		local vapen = args.vapen
		if vapen and vapen ~= '' then
			table.insert(a, "\n| symbol      = " .. args.vapen)
			table.insert(a, "\n| symbol_type = [[Heraldiskt vapen|Vapen]]")
		elseif not vapen then
			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 = [[Heraldiskt vapen|Vapen]]")
				end
			end
		end
	elseif (f1 and f1 ~= '') or (f2 and f2 ~= '') or (f3 and f3 ~= '') then  -- 
		local flagga = args.flagga
		if flagga and flagga ~= '' then
			table.insert(a, "\n| flag      = " .. flagga)
			table.insert(a, "\n| flag_type = Flagga " )
		elseif not flagga then
			local data = wikidata({entity = entity, property = 'P41', raw = 'true', noref='true', enbarten = 'true'})
			if data and #data > 0 then
				table.insert(a, "\n| flag = " .. data[1].value)
				local data2 = wikidata({entity = entity, property = 'P163', raw = 'true', noref='true', enbarten = 'true'})
				if data2 and #data2 > 0 then
					table.insert(a, "\n| flag_type = " .. data2[1].value)
				else
					table.insert(a, "\n| flag_type = Flagga" )
				end
			end
		end
		if args.sigill then
			table.insert(a, "\n| flag      = " .. args.sigill)
			table.insert(a, "\n| flag_type = [[Sigill]]")
		elseif not args.sigill then
			local data = wikidata({entity = entity, property = 'P158', raw = 'true', noref='true', enbarten = 'true'})
			local data2 = wikidata({entity = entity, property = 'P418', raw = 'true', noref='true', enbarten = 'true', label = 'Sigill'})
			if data and #data > 0 then
				table.insert(a, "\n| flag = " .. data[1].value)
				if data2 and #data2 > 0 then
					table.insert(a, "\n| flag_type = " .. data2[1].value)
				else
					table.insert(a, "\n| flag_type = [[Sigill]]")
				end
			end
		end
		local vapen = args.vapen
		if vapen and vapen ~= '' then
			table.insert(a, "\n| symbol      = " .. args.vapen)
			table.insert(a, "\n| symbol_type = [[Heraldiskt vapen|Vapen]]")
		elseif not vapen then
			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 = [[Heraldiskt vapen|Vapen]]")
				end
			end
		end
	end
	
	table.insert(a, "\n| country       = USA")
	table.insert(a, "\n| country_flag  = ja")

	local delstat = args['delstat'] or args['territorium']
	local delstatqid = nil
	local deltyp = 'Delstat'
	local antalkommuner = 0
	local kommun = args['county'] or args['county1']
	local kommunqid = nil
	local kommun2 = args['county2']
	local kommun3 = args['county3']
	local countytyp = '[[USA:s countyn|Countyn]]'
	if not args['delstat'] and args['territorium'] then
		deltyp = '[[Lista över USA:s delstater och territorier|Territorium]]'
	end
	local stateflag = nil
	if not delstat then
		local v = USAharva(entity, {})  
		if v and #v > 0 then
			deltyp = v[#v].typ
			if v[#v].flag and v[#v].flag ~= '' then
				stateflag = '[[File:' .. v[#v].flag .. '|22x20px]] '
			end
			delstat = v[#v].value
			delstatqid = v[#v].item
		end
		if v and #v > 1 then
			countytyp = v[#v - 1].typ
			kommun = v[#v - 1].value
			kommunqid = v[#v - 1].item
		end
		if v and #v > 2 then
			table.insert(a, "\n| commune      = " .. v[#v - 2].value)
			table.insert(a, "\n| commune_type = " .. v[#v - 2].typ or 'What?')
		end
	end
	if delstat and delstat ~= ''  then
		table.insert(a, "\n| state                 = " .. (stateflag or '' ) .. delstat )
		table.insert(a, "\n| state_type            = " .. deltyp )
	end

	if kommun and kommun ~= '' then
		table.insert(a, "\n| district = " .. kommun)
		antalkommuner = 1
		if kommun2 and kommun2 ~= '' then
			table.insert(a, "\n| district1 = " .. kommun2)
			antalkommuner = 2
			if kommun3 and kommun3 ~= '' then
				table.insert(a, "\n| district2 = " .. kommun3)
				antalkommuner = 3
			end
		end
		if kommun2 and kommun2 ~= '' then
			table.insert(a, "\n| district_type     = [[USA:s countyn|Countyn]]")
			table.insert(a, "\n| district_label    = countyn orten ligger i")
		else
			table.insert(a, "\n| district_type     = " .. countytyp )
			table.insert(a, "\n| district_label    = county orten ligger i")
		end
	end	
	
	
	local form = args.form or args.typ
	if form then 
		if form == '' then
			table.insert(a, "\n| category_hide = true ")
		else
			table.insert(a, "\n| category = " .. form)
		end
	else
		form =  wikidata({entity = entity, property = 'P31', avoidqualifier='P582', raw = 'true', noref = 'true'})
		if test('Q1093829', form) or test('Q21518270', form) or test('Q15221373', form) or test('Q63440326', form) then -- stad i USA/New England, delstatshuvudstad, fouth class city in Wisconsin, stad i Oregon
			table.insert(a, "\n| category = Stad (city)")
		elseif test('Q15221310', form) then -- 2nd class city, Wisconsin
			table.insert(a, "\n| category = Stad (city)" )	
		elseif test('Q21010817', form) then -- city, Pennsylvania
			table.insert(a, "\n| category = Stad (city)" )
		elseif test('Q2974552', form) then -- city, New Jersey
			table.insert(a, "\n| category = Stad (city)" )
		elseif test('Q2911266', form) then -- borough, New Jersey
			table.insert(a, "\n| category = Kommun (borough)" )
		elseif test('Q7829892', form) then -- town, New Jersey
			table.insert(a, "\n| category = Kommun (town)" )
		elseif test('Q2154459', form) then -- town, New England
			table.insert(a, "\n| category = Kommun (town)" )
		elseif test('Q15411479', form) then -- town, Connecticut
			table.insert(a, "\n| category = Kommun (town)" )
		elseif (test('Q3957', form) or test('Q15127012', form)) and town[delstatqid] then -- town
			table.insert(a, "\n| category = " .. town[delstatqid] )
		elseif test('Q9035798', form) or test('Q7830213', form) then -- Township i Pennsylvania/New Jersey
			table.insert(a, "\n| category = Stad (township)" )
		elseif test('Q42211429', form) then -- township of Minnesota
			table.insert(a, "\n| category = Kommun (township)" )
		elseif test('Q15219655', form) then -- civil town of Wisconsin
			table.insert(a, "\n| category = Stad (civil town)" )
		elseif test('Q106071004', form) then -- town, NY
			table.insert(a, "\n| category = Stad (town)" )
		elseif test('Q15411644', form) then -- town, Virginia
			table.insert(a, "\n| category = Stad (town)" )
		elseif test('Q408804', form) then -- Bourogh
			table.insert(a, "\n| category = [[Borough]]" )
		elseif test('Q751708', form) then -- Bourogh
			table.insert(a, "\n| category = [[By#USA|By]] (village)" )
		elseif test('Q498162', form) then
			table.insert(a, "\n| category = [[Census-designated place|CDP]]" )
		elseif test('Q7885250', form) then -- Kommunfri stad i Nevada
			table.insert(a, "\n| category = Kommunfri stad" )
		elseif test('Q17343829', form) then -- Kommunfritt område
			table.insert(a, "\n| category = Ort" )
		elseif test('Q74047', form) then 
			table.insert(a, "\n| category = [[Spökstad]]" )
		elseif test('Q486972', form) then
			table.insert(a, "\n| category = [[Samhälle (geografi)|Samhälle]]" )
		elseif test('Q15221242', form) then -- by i Wisconsin
			table.insert(a, "\n| category = [[By#USA|By]]" )
		elseif test('Q777120', form) then -- borough i Pennsylvania
			table.insert(a, "\n| category = Kommun (borough)" )		
		elseif test('Q16936588', form) then -- by i Pennsylvania
			table.insert(a, "\n| category = Ort" )	
		elseif test('Q55237813', form) then -- by i New York
			table.insert(a, "\n| category = [[By#USA|By]]" )
		elseif test('Q532', form) then -- by
			table.insert(a, "\n| category = [[By#USA|By]]" )
		elseif test('Q263639', form) then -- kommun i Puerto Rico
			table.insert(a, "\n| category = Kommun" )
		elseif test('Q5398059', form) then -- indianreservat
			table.insert(a, "\n| category = [[Indianreservat]]" )
		elseif test('Q475050', form) then -- Federalt distrikt
			table.insert(a, "\n| category = Federalt distrikt" )		
		else
			table.insert(a, "\n| category_hide = true ")
		end
	end


	if args.koordinat then
		table.insert(a, "\n| location = " .. args.koordinat)
	end
	
	if args.etymologi and args.etymologi ~= '' then
		table.insert(a, "\n| etymology     = " .. args.etymologi )
	elseif args.etymologi ~= '' then
		local data = wikidata({entity = entity, property = 'P138'})
		if data and data ~= '' then
			table.insert(a, "\n| etymology       = " .. data )
			table.insert(a, "\n| etymology_type = Uppkallad efter" )
		end
	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(math.floor( 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(math.floor (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(math.floor( 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
			longitude = tonumber(v[1].longitude)
			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

	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', modifytime = 'longdate'})
		if v and v ~= '' then
			table.insert(a, "\n| established = " .. v)
		end
	end
	
	local manualtime = false
	if args.tidszon or args['UTC skilln'] then
		if args.tidszon then
			table.insert(a,      '\n| timezone       = ' .. args.tidszon )
		end
		if args['UTC skilln'] then
			table.insert(a,      '\n| utc_offset     = ' .. args['UTC skilln'] )
		end
		if args['tidszon_DST'] then
			table.insert(a,      '\n| timezone_DST   = ' .. args['tidszon_DST'] )
		end
		if args['DST skilln'] then
			table.insert(a,      '\n| utc_offset_DST = ' .. args['DST skilln'] )
		end
	elseif delstatqid and tidzon[delstatqid] and tidzon[delstatqid].normaltid then
		table.insert(a,     '\n| timezone         = ' .. tidzon[delstatqid].normaltid )
		table.insert(a,     '\n| utc_offset       = ' .. tidzon[delstatqid].offsetnormal )
		if tidzon[delstatqid].sommartid then
			table.insert(a, '\n| timezone_DST     = ' .. tidzon[delstatqid].sommartid )
			table.insert(a, '\n| utc_offset_DST   = ' .. tidzon[delstatqid].offsetsommar )
		end
	elseif delstatqid and tidzon[delstatqid] and tidzon[delstatqid].time1 and kommunqid then -- delstater med mer än en tidszon
		if test2(kommunqid, tidzon[delstatqid].set1) then -- Kontrollerar om countyt finns i första mängden
			table.insert(a,     '\n| timezone         = ' .. tidzon[delstatqid].time1.normaltid )
			table.insert(a,     '\n| utc_offset       = ' .. tidzon[delstatqid].time1.offsetnormal )
			if tidzon[delstatqid].time1.sommartid then
				table.insert(a, '\n| timezone_DST     = ' .. tidzon[delstatqid].time1.sommartid )
				table.insert(a, '\n| utc_offset_DST   = ' .. tidzon[delstatqid].time1.offsetsommar )
			end 
		elseif type(tidzon[delstatqid].set2) == 'string' or test2(kommunqid, tidzon[delstatqid].set2 ) then -- i resten eller i andra mängden
			table.insert(a,     '\n| timezone         = ' .. tidzon[delstatqid].time2.normaltid )
			table.insert(a,     '\n| utc_offset       = ' .. tidzon[delstatqid].time2.offsetnormal )
			if tidzon[delstatqid].time2.sommartid then
				table.insert(a, '\n| timezone_DST     = ' .. tidzon[delstatqid].time2.sommartid )
				table.insert(a, '\n| utc_offset_DST   = ' .. tidzon[delstatqid].time2.offsetsommar )
			end 
		else
			manualtime = true -- Om inte i någon av mängderna (eller resten) så gör en manuell sökning i objektet på Wikidata
		end
	elseif delstat == '[[:Alaska|Alaska]]' then -- Alaska har speciallösning 
		if longitude and longitude < -169.5 then
			table.insert(a, '\n| timezone         = [[Hawaii-Aleutian Standard Time|HST]]')
			table.insert(a, '\n| utc_offset       = -10')
			table.insert(a, '\n| timezone_DST     = [[Hawaii-Aleutian Standard Time|HDT]]')
			table.insert(a, '\n| utc_offset_DST   = -9')
		elseif entity.id == 'Q1640042' then
			table.insert(a, '\n| timezone         = [[Pacific Standard Time|PST]]')
			table.insert(a, '\n| utc_offset       = -8')
			table.insert(a, '\n| timezone_DST     = [[Pacific Standard Time|PDT]]')
			table.insert(a, '\n| utc_offset_DST   = -7')
		else
			table.insert(a, '\n| timezone         = [[Alaska Standard Time|AST]]')
			table.insert(a, '\n| utc_offset       = -9')
			table.insert(a, '\n| timezone_DST     = [[Alaska Standard Time|ADT]]')
			table.insert(a, '\n| utc_offset_DST   = -8')
		end
	else
		manualtime = true
	end
	if manualtime == true then
		local v = wikidata({entity = entity, property = 'P421', raw = 'true', avoidqualifier = 'P582', preferqualifier = 'P1264', preferqualifiervalue = 'Q1777301', noref = 'true' }) -- undvik utdaterad, föredra giltig under tidsperiod = normaltid
		if not v or #v == 0 then
			v = wikidata({entity = entity, property = 'P421', raw = 'true', avoidqualifier = 'P582', noref = 'true' }) -- undvik utdaterad
		end
		if v and #v > 0 then
			if v[1].item == 'Q2212' or v[1].item == 'Q3134980' then -- UTC -07/MST
				table.insert(a,      '\n| timezone       = [[Mountain Standard Time|MST]]' )
				table.insert(a,      '\n| utc_offset     = -7' )
			elseif v[1].item == 'Q5385' or v[1].item == 'Q3134980' then -- UTC−06ː00/CST
				table.insert(a,      '\n| timezone       = [[Central Standard Time|CST]]' )
				table.insert(a,      '\n| utc_offset     = -6' )
			end
			v = wikidata({entity = entity, property = 'P421', raw = 'true', avoidqualifier = 'P582', preferqualifier = 'P1264', preferqualifiervalue = 'Q36669', noref = 'true' }) -- undvik utdaterad, föredra giltig under tidsperiod = sommartid
			if v and #v > 0 then
				if v[1].item == 'Q5385' then -- UTC -06/MDT
					table.insert(a, '\n| timezone_DST    = [[Mountain Daylight Saving Time|MDT]]' )
					table.insert(a, '\n| utc_offset_DST  = -6' )
				elseif v[1].item == 'Q5390' or v[1].item == 'Q11367286' then -- UTC -05:00/CDT
					table.insert(a, '\n| timezone_DST     = [[Central Daylight Saving Time|CDT]]')
					table.insert(a, '\n| utc_offset_DST   = -5')
				end
			end
		else
			table.insert(a,      '\n| timezone = [[Kategori:Underhållskategori för Ortsfakta WD|Ψ]]' )
		end
	end
	

	if args.postnummer then
		table.insert(a, '\n| postal_code = ' .. args.postnummer)
		table.insert(a, '\n| postal_code_type = [[ZIP-kod]]')
	else
		local v = wikidata({entity = entity, property = 'P281', noref = 'noimport'})
		if v and v ~= '' then
			table.insert(a, '\n| postal_code = ' .. v)
			table.insert(a, '\n| postal_code_type = [[ZIP-kod]]')
		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', noref = 'noimport'})
		if v and v ~= '' then
			table.insert(a, '\n| area_code = ' .. v)
			table.insert(a, '\n| area_code_type = [[Riktnummer]]')
		end	
	end
	
	if args['borgmästare'] and args['borgmästare'] ~= '' then
		ledare = true
		table.insert(a, '\n| leader = ' .. args['borgmästare'])
		table.insert(a, '\n| leader_type = [[Borgmästare]]')
	elseif not args['borgmästare'] then
		local data = wikidata({entity = entity, property = 'P6', withintervall = 'yes', modifyqualifiertime = 'Y', noref = 'noimport', raw = 'true', avoidqualifier = 'P582', relevantred = 'true'})
		if data and #data > 0 then
			ledare = true
			if data[1].tifr then
				table.insert(a, '\n| leader = ' .. data[1].value .. '<small> (' .. data[1].tifr .. ')</small>')
			else
				table.insert(a, '\n| leader = ' .. data[1].value )
			end
			if data[1].parti and data[1].parti ~= '' then
				table.insert(a, '\n| leader_party = ' .. data[1].parti)
			end
			table.insert(a, '\n| leader_type = [[Borgmästare]]')
			if data[1].ref then
				table.insert(a, '\n| leader_note = ' .. data[1].ref)
			end
		end
	end
	
	local area = args.areaort
	local y1 = false
	local y2 = false
	if area and area ~= '' then
		table.insert(a, "\n| area = " .. area)
		if args['areaår'] then
			table.insert(a, "\n| area_date = " .. args['areaår'])
		end
		if args['areanot'] then
			table.insert(a, "\n| area_note = " .. args['areanot'])
		end
		if args['arealand'] then
			table.insert(a, "\n| area_land = " .. args['arealand'])
			if args['arealandnot'] then
				table.insert(a, "\n| area_land_note = " .. args['arealandnot'])
			end
		end
		if args['areavatten'] then
			table.insert(a, "\n| area_water = " .. args['areavatten'])
			if args['areavattennot'] then
				table.insert(a, "\n| area_water_note = " .. args['areavattennot'])
			end
		end
	elseif not area then
		local v = wikidata({entity = entity, property = "P2046", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', preferqualifier ='P518', rank = 'valid', preferqualifiervalue = 'Q16868672', modifyqualifiertime = 'Y', convertunit = 'Q712226', noref='noimport'})		
		if not v or #v == 0 then 
			-- area total, utan qualifier om ovan inte funkade
			v = wikidata({entity = entity, property = "P2046", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', rank = 'valid', modifyqualifiertime = 'Y', convertunit = 'Q712226', noref='noimport'})
		end
		if v and #v > 0 then
			y1 = true
			table.insert(a, "\n| area = " .. tonumber(plus(v[1].amount)))
			table.insert(a, "\n| area_decimals = 2")
			if v[1].datum and v[1].datum ~= '' then
				table.insert(a, "\n| area_date = " .. v[1].datum)
			end
			if v[1].ref then
				table.insert(a, "\n| area_note = " .. v[1].ref)
			end
		end
		local v2 = wikidata({entity = entity, property = "P2046", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', preferqualifier ='P518', rank = 'valid', preferqualifiervalue = 'Q11081619', modifyqualifiertime = 'Y', convertunit = 'Q712226', noref='true'})
--		local v4 = wikidata({entity = entity, property = "P2046", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', preferqualifier ='P518', rank = 'valid', preferqualifiervalue = 'Q23397', convertunit = 'Q712226', noref='true', modifyqualifiertime = 'Y'}) -- insjövatten
--		local v5 = wikidata({entity = entity, property = "P2046", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', preferqualifier ='P518', rank = 'valid', preferqualifiervalue = 'Q4022', convertunit = 'Q712226', noref='true', modifyqualifiertime = 'Y'}) -- floder
		if v2 and #v2 > 0 then
			local land = tonumber(plus(v2[1].amount))
			--if v4 and #v4 > 0 then
			--	land = land - tonumber(plus(v4[1].amount))
			--end
			--if v5 and #v5 > 0 then
			--	land = land - tonumber(plus(v5[1].amount))
			--end
			table.insert(a, "\n| area_land = " .. land )
			y1 = true
			table.insert(a, "\n| area_land_decimals = 2")
			if v2[1].datum and v2[1].datum ~= '' then
				table.insert(a, "\n| area_land_date = " .. v2[1].datum)
			end
		end		
		local vatten = 0
		local v3 = wikidata({entity = entity, property = "P2046", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', preferqualifier ='P518', rank = 'valid', preferqualifiervalue = 'Q15324', convertunit = 'Q712226', noref='true', modifyqualifiertime = 'Y'})
		if v3 and #v3 > 0 then
			vatten = vatten + tonumber(plus(v3[1].amount))
		end
--		if v4 and #v4 > 0 then
--			vatten = vatten + tonumber(plus(v4[1].amount))
--		end
--		if v5 and #v5 > 0 then
--			vatten = vatten + tonumber(plus(v5[1].amount))
--		end
		if vatten > 0 then
			table.insert(a, "\n| area_water = " .. vatten)
			table.insert(a, "\n| area_water_decimals = 2")
			if v3[1] and v3[1].datum then --and v4[1] and v4[1].datum and v5[1] and v5[1].datum and v3[1].datum == v4[1].datum and v3[1].datum == v5[1].datum 
				table.insert(a, "\n| area_water_date = " .. v3[1].datum)
			end
		end		
	end
	local inv = args['invort']
	local pop = nil
	if inv and inv ~= '' then
		table.insert(a, "\n| population = " .. inv)
		y2 = true
		if args['invår'] then
			table.insert(a, "\n| population_date = " .. args['invår'] )
		end
		if args['invnot'] then
			table.insert(a, "\n| population_note = " .. args['invnot'] )
		end
	elseif not inv then
		local v = wikidata({entity = entity, property='P1082', sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', modifyqualifiertime = 'Y', noref='noimport'})
		if v and v[1] and v[1].amount then
			table.insert(a, "\n| population = " .. tonumber(plus(v[1].amount)))
			pop = tonumber(plus(v[1].amount))
			y2 = true
			if v[1].datum then
				table.insert(a, "\n| population_date = " .. v[1].datum)
			end
			if v[1].ref then
				table.insert(a, "\n| population_note = " .. v[1].ref)
			end
		end
	end
	if y1 and y2 then
		table.insert(a, "\n| population_density = auto ")
	elseif args['täthet'] then
		table.insert(a, "\n| population_density = " .. args['täthet'] )
	end
	
	local area = args.areastorstad
	local y1 = false
	local y2 = false
	local entitystorstad = nil
	if area and area ~= '' then
		table.insert(a, "\n| area_metro = " .. area)
		y1 = true
		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_note = " .. args['areastorstadnot'])
		end
		table.insert(a, "\n| area_metro_type = [[metropolitan statistical area|metropolitan area]]")
	elseif area ~= '' and storstad[entity.id] then
		entitystorstad = mw.wikibase.getEntityObject( storstad[entity.id] )
		local v = wikidata({entity = entitystorstad, property = "P2046", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', preferqualifier ='P518', rank = 'valid', preferqualifiervalue = 'Q16868672', modifyqualifiertime = 'Y', convertunit = 'Q712226', noref='noimport'})
		if not v or #v == 0 then 
			-- area total, utan qualifier om ovan inte funkade
			v = wikidata({entity = entitystorstad, property = "P2046", sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', rank = 'valid', modifyqualifiertime = 'Y', convertunit = 'Q712226', noref='noimport'})
		end
		if v and #v > 0 then
			y1 = true
			table.insert(a, "\n| area_metro = " .. tonumber(plus(v[1].amount)))
			table.insert(a, "\n| area_metro_decimals = 2")
			if v[1].datum and v[1].datum ~= '' then
				table.insert(a, "\n| area_metro_date = " .. v[1].datum)
			end
			if v[1].ref then
				table.insert(a, "\n| area_metro_note = " .. v[1].ref)
			end
			if entitystorstad.sitelinks and entitystorstad.sitelinks['svwiki'] then
				table.insert(a, "\n| area_metro_type = [[" .. entitystorstad.sitelinks['svwiki'].title ..  "|metropolitan area]]")
			else
				table.insert(a, "\n| area_metro_type = [[metropolitan statistical area|metropolitan area]]")
			end
		end
	end
	local inv = args['invstorstad']
	if inv and inv ~= '' then
		table.insert(a, "\n| population_metro = " .. inv)
		y2 = true
		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_note = " .. args['invstorstadnot'] )
		end
		table.insert(a, "\n| population_metro_type = [[metropolitan statistical area|metropolitan area]]")
	elseif inv ~= '' and storstad[entity.id] then
		if not entitystorstad then
			entitystorstad = mw.wikibase.getEntityObject( storstad[entity.id] )
		end
		local v = wikidata({entity = entitystorstad, property='P1082', sortbytime = 'chronological', enbarten = 'yes', raw = 'true', avoidqualifier='P518', modifyqualifiertime = 'Y', noref='noimport'})
		if v and v[1] and v[1].amount then
			table.insert(a, "\n| population_metro = " .. tonumber(plus(v[1].amount)))
			pop = tonumber(plus(v[1].amount))
			y2 = true
			if v[1].datum then
				table.insert(a, "\n| population_metro_date = " .. v[1].datum)
			end
			if v[1].ref then
				table.insert(a, "\n| population_metro_note = " .. v[1].ref)
			end
			if entitystorstad.sitelinks and entitystorstad.sitelinks['svwiki'] then
				table.insert(a, "\n| population_metro_type = [[" .. entitystorstad.sitelinks['svwiki'].title ..  "|metropolitan area]]")
			else
				table.insert(a, "\n| population_metro_type = [[metropolitan statistical area|metropolitan area]]")
			end
		end
	end
	if y1 and y2 then
		table.insert(a, "\n| population_metro_density = auto ")
		table.insert(a, "\n| population_metro_density_type = [[metropolitan statistical area|metropolitan area]]")
	elseif args['täthetstorstad'] then
		table.insert(a, "\n| population_metro_density      = " .. args['täthetstorstad'] )
		table.insert(a, "\n| population_metro_density_type = [[metropolitan statistical area|metropolitan area]]")
	end


	if (args.GNIS or args['GNIS-id']) and (args.GNIS or args['GNIS-id']) ~= '' then
		table.insert(a, '\n| code     = [https://geonames.usgs.gov/apex/f?p=gnispq:3:::NO::P3_FID:' .. (args.GNIS or args['GNIS-id']) .. ' ' .. (args.GNIS or args['GNIS-id']) .. ']' )
		table.insert(a, '\n| code1_type = GNIS-kod')
	elseif not args.GNIS and not args['GNIS-id'] then
		local v = wikidata({entity = entity, property='P590', noref='true', enbarten = 'true'})
		if v and v ~= '' then
			table.insert(a, '\n| code     = [https://geonames.usgs.gov/apex/f?p=gnispq:3:::NO::P3_FID:' .. v .. ' ' .. v .. ']' )
			table.insert(a, '\n| code_type = GNIS-kod')
		end
	end
	if args.geonames then
		table.insert(a, '\n| geonames = ' .. args.geonames)
	end

	local karta1 = false
	if args['karta'] and args['karta'] ~= '' then
		table.insert(a, '\n| map            = ' .. args.karta )
		table.insert(a, '\n| map_caption    = ' .. (args.karttext or ''))
		table.insert(a, '\n| map_size       = ' .. (args.kartstorlek or ''))
	elseif args['karta'] ~= '' then
		local v = wikidata({entity = entity, property = 'P242', noref = 'true', enbarten = 'true', raw = 'true' })
		if v and #v > 0 then
			table.insert(a, '\n| map            = ' .. v[1].value )
			karta1 = true
			if v[1].bildtext or v[1].motiv then
				table.insert(a, '\n| map_caption    = ' .. (v[1].bildtext or v[1].motiv))
			else
				table.insert(a, '\n| map_caption    = Översiktskarta' )
			end
		end
	end

	if args['positionskarta'] and args['positionskarta'] ~= '' then
		if args['positionskartnamn'] then
			table.insert(a, '\n| map1            = ' .. args['positionskartnamn'] )
			table.insert(a, '\n| map1_caption    = ' .. args['karttext2'] )
			table.insert(a, '\n| map1_locator    = ' .. args['positionskarta'] )
		elseif args['prickpos x'] then
			table.insert(a, '\n| map1            = USA location map.svg' ) 
			table.insert(a, '\n| map1_caption    = ' .. args['karttext2'] )			
			table.insert(a, '\n| map1_locator_x = ' .. args['prickpos x'] )
			table.insert(a, '\n| map1_locator_y = ' .. args['prickpos y'] )
		end
	elseif not karta1 and kartor[delstatqid] and kartor[delstatqid].karta then
		table.insert(a, '\n| map1            = ' .. kartor[delstatqid].karta )
		table.insert(a, '\n| map1_locator   = ' .. kartor[delstatqid].positionskarta )
		if deltyp == '[[USA:s delstater|Delstat]]' then
			table.insert(a, '\n| map1_caption   = Ortens läge i delstaten' )
		else
			table.insert(a, '\n| map1_caption   = Ortens läge i territoriet' )
		end
	elseif not karta1 and not kartor[delstatqid] and deltyp == '[[USA:s delstater|Delstat]]' then
		table.insert(a, '\n| map1           = Usa edcp (+HI +AK) location map.svg' )
		table.insert(a, '\n| map1_locator   = USA2' )
		table.insert(a, '\n| map1_caption   = Ortens läge i USA' )
	end
	
	if args['karta3'] and args['karta3'] ~= '' then
		table.insert(a, '\n| map2         = ' .. args['karta3'] )
		table.insert(a, '\n| map2_caption = ' .. args['karttext3'] )
		table.insert(a, '\n| map2_size    = ' .. args['kartstorlek3'] )
	elseif args['karta3'] ~= '' and kartor[delstatqid] then
		table.insert(a, '\n| map2         = ' .. kartor[delstatqid].karta2 )
		table.insert(a, '\n| map2_caption = ' .. kartor[delstatqid].karta2text )
	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 />[[d:'.. entity.id .. '|Redigera Wikidata]]')
	else
		table.insert(a, '\n| footnotes         = [[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

	if args.hemsida and args.hemsida ~= '' then
		table.insert(a, '\n| website = ' .. args.hemsida)
	elseif not args.hemsida then
		local v = wikidata({entity = entity, property = "P856", enbarten = 'yes', noref='true'})
		if v and v ~= '' then
			table.insert(a, '\n| website = ' .. v)
		end
	end

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


function indexera(v, i)
	if v and #v >= i then
		return v[i].value
	end
	return ''
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

function USAharva(entity, s)
	local v = wikidata({entity = entity, property = 'P131', noref = 'true', raw = 'true'})
	if not v or v[1].item == 'Q30' then 
		return s
	elseif #v > 1 then -- bypass en nivå om den innhåller mer än en p131
		local entity2 = mw.wikibase.getEntityObject( v[1].item )
		return USAharva( entity2, s)
	else
		local entity2 = mw.wikibase.getEntityObject( v[1].item )
		local flag = wikidata({entity = entity2, property = 'P41', noref = 'true', enbarten = 'true'})
		local p31 = wikidata({entity = entity2, property = 'P31', noref = 'true'})
		local p32 = wikidata({entity = entity2, property = 'P31', noref = 'true', raw = true})
		if test("Q55030674", p32) then
			p31 = '[[Amerikanska Samoas administrativa indelning|County]]'
		elseif test("Q1247537", p32) then
			p31 = '[[Amerikanska Samoas administrativa indelning|Distrikt]]'
		elseif test('Q57889455', p32) then
			p31 = '[[Amerikanska Jungfruöarnas administrativa indelning|Distrikt]]'
		elseif test('Q20830236', p32) then
			p31 = '[[Amerikanska Jungfruöarnas administrativa indelning|Underdistrikt]]'
		elseif test('Q96857311', p32) then	
			p31 = '[[Guams administrativa indelning|By]]'
		elseif string.match(p31, 'county') then
			p31 = '[[USA:s countyn|County]]'
		elseif test("Q13410508", p32) then
			p31 = '[[USA:s countyn|County]]'
		elseif test("Q13410524", p32) then
			p31 = '[[Lista över parishes i Louisiana|Parish]]'
		elseif test("Q13410522", p32) then
			p31 = '[[Alaskas administrativa indelning|Borough]]'
		elseif test("Q56064719", p32) then
			p31 = '[[Alaskas administrativa indelning|Folkräkningsområde]]'
		elseif test('Q28111', p32) then
			p31 = 'Township'
		elseif test('Q15613737', p32) then
			p31 = '[[Nordmarianernas administrativa indelning|Kommun]]'
		elseif test('Q263639', p32) then
			p31 = '[[Puerto Ricos kommuner|Kommun]]'
		elseif test("Q35657", p32) then
			p31 = '[[USA:s delstater|Delstat]]'
		elseif test("Q783733", p32) or test("Q1352230", p32) then
			p31 = '[[Lista över USA:s delstater och territorier#Territorier|Territorium]]'
		elseif test('Q1093829', p32) then -- Stad (city) 
			p31 = 'Stad'
		elseif test('Q106071004', p32) then -- Stad (town) -- town i New York
			p31 = 'Stad'
		elseif string.match(p31, 'distri') then
			p31 = 'Distrikt'
		end
		table.insert(s, {item = v[1].item, flag = flag, typ = p31, value = v[1].value}) -- 
		return USAharva( entity2, s)
	end
end

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


return p