Tato stránka je zamčena pro neregistrované a nové uživatele

Modul:Souřadnice

Z Wikipedie, otevřené encyklopedie
Skočit na navigaci Skočit na vyhledávání
require 'Modul: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 = "earth",
	Q308 = "mercury",
	Q313 = "venus",
	Q405 = "moon",
	Q111 = "mars",
	Q7547 = "phobos",
	Q7548 = "deimos",
	Q3169 = "ganymede",
	Q3134 = "callisto",
	Q3123 = "io",
	Q3143 = "europa",
	Q15034 = "mimas",
	Q3303 = "enceladus",
	Q15047 = "tethys",
	Q15040 = "dione",
	Q15050 = "rhea",
	Q2565 = "titan",
	Q15037 = "hyperion",
	Q17958 = "iapetus",
	Q17975 = "phoebe",
	Q3352 = "miranda",
	Q3343 = "ariel",
	Q3338 = "umbriel",
	Q3322 = "titania",
	Q3332 = "oberon",
	Q3359 = "triton",
	Q339 = "pluto",
}

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