Modul:Great circle distance

Dokumentaci tohoto modulu lze vytvořit na stránce Modul:Great circle distance/Dokumentace

-- Modul:Great circle distance
-- Getting Great Circle Distance
-- založeno kopií z dewikivoyage (https://de.wikivoyage.org/w/index.php?title=Modul:Great_circle_distance&oldid=1219780)
-- copy from dewikivoyage

local gcd = {}

local function round( n, idp )
	local m = 10^( idp or 0 )
	if n >= 0 then
		return math.floor( n * m + 0.5 ) / m
	else
		return math.ceil( n * m - 0.5 ) / m
	end
end

-- Getting latitude-dependent earth radius
-- lat ranges from -90 to 90, decimal degree
function gcd.earthRadius( lat )
	local equatorR = 6378.137
	local poleR = 6356.752
	if not lat then
		lat = 35.4
	end
	lat = lat * math.pi / 180
	return math.sqrt( ( ( equatorR * equatorR * math.cos( lat ) )^2 + ( poleR * poleR * math.sin( lat ) )^2 ) /
		( ( equatorR * math.cos( lat ) )^2 + ( poleR * math.sin( lat ) )^2 ) )
end

-- Getting great circle distance by kilometers or unit of radius
-- Haversine formula
-- See: https://en.wikipedia.org/wiki/Haversine_formula
function gcd.getGcd( lat1, long1, lat2, long2, radius )
	if not radius then
		radius = ( gcd.earthRadius( lat1 ) + gcd.earthRadius( lat2 ) ) / 2
	end
	local factor = math.pi / 180
	lat1 = lat1 * factor
	long1 = long1 * factor
	lat2 = lat2 * factor
	long2 = long2 * factor
	return 2 * radius * math.asin ( math.sqrt (
		math.pow( math.sin( ( lat2 - lat1 ) / 2 ), 2 )
		+ math.cos( lat1 ) * math.cos( lat2 ) * math.pow( math.sin( ( long2 - long1 ) / 2 ), 2 )
		) )
end

-- Template call
function gcd.gcd( frame )
	local args = frame:getParent().args
	args.lat1  = tonumber( args.lat1 or args[ 1 ] or '0' ) or 0
	args.long1 = tonumber( args.long1 or args[ 2 ] or '0' ) or 0
	args.lat2  = tonumber( args.lat2 or args[ 3 ] or '0' ) or 0
	args.long2 = tonumber( args.long2 or args[ 4 ] or '0' ) or 0
	args.precision = tonumber( args.precision or args[ 5 ] or '0' ) or 0
	args.precision = round( args.precision, 0 )

	return round( gcd.getGcd( args.lat1, args.long1, args.lat2, args.long2, args.radius ),
		args.precision )
end

return gcd