Module:Ordnance Survey coordinates/sandbox: Difference between revisions

Content deleted Content added
nope
use tables as return values, not tuples
Line 35:
local preview = require('Module:If preview')
local namespace = mw.title.getCurrentTitle().namespace;
 
 
local function northeast(lett,num,shift)
Line 41 ⟶ 40:
local le=mw.ustring.len(num)
if le%2 == 1 then
return {err="Malformed numerical part of NGR"}
end
local pr=le/2
Line 67 ⟶ 66:
end
end
return nil,{n=n,e=e,pr=pr,T1=T1,T2=T2}
end
 
Line 150 ⟶ 149:
until abs(phinewwgs84-phiradwgs84)<=0.000000000001 or nextcounter>=100
lat=phinewwgs84*dr
return {country="GB",nil,lat=lat,long=long}
end
 
Line 158 ⟶ 157:
-- first caclulate e,n
-- computing e and n exactly, to get SW corner of box
local err, n, e, pr, T1, T2ne = northeast(lett,num,0)
if ne.err ~= nil then
return {country="GB",err,0=ne.0,0.0err}
end
-- use British definition of e and n
local e=500000.0*(ne.T1%5)+100000.0*(ne.T2%5)-1000000.0+ne.e*ne.pr
local n=1900000.0-500000.0*math.floor(ne.T1/5)-100000.0*math.floor(ne.T2/5)+ne.n*ne.pr
returnlocal result = GBEN2LL(e,n)
result.prec = 0.8165*ne.pr
return result
end
Line 249 ⟶ 250:
until abs(phinewwgs84-phiradwgs84)<=0.000000000001 or nextcounter>=10000
lat=phinewwgs84*dr
return {country="IE",nil,lat=lat,long=long}
end
 
Line 256 ⟶ 257:
-- first caclulate e,n
-- computing e and n exactly, to get SW corner of box
local err, n, e, pr, T1ne = northeast(lett,num,0)
if ne.err ~= nil then
return {country="IE", err,0=ne.0,0.0err}
end
-- use Irish definition of northing and easting
local e=100000.0*(ne.T1%5.0)+ne.e*ne.pr
local n=ne.n*ne.pr+100000.0*(4.0-math.floor(ne.T1/5.0))
returnlocal result = IrishEN2LL(e,n)
result.prec = 0.8165*ne.pr -- useful @ Commons
return result
end
 
Line 271 ⟶ 274:
 
local function NGR2LL(ngr)
local result = {}
-- returns a country,error,lat,long list
ngr = mw.ustring.gsub(mw.ustring.upper(ngr),"[%s%p]","")
local first, last, lett, num = mw.ustring.find(ngr,"^([A-Z]+)(%d+)$")
if first == nil or empty(lett) or empty(num) or mw.ustring.len(lett) > 2 then
return nil,{err="Malformed NGR",0.0,0.0}
end
if mw.ustring.len(lett) == 1 then
Line 332 ⟶ 335:
local firstArg = mw.ustring.sub(args[1],2)
if alldigits(firstArg) then
LL = {IrishEN2LL(firstArg,args[2])}
restargs = 3
if empty(linktitle) then
Line 339 ⟶ 342:
end
elseif alldigits(args[1]) then
LL = {GBEN2LL(args[1],args[2])}
restargs = 3
if empty(linktitle) then
Line 346 ⟶ 349:
end
else
LL = {NGR2LL(args[1])}
restargs = 2
if empty(linktitle) then
Line 353 ⟶ 356:
end
linktitle = trim(linktitle)
if not empty(LL[2].err) then
return linktitle ..warning(LL[2].err)
end
-- https://geohack.toolforge.org/geohack.php?pagename=Mount_Whitney&params=36.578580925_N_118.29199495_W_type:mountain_region:US-CA_scale:100000_source:NGS
Line 365 ⟶ 368:
url = url..'pagename='..pagename..'&'
end
LL.lat = LL.lat or 0
url = url..'params='..LL[3]..'_N_'
ifLL.long = LL[4].long <or 0 then
url = url..(-'params='..LL[4]).lat..'_W_N_'
if LL.long < 0 then
url = url..'params='(-LL..LL[3]long)..'_N__W'
else
url = url..LL[4].long..'_E'
end
for i = restargs,#args do
url = url..'_'..args[i]
end
if mw.ustring.find(input,"region") == nil and LL.country then
url = url..'_region:'..LL[1].country
end
if not empty(namearg) then