Module:Titled coords/sandbox: Difference between revisions
Appearance
Content deleted Content added
strict lua fix |
cleanup |
||
Line 3: | Line 3: | ||
-- Module entry point |
-- Module entry point |
||
function p.build( |
function p.build(rawCoords, title, options) |
||
local has_seconds = |
local has_seconds = true |
||
local output = "" |
local output = "" |
||
--local lat_d, lat_m, lat_s, lat_NS, lon_d, lon_m, lon_s, lon_WE |
|||
if not title then title = "" end |
if not title then title = "" end |
||
Line 13: | Line 12: | ||
-- ** Display the raw coordinates obtained from {{wikidata}} ** |
-- ** Display the raw coordinates obtained from {{wikidata}} ** |
||
if string.find(options,"raw") ~= nil then |
if string.find(options,"raw") ~= nil then |
||
output = output .. " |
output = output .. "rawCoords:" .. rawCoords |
||
end |
end |
||
Line 19: | Line 18: | ||
-- ** See https://www.lua.org/manual/5.3/manual.html#6.4.1 ** |
-- ** See https://www.lua.org/manual/5.3/manual.html#6.4.1 ** |
||
local sidx, _, lat_d, lat_m, lat_s, lat_NS, lon_d, lon_m, lon_s, lon_WE = |
local sidx, _, lat_d, lat_m, lat_s, lat_NS, lon_d, lon_m, lon_s, lon_WE = |
||
string.find( |
string.find(rawCoords, "(%d+)/(%d+)/(%d+%.?%d*)/(%a)/(%d+)/(%d+)/(%d+%.?%d*)/(%a)") |
||
if sidx == nil then -- extract without seconds |
if sidx == nil then -- extract without seconds |
||
local eidx |
local eidx |
||
sidx, eidx, lat_d, lat_m, lat_NS, lon_d, lon_m, lon_WE = |
sidx, eidx, lat_d, lat_m, lat_NS, lon_d, lon_m, lon_WE = |
||
string.find( |
string.find(rawCoords, "(%d+)/(%d+)/(%a)/(%d+)/(%d+)/(%a)") |
||
if sidx == nil then |
if sidx == nil then |
||
return output .. "<br/>Error: Failed to process |
return output .. "<br/>Error: Failed to process rawCoords: " .. rawCoords |
||
end |
end |
||
if string.find(options,"idx") ~= nil then |
if string.find(options,"idx") ~= nil then |
||
Line 32: | Line 31: | ||
return output |
return output |
||
end |
end |
||
has_seconds = |
has_seconds = false |
||
end |
end |
||
if string.find(options,"debug") |
if string.find(options,"debug") then |
||
if has_seconds |
if has_seconds then |
||
output = output .. "<br/> |
output = output .. "<br/>rawCoords:" .. rawCoords .. "<br/>lat dms = " .. lat_d .. "|" .. lat_m .. "|" .. lat_s .. "|" .. lat_NS .. |
||
"<br/>lon dms = " .. lon_d .. "|" .. lon_m .. "|" .. lon_s .. "|" .. lon_WE |
"<br/>lon dms = " .. lon_d .. "|" .. lon_m .. "|" .. lon_s .. "|" .. lon_WE .. "\n" |
||
else |
else |
||
output = output .. "<br/> |
output = output .. "<br/>rawCoords:" .. rawCoords .. "<br/>lat dm = " .. lat_d .. "|" .. lat_m .. "|" .. lat_NS .. |
||
"<br/>lon dms = " .. lon_d .. "|" .. lon_m .. "|" .. lon_WE |
"<br/>lon dms = " .. lon_d .. "|" .. lon_m .. "|" .. lon_WE .. "\n" |
||
end |
end |
||
end |
end |
||
Line 48: | Line 47: | ||
local enc_title = title:gsub(" ", "+") |
local enc_title = title:gsub(" ", "+") |
||
-- ** Call |
-- ** Call Coordinates passing individual fields and setting the title ** |
||
local coord = require('Module:Coordinates')._coord |
local coord = require('Module:Coordinates')._coord |
||
local type_title = "type:mountain&title=" .. enc_title |
local type_title = "type:mountain&title=" .. enc_title |
||
local coordsTxt -- declared here or lua strict complains |
local coordsTxt -- declared here or lua strict complains |
||
if has_seconds |
if has_seconds then |
||
coordsTxt = coord{lat_d, lat_m, lat_s, lat_NS, lon_d, lon_m, lon_s, lon_WE, type_title} |
coordsTxt = coord{lat_d, lat_m, lat_s, lat_NS, lon_d, lon_m, lon_s, lon_WE, type_title} |
||
--coords = frame:expandTemplate{title='coord', args= { lat_d, lat_m, lat_s, lat_NS, lon_d, lon_m, lon_s, lon_WE, type_title}} |
|||
else |
else |
||
coordsTxt = coord{lat_d, lat_m, lat_NS, lon_d, lon_m, lon_WE, type_title} |
coordsTxt = coord{lat_d, lat_m, lat_NS, lon_d, lon_m, lon_WE, type_title} |
||
--coords = frame:expandTemplate{title='coord', args= { lat_d, lat_m, lat_NS, lon_d, lon_m, lon_WE, type_title}} |
|||
-- ** Doesn't like me adding these args ** |
-- ** Doesn't like me adding these args ** |
||
--"type:mountain","format=dms","name=" .. name}} |
--"type:mountain","format=dms","name=" .. name}} |
||
Line 80: | Line 77: | ||
return p |
return p |
||
-- LUA debug console tests |
|||
-- print(p.build("50/27/40/N/122/17/42/W","test title","")) |
-- print(p.build("50/27/40/N/122/17/42/W","test title","")) |
||
-- print(p.build("50/27/N/122/17/W","title no seconds","")) |
|||
--[[ Tried to invoke {{wikidata}} directly but not working: |
--[[ Tried to invoke {{wikidata}} directly but not working: |
Revision as of 19:37, 6 March 2025
![]() | This is the module sandbox page for Module:Titled coords (diff). See also the companion subpage for test cases (run). |
![]() | 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 module depends on the following other modules: |
Description
This module generates titled coordinates from raw coordinates extracted from Wikidata. Currently, {{Wikidata}} does not support adding a title when requesting coordinates. So, when {{GeoGroup}} is used on a page with coordinates directly from Wikidata, the OpenStreetMap page will show an ordinal number as the title for each listed coordinate rather than a useful title. Using this module fixes the problem.
![]() | Pages should not call this module directly but rather use {{Titled_coords}} as it supports both named and unnamed parameters. The template also extracts the raw coordinates from Wikidata using {{Wikidata}}. |
Usage
Call from Template
{{#invoke:Titled coords|main|Wikidata raw coords|title|options}}
Wikidata raw coords is the unformatted raw coordinates where each field is separated by an forward slash.
e.g. 50/27/40/N/122/17/42/W
Call from LUA modules
local Titled_coords = require('Module:Titled_coords')
local coords = Titled_coords.build(raw_coords, title, options)
-- *** Version 1.2 (Beta) ***
local p = {}
-- Module entry point
function p.build(rawCoords, title, options)
local has_seconds = true
local output = ""
if not title then title = "" end
if not options then options = "" end
-- ** Display the raw coordinates obtained from {{wikidata}} **
if string.find(options,"raw") ~= nil then
output = output .. "rawCoords:" .. rawCoords
end
-- ** Extract the individual fields from the raw coordinates **
-- ** See https://www.lua.org/manual/5.3/manual.html#6.4.1 **
local sidx, _, lat_d, lat_m, lat_s, lat_NS, lon_d, lon_m, lon_s, lon_WE =
string.find(rawCoords, "(%d+)/(%d+)/(%d+%.?%d*)/(%a)/(%d+)/(%d+)/(%d+%.?%d*)/(%a)")
if sidx == nil then -- extract without seconds
local eidx
sidx, eidx, lat_d, lat_m, lat_NS, lon_d, lon_m, lon_WE =
string.find(rawCoords, "(%d+)/(%d+)/(%a)/(%d+)/(%d+)/(%a)")
if sidx == nil then
return output .. "<br/>Error: Failed to process rawCoords: " .. rawCoords
end
if string.find(options,"idx") ~= nil then
output = output .. "<br/>start: " .. sidx .. " end: " .. eidx .. " lat_d: " .. lat_d
return output
end
has_seconds = false
end
if string.find(options,"debug") then
if has_seconds then
output = output .. "<br/>rawCoords:" .. rawCoords .. "<br/>lat dms = " .. lat_d .. "|" .. lat_m .. "|" .. lat_s .. "|" .. lat_NS ..
"<br/>lon dms = " .. lon_d .. "|" .. lon_m .. "|" .. lon_s .. "|" .. lon_WE .. "\n"
else
output = output .. "<br/>rawCoords:" .. rawCoords .. "<br/>lat dm = " .. lat_d .. "|" .. lat_m .. "|" .. lat_NS ..
"<br/>lon dms = " .. lon_d .. "|" .. lon_m .. "|" .. lon_WE .. "\n"
end
end
-- ** URL encode spaces in the title with + **
local enc_title = title:gsub(" ", "+")
-- ** Call Coordinates passing individual fields and setting the title **
local coord = require('Module:Coordinates')._coord
local type_title = "type:mountain&title=" .. enc_title
local coordsTxt -- declared here or lua strict complains
if has_seconds then
coordsTxt = coord{lat_d, lat_m, lat_s, lat_NS, lon_d, lon_m, lon_s, lon_WE, type_title}
else
coordsTxt = coord{lat_d, lat_m, lat_NS, lon_d, lon_m, lon_WE, type_title}
-- ** Doesn't like me adding these args **
--"type:mountain","format=dms","name=" .. name}}
end
if output ~= "" then
return output .. "<br/>" .. coordsTxt
end
return coordsTxt
end
-- Template entry point
function p.main(frame)
local raw_coords = frame.args[1]
local title = frame.args[2] or ""
local options = frame.args[3] or ""
return build(raw_coords, title, options)
end
return p
-- LUA debug console tests
-- print(p.build("50/27/40/N/122/17/42/W","test title",""))
-- print(p.build("50/27/N/122/17/W","title no seconds",""))
--[[ Tried to invoke {{wikidata}} directly but not working:
local raw_coords = frame:expandTemplate{title='wikidata', args={'property','page=' .. page,'raw','coord'}}
]]