Jump to content

Module:OSM

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 06:22, 5 August 2015 (make variables local instead of global). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local function buildquery(frame, target)
	local itemID, query, coord, bbox, remark, overpassUrl, primitives
	if frame.args['id'] then
		-- build query for specific Q-item(s) 
		itemID = frame.args['id']
	else
		itemID = mw.wikibase.getEntityObject()
		if itemID == nil then
			return "This page doesn't have a wikidata entry"
		end
		-- build query for current page
		itemID = itemID.id
	end
	if itemID:find(";") == nil then
		-- If there are no ; in the itemID, we only need to search for 1 value
		itemID = '"="' .. itemID
	else
		-- If more than 1 Q-number is provided, perform a regular expression search instead
		itemID = '"~"' .. '[' .. itemID:gsub(";", "\|") .. ']'
	end
	if frame.args['query'] then
		query = frame.args['query']
	else
		query = ''
	end
	if frame.args['coord'] then
		-- The user can provide coordinates and a zoom factor
		coord = '&C=' .. frame.args['coord']
		-- In that case we can limit the search to the area in view
		bbox = '({{bbox}})'
		-- and tell them how to search wider.
		remark = ' // remove the ' .. bbox .. 'if you want the query to be executed globally'
	else
		coord = ''
		bbox = ''
		remark = ''
	end
	overpassUrl = '(\n'
	-- if the user specifies prim(itives), but then leaves the string empty, abort
	if frame.args['prim'] then
		if frame.args['prim']=='' then
			return "Please indicate which primitives you want to query for"
		end
		primitives = frame.args['prim']
	else
		primitives = 'nwr'
	end
	
	if primitives:find("n") then
		-- Include nodes
		overpassUrl = overpassUrl .. 'node["' .. target .. itemID ..'"]' .. query .. bbox .. ';' .. remark .. '\n'
	end
	if primitives:find("w") then
		-- Include ways
		overpassUrl = overpassUrl .. 'way["' .. target .. itemID .. '"]' .. query .. bbox .. ';\n'
	end
	if primitives:find("r") then
		-- Include relations
		overpassUrl = overpassUrl .. 'relation["' .. target .. itemID .. '"]' .. query .. bbox .. ';\n'
	end	
	overpassUrl = overpassUrl .. ');\n'
	overpassUrl = overpassUrl .. 'out;\n>;\n'
	overpassUrl = overpassUrl .. 'out meta qt;\n'
    return '[http://overpass-turbo.eu/?Q=' .. mw.uri.encode(overpassUrl, "PATH" ) .. coord .. '&R ' .. frame.args['linktext'] .. ']'
end

local p = {}

function p.wd( frame )
	return buildquery(frame, 'wikidata')
end

function p.etym( frame )
	return buildquery(frame, 'name:etymology:wikidata')
end

function p.subject( frame )
	return buildquery(frame, 'subject:wikidata')
end

function p.artist( frame )
	return buildquery(frame, 'artist:wikidata')
end

function p.architect( frame )
	return buildquery(frame, 'architect:wikidata')
end

function p.operator( frame )
	return buildquery(frame, 'operator:wikidata')
end

function p.brand( frame )
	return buildquery(frame, 'brand:wikidata')
end

return p