Class: Osgb::Gridref
- Inherits:
-
Object
- Object
- Osgb::Gridref
- Defined in:
- lib/osgb/gridref.rb
Overview
Implementation derived from the Ordnance Survey guide to coordinate systems in the UK www.ordnancesurvey.co.uk/oswebsite/gps/information/coordinatesystemsinfo/guidecontents/ with help from CPAN module Geography::NationalGrid by and © P Kent
Constant Summary collapse
- OS_TILES =
maps OS letter codes onto their coordinates in the master grid
{ :a => [0,4], :b => [1,4], :c => [2,4], :d => [3,4], :e => [4,4], :f => [0,3], :g => [1,3], :h => [2,3], :j => [3,3], :k => [4,3], :l => [0,2], :m => [1,2], :n => [2,2], :o => [3,2], :p => [4,2], :q => [0,1], :r => [1,1], :s => [2,1], :t => [3,1], :u => [4,1], :v => [0,0], :w => [1,0], :x => [2,0], :y => [3,0], :z => [4,0], }
- FALSE_ORIGIN =
the offset makes all coordinates positive and <1000km
{:e => 2, :n => 1}
- SQUARE_SIZE =
a shorter grid ref denotes a larger square
[nil, 10000, 1000, 100, 10, 1]
- @@default_datum =
:osgb36
- @@iteration_ceiling =
1000
- @@defaults =
{ :projection => :gb, # mercator projection of input gridref. Can be any projection name: usually :ie or :gb :precision => 6, # decimal places in the output lat/long }
Instance Attribute Summary collapse
-
#ellipsoid ⇒ Object
Returns the value of attribute ellipsoid.
-
#gridref ⇒ Object
Returns the value of attribute gridref.
-
#options ⇒ Object
Returns the value of attribute options.
-
#precision ⇒ Object
Returns the value of attribute precision.
-
#projection ⇒ Object
Returns the value of attribute projection.
Class Method Summary collapse
Instance Method Summary collapse
- #digits ⇒ Object
- #easting ⇒ Object
-
#initialize(string, options = {}) ⇒ Gridref
constructor
A new instance of Gridref.
- #lat(datum = nil) ⇒ Object
- #lng(datum = nil) ⇒ Object
- #northing ⇒ Object
- #offsets ⇒ Object
- #resolution ⇒ Object
- #tile ⇒ Object
-
#to_latlng(datum = nil) ⇒ Object
Returns an Osgb::Point corresponding to this grid reference and lying on the specified datum.
- #to_s ⇒ Object
Constructor Details
#initialize(string, options = {}) ⇒ Gridref
Returns a new instance of Gridref.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/osgb/gridref.rb', line 38 def initialize(string, ={}) raise ArgumentError, "invalid grid reference string '#{string}'." unless string.is_gridref? = @@defaults.merge() @gridref = string.upcase @projection = Osgb::Projection[[:projection]] @precision = [:precision] @ellipsoid = @projection.ellipsoid @datum = [:datum] self end |
Instance Attribute Details
#ellipsoid ⇒ Object
Returns the value of attribute ellipsoid.
23 24 25 |
# File 'lib/osgb/gridref.rb', line 23 def ellipsoid @ellipsoid end |
#gridref ⇒ Object
Returns the value of attribute gridref.
23 24 25 |
# File 'lib/osgb/gridref.rb', line 23 def gridref @gridref end |
#options ⇒ Object
Returns the value of attribute options.
23 24 25 |
# File 'lib/osgb/gridref.rb', line 23 def @options end |
#precision ⇒ Object
Returns the value of attribute precision.
23 24 25 |
# File 'lib/osgb/gridref.rb', line 23 def precision @precision end |
#projection ⇒ Object
Returns the value of attribute projection.
23 24 25 |
# File 'lib/osgb/gridref.rb', line 23 def projection @projection end |
Class Method Details
.iteration_ceiling ⇒ Object
33 34 35 |
# File 'lib/osgb/gridref.rb', line 33 def iteration_ceiling @@iteration_ceiling end |
Instance Method Details
#digits ⇒ Object
53 54 55 |
# File 'lib/osgb/gridref.rb', line 53 def digits @digits ||= gridref[2,10] end |
#easting ⇒ Object
74 75 76 |
# File 'lib/osgb/gridref.rb', line 74 def easting @east ||= offsets[:e] + digits[0, resolution].to_i * SQUARE_SIZE[resolution] end |
#lat(datum = nil) ⇒ Object
82 83 84 |
# File 'lib/osgb/gridref.rb', line 82 def lat(datum=nil) to_latlng(datum).lat end |
#lng(datum = nil) ⇒ Object
86 87 88 |
# File 'lib/osgb/gridref.rb', line 86 def lng(datum=nil) to_latlng(datum).lng end |
#northing ⇒ Object
78 79 80 |
# File 'lib/osgb/gridref.rb', line 78 def northing @north ||= offsets[:n] + digits[resolution, resolution].to_i * SQUARE_SIZE[resolution] end |
#offsets ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/osgb/gridref.rb', line 61 def offsets if tile major = OS_TILES[tile[0,1].downcase.to_sym ] minor = OS_TILES[tile[1,1].downcase.to_sym] @offset ||= { :e => (500000 * (major[0] - FALSE_ORIGIN[:e])) + (100000 * minor[0]), :n => (500000 * (major[1] - FALSE_ORIGIN[:n])) + (100000 * minor[1]) } else { :e => 0, :n => 0 } end end |
#resolution ⇒ Object
57 58 59 |
# File 'lib/osgb/gridref.rb', line 57 def resolution @resolution ||= digits.length / 2 end |
#tile ⇒ Object
49 50 51 |
# File 'lib/osgb/gridref.rb', line 49 def tile @tile ||= gridref[0,2] end |
#to_latlng(datum = nil) ⇒ Object
Returns an Osgb::Point corresponding to this grid reference and lying on the specified datum. We default to WGS84 since that is the representation most likely to be useful.
97 98 99 100 |
# File 'lib/osgb/gridref.rb', line 97 def to_latlng(datum=nil) datum ||= :wgs84 point.transform_to(datum) end |
#to_s ⇒ Object
90 91 92 |
# File 'lib/osgb/gridref.rb', line 90 def to_s gridref.to_s end |