Class: Clumpy::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/clumpy/builder.rb

Constant Summary collapse

MAX_LATITUDE_DISTANCE =
170.05115
MAX_LONGITUDE_DISTANCE =
360

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(points, options = {}) ⇒ Builder

Returns a new instance of Builder.



8
9
10
11
12
13
# File 'lib/clumpy/builder.rb', line 8

def initialize(points, options = {})
  @points            = points
  @options           = options || {}
  @distance_modifier = options.fetch(:distance_modifier) { 16 }
  @clusters          = []
end

Instance Attribute Details

#clustersObject

Returns the value of attribute clusters.



6
7
8
# File 'lib/clumpy/builder.rb', line 6

def clusters
  @clusters
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/clumpy/builder.rb', line 6

def options
  @options
end

#pointsObject

Returns the value of attribute points.



6
7
8
# File 'lib/clumpy/builder.rb', line 6

def points
  @points
end

Instance Method Details

#add_to_cluster(point) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/clumpy/builder.rb', line 26

def add_to_cluster(point)
  useable_point?(point) or return
  parent_cluster = find_parent_cluster(point)

  if parent_cluster
    parent_cluster.points << point
  else
    clusters << cluster_class.new(point, cluster_options)
  end
end

#clusterObject

Clusters the given points

Returns:

An array of cluster objects.



20
21
22
23
24
# File 'lib/clumpy/builder.rb', line 20

def cluster
  points.each { |point| add_to_cluster(point) }
  options[:precision] == :high and clusters.each(&:reposition)
  clusters
end

#cluster_classObject



62
63
64
# File 'lib/clumpy/builder.rb', line 62

def cluster_class
  @cluster_class ||= (options[:cluster_class] || Clumpy::Cluster)
end

#cluster_lengthObject



49
50
51
# File 'lib/clumpy/builder.rb', line 49

def cluster_length
  @cluster_length ||= latitude_distance / @distance_modifier
end

#cluster_optionsObject



53
54
55
56
57
58
59
60
# File 'lib/clumpy/builder.rb', line 53

def cluster_options
  {
    values_threshold: options[:values_threshold],
    include_values: options[:include_values],
    width: cluster_width,
    length: cluster_length,
  }
end

#cluster_widthObject



45
46
47
# File 'lib/clumpy/builder.rb', line 45

def cluster_width
  @cluster_width ||= longitude_distance / @distance_modifier
end

#find_parent_cluster(point) ⇒ Object



41
42
43
# File 'lib/clumpy/builder.rb', line 41

def find_parent_cluster(point)
  clusters.find { |c| c.contains?(point) }
end

#latitude_distanceObject



66
67
68
69
70
71
72
# File 'lib/clumpy/builder.rb', line 66

def latitude_distance
  if options[:nelat] && options[:swlat]
    (options[:nelat] - options[:swlat]).abs
  else
    MAX_LATITUDE_DISTANCE
  end
end

#longitude_distanceObject



74
75
76
77
78
79
80
# File 'lib/clumpy/builder.rb', line 74

def longitude_distance
  if options[:nelng] && options[:swlng]
    (options[:nelng] - options[:swlng]).abs
  else
    MAX_LONGITUDE_DISTANCE
  end
end

#useable_point?(point) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/clumpy/builder.rb', line 37

def useable_point?(point)
  point.respond_to?(:latitude) && point.respond_to?(:longitude)
end