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.
13 14 15 16 17 |
# 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 |
Class Method Details
.for(text) ⇒ Object
8 9 10 11 |
# File 'lib/os_map_ref/location.rb', line 8 def self.for(text) input_processor = InputProcessor.new(text) new input_processor.params end |
Instance Method Details
#build_map_reference ⇒ Object
27 28 29 |
# 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 ⇒ Object
51 52 53 |
# File 'lib/os_map_ref/location.rb', line 51 def chars_in_northing_start long_northing? ? 2 : 1 end |
#determine_prefix_coordinates ⇒ Object
92 93 94 |
# File 'lib/os_map_ref/location.rb', line 92 def determine_prefix_coordinates matrix.index(prefix) || raise_prefix_error(prefix) end |
#easting ⇒ Object
59 60 61 |
# File 'lib/os_map_ref/location.rb', line 59 def easting @easting ||= easting_from_map_reference end |
#easting_from_map_reference ⇒ Object
71 72 73 |
# 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 ⇒ 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’).
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# 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 ⇒ Object
31 32 33 |
# File 'lib/os_map_ref/location.rb', line 31 def grid_easting easting.to_s[0].to_i end |
#grid_northing ⇒ Object
43 44 45 |
# 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 ⇒ Object
55 56 57 |
# File 'lib/os_map_ref/location.rb', line 55 def grid_reference_prefix grid[grid_northing][grid_easting] end |
#long_northing? ⇒ Boolean
39 40 41 |
# File 'lib/os_map_ref/location.rb', line 39 def long_northing? northing.to_s.length >= 7 end |
#map_reference ⇒ Object
23 24 25 |
# File 'lib/os_map_ref/location.rb', line 23 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’]
77 78 79 |
# File 'lib/os_map_ref/location.rb', line 77 def map_reference_parts @map_reference_parts ||= map_reference.split end |
#matrix ⇒ Object
100 101 102 |
# File 'lib/os_map_ref/location.rb', line 100 def matrix @matrix ||= Matrix[*grid] end |
#northing ⇒ Object
63 64 65 |
# File 'lib/os_map_ref/location.rb', line 63 def northing @northing ||= northing_from_map_reference end |
#northing_from_map_reference ⇒ Object
67 68 69 |
# 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 ⇒ Object
81 82 83 |
# File 'lib/os_map_ref/location.rb', line 81 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.
88 89 90 |
# File 'lib/os_map_ref/location.rb', line 88 def prefix_coordinates @prefix_coordinates ||= determine_prefix_coordinates end |
#raise_prefix_error(prefix) ⇒ Object
96 97 98 |
# 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) ⇒ Object
19 20 21 |
# File 'lib/os_map_ref/location.rb', line 19 def remove_decimals(number) number.to_s.sub(/\.\d*/, "") end |
#short_easting ⇒ Object
35 36 37 |
# File 'lib/os_map_ref/location.rb', line 35 def short_easting easting.to_s[1..-1] end |
#short_northing ⇒ Object
47 48 49 |
# File 'lib/os_map_ref/location.rb', line 47 def short_northing northing.to_s[chars_in_northing_start..-1] end |