Moodul:Koord

Allikas: Vikipeedia
Jump to navigation Jump to search
Template-info.svg Mooduli dokumentatsioon

Vaata ka[muuda lähteteksti]

p = {}

-- calculate distance in meters
function p._getDistance(lat1, lon1, lat2, lon2)
	local lat1 = tonumber(lat1)
	local lon1 = tonumber(lon1)
	local lat2 = tonumber(lat2)
	local lon2 = tonumber(lon2)
	
	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

function p.getDistance(frame)
	local args = frame.args
	return p._getDistance(args[1], args[2], args[3], args[4])
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

--[[
dms2dec

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

return p