Mine sisu juurde

Moodul:Koord

Allikas: Vikipeedia
Mooduli dokumentatsioon
local p = {}

-- calculate distance in meters
function p.getDistance( frame )
	lat1 = tonumber(frame.args[1])
	lon1 = tonumber(frame.args[2])
	lat2 = tonumber(frame.args[3])
	lon2 = tonumber(frame.args[4])
	
	if lat1 == lat2 and lon1 == lon2 then
		return 0
	elseif (lat1 and lon1 and lat2 and lon2 ) then
		local dLat = math.rad(lat1-lat2)
		local dLon = math.rad(lon1-lon2)
		local d = math.pow(math.sin(dLat/2),2) + math.pow(math.sin(dLon/2),2) * math.cos(math.rad(lat1)) * math.cos(math.rad(lat2))
		d = 2 * math.atan2(math.sqrt(d), math.sqrt(1-d))  -- angular distance in radians
		d = 6371000 * d       -- radians to meters conversion
		d = math.floor(d+0.5) -- rind it to even meters
		return d
	end
end

-- kasutamiseks teistest moodulitest
function p.dms2dec_convert(instring, dimension)

	if tonumber(instring) then 
		return tonumber(instring) 
	elseif string.find(instring, '/') then
		local args = mw.text.split(instring, '/', true)
		if #args > 4 then
			error('Too many parameters.')
		end	
		local direction = ''
		if instring:match( '[NSEWO]') then
			direction = mw.text.trim(args[#args])
		elseif dimension == 'latitude' then
			direction = 'N'
		elseif dimension == 'longitude' then
			direction = 'E'
		end
		local degrees, minutes, seconds = tonumber(args[1]), tonumber(args[2]) or 0, tonumber(args[3]) or 0
	    
	    local factor
	    if direction == "S" or direction == "W" then
	        factor = -1
	    else
	        factor = 1
	    end
	    
	    local precision = 6
	    
	    local decimal = factor * (degrees+(minutes+seconds/60)/60)
	    return tonumber( string.format( "%." .. precision .. "f", decimal) )
	else
		error('Invalid format.')
	end
end

-- Wrapper to allow templates to call dms2dec directly.
function p.dms2dec(frame)
	local args = frame.args
	local dimension = args.dimension

    return p.dms2dec_convert(args[1], dimension)
end

-- koordinaadid Wikidatast
function p.wikidata( frame )
	local parameter = frame.args['parameter'] or 'latitude'
	local list = frame.args['kas_topelt']
	local id = mw.wikibase.getEntityIdForCurrentPage()
	if id then
		local P625 = mw.wikibase.getBestStatements( id, 'P625' )
		if P625[1] and P625[1].mainsnak.snaktype == 'value' then
			v = P625[1].mainsnak.datavalue.value
		end
		if not v then -- check for headquarters location (P159)
			local P159 = mw.wikibase.getBestStatements( id, 'P159' )
			if P159[1] and P159[1].qualifiers and P159[1].qualifiers.P625 then
				v = P159[1].qualifiers.P625[1].datavalue.value
			end
		end
		if list then
			if P625[2] then
				return '[[Kategooria:Topeltkoordinaadid Wikidatas]]'
			end
		elseif v then
			return v[parameter]
		end
	end
end

return p