Documentation for this module may be created at વિભાગ:Geobox map/doc

-- 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 using geobox with a locator map|' .. 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 using geobox with an unknown locator map]]'
			end
		else
			local scale = aspect_ratio('Image:' .. (args['map'] or ''))
			lpct = rnd2(x)
			tpct = rnd2(y*scale)
			tracking_cats = tracking_cats .. '[[Category:Pages using geobox with x and y locator map 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