Jump to content

Module:Football map

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Pppery (talk | contribs) at 21:54, 8 May 2018. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- All Lua modules on Wikipedia must begin by defining a variable that will hold their
-- externally accessible functions. They can have any name and may also hold data.
local p = {}


p.stadia = {
	-- stadium name , latitude, longitude 

	{ "White Hart Lane", 51.603333, -0.065833 },
	{ "Northumberland Development Project", 51.60475, -0.066417 },
	{ "Wembley Stadium", 51.555833, -0.279722 },
	{ "Emirates Stadium", 51.555, -0.108611 },
	{ "Arsenal Stadium", 51.557778, -0.102778 },
	{ "Stamford Bridge",  51.481667, -0.191111 },
	{ "London Stadium",  51.538611, -0.016389 },
	{ "Boleyn Ground",  51.531944, 0.039444 },
	{ "Craven Cottage",  51.475, -0.221667 },
	{ "The Valley (London)", 51.486389, 0.036389 },
	{ "Selhurst Park", 51.398333, -0.085556},
	{ "Kingsmeadow", 51.405083, -0.281944},
	{ "Vicarage Road", 51.649836, -0.401486},
	{ "The Hive Stadium", 51.602599, -0.291785},
	{ "Loftus Road", 51.509167, -0.232222},
	{ "Griffin Park", 51.488183, -0.302639},
	{ "Brisbane Road", 51.56015, -0.012658},
	{ "The Den", 51.485953, -0.05095},
	{ "The Old Den", 51.480883, -0.048175},
	{ "Wembley Stadium (1923)", 51.555556, -0.279722},
	{ "x", 51, 0}
}


p.clubs = {
	{ "Tottenham Hotspur F.C.", "White Hart Lane", 1899, 2017},
	{ "Tottenham Hotspur F.C.", "Northumberland Development Project", 2018, -1 },
	{ "Arsenal F.C.", "Emirates Stadium", 2006, -1 },
	{ "Arsenal F.C.", "Arsenal Stadium", 1913, 2006 },
	{ "Chelsea F.C.", "Stamford Bridge",  -1, -1 },
	{ "West Ham United F.C.", "London Stadium",  2016, -1 },
	{ "West Ham United F.C.", "Boleyn Ground",  1904, 2016 },
	{ "Fulham F.C.", "Craven Cottage",  -1, -1 },
	{ "Charlton Athletic F.C.", "The Valley (London)", -1, -1 },
	{ "Crystal Palace F.C.", "Selhurst Park", -1, -1},
	{ "A.F.C. Wimbledon", "Kingsmeadow", -1, -1},
	{ "Watford F.C.", "Vicarage Road", -1, -1},
	{ "Barnet F.C.", "The Hive Stadium", -1, -1},
	{ "Queens Park Rangers F.C.", "Loftus Road", -1, -1},
	{ "Brentford F.C.", "Griffin Park", -1, -1},
	{ "Leyton Orient F.C.", "Brisbane Road", -1, -1},
	{ "Millwall F.C.", "The Den", 1993, -1},
	{ "Millwall F.C.", "The Old Den", 1910, 1993},
	{ "England national team", "Wembley", 2007, -1 },
	{ "England national team", "Wembley Stadium (1923)", 1923, 2002}
}
p.aliases  = {
	
	-- name of wikipedia article, alias
	{"Northumberland Development Project", "New White Hart Lane"},
	{"Arsenal Stadium", "Highbury"},
	{"London Stadium", "Olympic Stadium"},
	{"Boleyn Ground", "Upton Park"}
}

-- Add a function to the variable. These are callable in Wikipedia via the #invoke command.
-- "frame" will contain the data that Wikipedia sends this function when it is called. 
p.hello = function(frame) 

    -- Declare a local variable and assign data to it.
    local str = "Hello World!"  
    --str = '<table><tr><td>Hello</td><td>World</td></tr><tr><td>in HTML</td><td>table</td></tr></table>'

    --str = p.getMapframeString()
    str = p.getTestJSONstring()

    -- Quit this function and send the information in "str" back to Wikipedia.
    -- The "print" function is not allowed, so all output is accomplished via 
    -- returning strings in this fashion.
    return str    
