class OsMapRef::Location

Public Class Methods

for(text) click to toggle source
# File lib/os_map_ref/location.rb, line 8
def self.for(text)
  input_processor = InputProcessor.new(text)
  new input_processor.params
end
new(args = {}) click to toggle source
# File lib/os_map_ref/location.rb, line 13
def initialize(args = {})
  @map_reference = args[:map_reference].freeze if args[:map_reference]
  @easting = remove_decimals(args[:easting]) if args[:easting]
  @northing = remove_decimals(args[:northing]) if args[:northing]
end

Public Instance Methods

build_map_reference() click to toggle source
# File lib/os_map_ref/location.rb, line 27
def build_map_reference
  [grid_reference_prefix, short_easting, short_northing].join(" ")
end
chars_in_northing_start() click to toggle source
# File lib/os_map_ref/location.rb, line 51
def chars_in_northing_start
  long_northing? ? 2 : 1
end
determine_prefix_coordinates() click to toggle source
# File lib/os_map_ref/location.rb, line 92
def determine_prefix_coordinates
  matrix.index(prefix) || raise_prefix_error(prefix)
end
easting() click to toggle source
# File lib/os_map_ref/location.rb, line 59
def easting
  @easting ||= easting_from_map_reference
end
easting_from_map_reference() click to toggle source
# File lib/os_map_ref/location.rb, line 71
def easting_from_map_reference
  prefix_coordinates[1].to_s + map_reference_parts[1]
end
grid() click to toggle source

Grid of 100km squares as they are arranged over the UK. The grid is reversed so that the origin (0,0) is the bottom left corner ('SV').

# File lib/os_map_ref/location.rb, line 107
def grid
  @grid ||= [
    %w[HL HM HN HO HP JL JM JN JO JP],
    %w[HQ HR HS HT HU JQ JR JS JT JU],
    %w[HV HW HX HY HZ JV JW JX JY JZ],
    %w[NA NB NC ND NE OA OB OC OD OE],
    %w[NF NG NH NJ NK OF OG OH OJ OK],
    %w[NL NM NN NO NP OL OM ON OO OP],
    %w[NQ NR NS NT NU OQ OR OS OT OU],
    %w[NV NW NX NY NZ OV OW OX OY OZ],
    %w[SA SB SC SD SE TA TB TC TD TE],
    %w[SF SG SH SJ SK TF TG TH TJ TK],
    %w[SL SM SN SO SP TL TM TN TO TP],
    %w[SQ SR SS ST SU TQ TR TS TT TU],
    %w[SV SW SX SY SZ TV TW TX TY TZ]
  ].reverse
end
grid_easting() click to toggle source
# File lib/os_map_ref/location.rb, line 31
def grid_easting
  easting.to_s[0].to_i
end
grid_northing() click to toggle source
# File lib/os_map_ref/location.rb, line 43
def grid_northing
  northing.to_s[0..(chars_in_northing_start - 1)].to_i
end
grid_reference_prefix() click to toggle source
# File lib/os_map_ref/location.rb, line 55
def grid_reference_prefix
  grid[grid_northing][grid_easting]
end
long_northing?() click to toggle source
# File lib/os_map_ref/location.rb, line 39
def long_northing?
  northing.to_s.length >= 7
end
map_reference() click to toggle source
# File lib/os_map_ref/location.rb, line 23
def map_reference
  @map_reference ||= build_map_reference.freeze
end
map_reference_parts() click to toggle source

The parts should be a pair of letters then two sets of numbers 'ST 58901 71053' becomes ['ST', '58901', '71053']

# File lib/os_map_ref/location.rb, line 77
def map_reference_parts
  @map_reference_parts ||= map_reference.split
end
matrix() click to toggle source
# File lib/os_map_ref/location.rb, line 100
def matrix
  @matrix ||= Matrix[*grid]
end
northing() click to toggle source
# File lib/os_map_ref/location.rb, line 63
def northing
  @northing ||= northing_from_map_reference
end
northing_from_map_reference() click to toggle source
# File lib/os_map_ref/location.rb, line 67
def northing_from_map_reference
  prefix_coordinates[0].to_s + map_reference_parts[2]
end
prefix() click to toggle source
# File lib/os_map_ref/location.rb, line 81
def prefix
  map_reference_parts[0]
end
prefix_coordinates() click to toggle source

Returns array of [grid_northing, grid_easting] for the grid element matching the map reference prefix. So 'ST 58901 71053' will return [1, 3] which are the coordinates of the prefix 'ST' in the grid.

# File lib/os_map_ref/location.rb, line 88
def prefix_coordinates
  @prefix_coordinates ||= determine_prefix_coordinates
end
raise_prefix_error(prefix) click to toggle source
# File lib/os_map_ref/location.rb, line 96
def raise_prefix_error(prefix)
  raise OsMapRef::Error, "Unable to process map reference #{@map_reference}: #{prefix} not found"
end
remove_decimals(number) click to toggle source
# File lib/os_map_ref/location.rb, line 19
def remove_decimals(number)
  number.to_s.sub(/\.\d*/, "")
end
short_easting() click to toggle source
# File lib/os_map_ref/location.rb, line 35
def short_easting
  easting.to_s[1..-1]
end
short_northing() click to toggle source
# File lib/os_map_ref/location.rb, line 47
def short_northing
  northing.to_s[chars_in_northing_start..-1]
end