Przejdź do zawartości

Moduł:Wikidane/format/globeCoordinates

Z Wikibooks, biblioteki wolnych podręczników.
 Dokumentacja modułu [zobacz] [edytuj] [historia] [odśwież]

Dedykowana uniwersalna wtyczka formatująca wartość cechy ze współrzędnymi geograficznymi.

Wtyczka obsługuje parametr linkuj oraz parametr format, który przyjmując jedną z wartości kątowo lub dziesiętnie pozwala zawęzić sposób prezentacji współrzędnych.

Zobacz też

local moduleData = mw.loadData("Module:Wikidane/data")

return {

scope = "snak",

format = function(snak, options)

	local function fullpagenamee()
		local title = mw.title.getCurrentTitle()
		return title.namespace == 0 and title:partialUrl() or (mw.site.namespaces[title.namespace].name .. ":" .. title:partialUrl())
	end

	local function findGeoFormatData(precision)
		local lastFormat = false
		local arc = moduleData.arcFormats[options.format]
		for i, v in ipairs(moduleData.geoformats) do
			if (arc == nil) or (v.arc == arc) then
				local prec = v.precision
				if (prec - (prec / 64)) < precision then
					return v
				end

				lastFormat = v
			end
		end

		return lastFormat
	end

	local function formatAngle(value, formatdata, markers, decimalSeparator)

		local prefix = value < 0 and markers.np or markers.pp
		local suffix = value < 0 and markers.ns or markers.ps

		value = math.abs(value)

		if not formatdata.arc then
			if formatdata.precision > 1 then
				-- round the value
				value = math.floor(value / formatdata.precision) * formatdata.precision
			end

			value = string.format(formatdata.format, value, markers.d)
		else
			local halfPrecInSec = formatdata.precision * 3600
			local totalSeconds = value * 3600
			local distanceToNextMinuteInSec = (1 + math.floor(60*value) - (60*value)) * 60
			if distanceToNextMinuteInSec <= halfPrecInSec then
				-- round the value to avoid 60 seconds
				totalSeconds = totalSeconds + halfPrecInSec
			end
			
			local totalMinutes = math.floor(totalSeconds / 60)
			local angle = math.floor(totalMinutes / 60)
			local minutes = totalMinutes - 60 * angle
			local seconds = totalSeconds - 60 * totalMinutes

	 		value = string.format(formatdata.format, angle, markers.d, minutes, markers.m, seconds, markers.s)
		end

		if decimalSeparator and (decimalSeparator ~= ".") then
			value, _ = string.gsub(value, "%.", decimalSeparator)
		end

		return prefix .. value .. suffix
	end

	local function formatCoordinates(value)
		local geoformat = findGeoFormatData(value.precision or moduleData.geoformats.defaultPrecision)
		local globe = moduleData.globes[value.globe] or moduleData.globes.unknownGlobe
		if globe.sky then
			mw.log(warnSkyIsNotSupported)
			return nil
		end

		-- TODO
		-- markers = sky and skyMarkers or globeMarkers

		local prettyLatitude  = formatAngle(value.latitude, geoformat, moduleData.latitudeGlobeMarkers, moduleData.decimalSeparator)
		local prettyLongitude = formatAngle(value.longitude, geoformat, moduleData.longitudeGlobeMarkers, moduleData.decimalSeparator)
		local link = globe.link and options.linkCoordinates
		if not link then
			return string.format(moduleData.geohack_plain, prettyLatitude, prettyLongitude)
		end

		local dim = false -- TODO maybe someday "dim = value.dimension"
		if globe.radius and value.precision then
			dim = math.ceil(math.rad(value.precision) * math.cos(math.rad(value.latitude)) * globe.radius)
		end

		local params = {
			formatAngle(value.latitude, moduleData.geoformats.linkFormat, moduleData.latitudeLinkMarkers),
			formatAngle(value.longitude, moduleData.geoformats.linkFormat, moduleData.longitudeLinkMarkers),
		}

		table.insert(params, globe.link)
		if dim and (dim > 0) then
			table.insert(params, "dim:"..dim)
		end

		local geohack_link = string.format(moduleData.geohack_link, fullpagenamee(), table.concat(params,"_"))
		local pretty_hint = string.format(moduleData.geohack_hint, prettyLatitude, prettyLongitude)
		local pretty_text = string.format(moduleData.geohack_text, prettyLatitude, prettyLongitude)
		local degree_hint = string.format(moduleData.geohack_hint, prettyLatitude, prettyLongitude)
		local degree_text = string.format(moduleData.geohack_text, prettyLatitude, prettyLongitude)

		local result = {
			globe.symbol, #globe.symbol > 0 and " " or "",
			"[",
			geohack_link,
			" ",
			"<span class=\"geo-default\">",
				"<span class=\"geo-dms\" title=\"", pretty_hint, "\">",
					pretty_text,
				"</span>",
			"</span>",
			"<span class=\"geo-multi-punct\">/</span>",
			"<span class=\"geo-nondefault\">",
				"<span class=\"geo-dms\" title=\"", degree_hint, "\">",
					degree_text,
				"</span>",
			"</span>",
			"]"
		}

		return table.concat(result, "")
	end

	local result = formatCoordinates(snak.datavalue.value)
	mw.log(result)
	return result
end,
}