Class: Clumpy::Builder
- Inherits:
-
Object
- Object
- Clumpy::Builder
- Defined in:
- lib/clumpy/builder.rb
Constant Summary collapse
- MAX_LATITUDE_DISTANCE =
170.05115- MAX_LONGITUDE_DISTANCE =
360
Instance Attribute Summary collapse
-
#clusters ⇒ Object
Returns the value of attribute clusters.
-
#options ⇒ Object
Returns the value of attribute options.
-
#points ⇒ Object
Returns the value of attribute points.
Instance Method Summary collapse
- #add_to_cluster(point) ⇒ Object
-
#cluster ⇒ Object
Clusters the given points.
- #cluster_class ⇒ Object
- #cluster_options ⇒ Object
- #cluster_side_length ⇒ Object
- #find_parent_cluster(point) ⇒ Object
-
#initialize(points, options = {}) ⇒ Builder
constructor
A new instance of Builder.
- #latitude_distance ⇒ Object
- #longitude_distance ⇒ Object
- #useable_point?(point) ⇒ Boolean
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, = {}) @points = points = || {} @distance_modifier = .fetch(:distance_modifier) { 16 } @clusters = [] end |
Instance Attribute Details
#clusters ⇒ Object
Returns the value of attribute clusters.
6 7 8 |
# File 'lib/clumpy/builder.rb', line 6 def clusters @clusters end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/clumpy/builder.rb', line 6 def end |
#points ⇒ Object
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, ) end end |
#cluster ⇒ Object
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) } [:precision] == :high and clusters.each(&:reposition) clusters end |
#cluster_class ⇒ Object
57 58 59 |
# File 'lib/clumpy/builder.rb', line 57 def cluster_class @cluster_class ||= ([:cluster_class] || Clumpy::Cluster) end |
#cluster_options ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/clumpy/builder.rb', line 49 def { values_threshold: [:values_threshold], include_values: [:include_values], side_length: cluster_side_length } end |
#cluster_side_length ⇒ Object
45 46 47 |
# File 'lib/clumpy/builder.rb', line 45 def cluster_side_length @cluster_size ||= (latitude_distance + 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_distance ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/clumpy/builder.rb', line 61 def latitude_distance if [:nelat] && [:swlat] ([:nelat] - [:swlat]).abs else MAX_LATITUDE_DISTANCE end end |
#longitude_distance ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/clumpy/builder.rb', line 69 def longitude_distance if [:nelng] && [:swlng] ([:nelng] - [:swlng]).abs else MAX_LONGITUDE_DISTANCE end end |
#useable_point?(point) ⇒ Boolean
37 38 39 |
# File 'lib/clumpy/builder.rb', line 37 def useable_point?(point) point.respond_to?(:latitude) && point.respond_to?(:longitude) end |