Module:Infobox mapframe: Difference between revisions
Jump to navigation
Jump to search
only allow shapes/lines based on wikidata id if there is an id |
No edit summary |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
local mf = require('Module:Mapframe') | local mf = require('Module:Mapframe') | ||
function setCleanArgs(argsTable) | |||
function | |||
local cleanArgs = {} | local cleanArgs = {} | ||
for key, val in pairs(argsTable) do | for key, val in pairs(argsTable) do | ||
Line 37: | Line 16: | ||
end | end | ||
function | function hasWikidataProperty(item_id, property_id) | ||
if not(item_id) or not(mw.wikibase.isValidEntityId(item_id)) or not(mw.wikibase.entityExists(item_id)) then | if not(item_id) or not(mw.wikibase.isValidEntityId(item_id)) or not(mw.wikibase.entityExists(item_id)) then | ||
return false | return false | ||
Line 49: | Line 28: | ||
return false | return false | ||
end | end | ||
return | return true | ||
end | end | ||
function getZoom(length_km) | |||
function getZoom( | |||
-- max for zoom 2 is 6400km, for zoom 3 is 3200km, for zoom 4 is 1600km, etc | -- max for zoom 2 is 6400km, for zoom 3 is 3200km, for zoom 4 is 1600km, etc | ||
local zoom = math.floor(8 - (math.log10(length_km) - 2)/(math.log10(2))) | local zoom = math.floor(8 - (math.log10(length_km) - 2)/(math.log10(2))) | ||
-- limit to values | -- limit to values between 1 and 17 | ||
return math.max(1, math.min(17, zoom)) | |||
end | end | ||
local p = {} | local p = {} | ||
p.main = function(frame) | p.main = function(frame) | ||
Line 179: | Line 49: | ||
p._main = function(_config) | p._main = function(_config) | ||
local config = setCleanArgs(_config) | |||
local config = | |||
local wikidataId = config.id or mw.wikibase.getEntityIdForCurrentPage() | local wikidataId = config.id or mw.wikibase.getEntityIdForCurrentPage() | ||
if not | if not wikidataId then | ||
return '' | return '' | ||
end | end | ||
-- Require coords | -- Require wikidata item with coords, so something will be centred somewhere | ||
local hasCoordinates = hasWikidataProperty(wikidataId, 'P625') -- P625 = coordinate location | |||
local hasCoordinates = hasWikidataProperty(wikidataId, 'P625') | |||
if not hasCoordinates then | if not hasCoordinates then | ||
return '' | return '' | ||
end | end | ||
-- | -- arguments for mapframe module | ||
local args = {} | local args = {} | ||
Line 202: | Line 68: | ||
args.frame = "yes" | args.frame = "yes" | ||
args.plain = "yes" | args.plain = "yes" | ||
args["frame-width"] = config["frame-width"] or | args["frame-width"] = config["frame-width"] or "270" | ||
args["frame-height"] = config["frame-height"] or | args["frame-height"] = config["frame-height"] or "200" | ||
args["frame-align"] = "center" | args["frame-align"] = "center" | ||
args["frame-coord"] = config["frame-coordinates"] or config["frame-coord"] or config["coordinates"] or config["coord"] or "" | |||
args["frame-coord"] = config["frame-coordinates"] or config["frame-coord"] or | -- deprecated lat and long parameters | ||
args["frame-lat"] = config["frame-lat"] or config["frame-latitude"] or "" | args["frame-lat"] = config["frame-lat"] or config["frame-latitude"] or "" | ||
args["frame-long"] = config["frame-long"] or config["frame-longitude"] or "" | args["frame-long"] = config["frame-long"] or config["frame-longitude"] or "" | ||
Line 216: | Line 78: | ||
-- Calculate zoom from length or area (converted to km or km2) | -- Calculate zoom from length or area (converted to km or km2) | ||
if config.length_km then | if config.length_km then | ||
args.zoom = getZoom(config.length_km | args.zoom = getZoom(tonumber(config.length_km)) | ||
elseif config.length_mi then | elseif config.length_mi then | ||
args.zoom = getZoom(config.length_mi | args.zoom = getZoom(tonumber(config.length_mi)*1.609344) | ||
elseif config.area_km2 then | elseif config.area_km2 then | ||
args.zoom = getZoom(config.area_km2 | args.zoom = getZoom(math.sqrt(tonumber(config.area_km2))) | ||
elseif config.area_mi2 then | elseif config.area_mi2 then | ||
args.zoom = getZoom(config.area_mi2 | args.zoom = getZoom(math.sqrt(tonumber(config.area_mi2))*1.609344) | ||
else | else | ||
args.zoom = config.zoom or | args.zoom = config.zoom or 10 | ||
end | end | ||
-- | -- Shape | ||
args.type = "shape" | |||
if config.id then args.id = config.id end | |||
args["stroke-width"] = config["shape-stroke-width"] or config["stroke-width"] or "3" | |||
args["stroke-color"] = config["shape-stroke-color"] or config["shape-stroke-colour"] or config["stroke-color"] or config["stroke-colour"] or "#FF0000" | |||
-- Line | -- Line | ||
args.type2 = "line" | |||
if config.id then args.id2 = config.id end | |||
args["stroke-width2"] = config["line-stroke-width"] or config["stroke-width"] or "5" | |||
args["stroke-color2"] = config["line-stroke-color"] or config["line-stroke-colour"] or config["stroke-color"] or config["stroke-colour"] or "#FF0000" | |||
-- Point | -- Point | ||
local hasOsmRelationId = hasWikidataProperty(wikidataId, 'P402') -- P402 is OSM relation ID | |||
local shouldShowPointMarker = not(hasOsmRelationId) or (config.marker and config.marker ~= 'none') | |||
if shouldShowPointMarker then | if shouldShowPointMarker then | ||
args | args.type3 = "point" | ||
if config.id then args | if config.id then args.id3 = config.id end | ||
if config.marker then args.marker3 = config.marker end | |||
if config.marker then args | args["marker-color3"] = config["marker-color"] or config["marker-colour"] or "#5E74F3" | ||
args["marker- | |||
end | end | ||
local mapframe = mf._main(args) | |||
local mapframe = | |||
local tracking = hasOsmRelationId and '' or '[[Category:Infobox mapframe without OSM relation ID on Wikidata]]' | local tracking = hasOsmRelationId and '' or '[[Category:Infobox mapframe without OSM relation ID on Wikidata]]' | ||
return mapframe .. tracking | return mapframe .. tracking |
Latest revision as of 20:48, 17 April 2024
This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
This Lua module is used on approximately 427,000 pages, or roughly 3576% of all pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
Related pages |
---|
Usage
Module that automatically makes a mapframe suitable for an infobox automatically, with minimal user input.
Generic usage
Just use the template {{Infobox mapframe}}, following the documentation there. This module may also be imported to another Lua module.
Automatic maps in infoboxes
- Example edits: Template:Infobox prison, Template:Infobox prison/doc
Edit the infobox template (or its sandbox). Add lines like the following examples to the infobox. Replace numbers (4, or 97 to 99), with the appropriate number based on how many other image or data parameters are already present.
If placing near the top of infobox | If placing at/near the bottom of the infobox |
---|---|
| image4 = {{#invoke:Infobox mapframe|auto}} | caption4 = {{#invoke:Infobox mapframe|autocaption}} |
| header97 = {{#if:{{{mapframe|}}}|Location}} | data98 = {{#invoke:Infobox mapframe|auto}} | data99 = {{#invoke:Infobox mapframe|autocaption}} |
If the template has a {{#invoke:Check for unknown parameters}}
, add the following parameters:
| mapframe | mapframe-area_km2 | mapframe-area_mi2 | mapframe-caption | mapframe-coord | mapframe-coordinates | mapframe-custom | mapframe-frame-coord | mapframe-frame-coordinates | mapframe-frame-height | mapframe-frame-width | mapframe-geomask | mapframe-geomask-fill | mapframe-geomask-fill-opacity | mapframe-geomask-stroke-color | mapframe-geomask-stroke-colour | mapframe-geomask-stroke-width | mapframe-height | mapframe-id | mapframe-length_km | mapframe-length_mi | mapframe-marker | mapframe-marker-color | mapframe-marker-colour | mapframe-point | mapframe-shape | mapframe-shape-fill | mapframe-shape-fill-opacity | mapframe-stroke-color | mapframe-stroke-colour | mapframe-stroke-width | mapframe-switcher | mapframe-width | mapframe-wikidata | mapframe-zoom
Once this is done, the above parameters will be available to users of the template.
- Defaults values for these parameters can also be specified in the #invoke calls above, e.g.
{{#invoke:Infobox mapframe|auto|mapframe-marker=library}}
means that the library marker will be used, unless a different value is passed in to the template. - The maps are off by default, which means maps will not be displayed unless
|mapframe=yes
is present in the template call. To turn maps on by default, in the #invoke calls above add|onByDefault=yes
– which means maps will be displayed unless|mapframe=no
is present in the template call. onByDefault can also be set to a conditional, such as if another parameters is present, e.g.|onByDefault={{#if:{{{pushpin_map|}}}|no|yes}}
- Add the new parameters to the document. You can use
{{Infobox mapframe/doc/parameters}}
. Specify default values (if any are used in the #invoke calls) using by adding parameters in the form Template:Ttparameter-nameTemplate:Ttvalue. The default output is shown here collapsed:
Extended content
|
---|
|
- Parameters can also be added to the TemplateData can also be added by copy-pasting from
See also
local mf = require('Module:Mapframe')
function setCleanArgs(argsTable)
local cleanArgs = {}
for key, val in pairs(argsTable) do
if type(val) == 'string' then
val = val:match('^%s*(.-)%s*$')
if val ~= '' then
cleanArgs[key] = val
end
else
cleanArgs[key] = val
end
end
return cleanArgs
end
function hasWikidataProperty(item_id, property_id)
if not(item_id) or not(mw.wikibase.isValidEntityId(item_id)) or not(mw.wikibase.entityExists(item_id)) then
return false
end
local statements = mw.wikibase.getBestStatements(item_id, property_id)
if not statements or #statements == 0 then
return false
end
local hasNoValue = ( statements[1].mainsnak and statements[1].mainsnak.snaktype == 'novalue' )
if hasNoValue then
return false
end
return true
end
function getZoom(length_km)
-- max for zoom 2 is 6400km, for zoom 3 is 3200km, for zoom 4 is 1600km, etc
local zoom = math.floor(8 - (math.log10(length_km) - 2)/(math.log10(2)))
-- limit to values between 1 and 17
return math.max(1, math.min(17, zoom))
end
local p = {}
p.main = function(frame)
local parent = frame.getParent(frame)
local parentArgs = parent.args
local mapframe = p._main(parentArgs)
return frame:preprocess(mapframe)
end
p._main = function(_config)
local config = setCleanArgs(_config)
local wikidataId = config.id or mw.wikibase.getEntityIdForCurrentPage()
if not wikidataId then
return ''
end
-- Require wikidata item with coords, so something will be centred somewhere
local hasCoordinates = hasWikidataProperty(wikidataId, 'P625') -- P625 = coordinate location
if not hasCoordinates then
return ''
end
-- arguments for mapframe module
local args = {}
-- Some defaults/overrides for infobox presentation
args.display = "inline"
args.frame = "yes"
args.plain = "yes"
args["frame-width"] = config["frame-width"] or "270"
args["frame-height"] = config["frame-height"] or "200"
args["frame-align"] = "center"
args["frame-coord"] = config["frame-coordinates"] or config["frame-coord"] or config["coordinates"] or config["coord"] or ""
-- deprecated lat and long parameters
args["frame-lat"] = config["frame-lat"] or config["frame-latitude"] or ""
args["frame-long"] = config["frame-long"] or config["frame-longitude"] or ""
-- Calculate zoom from length or area (converted to km or km2)
if config.length_km then
args.zoom = getZoom(tonumber(config.length_km))
elseif config.length_mi then
args.zoom = getZoom(tonumber(config.length_mi)*1.609344)
elseif config.area_km2 then
args.zoom = getZoom(math.sqrt(tonumber(config.area_km2)))
elseif config.area_mi2 then
args.zoom = getZoom(math.sqrt(tonumber(config.area_mi2))*1.609344)
else
args.zoom = config.zoom or 10
end
-- Shape
args.type = "shape"
if config.id then args.id = config.id end
args["stroke-width"] = config["shape-stroke-width"] or config["stroke-width"] or "3"
args["stroke-color"] = config["shape-stroke-color"] or config["shape-stroke-colour"] or config["stroke-color"] or config["stroke-colour"] or "#FF0000"
-- Line
args.type2 = "line"
if config.id then args.id2 = config.id end
args["stroke-width2"] = config["line-stroke-width"] or config["stroke-width"] or "5"
args["stroke-color2"] = config["line-stroke-color"] or config["line-stroke-colour"] or config["stroke-color"] or config["stroke-colour"] or "#FF0000"
-- Point
local hasOsmRelationId = hasWikidataProperty(wikidataId, 'P402') -- P402 is OSM relation ID
local shouldShowPointMarker = not(hasOsmRelationId) or (config.marker and config.marker ~= 'none')
if shouldShowPointMarker then
args.type3 = "point"
if config.id then args.id3 = config.id end
if config.marker then args.marker3 = config.marker end
args["marker-color3"] = config["marker-color"] or config["marker-colour"] or "#5E74F3"
end
local mapframe = mf._main(args)
local tracking = hasOsmRelationId and '' or '[[Category:Infobox mapframe without OSM relation ID on Wikidata]]'
return mapframe .. tracking
end
return p