Module:Ordnance Survey coordinates: Difference between revisions

if scale/type/dim not specified, attempt a guess from the precision of the OS grid reference
m (1 revision imported)
imported>Hike395
(if scale/type/dim not specified, attempt a guess from the precision of the OS grid reference)
 
Line 35: Line 35:
local preview = require('Module:If preview')
local preview = require('Module:If preview')
local namespace = mw.title.getCurrentTitle().namespace;
local namespace = mw.title.getCurrentTitle().namespace;


local function northeast(lett,num,shift)
local function northeast(lett,num,shift)
Line 41: Line 40:
   local le=mw.ustring.len(num)
   local le=mw.ustring.len(num)
   if le%2 == 1 then
   if le%2 == 1 then
    return "Malformed numerical part of NGR"
  return {err="Malformed numerical part of NGR"}
   end
   end
   local pr=le/2
   local pr=le/2
Line 67: Line 66:
     end
     end
   end
   end
   return nil,n,e,pr,T1,T2
   return {n=n,e=e,pr=pr,T1=T1,T2=T2}
end       
end       


Line 150: Line 149:
   until abs(phinewwgs84-phiradwgs84)<=0.000000000001 or nextcounter>=100
   until abs(phinewwgs84-phiradwgs84)<=0.000000000001 or nextcounter>=100
   lat=phinewwgs84*dr
   lat=phinewwgs84*dr
   return "GB",nil,lat,long
   return {region="GB",lat=lat,long=long}
end
end


Line 158: Line 157:
   -- first caclulate e,n
   -- first caclulate e,n
   -- computing e and n exactly, to get SW corner of box
   -- computing e and n exactly, to get SW corner of box
   local err, n, e, pr, T1, T2 = northeast(lett,num,0)
   local ne = northeast(lett,num,0)
   if err ~= nil then
   if ne.err then
    return "GB",err,0.0,0.0
  return {region="GB",err=ne.err}
   end
   end
   -- use British definition of e and n
   -- use British definition of e and n
   e=500000.0*(T1%5)+100000.0*(T2%5)-1000000.0+e*pr
   local e=500000.0*(ne.T1%5)+100000.0*(ne.T2%5)-1000000.0+ne.e*ne.pr
   n=1900000.0-500000.0*math.floor(T1/5)-100000.0*math.floor(T2/5)+n*pr
   local n=1900000.0-500000.0*math.floor(ne.T1/5)-100000.0*math.floor(ne.T2/5)+ne.n*ne.pr
   return GBEN2LL(e,n)
   local result = GBEN2LL(e,n)
  result.prec = 0.8165*ne.pr
  return result
end
end
    
    
Line 249: Line 250:
   until abs(phinewwgs84-phiradwgs84)<=0.000000000001 or nextcounter>=10000
   until abs(phinewwgs84-phiradwgs84)<=0.000000000001 or nextcounter>=10000
   lat=phinewwgs84*dr
   lat=phinewwgs84*dr
   return "IE",nil,lat,long
   return {region="IE",lat=lat,long=long}
end
end


Line 256: Line 257:
   -- first caclulate e,n
   -- first caclulate e,n
   -- computing e and n exactly, to get SW corner of box
   -- computing e and n exactly, to get SW corner of box
   local err, n, e, pr, T1 = northeast(lett,num,0)
   local ne = northeast(lett,num,0)
   if err ~= nil then
   if ne.err then
    return "IE",err,0.0,0.0
  return {region="IE", err=ne.err}
   end
   end
   -- use Irish definition of northing and easting
   -- use Irish definition of northing and easting
   local e=100000.0*(T1%5.0)+e*pr
   local e=100000.0*(ne.T1%5.0)+ne.e*ne.pr
   local n=n*pr+100000.0*(4.0-math.floor(T1/5.0))
   local n=ne.n*ne.pr+100000.0*(4.0-math.floor(ne.T1/5.0))
   return IrishEN2LL(e,n)
   local result = IrishEN2LL(e,n)
  result.prec = 0.8165*ne.pr  -- useful @ Commons
  return result
end
end


Line 271: Line 274:


local function NGR2LL(ngr)
local function NGR2LL(ngr)
-- returns a country,error,lat,long list
  local result = {}
   ngr = mw.ustring.gsub(mw.ustring.upper(ngr),"[%s%p]","")
   ngr = mw.ustring.gsub(mw.ustring.upper(ngr),"[%s%p]","")
   local first, last, lett, num = mw.ustring.find(ngr,"^([A-Z]+)(%d+)$")
   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
   if first == nil or empty(lett) or empty(num) or mw.ustring.len(lett) > 2 then
   return nil,"Malformed NGR",0.0,0.0
   return {err="Malformed NGR"}
   end
   end
   if mw.ustring.len(lett) == 1 then
   if mw.ustring.len(lett) == 1 then
Line 315: Line 318:


function oscoord.main(frame)
function oscoord.main(frame)
   local args = getArgs(frame,{parentFirst=true,parentOnly=false,frameOnly=false})
   local args = getArgs(frame)
   local input = args[1]
   local input = args[1]
   if empty(input) then
   if empty(input) then
Line 332: Line 335:
       local firstArg = mw.ustring.sub(args[1],2)
       local firstArg = mw.ustring.sub(args[1],2)
       if alldigits(firstArg) then
       if alldigits(firstArg) then
         LL = {IrishEN2LL(firstArg,args[2])}
         LL = IrishEN2LL(firstArg,args[2])
    restargs = 3
    restargs = 3
    if empty(linktitle) then
    if empty(linktitle) then
Line 339: Line 342:
       end
       end
     elseif alldigits(args[1]) then
     elseif alldigits(args[1]) then
       LL = {GBEN2LL(args[1],args[2])}
       LL = GBEN2LL(args[1],args[2])
       restargs = 3
       restargs = 3
       if empty(linktitle) then
       if empty(linktitle) then
Line 346: Line 349:
     end
     end
   else
   else
     LL = {NGR2LL(args[1])}
     LL = NGR2LL(args[1])
     restargs = 2
     restargs = 2
     if empty(linktitle) then
     if empty(linktitle) then
Line 353: Line 356:
   end
   end
   linktitle = trim(linktitle)
   linktitle = trim(linktitle)
   if not empty(LL[2]) then
   if not empty(LL.err) then
     return linktitle ..warning(LL[2])
     return linktitle ..warning(LL.err)
   end
   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
   -- 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: Line 368:
   url = url..'pagename='..pagename..'&'
   url = url..'pagename='..pagename..'&'
   end
   end
   url = url..'params='..LL[3]..'_N_'
  LL.lat = LL.lat or 0
   if LL[4] < 0 then
  LL.long = LL.long or 0
   url = url..(-LL[4])..'_W'
   url = url..'params='..LL.lat..'_N_'
   if LL.long < 0 then
   url = url..(-LL.long)..'_W'
   else
   else
   url = url..LL[4]..'_E'
   url = url..LL.long..'_E'
   end
   end
   for i = restargs,#args do
   for i = restargs,#args do
   url = url..'_'..args[i]
   url = url..'_'..args[i]
   end
   end
   if mw.ustring.find(input,"region") == nil then
   if not mw.ustring.find(input,"region") and LL.region then
     url = url..'_region:'..LL[1]
     url = url..'_region:'..LL.region
  end
  if not mw.ustring.find(input,"scale") and
    not mw.ustring.find(input,"type") and
    not mw.ustring.find(input,"dim") and LL.prec then
    url = url..'_dim:'..math.floor(50*LL.prec+0.5)..'m'
   end
   end
   if not empty(namearg) then
   if not empty(namearg) then
Anonymous user