end  -- End the function.

-- main function
p.main = function(frame) 
	str = p.getMapframeString()
	return frame:preprocess(str)   -- the mapframe needs to be preprocessed!!!!!
end  -- End the function.

--[[ function to construct mapframe string
	The appropriate string with JSON between mapframe elements is generated correctly (test a copy),
	but it is displaces as the string rather than embedding the map 
	i.e. it is not behaving like normal HTML
]]
p.getMapframeString = function(frame) 

    --mapString = '<mapframe text="London football stadia" width=800 height=650 align=left zoom=11 latitude=51.530 longitude=-0.16 >'
    
    mapWidth = mw.getCurrentFrame():getParent().args['width'] or "400"
    mapHeight = mw.getCurrentFrame():getParent().args['height'] or "300"
    mapLatitude = mw.getCurrentFrame():getParent().args['latitude'] or "51.5"
    mapLongitude = mw.getCurrentFrame():getParent().args['longitude'] or "-0.15"
    mapAlign = mw.getCurrentFrame():getParent().args['align'] or "right"
    mapText = mw.getCurrentFrame():getParent().args['text'] or ""
    mapZoom = mw.getCurrentFrame():getParent().args['zoom'] or "9"
    
    
    mapData = p.getStadiumJSON()
    local mapString = ""
     	
    if mapData ~= "" then

	    mapString = '<mapframe text="' .. mapText ..'" width=' .. mapWidth .. ' height=' .. mapHeight 
	    mapString = mapString .. ' align=' .. mapAlign .. ' zoom=' .. mapZoom
	    mapString =mapString .. ' latitude=' .. mapLatitude .. ' longitude=' .. mapLongitude .. ' >'
	   
	    mapString = mapString .. mapData .. '</mapframe>'
    end
    
    return mapString    

end  -- End the function.

-- function to construct JSON string
p.getStadiumJSON = function(frame) 

    -- now we need to iterate through the stadiumN parameters and get data for the feature markers
    local maxNumber = 50 -- maximum number looked for
    local mapData = ""
    local stadiumName = ""
    
    local index=0
    while index < maxNumber do 
    	index = index + 1
	    stadiumName = mw.getCurrentFrame():getParent().args['stadium'..tostring(index)] or ""
	    local featureName=""
	    local featureData=""
	    local featureDescription=""
	    local featureLongitude=0
	    local featureLatitude=0
	    local stadiumText = mw.getCurrentFrame():getParent().args['text'..tostring(index)] or ""
	    local stadiumImage = mw.getCurrentFrame():getParent().args['image'..tostring(index)] or ""
	    
	    -- if we have retrieved a name from the |stadiumN parameter
	    if stadiumName ~= "" then  

		    -- check the stadium list for name match
		    for _, params in pairs( p.stadia ) do
		    	if stadiumName==params[1] then 
		    		featureName=params[1]
		    		featureLongitude = params[3]
		    		featureLatitude = params[2]
		    		-- we have a match from the list
		    		break
		        end
		    end
	    end
    
        -- if featureName set, we now have a stadium name match and coordinates from the stadium index list
        -- but what if the name doesn't match? Here we could seek info from wikiData
        if stadiumName ~= ""  and featureName == "" then 
        	--        	have a look in wikidata
        	local WikidataId = mw.wikibase.getEntityIdForTitle(stadiumName)
        	local item = mw.wikibase.getEntity(WikidataId)
        	local statements = item:getBestStatements('P625')[1] --coordinate location
        	local datavalue = statements.mainsnak.datavalue.value
        	
        	
        	-- TODO must add some error checking here
        	
        	
        	-- add data for unindexed stadium
        	featureName=stadiumName 
        	featureLatitude = statements.mainsnak.datavalue.value.latitude
        	featureLongitude = statements.mainsnak.datavalue.value.longitude
        	--featureDescription = "coordinate testing: " .. datavalue.latitude .. ', ' .. datavalue.longitude
     
        end
	    if featureName ~= "" then 
	    	
	    	featureDescription = stadiumImage .. stadiumText
	    	--mapData = mapStadium1
	    	featureData = '{  "type": "Feature",  "geometry": { "type": "Point", "coordinates": ['
	    	.. featureLongitude .. ',' .. featureLatitude .. '] }, "properties": { "title": "' .. featureName 
	    	.. '",  "description": "'..featureDescription..'", "marker-symbol": "soccer", "marker-size": "medium",  "marker-color": "0050d0"   }  } '
	    	
	    	if index > 1 then
	    	    mapData = mapData .. ',' .. featureData
	    	else
	    		mapData = featureData 
	    	end
	    else
	    	--mapData = '{  "type": "Feature",  "geometry": { "type": "Point", "coordinates": [-0.066417, 51.60475] }, "properties": { "title": "White Hart Lane (default)",  "description": "Stadium of Tottenham Hotspur F.C.", "marker-symbol": "soccer", "marker-size": "large",  "marker-color": "0050d0"   }  } '
		end
	 end -- end while loop
	 
	 if index > 1 then
	 	mapData = '[' .. mapData .. ']'
	 end
     
     return mapData
     
