Class: Silva::System::Gridref

Inherits:
Base
  • Object
show all
Includes:
OsEn
Defined in:
lib/silva/system/gridref.rb

Overview

Location system representing Ordnance Survey Standard Grid References.

Can be created given the options :easting => e, :northing => n or :gridref => g

Constant Summary collapse

DEFAULT_PARAMS =

:digits can be 6, 8 or 10

{ :digits => 8 }
OSGB_PREFIXES =

UK two-letter prefixes

["SV", "SW", "SX", "SY", "SZ", "TV", "TW",
"SQ", "SR", "SS", "ST", "SU", "TQ", "TR",
"SL", "SM", "SN", "SO", "SP", "TL", "TM",
"SF", "SG", "SH", "SJ", "SK", "TF", "TG",
"SA", "SB", "SC", "SD", "SE", "TA", "TB",
"NV", "NW", "NX", "NY", "NZ", "OV", "OW",
"NQ", "NR", "NS", "NT", "NU", "OQ", "OR",
"NL", "NM", "NN", "NO", "NP", "OL", "OM",
"NF", "NG", "NH", "NJ", "NK", "OF", "OG",
"NA", "NB", "NC", "ND", "NE", "OA", "OB",
"HV", "HW", "HX", "HY", "HZ", "JV", "JW",
"HQ", "HR", "HS", "HT", "HU", "JQ", "JR",
"HL", "HM", "HN", "HO", "HP", "JL", "JM"]
OSGB_GRID_WIDTH =

Width of the UK grid

7
OSGB_GRID_SCALE =

Height of the UK grid

100000

Constants included from OsEn

OsEn::EASTING_RANGE, OsEn::NORTHING_RANGE, OsEn::REQUIRED_PARAMS

Constants inherited from Base

Base::REQUIRED_PARAMS

Instance Attribute Summary

Attributes included from OsEn

#easting, #northing

Instance Method Summary collapse

Methods inherited from Base

#initialize, #to

Constructor Details

This class inherits a constructor from Silva::System::Base

Instance Method Details

#gridrefString

Lazily create the gridref from given eastings and northings, or just return it if already set.

Returns:

  • (String)

    A standard Ordnance Survey grid reference with the given number of digits.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/silva/system/gridref.rb', line 40

def gridref
  unless @gridref
    e100k = (easting / 100000).floor
    n100k = (northing / 100000).floor
    index = n100k * OSGB_GRID_WIDTH + e100k
    prefix = OSGB_PREFIXES[index]

    e = ((easting % OSGB_GRID_SCALE) / (10**(5 - @digits / 2))).round
    n = ((northing % OSGB_GRID_SCALE) / (10**(5 - @digits / 2))).round

    @gridref = prefix + e.to_s.rjust(@digits / 2, '0') + n.to_s.rjust(@digits / 2, '0')
  end

  @gridref
end

#to_sObject



56
57
58
# File 'lib/silva/system/gridref.rb', line 56

def to_s
  gridref
end