Class: Kisyo::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/kisyo/location.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefecture_id, block_id) ⇒ Location

Returns a new instance of Location.



30
31
32
33
# File 'lib/kisyo/location.rb', line 30

def initialize(prefecture_id, block_id)
  @prefecture_id = prefecture_id
  @block_id = block_id
end

Instance Attribute Details

#block_idObject (readonly)

Returns the value of attribute block_id.



6
7
8
# File 'lib/kisyo/location.rb', line 6

def block_id
  @block_id
end

#prefecture_idObject (readonly)

Returns the value of attribute prefecture_id.



6
7
8
# File 'lib/kisyo/location.rb', line 6

def prefecture_id
  @prefecture_id
end

Class Method Details

.distance(lat1, lng1, lat2, lng2) ⇒ Object



8
9
10
# File 'lib/kisyo/location.rb', line 8

def self.distance(lat1, lng1, lat2, lng2)
  Geocoder::Calculations.distance_between([lat1, lng1], [lat2, lng2])
end

.dms_to_degrees(d, m, s = 0) ⇒ Object



12
13
14
# File 'lib/kisyo/location.rb', line 12

def self.dms_to_degrees(d, m, s = 0)
  d + m / 60.0 + s / 3600.0
end

.nearest(lat, lng) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kisyo/location.rb', line 16

def self.nearest(lat, lng)
  distances =
    BLOCKS.map do |block|
      la = dms_to_degrees(block[3].to_f, block[4].to_f)
      ln = dms_to_degrees(block[5].to_f, block[6].to_f)

      [distance(lat, lng, la, ln), block]
    end

  block = distances.min_by { |(dist, _)| dist }[1]

  new(block[1], block[2])
end