Module:Souřadnice

Από τη Βικιπαίδεια, την ελεύθερη εγκυκλοπαίδεια
Documentation icon Τεκμηρίωση module[δημιουργία]
--require "Module:No globals"

local getArgs = require('Module:Arguments').getArgs

local p = {}

--- Mapování z ID entity na Wikidatech na identifikátor tělesa v GeoHacku.
p.myGlobes = {
	Q2 = "Γη",
	Q308 = "Ερμής",
	Q313 = "Αφροδίτη",
	Q405 = "Φεγγάρι",
	Q111 = "Άρης",
	Q7547 = "Φόβος",
	Q7548 = "Δείμος",
	Q3169 = "Γανυμήδης",
	Q3134 = "Καλλιστώ",
	Q3123 = "Ιώ",
	Q3143 = "Ευρώπη",
	Q15034 = "Μίμας",
	Q3303 = "Εγκέλαδος",
	Q15047 = "Τηθύς",
	Q15040 = "Διώνη",
	Q15050 = "Ρέα",
	Q2565 = "Τιτάνας",
	Q15037 = "Υπερίων",
	Q17958 = "Ιαπετός",
	Q17975 = "Φοίβη",
	Q3352 = "Μιράντα",
	Q3343 = "Άριελ",
	Q3338 = "Ουμβριήλ",
	Q3322 = "Τιτάνια",
	Q3332 = "Όμπερον",
	Q3359 = "Τρίτωνας",
	Q339 = "Πλούτωνας",
}

local roundingForType = {
	landmark = 2,
	railwaystation = 2,
	pass = 2,
	edu = 2
}

-- dec2dms(0) --> 0°
-- dec2dms(10.5) --> 10°30′
-- dec2dms(0.016667) --> 0°1′
-- dec2dms(0.004166) --> 0°0′15″

function p._dec2dms(value, precision)
	value = math.abs(value)
	local sec, secfrac = math.modf(value * 3600 + 0.005)
	local minTotal = math.floor(value * 60 + 0.5)
	if not precision then
		if sec % 60 > 0 then
			precision = 0
		elseif minTotal % 60 > 0 then
			precision = -1
		else
			precision = -2
		end
	end

	if precision == -2 then
		local deg = math.floor(value + 0.5)
		return string.format("%d°", deg)
	elseif precision == -1 then
		local min = minTotal % 60
		local deg = math.floor(minTotal / 60)
		return string.format("%d°%d′", deg, min)
	else
		local mult = 10^precision
		local secTotal = math.floor(value * 3600 * mult + 0.5) / mult
		local min = math.floor(secTotal / 60)
		local sec = secTotal - min * 60
		local deg = math.floor(min / 60)
		min = min % 60
		-- ošetření zaokrouhlovací chyby
		sec = math.floor(sec * mult + 0.1) / mult

		local lang = mw.language.getContentLanguage()
		return string.format("%d°%d′%s″", deg, min, lang:formatNum(sec))
	end
end

function p.dec2dms(frame)
	return p._dec2dms(frame.args[1], frame.args[2])
end

function p._renderCoordinates(args, frame)
	local pageTitle = mw.title.getCurrentTitle()
	local lat = tonumber(args[1])
	local lon = tonumber(args[2])
	if not lat or not lon then
		local err = "<span class='error'>Chybná syntaxe souřadnic: <code>" .. mw.text.nowiki(args[1] or "") .. "</code>, <code>" .. mw.text.nowiki(args[2] or "") .. "</code></span>"
		if pageTitle.namespace == 0 then err = err .. "[[Kategorie:Údržba:Články s chybným vložením souřadnic]]" end
		return err
	end
	local display = args.display
	local isMain = display == "top" or display == "infobox"
	local globe = args.globe
	local explicitName = args.name
	local name = explicitName or (isMain and pageTitle.text)
	local typ = args.typ
	local region = args.region
	local scale = args.scale

	local rounding = typ and roundingForType[typ]
	typ = typ or "landmark"

	local geoParams = { tostring(lat), "N", tostring(lon), "E" }
	if globe then table.insert(geoParams, "globe:" .. globe) end
	if typ then table.insert(geoParams, "type:" .. typ) end
	if region then table.insert(geoParams, "region:" .. region) end
	if scale then table.insert(geoParams, "scale:" .. scale) end

	local urlParams = {
		language = "cs",
		pagename = pageTitle.prefixedText,
		params = table.concat(geoParams, "_")
	}
	if explicitName then urlParams.title = explicitName end

	local geohackUrl = "//tools.wmflabs.org/geohack/geohack.php?" .. mw.uri.buildQueryString(urlParams)

	local coordStr =
		"<span style='white-space:nowrap'>" .. p._dec2dms(lat, rounding) .. (lat >= 0 and " s." or " j.") .. " š.</span>, "
		.. "<span style='white-space:nowrap'>" .. p._dec2dms(lon, rounding) .. (lon >= 0 and " v." or " z.") .. " d.</span>"

	local result = {}

	if pageTitle.namespace == 0 then
		local coordTagArgs = { isMain and "primary" or "", tostring(lat), tostring(lon) }
		if globe then coordTagArgs["globe"] = globe end
		if name then coordTagArgs["name"] = name end
		table.insert(result, frame:callParserFunction("#coordinates", coordTagArgs))
	end

	if isMain then
		table.insert(result, "<span>")
		table.insert(result, frame:extensionTag('indicator', 'Souřadnice: [' .. geohackUrl .. ' ' .. coordStr .. ']', { name = 'coordinates' }))
		table.insert(result, "</span>")
	end

	if display ~= "top" then
		table.insert(result, "<span class='coordinates'>[")
		table.insert(result, geohackUrl)
		table.insert(result, " ")
		table.insert(result, coordStr)
		table.insert(result, "]</span>")
	end

	return table.concat(result, "")
end

function p.renderCoordinates(frame)
	local args = getArgs(frame, { removeBlanks = true })
	return p._renderCoordinates(args, frame)
end

return p