Modul:Mapframe: Forskjell mellom sideversjoner
Hopp til navigering
Hopp til søk
(Legger til useLanguage for Syria) |
(Noen deklarasjoner manglet local og bedrer rekkefølge på funksjoner) |
||
Linje 19: | Linje 19: | ||
local p = {} |
local p = {} |
||
function lonlat(args) |
local function lonlat(args) |
||
local newargs = args |
local newargs = args |
||
if not args["lat"] and not args["lon"] then |
if not args["lat"] and not args["lon"] then |
||
Linje 30: | Linje 30: | ||
end |
end |
||
function |
local function selectSingleClaim(claims) |
||
if not claims then |
|||
-- return "<pre>" .. mw.text.jsonEncode(frame:getParent().args, mw.text.JSON_PRETTY) .. "</pre>" |
|||
return nil |
|||
local args = lonlat(mw.getCurrentFrame():getParent().args) |
|||
end |
|||
local argframe = mw.getCurrentFrame().args |
|||
local selectedClaim = nil |
|||
local vis = argframe["vis"] or "" |
|||
for idx,claim in pairs(claims) do |
|||
if vis == "nei" then |
|||
if claim.rank == 'preferred' then |
|||
return "" |
|||
return claim |
|||
end |
|||
if claim.rank == 'normal' then |
|||
if not selectedClaim then |
|||
selectedClaim = claim |
|||
end |
|||
end |
|||
end |
|||
return selectedClaim |
|||
end |
|||
local function wdCoords(entity) |
|||
local wdlat,wdlon = nil,nil |
|||
if entity.claims and entity.claims["P625"] then |
|||
local claim = selectSingleClaim(entity.claims["P625"]) |
|||
local coord = claim.mainsnak.datavalue.value or nil |
|||
wdlat = coord["latitude"] or nil |
|||
wdlon = coord["longitude"] or nil |
|||
end |
|||
return wdlat,wdlon |
|||
end |
|||
local function wdGeoShape(entity) |
|||
local shape = nil |
|||
if entity.claims and entity.claims["P3896"] then |
|||
local claim = selectSingleClaim(entity.claims["P3896"]) |
|||
local value = claim.mainsnak.datavalue.value or nil |
|||
if value then |
|||
local shape = { |
|||
["type"] = "ExternalData", |
|||
["service"] = "page", |
|||
["title"] = value |
|||
} |
|||
return shape |
|||
end |
|||
end |
|||
return nil |
|||
end |
|||
local function selectMapLanguage(entity) |
|||
local lang = "local" |
|||
if entity.claims and entity.claims["P17"] then |
|||
local claim = selectSingleClaim(entity.claims["P17"]) |
|||
if claim.mainsnak.datavalue then |
|||
local value = claim.mainsnak.datavalue.value or nil |
|||
if value then |
|||
lang = useLanguage[value["id"]] or "local" |
|||
end |
|||
end |
|||
end |
|||
return lang |
|||
end |
|||
local function mappoint(latitude,longitude,title,description) |
|||
local point = {} |
|||
point["type"] = "Feature" |
|||
local geo = {} |
|||
geo["type"] = "Point" |
|||
local pos = {} |
|||
geo["coordinates"] = pos |
|||
-- ensure the coordinates are not too long for the geojson |
|||
geo["coordinates"][1] = math_mod._round( longitude, 6 ) |
|||
geo["coordinates"][2] = math_mod._round( latitude, 6 ) |
|||
local prop = {} |
|||
if title then |
|||
prop["title"] = title |
|||
end |
end |
||
if description then |
|||
args["width"] = args["width"] or argframe["width"] or 250 |
|||
prop["description"] = description |
|||
args["zoom"] = tonumber(args["zoom"]) or tonumber(argframe["zoom"]) or 8 |
|||
end |
|||
prop["marker-size"] = "small" |
|||
point["properties"] = prop |
|||
local maptxt = map2(args,1) |
|||
point["geometry"] = geo |
|||
if not maptxt or maptxt =="" then |
|||
return point |
|||
end |
|||
local function geoline(id,stroke) |
|||
local line = { |
|||
["type"] = "ExternalData", |
|||
["service"] = "geoline", |
|||
["ids"] = id, |
|||
['properties'] = { |
|||
["stroke"] = stroke, |
|||
["stroke-width"]= 2 |
|||
} |
|||
} |
|||
return line |
|||
end |
|||
local function geoshape(id,stroke,opacity) |
|||
local line = { |
|||
["type"] = "ExternalData", |
|||
["service"] = "geoshape", |
|||
["ids"] = id, |
|||
['properties'] = { |
|||
["stroke"] = stroke, |
|||
["stroke-width"]= 2, |
|||
["fill-opacity"] = opacity or 0.1 |
|||
} |
|||
} |
|||
return line |
|||
end |
|||
local function includeProp(geojson,entity,prop) |
|||
if not entity or not prop or not entity.claims then |
|||
return nil |
|||
end |
end |
||
local claims = entity.claims[prop] or nil |
|||
return maptxt |
|||
if not claims then |
|||
return nil |
|||
end |
|||
local stroke = "#880000" |
|||
for idx,claim in pairs(claims) do |
|||
local qualifiers = claim.qualifiers |
|||
local endtime = nil |
|||
if qualifiers then |
|||
endtime = claim.qualifiers["P582"] or nil |
|||
end |
|||
-- todo: Test on end time simplified. This assumes that all end times are in the past |
|||
if not endtime then |
|||
local id = claim.mainsnak.datavalue.value['id'] or nil |
|||
visEntity(geojson,id,stroke,0.1) |
|||
end |
|||
end |
|||
return geojson |
|||
end |
end |
||
function mapNoWikidata(args,infoboks) |
|||
local function showEntity(geojson,id,entity,stroke,title,opacity,createLink) |
|||
local wdlat,wdlon = wdCoords(entity) |
|||
local text = title or mw.wikibase.label(id) |
|||
local link = mw.wikibase.sitelink(id) |
|||
if createLink and text and link then |
|||
text = "[[" .. link .. "|" .. text .. "]]" |
|||
end |
|||
local description = nil |
|||
if createLink then |
|||
if entity.claims and entity.claims["P18"] then |
|||
local claim = selectSingleClaim(entity.claims["P18"]) |
|||
local image = claim.mainsnak.datavalue.value or nil |
|||
if image then |
|||
description = "[[File:" .. image .. "]]" |
|||
end |
|||
end |
|||
end |
|||
local shape = geoshape(id,stroke,opacity) |
|||
table.insert(geojson, shape) |
|||
local line = geoline(id,stroke) |
|||
table.insert(geojson, line) |
|||
if wdlat and wdlon then |
|||
local point = mappoint(wdlat,wdlon,text,description) |
|||
table.insert(geojson, point) |
|||
end |
|||
return geojson |
|||
end |
|||
-- Visualize an entity through boundary, marker with name of entity. geojson updated by showEntity |
|||
local function visEntity(geojson,id,stroke,opacity) |
|||
if not id then |
|||
return nil |
|||
end |
|||
local entity = mw.wikibase.getEntity(id) |
|||
if not entity then |
|||
return nil |
|||
end |
|||
return showEntity(geojson,id,entity,stroke,nil,opacity,"createLink") |
|||
end |
|||
local function mapNoWikidata(args,infoboks) |
|||
local geojson = {} |
local geojson = {} |
||
local zoom = args["zoom"] or 8 |
local zoom = args["zoom"] or 8 |
||
Linje 88: | Linje 248: | ||
end |
end |
||
function |
local function map2(args,infobox) |
||
-- return "<pre>" .. mw.text.jsonEncode(frame:getParent().args, mw.text.JSON_PRETTY) .. "</pre>" |
|||
return map2(mw.getCurrentFrame():getParent().args) |
|||
end |
|||
function map2(args,infobox) |
|||
local id = args['id'] or args[1] or nil |
local id = args['id'] or args[1] or nil |
||
if id == "" then |
if id == "" then |
||
Linje 131: | Linje 286: | ||
local geojson = {} |
local geojson = {} |
||
local stroke = "#ff0000" |
local stroke = "#ff0000" |
||
local title = nil |
|||
local opacity = 0.1 |
local opacity = 0.1 |
||
if includeProperty then |
if includeProperty then |
||
Linje 164: | Linje 320: | ||
['lang'] = lang |
['lang'] = lang |
||
} |
} |
||
if frameless then |
|||
frameargs['frameless'] = 'frameless' |
|||
end |
|||
local mlink = '' |
local mlink = '' |
||
if text then |
if text then |
||
Linje 181: | Linje 334: | ||
end |
end |
||
function |
function p.infoboxLocation(frame) |
||
-- return "<pre>" .. mw.text.jsonEncode(frame:getParent().args, mw.text.JSON_PRETTY) .. "</pre>" |
|||
if not entity or not prop or not entity.claims then |
|||
local args = lonlat(mw.getCurrentFrame():getParent().args) |
|||
return nil |
|||
local argframe = mw.getCurrentFrame().args |
|||
local vis = argframe["vis"] or "" |
|||
if vis == "nei" then |
|||
return "" |
|||
end |
|||
args["width"] = args["width"] or argframe["width"] or 250 |
|||
args["zoom"] = tonumber(args["zoom"]) or tonumber(argframe["zoom"]) or 8 |
|||
args[1] = "" |
|||
args["align"] = "center" |
|||
local maptxt = map2(args,1) |
|||
if not maptxt or maptxt =="" then |
|||
return mapNoWikidata(args,1) |
|||
end |
end |
||
return maptxt |
|||
local claims = entity.claims[prop] or nil |
|||
if not claims then |
|||
return nil |
|||
end |
|||
local stroke = "#880000" |
|||
for idx,claim in pairs(claims) do |
|||
local qualifiers = claim.qualifiers |
|||
local endtime = nil |
|||
if qualifiers then |
|||
endtime = claim.qualifiers["P582"] or nil |
|||
end |
|||
-- todo: Test on end time simplified. This assumes that all end times are in the past |
|||
if not endtime then |
|||
local id = claim.mainsnak.datavalue.value['id'] or nil |
|||
visEntity(geojson,id,stroke,0.1) |
|||
end |
|||
end |
|||
return geojson |
|||
end |
end |
||
function p.map(frame) |
|||
-- Visualize an entity through boundary, marker with name of entity. geojson updated by showEntity |
|||
-- return "<pre>" .. mw.text.jsonEncode(frame:getParent().args, mw.text.JSON_PRETTY) .. "</pre>" |
|||
function visEntity(geojson,id,stroke,opacity) |
|||
return map2(mw.getCurrentFrame():getParent().args) |
|||
if not id then |
|||
return nil |
|||
end |
|||
local entity = mw.wikibase.getEntity(id) |
|||
if not entity then |
|||
return nil |
|||
end |
|||
return showEntity(geojson,id,entity,stroke,nil,opacity,"createLink") |
|||
end |
|||
function showEntity(geojson,id,entity,stroke,title,opacity,createLink) |
|||
local wdlat,wdlon = wdCoords(entity) |
|||
local text = title or mw.wikibase.label(id) |
|||
local link = mw.wikibase.sitelink(id) |
|||
if createLink and text and link then |
|||
text = "[[" .. link .. "|" .. text .. "]]" |
|||
end |
|||
description = nil |
|||
if createLink then |
|||
if entity.claims and entity.claims["P18"] then |
|||
local claim = selectSingleClaim(entity.claims["P18"]) |
|||
local image = claim.mainsnak.datavalue.value or nil |
|||
if image then |
|||
description = "[[File:" .. image .. "]]" |
|||
end |
|||
end |
|||
end |
|||
local shape = geoshape(id,stroke,opacity) |
|||
table.insert(geojson, shape) |
|||
local line = geoline(id,stroke) |
|||
table.insert(geojson, line) |
|||
if wdlat and wdlon then |
|||
local point = mappoint(wdlat,wdlon,text,description) |
|||
table.insert(geojson, point) |
|||
end |
|||
return geojson |
|||
end |
|||
function wdCoords(entity) |
|||
local wdlat,wdlon = nil,nil |
|||
if entity.claims and entity.claims["P625"] then |
|||
local claim = selectSingleClaim(entity.claims["P625"]) |
|||
local coord = claim.mainsnak.datavalue.value or nil |
|||
wdlat = coord["latitude"] or nil |
|||
wdlon = coord["longitude"] or nil |
|||
end |
|||
return wdlat,wdlon |
|||
end |
|||
function wdGeoShape(entity) |
|||
local shape = nil |
|||
if entity.claims and entity.claims["P3896"] then |
|||
local claim = selectSingleClaim(entity.claims["P3896"]) |
|||
local value = claim.mainsnak.datavalue.value or nil |
|||
if value then |
|||
local shape = { |
|||
["type"] = "ExternalData", |
|||
["service"] = "page", |
|||
["title"] = value |
|||
} |
|||
return shape |
|||
end |
|||
end |
|||
return nil |
|||
end |
|||
function selectMapLanguage(entity) |
|||
local lang = "local" |
|||
if entity.claims and entity.claims["P17"] then |
|||
local claim = selectSingleClaim(entity.claims["P17"]) |
|||
if claim.mainsnak.datavalue then |
|||
local value = claim.mainsnak.datavalue.value or nil |
|||
if value then |
|||
lang = useLanguage[value["id"]] or "local" |
|||
end |
|||
end |
|||
end |
|||
return lang |
|||
end |
|||
function selectSingleClaim(claims) |
|||
if not claims then |
|||
return nil |
|||
end |
|||
local selectedClaim = nil |
|||
for idx,claim in pairs(claims) do |
|||
if claim.rank == 'preferred' then |
|||
return claim |
|||
end |
|||
if claim.rank == 'normal' then |
|||
if not selectedClaim then |
|||
selectedClaim = claim |
|||
end |
|||
end |
|||
end |
|||
return selectedClaim |
|||
end |
|||
function mappoint(latitude,longitude,title,description) |
|||
local point = {} |
|||
point["type"] = "Feature" |
|||
local geo = {} |
|||
geo["type"] = "Point" |
|||
local pos = {} |
|||
geo["coordinates"] = pos |
|||
-- ensure the coordinates are not too long for the geojson |
|||
geo["coordinates"][1] = math_mod._round( longitude, 6 ) |
|||
geo["coordinates"][2] = math_mod._round( latitude, 6 ) |
|||
local prop = {} |
|||
if title then |
|||
prop["title"] = title |
|||
end |
|||
if description then |
|||
prop["description"] = description |
|||
end |
|||
prop["marker-size"] = "small" |
|||
point["properties"] = prop |
|||
point["geometry"] = geo |
|||
return point |
|||
end |
|||
function geoline(id,stroke) |
|||
local line = { |
|||
["type"] = "ExternalData", |
|||
["service"] = "geoline", |
|||
["ids"] = id, |
|||
['properties'] = { |
|||
["stroke"] = stroke, |
|||
["stroke-width"]= 2 |
|||
} |
|||
} |
|||
return line |
|||
end |
|||
function geoshape(id,stroke,opacity) |
|||
local line = { |
|||
["type"] = "ExternalData", |
|||
["service"] = "geoshape", |
|||
["ids"] = id, |
|||
['properties'] = { |
|||
["stroke"] = stroke, |
|||
["stroke-width"]= 2, |
|||
["fill-opacity"] = opacity or 0.1 |
|||
} |
|||
} |
|||
return line |
|||
end |
end |
||
Sideversjonen fra 11. des. 2019 kl. 02:48
![]() | Denne modulen brukes på over 70 000 sider. Det anbefales at endringer testes i en sandkasse før de legges inn. Diskuter gjerne også endringer på diskusjonssiden først. |
Denne modulen benyttes av kartmalen {{Kart}}. Se dokumentasjonssiden til denne malen for bruk og instruksjoner.
local math_mod = require( "Module:Math" ) local useLanguage = { ["Q17"] = "en", -- Japan ["Q79"] = "en", -- Egypt ["Q148"] = "en", -- Kina ["Q668"] = "en", -- India ["Q794"] = "en", -- Iran ["Q796"] = "en", -- Irak ["Q810"] = "en", -- Jordan ["Q843"] = "en", -- Pakistan ["Q851"] = "en", -- Saudi-Arabia ["Q858"] = "en", -- Syria ["Q865"] = "en", -- Republikken Kina - Taiwan ["Q889"] = "en" -- Afghanistan } local p = {} local function lonlat(args) local newargs = args if not args["lat"] and not args["lon"] then if args["breddegrad"] and args["lengdegrad"] then newargs["lat"] = tonumber(args["breddegrad"]) or nil newargs["lon"] = tonumber(args["lengdegrad"]) or nil end end return newargs end local function selectSingleClaim(claims) if not claims then return nil end local selectedClaim = nil for idx,claim in pairs(claims) do if claim.rank == 'preferred' then return claim end if claim.rank == 'normal' then if not selectedClaim then selectedClaim = claim end end end return selectedClaim end local function wdCoords(entity) local wdlat,wdlon = nil,nil if entity.claims and entity.claims["P625"] then local claim = selectSingleClaim(entity.claims["P625"]) local coord = claim.mainsnak.datavalue.value or nil wdlat = coord["latitude"] or nil wdlon = coord["longitude"] or nil end return wdlat,wdlon end local function wdGeoShape(entity) local shape = nil if entity.claims and entity.claims["P3896"] then local claim = selectSingleClaim(entity.claims["P3896"]) local value = claim.mainsnak.datavalue.value or nil if value then local shape = { ["type"] = "ExternalData", ["service"] = "page", ["title"] = value } return shape end end return nil end local function selectMapLanguage(entity) local lang = "local" if entity.claims and entity.claims["P17"] then local claim = selectSingleClaim(entity.claims["P17"]) if claim.mainsnak.datavalue then local value = claim.mainsnak.datavalue.value or nil if value then lang = useLanguage[value["id"]] or "local" end end end return lang end local function mappoint(latitude,longitude,title,description) local point = {} point["type"] = "Feature" local geo = {} geo["type"] = "Point" local pos = {} geo["coordinates"] = pos -- ensure the coordinates are not too long for the geojson geo["coordinates"][1] = math_mod._round( longitude, 6 ) geo["coordinates"][2] = math_mod._round( latitude, 6 ) local prop = {} if title then prop["title"] = title end if description then prop["description"] = description end prop["marker-size"] = "small" point["properties"] = prop point["geometry"] = geo return point end local function geoline(id,stroke) local line = { ["type"] = "ExternalData", ["service"] = "geoline", ["ids"] = id, ['properties'] = { ["stroke"] = stroke, ["stroke-width"]= 2 } } return line end local function geoshape(id,stroke,opacity) local line = { ["type"] = "ExternalData", ["service"] = "geoshape", ["ids"] = id, ['properties'] = { ["stroke"] = stroke, ["stroke-width"]= 2, ["fill-opacity"] = opacity or 0.1 } } return line end local function includeProp(geojson,entity,prop) if not entity or not prop or not entity.claims then return nil end local claims = entity.claims[prop] or nil if not claims then return nil end local stroke = "#880000" for idx,claim in pairs(claims) do local qualifiers = claim.qualifiers local endtime = nil if qualifiers then endtime = claim.qualifiers["P582"] or nil end -- todo: Test on end time simplified. This assumes that all end times are in the past if not endtime then local id = claim.mainsnak.datavalue.value['id'] or nil visEntity(geojson,id,stroke,0.1) end end return geojson end local function showEntity(geojson,id,entity,stroke,title,opacity,createLink) local wdlat,wdlon = wdCoords(entity) local text = title or mw.wikibase.label(id) local link = mw.wikibase.sitelink(id) if createLink and text and link then text = "[[" .. link .. "|" .. text .. "]]" end local description = nil if createLink then if entity.claims and entity.claims["P18"] then local claim = selectSingleClaim(entity.claims["P18"]) local image = claim.mainsnak.datavalue.value or nil if image then description = "[[File:" .. image .. "]]" end end end local shape = geoshape(id,stroke,opacity) table.insert(geojson, shape) local line = geoline(id,stroke) table.insert(geojson, line) if wdlat and wdlon then local point = mappoint(wdlat,wdlon,text,description) table.insert(geojson, point) end return geojson end -- Visualize an entity through boundary, marker with name of entity. geojson updated by showEntity local function visEntity(geojson,id,stroke,opacity) if not id then return nil end local entity = mw.wikibase.getEntity(id) if not entity then return nil end return showEntity(geojson,id,entity,stroke,nil,opacity,"createLink") end local function mapNoWikidata(args,infoboks) local geojson = {} local zoom = args["zoom"] or 8 local lat = args["latitude"] or args["lat"] local lon = args["longitude"] or args["lon"] if not lat or not lon then return "" end local point = mappoint(lat,lon) local width = tonumber(args["width"]) or 250 local height = tonumber(args["height"]) or width local text = args["text"] or "" table.insert(geojson, point) local frameargs = { ['height'] = height, ['width'] = width, ['align'] = align } if zoom then frameargs['zoom'] = zoom frameargs['latitude'] = lat frameargs['longitude'] = lon end local linkargs = { ['height'] = height, ['width'] = width, ['align'] = align, ['latitude'] = lat, ['longitude'] = lon, ['zoom'] = zoom or 8 } local mlink = '' if infobox then mlink = '<br/>' .. mw.getCurrentFrame():extensionTag('maplink', mw.text.jsonEncode(geojson), linkargs) end frameargs['text'] = text .. mlink return mw.getCurrentFrame():extensionTag('mapframe', mw.text.jsonEncode(geojson), frameargs) end local function map2(args,infobox) local id = args['id'] or args[1] or nil if id == "" then id = nil end local entity = mw.wikibase.getEntity(id) or nil if not id then if not entity then return "" end id = entity.id end local width = args["width"] or nil if width ~= "full" then width = tonumber(args["width"]) or 350 end local height = tonumber(args["height"]) or width if height == "full" then return "" end local zoom = args["zoom"] or 8 local lat = args["latitude"] or args["lat"] local lon = args["longitude"] or args["lon"] local includeProperty = args["include property"] or nil local wdlat,wdlon = wdCoords(entity) if not lat or not lon then lat = wdlat lon = wdlon end if not lon or not lat then return "" end local text = args["text"] or mw.wikibase.label(id) or "" local align = args["align"] or 'right' local lang = selectMapLanguage(entity) local geojson = {} local stroke = "#ff0000" local title = nil local opacity = 0.1 if includeProperty then opacity = 0.0 end showEntity(geojson,id,entity,stroke,title,opacity) if args['marker'] and lat and lon then local point = mappoint(lat,lon) table.insert(geojson, point) end if includeProperty then includeProp(geojson,entity,includeProperty) end local frameargs = { ['height'] = height, ['width'] = width, ['align'] = align, ['lang'] = lang } if zoom then frameargs['zoom'] = zoom frameargs['latitude'] = lat frameargs['longitude'] = lon end local linkargs = { ['height'] = height, ['width'] = width, ['align'] = align, ['latitude'] = lat, ['longitude'] = lon, ['zoom'] = zoom or 8, ['lang'] = lang } local mlink = '' if text then if infobox then mlink = '<br/>' .. mw.getCurrentFrame():extensionTag('maplink', mw.text.jsonEncode(geojson), linkargs) frameargs['frameless'] = 'frameless' return mw.getCurrentFrame():extensionTag('mapframe', mw.text.jsonEncode(geojson), frameargs) .. '<br/>' .. text .. mlink end frameargs['text'] = text .. mlink end return mw.getCurrentFrame():extensionTag('mapframe', mw.text.jsonEncode(geojson), frameargs) -- return "<pre>" .. mw.text.jsonEncode(geojson, mw.text.JSON_PRETTY) .. "</pre>" end function p.infoboxLocation(frame) -- return "<pre>" .. mw.text.jsonEncode(frame:getParent().args, mw.text.JSON_PRETTY) .. "</pre>" local args = lonlat(mw.getCurrentFrame():getParent().args) local argframe = mw.getCurrentFrame().args local vis = argframe["vis"] or "" if vis == "nei" then return "" end args["width"] = args["width"] or argframe["width"] or 250 args["zoom"] = tonumber(args["zoom"]) or tonumber(argframe["zoom"]) or 8 args[1] = "" args["align"] = "center" local maptxt = map2(args,1) if not maptxt or maptxt =="" then return mapNoWikidata(args,1) end return maptxt end function p.map(frame) -- return "<pre>" .. mw.text.jsonEncode(frame:getParent().args, mw.text.JSON_PRETTY) .. "</pre>" return map2(mw.getCurrentFrame():getParent().args) end return p