Module: Clusto

Extended by:
Clusto
Included in:
Clusto
Defined in:
lib/clusto.rb,
lib/clusto/point.rb,
lib/clusto/cluster.rb,
lib/clusto/distance.rb

Defined Under Namespace

Classes: Cluster, Distance, Point

Constant Summary collapse

INFINITY =
+1.0/0
RADIANS_PER_DEGREES =
Math::PI / 180
GREAT_CIRCLE_RADIUS =
6371
DEFAULT_GRID_SIZE =
8

Instance Method Summary collapse

Instance Method Details

#cluster(points, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/clusto.rb', line 21

def cluster(points, options={})
  bounds = options[:bounds] || Clusto.minimum_bounding_rectangle(points)
  distance_method = options[:distance_method] || :haversine
  grid_size = options[:grid_size] || DEFAULT_GRID_SIZE
  
  # Create clusters
  
  clusters = []
  
  x = bounds[0]
  y = bounds[3]
  w = bounds[2] - bounds[0]
  h = bounds[3] - bounds[1]
  grid_w = w / grid_size
  grid_h = h / grid_size
  
  column = 0
  row = 0
  
  (grid_size ** 2).times do |i|
    offset_x = x + (column * grid_w)
    offset_y = y - (row * grid_h)
    
    center = Clusto::Point.new(offset_x + (grid_w / 2), offset_y - (grid_h / 2))
    clusters.push(Clusto::Cluster.new(center))
    
    column = column+1
    if column == grid_size
      column = 0
      row = row+1
    end
  end
  
  # Assign points to clusters
  
  points.each do |point|
    distance = INFINITY
    nearest = nil

    clusters.each do |cluster|
      begin
        d = Clusto::Distance.send(distance_method, cluster.location, point)
      rescue NoMethodError
        raise "Unknown distance method '#{distance_method}."
      end
      
      if d < distance
        distance = d
        nearest = cluster
      end
    end

    nearest.points.push(point)
  end

  # Reposition clusters
  
  clusters.each { |c| c.reposition }
end

#minimum_bounding_rectangle(points) ⇒ Object



14
15
16
17
18
19
# File 'lib/clusto.rb', line 14

def minimum_bounding_rectangle(points)
  xs = points.collect { |p| p.x }.minmax
  ys = points.collect { |p| p.y }.minmax
  
  [xs[0], ys[0], xs[1], ys[1]]
end