Відкрити головне меню
{{i}} Документація модуля[створити]
-- some utils for work with coords

p = {}
-- helper function to round value
function round(num, numDecimalPlaces)
    local mult = 10^(numDecimalPlaces or 0)
    return math.floor(num * mult + 0.5) / mult
end

-- distance between points on geotope
-- by their decimal coordinates in WGS 84

function p.distance (ns1, we1, ns2, we2, unit)
    -- перевести координати в радіани
    lat1 = ns1 * math.pi / 180
    lat2 = ns2 * math.pi / 180
    long1 = we1 * math.pi / 180
    long2 = we2 * math.pi / 180
    -- косінуси, сінуси широт, різниці довготи
    cl1 = math.cos(lat1) cl2 = math.cos(lat2)
    sl1 = math.sin(lat1) sl2 = math.sin(lat2)
    delta = long2 - long1
    cdelta = math.cos(delta)
    sdelta = math.sin(delta)
    -- довжина великого кола
    y = math.sqrt((cl2 * sdelta)^2 + (cl1 * sl2 - sl1 * cl2 * cdelta)^2)
    x = sl1 * sl2 + cl1 * cl2 * cdelta
    -- distance in meters, geotope radius = 6372795 meters
    dist = 6372795 * math.atan2(y, x)
    if unit == "km" and dist > 10000 then
        dist = round( dist / 1000, 0 )
    else dist = round( dist / 1000, 1 )
    end
    return dist
end

function p.azimuth(frame)
--assume 4 numbered params are coordinates of points
    ns1 = frame.args[1]
    we1 = frame.args[2]
    ns2 = frame.args[3]
    we2 = frame.args[4]
    unit = frame.args["unit"] or "degree"
--    if ns1 and we1 and ns2 and we2 then --all 4 coords may be in presence
--        return p.distance( ns1, we1, ns2, we2, unit )
--    else return "" --some parameters did not provided
--    end
    return ""
end

function p.dist(frame)
--assume 4 numbered params are coordinates of points
    ns1 = frame.args[1]
    we1 = frame.args[2]
    ns2 = frame.args[3]
    we2 = frame.args[4]
    unit = frame.args["unit"] or "km"
    if ns1 and we1 and ns2 and we2 then --all 4 coords may be in presence
        return p.distance( ns1, we1, ns2, we2, unit )
    else return "" -- [[Категорія:Модуль:Coordinates/utils:Службова]]
                   --some parameters did not provided
    end
end

function p.dist_old(frame)
--assume 4 numbered params are coordinates of points
    ns1 = frame.args[1]
    we1 = frame.args[2]
    ns2 = frame.args[3]
    we2 = frame.args[4]
    unit = frame.args["unit"] or "km"
    if ns1 and we1 and ns2 and we2 then --all 4 coords may be in presence
        return p.distance( ns1, we1, ns2, we2, unit )
    else return "" --some parameters did not provided
    end
end

return p