Naar inhoud springen

Module:Infobox plaats

Uit Wikipedia, de vrije encyclopedie

Documentatie voor deze module kan aangemaakt worden op de volgende pagina: Module:Infobox plaats/doc

local p = {}

function decodeEntities(encodedStr)
	encodedStr = encodedStr:gsub("'", "'")
	encodedStr = encodedStr:gsub(""", "\"")
	return encodedStr
end

function parseDMS(coord)
	coord = decodeEntities(coord)
	local degrees, minutes, seconds, direction = coord:match("(%d+)°(%d*)'?(%d*%.?%d*)\"?([NSEW])")
	if not degrees or not direction then
		error("Invalid coordinate format: " .. coord)
	end
	degrees = tonumber(degrees)
	minutes = tonumber(minutes) or 0
	seconds = tonumber(seconds) or 0
	local decimal = degrees + (minutes / 60) + (seconds / 3600)
	if direction == "S" or direction == "W" then
		decimal = -decimal
	end
	return decimal
end

function p.main(frame)
	local entity = mw.wikibase.getEntity()
	local coords = entity:formatPropertyValues('p625')["value"]
	
	if not coords or coords == '' then
		return ''
	end

	local latLonPairs = {}
	for coord in coords:gmatch("([^,]+)") do
		table.insert(latLonPairs, coord)
	end

	-- Find the first valid pair of latitude and longitude
	local firstLatLon
	for i = 1, #latLonPairs, 2 do
		if latLonPairs[i + 1] then
			firstLatLon = {latLonPairs[i], latLonPairs[i + 1]}
			break
		end
	end

	if not firstLatLon then
		error("Invalid coordinate format: " .. coords)
	end

	local lat = parseDMS(firstLatLon[1])
	local lon = parseDMS(firstLatLon[2])

	if frame.args[1] == 'lat' then
		return lat
	elseif frame.args[1] == 'lon' then
		return lon
	else
		error("invalid operation")
	end
end

return p