Class: GeoHex::Zone

Inherits:
Object
  • Object
show all
Defined in:
lib/geo_hex/zone.rb

Overview

A positioned instance of a Unit, within a level-grid

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, level) ⇒ Zone

Returns a new instance of Zone.

Parameters:

  • x (Integer)

    the horizontal index

  • y (Integer)

    the vertical index

  • level (Integer)

    the level



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

#eastingFloat (readonly)

Returns the mercator easting.

Returns:

  • (Float)

    the mercator easting



16
17
18
# File 'lib/geo_hex/zone.rb', line 16

def easting
  @easting
end

#northingFloat (readonly)

Returns the mercator northing.

Returns:

  • (Float)

    the mercator northing



13
14
15
# File 'lib/geo_hex/zone.rb', line 13

def northing
  @northing
end

#unitGeoHex::Unit (readonly)

Returns the associated unit.

Returns:



10
11
12
# File 'lib/geo_hex/zone.rb', line 10

def unit
  @unit
end

#xInteger (readonly)

Returns the zone coordinates within the grid.

Returns:

  • (Integer)

    the zone coordinates within the grid



7
8
9
# File 'lib/geo_hex/zone.rb', line 7

def x
  @x
end

#yInteger (readonly)

Returns the zone coordinates within the grid.

Returns:

  • (Integer)

    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.

Parameters:

  • other (Zone, String)

    another Zone or a GeoHex code (String)

Returns:

  • (Boolean)

    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

#codeString Also known as: to_s

Returns GeoHex code.

Returns:

  • (String)

    GeoHex code



45
46
47
# File 'lib/geo_hex/zone.rb', line 45

def code
  @code ||= encode
end

#hashFixnum

Returns the object hash.

Returns:

  • (Fixnum)

    the object hash



98
99
100
# File 'lib/geo_hex/zone.rb', line 98

def hash
  [x, y, level].hash
end

#latFloat

Returns the latitude coordinate.

Returns:

  • (Float)

    the latitude coordinate



40
41
42
# File 'lib/geo_hex/zone.rb', line 40

def lat
  point.lat
end

#levelInteger

Returns the level.

Returns:

  • (Integer)

    the level



30
31
32
# File 'lib/geo_hex/zone.rb', line 30

def level
  unit.level
end

#lonFloat

Returns the longitude coordinate.

Returns:

  • (Float)

    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.

Returns:

  • (Boolean)

    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.

Parameters:

  • range (Integer) (defaults to: 1)

    the number of zones to search within

Returns:



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

#pointGeoHex::PP

Returns zone center, point coordinates.

Returns:



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.

Returns:



56
57
58
# File 'lib/geo_hex/zone.rb', line 56

def polygon
  @polygon ||= GeoHex::Polygon.new(easting, northing, unit.size)
end

#to_aArray

Returns x, y & level tuple.

Returns:

  • (Array)

    x, y & level tuple



103
104
105
# File 'lib/geo_hex/zone.rb', line 103

def to_a
  [x, y, level]
end