Class: OsMapRef::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/os_map_ref/location.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Location

Returns a new instance of Location.



10
11
12
13
14
# File 'lib/os_map_ref/location.rb', line 10

def initialize(args={})
  @map_reference = args[:map_reference].freeze if args[:map_reference]
  @easting = args[:easting].to_i if args[:easting]
  @northing = args[:northing].to_i if args[:northing]
end

Class Method Details

.for(text) ⇒ Object



5
6
7
8
# File 'lib/os_map_ref/location.rb', line 5

def self.for(text)
  input_processor = InputProcessor.new(text)
  new input_processor.params
end

Instance Method Details

#build_map_referenceObject



20
21
22
# File 'lib/os_map_ref/location.rb', line 20

def build_map_reference
  [grid_reference_prefix, short_easting, short_northing].join(' ')
end

#chars_in_northing_startObject



44
45
46
# File 'lib/os_map_ref/location.rb', line 44

def chars_in_northing_start
  long_northing? ? 2 : 1
end

#eastingObject



52
53
54
# File 'lib/os_map_ref/location.rb', line 52

def easting
  @easting ||= easting_from_map_reference.to_i
end

#easting_from_map_referenceObject



64
65
66
# File 'lib/os_map_ref/location.rb', line 64

def easting_from_map_reference
  northing_easting_from_map_reference[1].to_s + map_reference_parts[1]
end

#gridObject

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’).



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/os_map_ref/location.rb', line 87

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 OI OJ],
    %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 TI TJ],
    %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_eastingObject



24
25
26
# File 'lib/os_map_ref/location.rb', line 24

def grid_easting
  easting.to_s[0].to_i
end

#grid_northingObject



36
37
38
# File 'lib/os_map_ref/location.rb', line 36

def grid_northing
  northing.to_s[0..(chars_in_northing_start - 1)].to_i
end

#grid_reference_prefixObject



48
49
50
# File 'lib/os_map_ref/location.rb', line 48

def grid_reference_prefix
  grid[grid_northing][grid_easting]
end

#long_northing?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/os_map_ref/location.rb', line 32

def long_northing?
  northing.to_s.length >= 7
end

#map_referenceObject



16
17
18
# File 'lib/os_map_ref/location.rb', line 16

def map_reference
  @map_reference ||= build_map_reference.freeze
end

#map_reference_partsObject

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



70
71
72
# File 'lib/os_map_ref/location.rb', line 70

def map_reference_parts
  @map_reference_parts ||= map_reference.split
end

#matrixObject



80
81
82
# File 'lib/os_map_ref/location.rb', line 80

def matrix
  @matrix ||= Matrix[*grid]
end

#northingObject



56
57
58
# File 'lib/os_map_ref/location.rb', line 56

def northing
  @northing ||= northing_from_map_reference.to_i
end

#northing_easting_from_map_referenceObject

Returns array of [grid_northing, grid_easting] for the gird element matching the map reference start. So ‘ST 58901 71053’ will return [1, 3]



76
77
78
# File 'lib/os_map_ref/location.rb', line 76

def northing_easting_from_map_reference
  @northing_easting_from_map_reference ||= matrix.index map_reference_parts[0]
end

#northing_from_map_referenceObject



60
61
62
# File 'lib/os_map_ref/location.rb', line 60

def northing_from_map_reference
  northing_easting_from_map_reference[0].to_s + map_reference_parts[2]
end

#short_eastingObject



28
29
30
# File 'lib/os_map_ref/location.rb', line 28

def short_easting
  easting.to_s[1..-1].to_i
end

#short_northingObject



40
41
42
# File 'lib/os_map_ref/location.rb', line 40

def short_northing
  northing.to_s[chars_in_northing_start..-1].to_i
end