Class: RedisGeo::City

Inherits:
Struct
  • Object
show all
Extended by:
MethodCache::ModuleExtensions
Includes:
GeoKit::Mappable
Defined in:
lib/redis_geo/city.rb

Constant Summary collapse

@@all =
[]
@@by_num =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ City

Returns a new instance of City.



9
10
11
12
13
14
# File 'lib/redis_geo/city.rb', line 9

def initialize(*args)
  super
  @ll = GeoKit::LatLng.normalize(lat, lng)
  @@all << self
  @@by_num[num] = self
end

Instance Attribute Details

#distanceObject

Returns the value of attribute distance.



5
6
7
# File 'lib/redis_geo/city.rb', line 5

def distance
  @distance
end

#latObject

Returns the value of attribute lat

Returns:

  • (Object)

    the current value of lat



2
3
4
# File 'lib/redis_geo/city.rb', line 2

def lat
  @lat
end

#llObject

Returns the value of attribute ll.



5
6
7
# File 'lib/redis_geo/city.rb', line 5

def ll
  @ll
end

#lngObject

Returns the value of attribute lng

Returns:

  • (Object)

    the current value of lng



2
3
4
# File 'lib/redis_geo/city.rb', line 2

def lng
  @lng
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



2
3
4
# File 'lib/redis_geo/city.rb', line 2

def name
  @name
end

#numObject

Returns the value of attribute num

Returns:

  • (Object)

    the current value of num



2
3
4
# File 'lib/redis_geo/city.rb', line 2

def num
  @num
end

Class Method Details

.[](lat, lng) ⇒ Object



29
30
31
32
# File 'lib/redis_geo/city.rb', line 29

def self.[](lat, lng)
  return unless thing = GeoKit::LatLng.normalize(lat, lng)
  @@all.min{ |a, b| a.ll.distance_to(thing) <=> b.ll.distance_to(thing) }
end

.allObject



20
21
22
# File 'lib/redis_geo/city.rb', line 20

def self.all
  @@all
end

.closest(lat, lng) ⇒ Object



40
41
42
43
44
# File 'lib/redis_geo/city.rb', line 40

def self.closest(lat, lng)
  nlat = (lat.to_f*2000.0).round/2000.0
  nlng = (lng.to_f*2000.0).round/2000.0
  closest_city_cached(nlat, nlng)
end

.closest_city_cached(lat, lng) ⇒ Object



34
35
36
# File 'lib/redis_geo/city.rb', line 34

def self.closest_city_cached(lat, lng)
  City[lat, lng]
end

.num(num) ⇒ Object



16
17
18
# File 'lib/redis_geo/city.rb', line 16

def self.num(num)
  @@by_num[num.to_i]
end

Instance Method Details

#city_nameObject

TODO: local city names, etc



65
66
67
# File 'lib/redis_geo/city.rb', line 65

def city_name
  @city_name ||= name.split(', ').first
end

#country_codeObject



73
74
75
# File 'lib/redis_geo/city.rb', line 73

def country_code
  @country_code ||= COUNTRY_CODES.index(country_name)
end

#country_nameObject

TODO: local city names, etc



60
61
62
# File 'lib/redis_geo/city.rb', line 60

def country_name
  @country_name ||= name.split(', ').last
end

#distance_to_ll(*ll) ⇒ Object



50
51
52
# File 'lib/redis_geo/city.rb', line 50

def distance_to_ll(*ll)
  GeoKit::LatLng.normalize(*ll).distance_to(to_lat_lng, :units => :kms) * 1000
end

#jsObject



24
25
26
27
# File 'lib/redis_geo/city.rb', line 24

def js
  args = [num, name, lat, lng].to_json[1..-2]
  "city(#{args});\n"
end

#province_codeObject



69
70
71
# File 'lib/redis_geo/city.rb', line 69

def province_code
  @province_code ||= name.split(', ')[1..-2].first
end

#synonymsObject



54
55
56
57
# File 'lib/redis_geo/city.rb', line 54

def synonyms
  suffixes = [province_code, country_code, country_name].compact
  ([city_name] + suffixes.map{ |suf| "#{city_name} #{suf}" }).map(&:downcase)
end

#timezoneObject



77
78
79
80
81
82
# File 'lib/redis_geo/city.rb', line 77

def timezone
  raise "no country code for #{country_name}" unless country_code
  TZInfo::Country.get(country_code).zone_info.min_by do |tz_country|
    (lng - tz_country.longitude).abs
  end.timezone
end

#to_lat_lngObject



46
47
48
# File 'lib/redis_geo/city.rb', line 46

def to_lat_lng
  Geokit::LatLng.new(lat, lng)
end