class Silva::System::Gridref

Location system representing Ordnance Survey Standard Grid References.

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

Constants

DEFAULT_PARAMS

:digits can be 6, 8 or 10

OSGB_GRID_SCALE

Height of the UK grid

OSGB_GRID_WIDTH

Width of the UK grid

OSGB_PREFIXES

UK two-letter prefixes

Public Instance Methods

gridref() click to toggle source

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

@return [String] A standard Ordnance Survey grid reference with the given number of digits.

# 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_s() click to toggle source
# File lib/silva/system/gridref.rb, line 56
def to_s
  gridref
end

Private Instance Methods

get_prefix(g = nil) click to toggle source
# File lib/silva/system/gridref.rb, line 80
def get_prefix(g = nil)
  g ||= gridref
  g[0..1]
end
get_suffix(g = nil) click to toggle source
# File lib/silva/system/gridref.rb, line 85
def get_suffix(g = nil)
  g ||= gridref
  g[2..-1]
end
params_satisfied?(options) click to toggle source
Calls superclass method Silva::System::Base#params_satisfied?
# File lib/silva/system/gridref.rb, line 110
def params_satisfied?(options)
  super unless options.include?(:gridref)
end
prefix_to_en() click to toggle source
# File lib/silva/system/gridref.rb, line 90
def prefix_to_en
  index = OSGB_PREFIXES.index(get_prefix)
  e = index % OSGB_GRID_WIDTH
  n = index / OSGB_GRID_WIDTH

  [e * OSGB_GRID_SCALE, n * OSGB_GRID_SCALE]
end
to_en(options = nil) click to toggle source
# File lib/silva/system/gridref.rb, line 71
def to_en(options = nil)
  e100k, n100k = prefix_to_en
  en = get_suffix
  e = en[0, (en.length / 2)].ljust(5, '5').to_i + e100k
  n = en[(en.length / 2)..-1].ljust(5, '5').to_i + n100k

  System.create(:en, :easting => e, :northing => n )
end
to_osgb36(options = nil) click to toggle source
# File lib/silva/system/gridref.rb, line 67
def to_osgb36(options = nil)
  Silva::Transform.en_to_osgb36(to_en)
end
to_wgs84(options = nil) click to toggle source
# File lib/silva/system/gridref.rb, line 63
def to_wgs84(options = nil)
  Silva::Transform.osgb36_to_wgs84(to_osgb36)
end
valid_suffix?(suffix) click to toggle source
# File lib/silva/system/gridref.rb, line 106
def valid_suffix?(suffix)
  suffix.length > 1 && suffix.length % 2 == 0 && suffix =~ /[0-9]+/
end
validate_digits(digits) click to toggle source
# File lib/silva/system/gridref.rb, line 98
def validate_digits(digits)
  [6, 8, 10].include?(digits)
end
validate_gridref(g) click to toggle source
# File lib/silva/system/gridref.rb, line 102
def validate_gridref(g)
  OSGB_PREFIXES.include?(get_prefix(g)) && valid_suffix?(get_suffix(g))
end