Module: Tsuga::Model::Cluster

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

Overview

Concretions (provided by adapters) have the following accessors:

  • :depth

  • :parent_id

  • :children_type (Record or Cluster)

  • :children_ids

  • :weight (count of Record in subtree)

  • :sum_lat, :sum_lng

  • :ssq_lat, :ssq_lng

Respond to class methods:

  • :in_tile(Tile) (scopish, response responds to :find_each)

  • :at_depth(depth)

  • :delete_all

  • :find(id)

Respond to the following instance methods:

  • :destroy

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PointTrait

#&, #=~, #distance_to, #inspect, #lat=, #lng=, #prefix

Class Method Details

.included(by) ⇒ Object



126
127
128
# File 'lib/tsuga/model/cluster.rb', line 126

def self.included(by)
  by.extend(ClassMethods)
end

Instance Method Details

#densityObject

density (weight per unit area)



49
50
51
52
53
54
55
56
57
# File 'lib/tsuga/model/cluster.rb', line 49

def density
  @_density ||= begin
    # min. radius 1.4e-4 (about 15m at european latitudes)
    # for 1-point clusters where density would otherwise be infinite
    our_radius = [radius, 1.4e-4].max 
    # Math.log(weight / (our_radius ** 2)) / Math.log(2)
    weight / (our_radius ** 2)
  end
end

#depth=(value) ⇒ Object



65
66
67
68
69
# File 'lib/tsuga/model/cluster.rb', line 65

def depth=(value)
  super(value)
  _update_tilecode
  depth
end

#dlatObject

latitude deviation in cluster



34
35
36
# File 'lib/tsuga/model/cluster.rb', line 34

def dlat
  @_dlat ||= _safe_sqrt(ssq_lat/weight - (sum_lat/weight)**2)
end

#dlngObject

longitude deviation in cluster



39
40
41
# File 'lib/tsuga/model/cluster.rb', line 39

def dlng
  @_dlng ||= _safe_sqrt(ssq_lng/weight - (sum_lng/weight)**2)
end

#geohash=(value) ⇒ Object



59
60
61
62
63
# File 'lib/tsuga/model/cluster.rb', line 59

def geohash=(value)
  super(value)
  _update_tilecode
  geohash
end

#initializeObject



25
26
27
28
29
30
31
# File 'lib/tsuga/model/cluster.rb', line 25

def initialize
  super
  self.depth   ||= 1
  # equator/greenwich
  self.lat     ||= 0 
  self.lng     ||= 0
end

#merge(other) ⇒ Object

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tsuga/model/cluster.rb', line 72

def merge(other)
  raise ArgumentError, 'not same depth'  unless depth == other.depth
  raise ArgumentError, 'not same parent' unless parent_id == other.parent_id

  self.weight  += other.weight
  self.sum_lat += other.sum_lat
  self.sum_lng += other.sum_lng
  self.ssq_lat += other.ssq_lat
  self.ssq_lng += other.ssq_lng
  self.lat      = sum_lat/weight
  self.lng      = sum_lng/weight
  self.children_ids += other.children_ids

  # dirty calculated values
  @_dlng = @_dlat = @_radius = @_density = nil
end

#radiusObject

radius of cluster



44
45
46
# File 'lib/tsuga/model/cluster.rb', line 44

def radius
  @_radius ||= Math.sqrt(dlat ** 2 + dlng ** 2)
end