Class: RedisGeo::City
- Inherits:
-
Struct
- Object
- Struct
- RedisGeo::City
- Extended by:
- MethodCache::ModuleExtensions
- Includes:
- GeoKit::Mappable
- Defined in:
- lib/redis_geo/city.rb
Constant Summary collapse
- @@all =
[]
- @@by_num =
[]
Instance Attribute Summary collapse
-
#distance ⇒ Object
Returns the value of attribute distance.
-
#lat ⇒ Object
Returns the value of attribute lat.
-
#ll ⇒ Object
Returns the value of attribute ll.
-
#lng ⇒ Object
Returns the value of attribute lng.
-
#name ⇒ Object
Returns the value of attribute name.
-
#num ⇒ Object
Returns the value of attribute num.
Class Method Summary collapse
- .[](lat, lng) ⇒ Object
- .all ⇒ Object
- .closest(lat, lng) ⇒ Object
- .closest_city_cached(lat, lng) ⇒ Object
- .num(num) ⇒ Object
Instance Method Summary collapse
-
#city_name ⇒ Object
TODO: local city names, etc.
- #country_code ⇒ Object
-
#country_name ⇒ Object
TODO: local city names, etc.
- #distance_to_ll(*ll) ⇒ Object
-
#initialize(*args) ⇒ City
constructor
A new instance of City.
- #js ⇒ Object
- #province_code ⇒ Object
- #synonyms ⇒ Object
- #timezone ⇒ Object
- #to_lat_lng ⇒ Object
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
#distance ⇒ Object
Returns the value of attribute distance.
5 6 7 |
# File 'lib/redis_geo/city.rb', line 5 def distance @distance end |
#lat ⇒ Object
Returns the value of attribute lat
2 3 4 |
# File 'lib/redis_geo/city.rb', line 2 def lat @lat end |
#ll ⇒ Object
Returns the value of attribute ll.
5 6 7 |
# File 'lib/redis_geo/city.rb', line 5 def ll @ll end |
#lng ⇒ Object
Returns the value of attribute lng
2 3 4 |
# File 'lib/redis_geo/city.rb', line 2 def lng @lng end |
#name ⇒ Object
Returns the value of attribute name
2 3 4 |
# File 'lib/redis_geo/city.rb', line 2 def name @name end |
#num ⇒ Object
Returns the value of attribute 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 |
.all ⇒ Object
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_name ⇒ Object
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_code ⇒ Object
73 74 75 |
# File 'lib/redis_geo/city.rb', line 73 def country_code @country_code ||= COUNTRY_CODES.index(country_name) end |
#country_name ⇒ Object
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 |
#js ⇒ Object
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_code ⇒ Object
69 70 71 |
# File 'lib/redis_geo/city.rb', line 69 def province_code @province_code ||= name.split(', ')[1..-2].first end |
#synonyms ⇒ Object
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 |
#timezone ⇒ Object
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_lng ⇒ Object
46 47 48 |
# File 'lib/redis_geo/city.rb', line 46 def to_lat_lng Geokit::LatLng.new(lat, lng) end |