end -- End the function.

-- function to construct JSON string for WHL in London map
p.getTestJSONstring = function(frame) 
   
    return '{  "type": "Feature",  "geometry": { "type": "Point", "coordinates": [-0.066417, 51.60475] }, "properties": { "title": "White Hart Lane",  "description": "Stadium of Tottenham Hotspur F.C.", "marker-symbol": "soccer", "marker-size": "large",  "marker-color": "0050d0"   }  } '

end  -- End the function.

-- function to construct JSON string
p.getJSONstring = function(frame) 

    local stadiumName = mw.getCurrentFrame():getParent().args['stadium1'] or "default name"

    local str=stadiumName
    local jsonString = '{   "type": "Feature", '
    jsonString = jsonString .. ' "geometry": { "type": "Point", "coordinates": [-0.065833, 51.603333] }, '
    jsonString = jsonString .. ' "properties": {     "title": "[[White Hart Lane]]", '
    jsonString = jsonString .. ' "description": "[[File:White Hart Lane Aerial.jpg|150px]]Tottenham Hotspur Football Club (1899-2017)", '
    jsonString = jsonString .. ' "marker-symbol": "-number", "marker-size": "small", "marker-color": "dd50d0"  } } '
    
    --mapString = mapString ..  '</mapframe>'
   jsonString = '{  "type": "Feature",  "geometry": { "type": "Point", "coordinates": [-0.066417, 51.60475] }, "properties": { "title": "[[Northumberland Development Project]]",  "description": "[[File:NDProject2015.jpg|100px]]", "marker-symbol": "soccer", "marker-size": "large",  "marker-color": "0050d0"   }  } '
   
   jsonString = '{  "type": "Feature",  "geometry": { "type": "Point", "coordinates": [-0.066417, 51.60475] }, "properties": { "title": "title",  "description": "description", "marker-symbol": "soccer", "marker-size": "large",  "marker-color": "0050d0"   }  } '
    
    str = '<nowiki>' .. jsonString .. '</nowiki>'
    --str =  jsonString 

    return str    


end   -- End the function.

-- All modules end by returning the variable containing its functions to Wikipedia.
return p

-- We can now use this module by calling {{#invoke: HelloWorld | hello }}.
-- The #invoke command begins with the module's name, in this case "HelloWorld",
-- then takes the name of one of its functions as an argument, in this case "hello".