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_length ⇒ Object
- #cluster_options ⇒ Object
- #cluster_width ⇒ 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 @options = || {} @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 @options 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
62 63 64 |
# File 'lib/clumpy/builder.rb', line 62 def cluster_class @cluster_class ||= ([:cluster_class] || Clumpy::Cluster) end |
#cluster_length ⇒ Object
49 50 51 |
# File 'lib/clumpy/builder.rb', line 49 def cluster_length @cluster_length ||= latitude_distance / @distance_modifier end |
#cluster_options ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/clumpy/builder.rb', line 53 def { values_threshold: [:values_threshold], include_values: [:include_values], width: cluster_width, length: cluster_length, } end |
#cluster_width ⇒ Object
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_distance ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/clumpy/builder.rb', line 66 def latitude_distance if [:nelat] && [:swlat] ([:nelat] - [:swlat]).abs else MAX_LATITUDE_DISTANCE end end |
#longitude_distance ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/clumpy/builder.rb', line 74 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 |