module Tsuga::Model::Cluster

Concretions (provided by adapters) have the following accessors:

Respond to class methods:

Respond to the following instance methods:

Public Class Methods

included(by) click to toggle source
# File lib/tsuga/model/cluster.rb, line 126
def self.included(by)
  by.extend(ClassMethods)
end
new() click to toggle source
Calls superclass method
# File lib/tsuga/model/cluster.rb, line 25
def initialize
  super
  self.depth   ||= 1
  # equator/greenwich
  self.lat     ||= 0 
  self.lng     ||= 0
end

Public Instance Methods

density() click to toggle source

density (weight per unit area)

# 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) click to toggle source
Calls superclass method
# File lib/tsuga/model/cluster.rb, line 65
def depth=(value)
  super(value)
  _update_tilecode
  depth
end
dlat() click to toggle source

latitude deviation in cluster

# File lib/tsuga/model/cluster.rb, line 34
def dlat
  @_dlat ||= _safe_sqrt(ssq_lat/weight - (sum_lat/weight)**2)
end
dlng() click to toggle source

longitude deviation in cluster

# File lib/tsuga/model/cluster.rb, line 39
def dlng
  @_dlng ||= _safe_sqrt(ssq_lng/weight - (sum_lng/weight)**2)
end
geohash=(value) click to toggle source
Calls superclass method Tsuga::Model::PointTrait#geohash=
# File lib/tsuga/model/cluster.rb, line 59
def geohash=(value)
  super(value)
  _update_tilecode
  geohash
end
merge(other) click to toggle source
# 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
radius() click to toggle source

radius of cluster

# File lib/tsuga/model/cluster.rb, line 44
def radius
  @_radius ||= Math.sqrt(dlat ** 2 + dlng ** 2)
end

Private Instance Methods

_safe_sqrt(value) click to toggle source
# File lib/tsuga/model/cluster.rb, line 134
def _safe_sqrt(value)
  (value < 0) ? 0 : Math.sqrt(value)
end
_update_tilecode() click to toggle source
# File lib/tsuga/model/cluster.rb, line 139
def _update_tilecode
  if geohash && depth
    self.tilecode = prefix(depth)
  else
    self.tilecode = nil
  end
end