Module: Chimera::GeoIndexes::ClassMethods

Defined in:
lib/chimera/geo_indexes.rb

Instance Method Summary collapse

Instance Method Details

#find_with_geo_index(name, opts_or_query) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chimera/geo_indexes.rb', line 19

def find_with_geo_index(name, opts_or_query)
  if props = self.defined_indexes[name.to_sym]
    case props[:type]
    when :geo then
      step_size = props[:step_size]
      num_steps = opts_or_query[:steps] || 5
      steps = [50,num_steps].min * step_size
      lat, lon = opts_or_query[:coordinate]
      union_keys = []
      curr_lat = lat - steps
      while curr_lat < lat+steps
        curr_lon = lon - steps
        while curr_lon < lon+steps
          union_keys << key_for_geo_index(:geo,name,curr_lat,curr_lon,step_size)
          curr_lon += step_size
        end
        curr_lat += step_size
      end
      keys = self.connection(:redis).sunion(union_keys.join(" "))
      find_many(keys)
    end # case
  end # if props =
end

#geo_square_coord(lat_or_lon, step = 0.05) ⇒ Object



43
44
45
46
47
# File 'lib/chimera/geo_indexes.rb', line 43

def geo_square_coord(lat_or_lon, step=0.05)
  i = (lat_or_lon*1000000).floor
  i += (step/2)*1000000
  (i - (i % (step * 1000000)))/1000000
end

#key_for_geo_index(type, name, lat, lon, step_size = 0.05) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/chimera/geo_indexes.rb', line 9

def key_for_geo_index(type, name, lat, lon, step_size=0.05)
  step_size ||= 0.05
  case type.to_sym
  when :geo then
    lat = geo_square_coord(lat)
    lon = geo_square_coord(lon)
    "#{self.to_s}::Indexes::#{type}::#{name}::#{step_size}::#{lat}::#{lon}"
  end
end