Class: GeoHex::Zone
- Inherits:
-
Object
- Object
- GeoHex::Zone
- Defined in:
- lib/geo_hex/zone.rb
Overview
A positioned instance of a Unit, within a level-grid
Instance Attribute Summary collapse
-
#easting ⇒ Float
readonly
The mercator easting.
-
#northing ⇒ Float
readonly
The mercator northing.
-
#unit ⇒ GeoHex::Unit
readonly
The associated unit.
-
#x ⇒ Integer
readonly
The zone coordinates within the grid.
-
#y ⇒ Integer
readonly
The zone coordinates within the grid.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
True, if given Zone or GeoHex code (String) matches self.
-
#code ⇒ String
(also: #to_s)
GeoHex code.
-
#hash ⇒ Fixnum
The object hash.
-
#initialize(x, y, level) ⇒ Zone
constructor
A new instance of Zone.
-
#lat ⇒ Float
The latitude coordinate.
-
#level ⇒ Integer
The level.
-
#lon ⇒ Float
The longitude coordinate.
-
#meridian_180? ⇒ Boolean
True if the zone is placed on the 180th meridian.
-
#neighbours(range = 1) ⇒ Array<GeoHex::Zone>
(also: #neighbors)
The neighbouring zones.
-
#point ⇒ GeoHex::PP
Zone center, point coordinates.
-
#polygon ⇒ <GeoHex::Polygon>
Zone’s NE, E, SE, SW, W and NW points.
-
#to_a ⇒ Array
X, y & level tuple.
Constructor Details
#initialize(x, y, level) ⇒ Zone
Returns a new instance of Zone.
21 22 23 24 25 26 27 |
# File 'lib/geo_hex/zone.rb', line 21 def initialize(x, y, level) @x, @y = x, y @unit = Unit[level] @northing = (H_K * @x * @unit.width + @y * @unit.height) / 2.0 @easting = (@northing - @y * @unit.height) / H_K @x, @y = @y, @x if meridian_180? end |
Instance Attribute Details
#easting ⇒ Float (readonly)
Returns the mercator easting.
16 17 18 |
# File 'lib/geo_hex/zone.rb', line 16 def easting @easting end |
#northing ⇒ Float (readonly)
Returns the mercator northing.
13 14 15 |
# File 'lib/geo_hex/zone.rb', line 13 def northing @northing end |
#unit ⇒ GeoHex::Unit (readonly)
Returns the associated unit.
10 11 12 |
# File 'lib/geo_hex/zone.rb', line 10 def unit @unit end |
#x ⇒ Integer (readonly)
Returns the zone coordinates within the grid.
7 8 9 |
# File 'lib/geo_hex/zone.rb', line 7 def x @x end |
#y ⇒ Integer (readonly)
Returns the zone coordinates within the grid.
7 8 9 |
# File 'lib/geo_hex/zone.rb', line 7 def y @y end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Returns true, if given Zone or GeoHex code (String) matches self.
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/geo_hex/zone.rb', line 85 def ==(other) case other when String to_s == other when self.class x == other.x && y == other.y && level == other.level else super end end |
#code ⇒ String Also known as: to_s
Returns GeoHex code.
45 46 47 |
# File 'lib/geo_hex/zone.rb', line 45 def code @code ||= encode end |
#hash ⇒ Fixnum
Returns the object hash.
98 99 100 |
# File 'lib/geo_hex/zone.rb', line 98 def hash [x, y, level].hash end |
#lat ⇒ Float
Returns the latitude coordinate.
40 41 42 |
# File 'lib/geo_hex/zone.rb', line 40 def lat point.lat end |
#level ⇒ Integer
Returns the level.
30 31 32 |
# File 'lib/geo_hex/zone.rb', line 30 def level unit.level end |
#lon ⇒ Float
Returns the longitude coordinate.
35 36 37 |
# File 'lib/geo_hex/zone.rb', line 35 def lon meridian_180? ? 180.0 : point.lon end |
#meridian_180? ⇒ Boolean
Returns true if the zone is placed on the 180th meridian.
108 109 110 111 |
# File 'lib/geo_hex/zone.rb', line 108 def meridian_180? return @meridian_180 if defined?(@meridian_180) @meridian_180 = H_BASE - easting < unit.size end |
#neighbours(range = 1) ⇒ Array<GeoHex::Zone> Also known as: neighbors
Returns the neighbouring zones.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/geo_hex/zone.rb', line 62 def neighbours(range = 1) zones = [] x0, xn = x - range, x + range x0.upto(xn) do |xi| zones << self.class.new(xi, y, level) unless xi == x end 1.upto(range) do |i| y+i % 2 == 1 ? xn-=1 : x0+=1 x0.upto(xn) do |xi| zones << self.class.new(xi, y+i, level) zones << self.class.new(xi-i, y-i, level) end end zones end |
#point ⇒ GeoHex::PP
Returns zone center, point coordinates.
51 52 53 |
# File 'lib/geo_hex/zone.rb', line 51 def point @point ||= GeoHex::PP.new(easting, northing) end |
#polygon ⇒ <GeoHex::Polygon>
Returns Zone’s NE, E, SE, SW, W and NW points.
56 57 58 |
# File 'lib/geo_hex/zone.rb', line 56 def polygon @polygon ||= GeoHex::Polygon.new(easting, northing, unit.size) end |
#to_a ⇒ Array
Returns x, y & level tuple.
103 104 105 |
# File 'lib/geo_hex/zone.rb', line 103 def to_a [x, y, level] end |