Module: Tsuga::Model::Cluster::ClassMethods

Defined in:
lib/tsuga/model/cluster.rb

Instance Method Summary collapse

Instance Method Details

#build_from(depth, other) ⇒ Object

Cluster factory. other is either a Cluster or a Record

FIXME: there’s a potential for overflow here on large datasets on the sum- and sum-of-squares fields. it can be mitigated by using double-precision fields, or calculating sums only on the children (instead of the subtree)



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/tsuga/model/cluster.rb', line 97

def build_from(depth, other)
  c = new()
  c.depth = depth

  c.lat           = other.lat
  c.lng           = other.lng
  c.children_ids  = [other.id]
  c.children_type = other.class.name

  case other
  when Cluster
    c.weight      = other.weight
    c.sum_lng     = other.sum_lng
    c.sum_lat     = other.sum_lat
    c.ssq_lng     = other.ssq_lng
    c.ssq_lat     = other.ssq_lat
  else
    c.weight      = 1
    c.sum_lng     = other.lng
    c.sum_lat     = other.lat
    c.ssq_lng     = other.lng ** 2
    c.ssq_lat     = other.lat ** 2
  end

  c.geohash # force geohash calculation
  return c
end