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 = remove_decimals(args[:easting]) if args[:easting]
  @northing = remove_decimals(args[:northing]) 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



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

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

#chars_in_northing_startObject



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

def chars_in_northing_start
  long_northing? ? 2 : 1
end

#eastingObject



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

def easting
  @easting ||= easting_from_map_reference
end

#easting_from_map_referenceObject



68
69
70
# File 'lib/os_map_ref/location.rb', line 68

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

#get_prefix_coordinatesObject



89
90
91
# File 'lib/os_map_ref/location.rb', line 89

def get_prefix_coordinates
  matrix.index(prefix) || raise_prefix_error(prefix)
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’).



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/os_map_ref/location.rb', line 104

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_eastingObject



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

def grid_easting
  easting.to_s[0].to_i
end

#grid_northingObject



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

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

#grid_reference_prefixObject



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

def grid_reference_prefix
  grid[grid_northing][grid_easting]
end

#long_northing?Boolean

Returns:

  • (Boolean)


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

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

#map_referenceObject



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

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’]



74
75
76
# File 'lib/os_map_ref/location.rb', line 74

def map_reference_parts
  @map_reference_parts ||= map_reference.split
end

#matrixObject



97
98
99
# File 'lib/os_map_ref/location.rb', line 97

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

#northingObject



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

def northing
  @northing ||= northing_from_map_reference
end

#northing_from_map_referenceObject



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

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

#prefixObject



78
79
80
# File 'lib/os_map_ref/location.rb', line 78

def prefix
  map_reference_parts[0]
end

#prefix_coordinatesObject

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.



85
86
87
# File 'lib/os_map_ref/location.rb', line 85

def prefix_coordinates
  @prefix_coordinates ||= get_prefix_coordinates
end

#raise_prefix_error(prefix) ⇒ Object

Raises:



93
94
95
# File 'lib/os_map_ref/location.rb', line 93

def raise_prefix_error(prefix)
  raise OsMapRef::Error, "Unable to process map reference #{@map_reference}: #{prefix} not found"
end

#remove_decimals(number) ⇒ Object



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

def remove_decimals(number)
  number.to_s.sub /\.\d*/, ""
end

#short_eastingObject



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

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

#short_northingObject



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

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