Module:Geobox map

-- this module implements [[template:geobox2 map]]

local getArgs = require('Module:Arguments').getArgs
local data_module = "Module:Geobox map/data"

local p = {}

local function rnd2(n)
	return math.floor(n * 100 + 0.5) / 100
end

local function aspect_ratio(s)
	local f = mw.title.new(s).file
	if f ~= nil and f ~= false then
		return f['width'] / f['height']
	else
		return 1
	end
end

-- Finds a parameter in a transclusion of {{Coord}}.
local function coord2text(para,coord)
	local hemisphereMultipliers = {
	longitude = { W = -1, w = -1, E = 1, e = 1 },
	latitude = { S = -1, s = -1, N = 1, n = 1 } }

	local result = mw.text.split(mw.ustring.match(coord,'[%.%d]+°[NS] [%.%d]+°[EW]') or '', '[ °]')
	if para == 'longitude' then result = {result[3], result[4]} end
	if not tonumber(result[1]) or not result[2] then return error('Malformed coordinates value', 2) end
	return tonumber(result[1]) * hemisphereMultipliers[para][result[2]]
end

function p.main(frame)
	local tracking_cats = ''
	local args = getArgs(frame)
	local root = mw.html.create('div')
	root:css('width', (args['width'] or '256') .. 'px')
		:css('margin-left', 'auto')
		:css('margin-right', 'auto')
	local div = root:tag('div'):css('position', 'relative')
	if args['background'] then
		local im = '[[File:' .. args['background']
		im = im .. '|' .. (args['width'] or '256') .. 'px'
		if args['map'] then
			im = im .. '|link='
		else
			im = im .. '|alt=' .. (args['alt'] or '')
		end
		im = im .. '|' .. (args['caption'] or '') .. ']]'
		div:tag('div')
			:css('position', 'absolute')
			:wikitext(im)
	end
	if args['map'] then
		local im = '[[File:' .. args['map']
		im = im .. '|' .. (args['width'] or '256') .. 'px'
		im = im .. '|alt=' .. (args['alt'] or '')
		im = im .. '|' .. (args['caption'] or '') .. ']]'
		div:tag('div')
			:css('position', 'relative')
			:wikitext(im)
	end
	if (args['x'] and args['y']) or (args['coordinates'] and args['locator']) then
		local x, y = 0, 0
		if (args['x'] and args['y']) then
			x = tonumber(args['x'] or '0') or 0
			y = tonumber(args['y'] or '0') or 0
		elseif args['locator'] then
			x = coord2text('longitude', args['coordinates'])
			y = coord2text('latitude', args['coordinates'])
		end
		local lpct, tpct = 0, 0
		if args['locator'] then
			-- local tname = 'Geobox locator ' .. args['locator']
			-- left = tonumber(frame:expandTemplate{title = tname, args = { 'left' }} or left) or left
			-- right = tonumber(frame:expandTemplate{title = tname, args = { 'right' }} or right) or right
			-- top = tonumber(frame:expandTemplate{title = tname, args = { 'top' }} or top) or top
			-- bottom = tonumber(frame:expandTemplate{title = tname, args = { 'bottom' }} or bottom) or bottom
			local all_maps = mw.loadData(data_module)
			local locator = mw.ustring.gsub(args['locator'], "[_ ][_ ]*", ' ')
			local map_data = all_maps[locator]
			if map_data and type(map_data) == 'table' then
				tracking_cats = tracking_cats .. '[[Category:Pages uisin geobox wi a locator cairt|' .. locator .. ']]'
				left = map_data['left']
				right = map_data['right']
				top = map_data['top']
				bottom = map_data['bottom']
				lpct = rnd2( (x - left)/(right - left)*100 )
				tpct = rnd2( (top - y) / (top - bottom)*100 )
			else
				tracking_cats = tracking_cats .. '[[Category:Pages uisin geobox wi an unkent locator cairt]]'
			end
		else
			local scale = aspect_ratio('Image:' .. (args['map'] or ''))
			lpct = rnd2(x)
			tpct = rnd2(y*scale)
			tracking_cats = tracking_cats .. '[[Category:Pages uisin geobox wi x an y locator cairt parameters|' .. (args['map'] or '') .. ']]'
		end
		div:tag('div')
			:css('position', 'absolute')
			:css('left', lpct .. '%')
			:css('top', tpct .. '%')
			:css('padding', '0')
			:css('line-height', '0')
			:tag('div')
				:css('position', 'absolute')
				:css('text-align', 'center')
				:css('left', '-4px')
				:css('top', '-4px')
				:css('width', '8px')
				:css('font-size', '8px')
				:css('line-height', '0')
				:wikitext('[[File:Red pog.svg|8px|link=|alt=]]')
	end
	if args['caption'] then
		root:tag('div')
			:css('padding-top', '0.4em')
			:css('text-align', 'center')
			:wikitext(args['caption'])
	end
	if args['table'] then
		return '<tr><td colspan=2>\n' .. tostring(root) .. tracking_cats .. '\n</td></tr>'
	else
		return tostring(root) .. tracking_cats
	end
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.