Module:Overpass

From the AARoads Wiki: Read about the road before you go
Jump to navigation Jump to search

Functions for generating links to Overpass API queries.

cite_length

Returns a citation to an Overpass query of the total length of all the specified OpenStreetMap elements combined.

Parameters:

|1=, |2=, etc.
OpenStreetMap element IDs. Each numeric ID is prefixed with a letter indicating the element type: n for a node, w for a way, or r for a relation. (In practice, a node cannot contribute to the length, but node IDs are accepted for completeness.)
|date=
The date of the query in any format accepted by the #time parser function.

See also


local p = {}
local lang = mw.getContentLanguage()

function p.cite_length(frame)
	local args = frame.args[1] and frame.args or frame:getParent().args
	
	local nodes = {}
	local ways = {}
	local relations = {}
	for i, id in ipairs(args) do
		local elementType = id:sub(1, 1)
		local num = tonumber(id:sub(2))
		if num then
			if elementType == "n" then
				table.insert(nodes, num)
			elseif elementType == "w" then
				table.insert(ways, num)
			elseif elementType == "r" then
				table.insert(relations, num)
			end
		end
	end
	table.sort(nodes)
	table.sort(ways)
	table.sort(relations)
	
	local elementLists = {}
	local rawElementLists = {}
	if #nodes > 0 then
		table.insert(elementLists, mw.ustring.format("%s %s", lang:plural(#nodes, {"node", "nodes"}), mw.text.listToText(nodes)))
		table.insert(rawElementLists, mw.ustring.format("node(id:%s);", table.concat(nodes, ",")))
	end
	if #ways > 0 then
		table.insert(elementLists, mw.ustring.format("%s %s", lang:plural(#ways, {"way", "ways"}), mw.text.listToText(ways)))
		table.insert(rawElementLists, mw.ustring.format("way(id:%s);", table.concat(ways, ",")))
	end
	if #relations > 0 then
		table.insert(elementLists, mw.ustring.format("%s %s", lang:plural(#relations, {"relation", "relations"}), mw.text.listToText(relations)))
		table.insert(rawElementLists, mw.ustring.format("relation(id:%s);", table.concat(relations, ",")))
	end
	
	local separator = (#nodes > 1 or #ways > 1 or #relations > 1) and ";" or ","
	local title = mw.ustring.format("Length of %s", mw.text.listToText(elementLists, separator))
	
	local date = args.date and lang:formatDate("F j, Y", args.date)
	local isoDate = args.date and lang:formatDate("c", args.date)
	local dateParam = isoDate and mw.ustring.format('[date:"%s"]', isoDate) or ""
	
	local queryFormat = [[[out:json][timeout:25]%s;
(%s);
make stats length=sum(length()) /* meters */;
out;]]
	local query = mw.ustring.format(queryFormat, dateParam, table.concat(rawElementLists))
	
	return frame:expandTemplate {
		title = "Template:Cite Overpass",
		args = {
			title = title,
			date = date,
			["access-date"] = args["access-date"] or args.accessdate,
			query = query,
		},
	}
end

return p