Class: OsMapRef::Location
- Inherits:
-
Object
- Object
- OsMapRef::Location
- Defined in:
- lib/os_map_ref/location.rb
Class Method Summary collapse
Instance Method Summary collapse
- #build_map_reference ⇒ Object
- #chars_in_northing_start ⇒ Object
- #determine_prefix_coordinates ⇒ Object
- #easting ⇒ Object
- #easting_from_map_reference ⇒ Object
-
#grid ⇒ Object
Grid of 100km squares as they are arranged over the UK.
- #grid_easting ⇒ Object
- #grid_northing ⇒ Object
- #grid_reference_prefix ⇒ Object
-
#initialize(args = {}) ⇒ Location
constructor
A new instance of Location.
- #long_northing? ⇒ Boolean
- #map_reference ⇒ Object
-
#map_reference_parts ⇒ Object
The parts should be a pair of letters then two sets of numbers ‘ST 58901 71053’ becomes [‘ST’, ‘58901’, ‘71053’].
- #matrix ⇒ Object
- #northing ⇒ Object
- #northing_from_map_reference ⇒ Object
- #prefix ⇒ Object
-
#prefix_coordinates ⇒ Object
Returns array of [grid_northing, grid_easting] for the grid element matching the map reference prefix.
- #raise_prefix_error(prefix) ⇒ Object
- #remove_decimals(number) ⇒ Object
- #short_easting ⇒ Object
- #short_northing ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Location
Returns a new instance of Location.
11 12 13 14 15 |
# File 'lib/os_map_ref/location.rb', line 11 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
6 7 8 9 |
# File 'lib/os_map_ref/location.rb', line 6 def self.for(text) input_processor = InputProcessor.new(text) new input_processor.params end |
Instance Method Details
#build_map_reference ⇒ Object
25 26 27 |
# File 'lib/os_map_ref/location.rb', line 25 def build_map_reference [grid_reference_prefix, short_easting, short_northing].join(" ") end |
#chars_in_northing_start ⇒ Object
49 50 51 |
# File 'lib/os_map_ref/location.rb', line 49 def chars_in_northing_start long_northing? ? 2 : 1 end |
#determine_prefix_coordinates ⇒ Object
90 91 92 |
# File 'lib/os_map_ref/location.rb', line 90 def determine_prefix_coordinates matrix.index(prefix) || raise_prefix_error(prefix) end |
#easting ⇒ Object
57 58 59 |
# File 'lib/os_map_ref/location.rb', line 57 def easting @easting ||= easting_from_map_reference end |
#easting_from_map_reference ⇒ Object
69 70 71 |
# File 'lib/os_map_ref/location.rb', line 69 def easting_from_map_reference prefix_coordinates[1].to_s + map_reference_parts[1] end |
#grid ⇒ Object
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’).
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/os_map_ref/location.rb', line 105 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 ⇒ Object
29 30 31 |
# File 'lib/os_map_ref/location.rb', line 29 def grid_easting easting.to_s[0].to_i end |
#grid_northing ⇒ Object
41 42 43 |
# File 'lib/os_map_ref/location.rb', line 41 def grid_northing northing.to_s[0..(chars_in_northing_start - 1)].to_i end |
#grid_reference_prefix ⇒ Object
53 54 55 |
# File 'lib/os_map_ref/location.rb', line 53 def grid_reference_prefix grid[grid_northing][grid_easting] end |
#long_northing? ⇒ Boolean
37 38 39 |
# File 'lib/os_map_ref/location.rb', line 37 def long_northing? northing.to_s.length >= 7 end |
#map_reference ⇒ Object
21 22 23 |
# File 'lib/os_map_ref/location.rb', line 21 def map_reference @map_reference ||= build_map_reference.freeze end |
#map_reference_parts ⇒ Object
The parts should be a pair of letters then two sets of numbers ‘ST 58901 71053’ becomes [‘ST’, ‘58901’, ‘71053’]
75 76 77 |
# File 'lib/os_map_ref/location.rb', line 75 def map_reference_parts @map_reference_parts ||= map_reference.split end |
#matrix ⇒ Object
98 99 100 |
# File 'lib/os_map_ref/location.rb', line 98 def matrix @matrix ||= Matrix[*grid] end |
#northing ⇒ Object
61 62 63 |
# File 'lib/os_map_ref/location.rb', line 61 def northing @northing ||= northing_from_map_reference end |
#northing_from_map_reference ⇒ Object
65 66 67 |
# File 'lib/os_map_ref/location.rb', line 65 def northing_from_map_reference prefix_coordinates[0].to_s + map_reference_parts[2] end |
#prefix ⇒ Object
79 80 81 |
# File 'lib/os_map_ref/location.rb', line 79 def prefix map_reference_parts[0] end |
#prefix_coordinates ⇒ Object
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.
86 87 88 |
# File 'lib/os_map_ref/location.rb', line 86 def prefix_coordinates @prefix_coordinates ||= determine_prefix_coordinates end |
#raise_prefix_error(prefix) ⇒ Object
94 95 96 |
# File 'lib/os_map_ref/location.rb', line 94 def raise_prefix_error(prefix) raise OsMapRef::Error, "Unable to process map reference #{@map_reference}: #{prefix} not found" end |
#remove_decimals(number) ⇒ Object
17 18 19 |
# File 'lib/os_map_ref/location.rb', line 17 def remove_decimals(number) number.to_s.sub(/\.\d*/, "") end |
#short_easting ⇒ Object
33 34 35 |
# File 'lib/os_map_ref/location.rb', line 33 def short_easting easting.to_s[1..-1] end |
#short_northing ⇒ Object
45 46 47 |
# File 'lib/os_map_ref/location.rb', line 45 def short_northing northing.to_s[chars_in_northing_start..-1] end |