Module:HLFan/Autostraßenabfrage

From OpenStreetMap Wiki
Jump to navigation Jump to search
[Create] Documentation
local p = {}
local toolkit = require("Module:HLFan/Toolkit")

local function getPaths()
	local title = mw.title.new("Austria/Liste Autostraßen")
    local wikitext = title:getContent()
    local paths = {}
    for s in wikitext:gmatch('[%d.,;]+') do
    	if #s >= 20 then
    		table.insert(paths, s)
        end
    end
    return paths
end

local function makeQuery(filter, area, target)
    local lineFilter = {}
    lineFilter["trunk"] = '[highway~"motorway|trunk|primary"]'
    lineFilter["motorroad"] = '[motorroad]'
    target = target or ''
    return 'way'  .. lineFilter[filter] .. '(' .. area .. ')' .. target .. ';'
end

local function makeFilter(filter)
    local out = {}
    out["trunk"] = [[
way.r[highway=trunk];
way.r[highway=trunk_link];
way.s[highway=motorway];
way.s[highway=motorway_link];
way.s[highway=primary];
way.s[highway=primary_link];
]]
    out["motorroad"] = [[
way.r[motorroad=yes];
way.s[motorroad=no];
]]
	return out[filter]
end

local function getQuery(filter)
	filter = filter or "trunk"
    local paths = getPaths()
    local out = [[
[out:json];
area(id:3600016239)->.at;
]] .. makeQuery(filter, 'area.at', '->.g') .. '\n(\n'
    for _, path in ipairs(paths) do
    	out = out .. makeQuery(filter, 'around:100' .. path:gsub('([%d.]+),([%d.]+);?', ',%2,%1')) .. '\n'
    end
	return out .. [[
)->.s;
(.g; - .s;)->.r;
(
]] .. makeFilter(filter) .. ');\nout geom;'
end

function p.generateQuery(frame)
    return getQuery(frame.args[1])
end

function p.generateUrl(frame)
    return 'https://overpass-turbo.eu?q=' .. toolkit.compress_query(getQuery(frame.args[1]))
end

return p