clumpy 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/.travis.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/lib/clumpy/bounds.rb +5 -4
- data/lib/clumpy/builder.rb +8 -3
- data/lib/clumpy/cluster_behavior.rb +13 -2
- data/lib/clumpy/version.rb +1 -1
- data/spec/builder_spec.rb +21 -21
- data/spec/cluster_behavior_spec.rb +18 -18
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 08e7eff2f7ea8e5cbfdfaeaf08844d831bb331d0
|
4
|
+
data.tar.gz: 906992f3fb8f0807b8f8e4a7637c5704ff56615e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1dd17137418eebd0270b3f8f8f97b99c2b98b6d574d50b54462c37b952f070c6a085a63e69337187736d07f3febe846955d3223a7f01e306c890591ccd3cc6af
|
7
|
+
data.tar.gz: 72ed5212c54be152b171a9067a57c3df44f8b8c1c3af54a6ac49aa22adc472dc0c89a727d32d9680ea1a7e4249d4c2becd1f4bcf5fcb57b6fe485371ce876ac9
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.2.3
|
data/.travis.yml
ADDED
data/CHANGELOG.md
CHANGED
data/lib/clumpy/bounds.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
module Clumpy
|
2
2
|
class Bounds
|
3
|
-
def initialize(latitude, longitude,
|
4
|
-
|
5
|
-
|
6
|
-
@
|
3
|
+
def initialize(latitude, longitude, width, length)
|
4
|
+
l = length / 2
|
5
|
+
w = width / 2
|
6
|
+
@latitude = (latitude - l)..(latitude + l)
|
7
|
+
@longitude = (longitude - w)..(longitude + w)
|
7
8
|
end
|
8
9
|
|
9
10
|
attr_reader :latitude
|
data/lib/clumpy/builder.rb
CHANGED
@@ -42,15 +42,20 @@ module Clumpy
|
|
42
42
|
clusters.find { |c| c.contains?(point) }
|
43
43
|
end
|
44
44
|
|
45
|
-
def
|
46
|
-
@
|
45
|
+
def cluster_width
|
46
|
+
@cluster_width ||= longitude_distance / @distance_modifier
|
47
|
+
end
|
48
|
+
|
49
|
+
def cluster_length
|
50
|
+
@cluster_length ||= latitude_distance / @distance_modifier
|
47
51
|
end
|
48
52
|
|
49
53
|
def cluster_options
|
50
54
|
{
|
51
55
|
values_threshold: options[:values_threshold],
|
52
56
|
include_values: options[:include_values],
|
53
|
-
|
57
|
+
width: cluster_width,
|
58
|
+
length: cluster_length,
|
54
59
|
}
|
55
60
|
end
|
56
61
|
|
@@ -8,7 +8,7 @@ module Clumpy
|
|
8
8
|
@points = [point]
|
9
9
|
@options = options
|
10
10
|
|
11
|
-
@bounds = Bounds.new(@latitude, @longitude, options[:
|
11
|
+
@bounds = Bounds.new(@latitude, @longitude, options[:width], options[:length])
|
12
12
|
end
|
13
13
|
|
14
14
|
def contains?(point)
|
@@ -22,11 +22,22 @@ module Clumpy
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def as_json(*)
|
25
|
+
bounds = {
|
26
|
+
northeast: {
|
27
|
+
latitude: @points.map(&:latitude).max,
|
28
|
+
longitude: @points.map(&:longitude).max,
|
29
|
+
},
|
30
|
+
southwest: {
|
31
|
+
latitude: @points.map(&:latitude).min,
|
32
|
+
longitude: @points.map(&:longitude).min,
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
25
36
|
{
|
26
37
|
latitude: latitude,
|
27
38
|
longitude: longitude,
|
28
39
|
size: @points.size,
|
29
|
-
bounds:
|
40
|
+
bounds: bounds,
|
30
41
|
values: value_list,
|
31
42
|
}
|
32
43
|
end
|
data/lib/clumpy/version.rb
CHANGED
data/spec/builder_spec.rb
CHANGED
@@ -10,66 +10,66 @@ describe Clumpy::Builder do
|
|
10
10
|
|
11
11
|
it "takes points and options" do
|
12
12
|
builder = Clumpy::Builder.new(points, { foo: :bar })
|
13
|
-
builder.points.
|
14
|
-
builder.options.
|
13
|
+
expect(builder.points).to eq points
|
14
|
+
expect(builder.options).to eq({ foo: :bar })
|
15
15
|
end
|
16
16
|
|
17
17
|
it "initializes the distance_modifier is none was given" do
|
18
18
|
builder = Clumpy::Builder.new([])
|
19
|
-
builder.instance_variable_get(:@distance_modifier).
|
19
|
+
expect(builder.instance_variable_get(:@distance_modifier)).not_to be_nil
|
20
20
|
end
|
21
21
|
|
22
22
|
it 'takes the given distance modifier' do
|
23
23
|
builder = Clumpy::Builder.new([], distance_modifier: 12)
|
24
|
-
builder.instance_variable_get(:@distance_modifier).
|
24
|
+
expect(builder.instance_variable_get(:@distance_modifier)).to eq 12
|
25
25
|
end
|
26
26
|
|
27
27
|
context "#cluster" do
|
28
28
|
it "creates clusters from points" do
|
29
29
|
clusters = builder.cluster
|
30
|
-
clusters.size.
|
31
|
-
clusters.first.points.
|
32
|
-
clusters.last.points.
|
30
|
+
expect(clusters.size).to eq 2
|
31
|
+
expect(clusters.first.points).to eq [point1, point2]
|
32
|
+
expect(clusters.last.points).to eq [point3]
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
36
|
it "passes the values threshold through to the cluster" do
|
37
37
|
builder = Clumpy::Builder.new([point1], values_threshold: 100, include_values: true)
|
38
|
-
builder.cluster_options.
|
38
|
+
expect(builder.cluster_options).to eq({ values_threshold: 100, include_values: true, width: 22, length: 10.628196875 })
|
39
39
|
clusters = builder.cluster
|
40
|
-
clusters.first.instance_variable_get(:@options).
|
40
|
+
expect(clusters.first.instance_variable_get(:@options)).to eq builder.cluster_options
|
41
41
|
end
|
42
42
|
|
43
43
|
context "#add_to_cluster" do
|
44
44
|
it "creates a new cluster if there is no matching" do
|
45
|
-
builder.clusters.
|
45
|
+
expect(builder.clusters).to be_empty
|
46
46
|
builder.add_to_cluster(point1)
|
47
|
-
builder.clusters.size.
|
47
|
+
expect(builder.clusters.size).to eq 1
|
48
48
|
end
|
49
49
|
|
50
50
|
it "will not create a cluster if the given value is not a true point" do
|
51
|
-
builder.clusters.
|
51
|
+
expect(builder.clusters).to be_empty
|
52
52
|
builder.add_to_cluster(:foo)
|
53
|
-
builder.clusters.
|
53
|
+
expect(builder.clusters).to be_empty
|
54
54
|
end
|
55
55
|
|
56
56
|
it "appends markers to a existing cluster if they match" do
|
57
57
|
builder.clusters << Clumpy::Cluster.new(point1, builder.cluster_options)
|
58
|
-
builder.clusters.size.
|
59
|
-
builder.clusters.first.points.size.
|
58
|
+
expect(builder.clusters.size).to eq 1
|
59
|
+
expect(builder.clusters.first.points.size).to eq 1
|
60
60
|
builder.add_to_cluster(point2)
|
61
|
-
builder.clusters.size.
|
62
|
-
builder.clusters.first.points.size.
|
61
|
+
expect(builder.clusters.size).to eq 1
|
62
|
+
expect(builder.clusters.first.points.size).to eq 2
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
66
|
it "calculates the latitude distance" do
|
67
|
-
builder.latitude_distance.
|
67
|
+
expect(builder.latitude_distance).to eq Clumpy::Builder::MAX_LATITUDE_DISTANCE
|
68
68
|
builder.options = { nelat: -30, swlat: 40 }
|
69
|
-
builder.latitude_distance.
|
69
|
+
expect(builder.latitude_distance).to eq 70
|
70
70
|
builder.options = { nelat: -30, swlat: -35 }
|
71
|
-
builder.latitude_distance.
|
71
|
+
expect(builder.latitude_distance).to eq 5
|
72
72
|
builder.options = { nelat: 20, swlat: 35 }
|
73
|
-
builder.latitude_distance.
|
73
|
+
expect(builder.latitude_distance).to eq 15
|
74
74
|
end
|
75
75
|
end
|
@@ -3,58 +3,58 @@ require 'spec_helper'
|
|
3
3
|
describe Clumpy::ClusterBehavior do
|
4
4
|
let(:point) { OpenStruct.new(latitude: 15, longitude: 20) }
|
5
5
|
let(:other_point) { OpenStruct.new(latitude: 40, longitude: 40) }
|
6
|
-
let(:cluster) { Clumpy::Cluster.new(point,
|
6
|
+
let(:cluster) { Clumpy::Cluster.new(point, width: 20, length: 10) }
|
7
7
|
|
8
8
|
it "has a nicish to_s representation" do
|
9
|
-
cluster.to_s.
|
9
|
+
expect(cluster.to_s).to include '# size: 1'
|
10
10
|
end
|
11
11
|
|
12
12
|
it "has a json represtation as well" do
|
13
|
-
cluster.as_json.
|
14
|
-
cluster.as_json.
|
15
|
-
cluster.as_json.
|
16
|
-
cluster.as_json.
|
13
|
+
expect(cluster.as_json).to have_key :size
|
14
|
+
expect(cluster.as_json).to have_key :latitude
|
15
|
+
expect(cluster.as_json).to have_key :longitude
|
16
|
+
expect(cluster.as_json).to have_key :bounds
|
17
17
|
end
|
18
18
|
|
19
19
|
context "#reposition" do
|
20
20
|
it "calculates new latitude and longitude values" do
|
21
21
|
cluster.points << other_point
|
22
22
|
cluster.reposition
|
23
|
-
cluster.latitude.
|
24
|
-
cluster.longitude.
|
23
|
+
expect(cluster.latitude).to eq (15.0+40.0)/2.0
|
24
|
+
expect(cluster.longitude).to eq (20.0+40.0)/2.0
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
context "#value_list" do
|
29
29
|
it "returns an empty array if threshold isn't reached" do
|
30
|
-
cluster = Clumpy::Cluster.new(point, values_threshold: 1)
|
30
|
+
cluster = Clumpy::Cluster.new(point, values_threshold: 1, width: 20, length: 10)
|
31
31
|
cluster.points << other_point
|
32
|
-
cluster.value_list.
|
32
|
+
expect(cluster.value_list).to eq []
|
33
33
|
end
|
34
34
|
|
35
35
|
it "returns an array of json points if under threshold" do
|
36
|
-
cluster = Clumpy::Cluster.new(point, values_threshold: 1)
|
37
|
-
cluster.value_list.
|
36
|
+
cluster = Clumpy::Cluster.new(point, values_threshold: 1, width: 20, length: 10)
|
37
|
+
expect(cluster.value_list).to eq [point]
|
38
38
|
end
|
39
39
|
|
40
40
|
it "returns a list of values if include_values is true" do
|
41
|
-
cluster = Clumpy::Cluster.new(point, values_threshold: 0, include_values: true)
|
42
|
-
cluster.value_list.
|
41
|
+
cluster = Clumpy::Cluster.new(point, values_threshold: 0, include_values: true, width: 20, length: 10)
|
42
|
+
expect(cluster.value_list).to eq [point]
|
43
43
|
end
|
44
44
|
|
45
45
|
it "returns a empty list if include_values is false" do
|
46
|
-
cluster = Clumpy::Cluster.new(point, values_threshold: 10, include_values: false)
|
47
|
-
cluster.value_list.
|
46
|
+
cluster = Clumpy::Cluster.new(point, values_threshold: 10, include_values: false, width: 20, length: 10)
|
47
|
+
expect(cluster.value_list).to eq []
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
51
|
context "#contains" do
|
52
52
|
it "returns true if the given point is within the bounds" do
|
53
|
-
cluster.contains?(point).
|
53
|
+
expect(cluster.contains?(point)).to eq true
|
54
54
|
end
|
55
55
|
|
56
56
|
it "returns false for a points that isn't contained" do
|
57
|
-
cluster.contains?(other_point).
|
57
|
+
expect(cluster.contains?(other_point)).to eq false
|
58
58
|
end
|
59
59
|
end
|
60
60
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clumpy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Johannes Opper
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-10-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -77,6 +77,7 @@ files:
|
|
77
77
|
- ".rspec"
|
78
78
|
- ".ruby-gemset"
|
79
79
|
- ".ruby-version"
|
80
|
+
- ".travis.yml"
|
80
81
|
- CHANGELOG.md
|
81
82
|
- Gemfile
|
82
83
|
- LICENSE.txt
|
@@ -112,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
113
|
version: '0'
|
113
114
|
requirements: []
|
114
115
|
rubyforge_project:
|
115
|
-
rubygems_version: 2.
|
116
|
+
rubygems_version: 2.4.5.1
|
116
117
|
signing_key:
|
117
118
|
specification_version: 4
|
118
119
|
summary: Cluster a bunch of points
|
@@ -120,3 +121,4 @@ test_files:
|
|
120
121
|
- spec/builder_spec.rb
|
121
122
|
- spec/cluster_behavior_spec.rb
|
122
123
|
- spec/spec_helper.rb
|
124
|
+
has_rdoc:
|