Module:Sandbox/Ahecht/coords
p = {}
function p._split(text)
text = mw.ustring.gsub(mw.text.trim(text), "[*′″]", {['*'] = '°', ['′'] = "'", ['″'] = '"'})
local lat, long
local regexes = {
"^([+%-%d.°'\"NS%s]+),%s*([+%-%d.°'\"EW%s]+)$", --split by commas
"^([+%-%d.°'\"%s]+[NS])%s*([+%-%d.°'\"%s]+[EW])$", --split by N/S
"^([+%-%d.°'%s]+\")%s*([+%-%d.°'%s]+\")$", --split by "
"^([+%-%d.°%s]+')%s*([+%-%d.°%s]+')$", --split by '
"^([+%-%d.%s]+°)%s*([+%-%d.%s]+°)$", --split by °
"^([+%-%d.]+)%s*([+%-%d.]+)$" --split by space
}
for _, regex in ipairs(regexes) do
lat, long = string.match(text, regex)
if lat and long then return {mw.text.trim(lat), mw.text.trim(long)} end
end
end
function p._parse(text)
local parts = p._split(text)
local errmsg = '<strong class="error">Unable to parse "' .. text .. '" as coordinates.</strong>'
if not parts then return errmsg end
local coords = {}
for k, v in ipairs(parts) do
local count, number
-- Convert N, S, E, and W
v, count = string.gsub(v, '(%s*[SW])$', '')
if count > 0 and v:sub(1,1) ~= '-' then
v = '-' .. v
else
v = string.gsub(v, '(%s*[NE])$', '')
end
-- attempt to convert text to number
coords[k] = tonumber(v)
-- attempt to convert dms to decimal
if not coords[k] and string.match(v, "[°'\"]") then
number = tonumber(string.match(v, "^[^'\"]+'%s*([%d.]+)%s*\"[^'\"]*$"))
if number and tonumber(number) then
coords[k] = (coords[k] or 0) + (number/3600)
end
number = tonumber(string.match(v, "^[^°']+°%s*([%d.]+)%s*'[^°']*$"))
if number and tonumber(number) then
coords[k] = (coords[k] or 0) + (number/60)
end
number = tonumber(string.match(v, "^%s*([+%-%d.]+)%s*°[^°]*$"))
if number and tonumber(number) then
coords[k] = (coords[k] or 0) + number
end
end
if not coords[k] then return errmsg end
end
return coords
end
function p.parse(frame)
parsed = p._parse(frame.args[1])
if not parsed then
return '<strong class="error">Unable to parse "' .. (frame.args[1] or '') .. '" as coordinates.</strong>'
end
if parsed[1] < 0 then
parsed[1] = (-1 * parsed[1]) .. "_S"
else
parsed[1] = parsed[1] .. "_N"
end
if parsed[2] < 0 then
parsed[2] = (-1 * parsed[2]) .. "_W"
else
parsed[2] = parsed[2] .. "_E"
end
return parsed[1] .. '_' .. parsed[2]
end
return p
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.