class NearestTimeZone::City

Attributes

id[RW]
latitude[RW]
longitude[RW]
time_zone_id[RW]

Public Class Methods

all() click to toggle source
# File lib/nearest_time_zone_jruby/city.rb, line 26
def self.all
  @all ||= load_all
end
find(id) click to toggle source
# File lib/nearest_time_zone_jruby/city.rb, line 30
def self.find(id)
  all[id.to_i]
end
kdtree() click to toggle source
# File lib/nearest_time_zone_jruby/city.rb, line 10
def self.kdtree
  @kdtree ||= build_kdtree
end
nearest(latitude, longitude) click to toggle source
# File lib/nearest_time_zone_jruby/city.rb, line 34
def self.nearest(latitude, longitude)
  result = kdtree.nearest([latitude, longitude])
  all[result.data] if result
end
new(id, latitude, longitude, time_zone_id) click to toggle source
# File lib/nearest_time_zone_jruby/city.rb, line 6
def initialize(id, latitude, longitude, time_zone_id)
  self.id, self.latitude, self.longitude, self.time_zone_id = id, latitude, longitude, time_zone_id
end

Private Class Methods

build_kdtree() click to toggle source
# File lib/nearest_time_zone_jruby/city.rb, line 58
def self.build_kdtree
  begin
    Marshal.load(File.read(File.expand_path("../../../data/cities.dump", __FILE__)))
  rescue
    Geokdtree::Tree.new(2).tap do |tree|
      all.each { |_, city| tree.insert [city.latitude, city.longitude], city.id }
    end
  end
end
load_all() click to toggle source
# File lib/nearest_time_zone_jruby/city.rb, line 45
def self.load_all
     begin
       Marshal.load(File.read(File.expand_path("../../../data/cities.dump", __FILE__)))
     rescue
       cities = CSV.open(File.expand_path("../../../data/cities.txt", __FILE__))
       Hash[
         cities.collect do |city|
           [city[0].to_i, NearestTimeZone::City.new(*(0..3).collect { |n| city[n] })]
         end
       ]
     end
   end

Public Instance Methods

id=(value) click to toggle source
# File lib/nearest_time_zone_jruby/city.rb, line 14
def id=(value)
  @id = value.to_i
end
latitude=(value) click to toggle source
# File lib/nearest_time_zone_jruby/city.rb, line 18
def latitude=(value)
  @latitude = value.to_f
end
longitude=(value) click to toggle source
# File lib/nearest_time_zone_jruby/city.rb, line 22
def longitude=(value)
  @longitude = value.to_f
end
time_zone() click to toggle source
# File lib/nearest_time_zone_jruby/city.rb, line 39
def time_zone
  TimeZone.find(time_zone_id)
end