Module: RedisGeo

Extended by:
MethodCache::ModuleExtensions
Defined in:
lib/redis_geo.rb,
lib/redis_geo/city.rb,
lib/redis_geo/locstring.rb

Defined Under Namespace

Modules: Locstring Classes: City, PretendGeo

Class Method Summary collapse

Class Method Details

.basic_geocode(loc, cc2 = nil) ⇒ Object



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

def basic_geocode(loc, cc2 = nil)
  g = GeoKit::Geocoders::GoogleGeocoder.geocode(loc, :bias => cc2)
  g if g && g.success
end

.city_radial_geocode(city_num, loc) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/redis_geo.rb', line 21

def city_radial_geocode(city_num, loc)
  spot = City.num(city_num).to_lat_lng
  [2, 5, 10, 30].each do |distance_in_miles|
    bounds = Geokit::Bounds.from_point_and_radius(spot, distance_in_miles)
    geoloc = GeoKit::Geocoders::GoogleGeocoder.geocode(loc, :bias => bounds) rescue nil
    next unless geoloc and geoloc.success and geoloc.distance_to(spot) < distance_in_miles
    geoloc
  end
end

.universal_geocode(loc, city = nil, delegate = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/redis_geo.rb', line 34

def universal_geocode(loc, city=nil, delegate=nil)
  loc.strip!
  loc.extend Locstring
  loc.blank? and return
  ll = loc.lat_lng? and return PretendGeo.new(ll.first, ll.last, 'building')
  city, loc = loc.city, loc.noncity_part if loc.city
  city = nil if loc.obviously_worldwide?
  loc = loc.normalized

  result = nil
  if city
    result ||= delegate && delegate.call(loc, city)
    result ||= basic_geocode("#{loc}, #{city.name}")
    result ||= radial_geocode(city.num, loc)
  end
  result ||= delegate && delegate.call(loc, nil)
  result ||= basic_geocode(loc)
  return result
end