Jump to content

Module:Geonotice map

From Wikipedia, the free encyclopedia

-- If the "country": "GB" feature is used instead of "corners", we need the Wikidata ID of the country for the map to use
local countries = {
	GB = "Q145",
	US = "Q30",
	CA = "Q16",
	MX = "Q96",
	JM = "Q766",
	TT = "Q754",
	GY = "Q734",
	GT = "Q774",
	DO = "Q786",
	BS = "Q778",
	CR = "Q800",
	BZ = "Q242",
	PA = "Q804",
	PH = "Q928",
}

local p = {}

function p.main(frame)
	local data = mw.loadJsonData("Wikipedia:Geonotice/list.json")
	local geojson = {}
	for name, info in pairs(data) do
		local properties = {
			title = name,
			description = info.text .. "<br>(Start: " .. info.begin .. ", end: " .. info["end"] .. ")"
		}
		local feature
		if info.country ~= nil then
			feature = {
				type = "ExternalData",
				service = "geoshape",
				ids = countries[info.country],
				properties = properties
			}
		else
			feature = {
				type = "Feature",
				properties = properties,
				geometry = {
					type = "Polygon",
					-- note that GeoJSON is lon, lat while Geonotice is lat, lon. sigh.
					coordinates = { {
						{ info.corners[1][2], info.corners[1][1] },
						{ info.corners[2][2], info.corners[1][1] },
						{ info.corners[2][2], info.corners[2][1] },
						{ info.corners[1][2], info.corners[2][1] },
						{ info.corners[1][2], info.corners[1][1] }
					} }
				}
			}
		end
		table.insert(geojson, feature)
	end
	local width = frame.args.width or "full"
	local height = frame.args.height or "500"
	return frame:extensionTag{ 
		name = 'mapframe', 
		content = mw.text.jsonEncode(geojson), 
		args = { 
			width = width, 
			height = height,
			text = "See a map of actively configured geonotices"
		}
	}
end

return p