kluster 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bc71f6255d1a306aa34cf392605aba5109363b05
4
+ data.tar.gz: 914b64508886a5851bec305a491c23df284bb747
5
+ SHA512:
6
+ metadata.gz: f67a84ec09ee90a0565611b1d3f29a5b803fc8d4cd99e1c8c567cf2c725e44824bdaf36fdb6520a29a8e259ff7e84ac0ab3ef18d12fd3eac4f2f2747f9c26e09
7
+ data.tar.gz: 88f01c7e5d82b2a6cce2f85184dfdab36a0371fdf395422e27facdded00f00f089def0c5fc0dbce4a2e9829f657d0db5f40a0f9c45a0cea6b547ad1b33f4c0f5
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kluster.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Chris O'Sullivan
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Kluster
2
+
3
+ Ever try to display annotations on a map, but find you have too many?
4
+
5
+ Kluster can help!
6
+
7
+ It'll take a bunch of pins that have a latitude and longitude, and decide whether they're too close together or not. If so, it'll create a cluster of em.
8
+
9
+ Sweet
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'kluster'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install kluster
24
+
25
+ ## Usage
26
+
27
+ Give kluster an array of objects that have a coordinate field and it'll spit back the array with some clusters.
28
+
29
+ The only parameter it takes is the radius of how big you want the cluster to look in KMs.
30
+
31
+ e.g.
32
+ clusterer = Clusterer.new(@pins)
33
+ @clustered_pins = clusterer.clusterize(params[:cluster_radius].to_f)
34
+
35
+ ## Dependencies
36
+ Kluster uses handy distance methods from the geocoder gem
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "bundler/gem_tasks"
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ task default: :spec
data/kluster.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kluster/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kluster"
8
+ spec.version = Kluster::VERSION
9
+ spec.authors = ["Chris O'Sullivan"]
10
+ spec.email = ["thechrisoshow@gmail.com"]
11
+ spec.description = %q{Create geographic clusters}
12
+ spec.summary = %q{Take a collection of geographic objects and figures out any clusters of them}
13
+ spec.homepage = "http://github.com/thechrisoshow/kluster"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_dependency "geocoder", "~> 1.1"
25
+ end
data/lib/kluster.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "kluster/version"
2
+ require "geocoder"
3
+ require "kluster/cluster"
4
+ require "kluster/clusterer"
@@ -0,0 +1,36 @@
1
+ module Kluster
2
+ class Cluster
3
+ include Geocoder::Calculations
4
+ attr_accessor :latitude, :longitude, :pins
5
+
6
+ def initialize(initial_pin=nil)
7
+ @pins = [initial_pin]
8
+ @number_of_pins = 1
9
+ end
10
+
11
+ def coordinate
12
+ [latitude, longitude]
13
+ end
14
+
15
+ def ll
16
+ "#{latitude},#{longitude}"
17
+ end
18
+
19
+ def latitude
20
+ center[0]
21
+ end
22
+
23
+ def longitude
24
+ center[1]
25
+ end
26
+
27
+ def number_of_pins
28
+ @pins.count
29
+ end
30
+
31
+ private
32
+ def center
33
+ @center ||= geographic_center(@pins.collect(&:coordinate))
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,62 @@
1
+ # rewritten from OCMapView bubble clustering
2
+
3
+ module Kluster
4
+ class Clusterer
5
+
6
+ include Geocoder::Calculations
7
+
8
+ def initialize(pins)
9
+ @pins = pins
10
+ end
11
+
12
+ def clusterize(radius)
13
+ clustered_annotations = []
14
+ @pins.each do |pin|
15
+ is_containing = false
16
+ if clustered_annotations.empty?
17
+ new_cluster = Cluster.new(pin)
18
+ clustered_annotations << new_cluster
19
+ else
20
+ clustered_annotations.each do |cluster|
21
+ if location_near_to_other_location(pin.coordinate, cluster.coordinate, radius)
22
+ is_containing = true
23
+ cluster.pins << pin
24
+ break
25
+ end
26
+ end
27
+
28
+ if !is_containing
29
+ new_cluster = Cluster.new(pin)
30
+ clustered_annotations << new_cluster
31
+ end
32
+ end
33
+ end
34
+
35
+ return_array = []
36
+ clustered_annotations.each do |cluster|
37
+ if cluster.pins.length <= 1
38
+ return_array << cluster.pins.last
39
+ else
40
+ return_array << cluster
41
+ end
42
+ end
43
+ return_array
44
+ end
45
+
46
+ private
47
+
48
+ def get_distance(first_location, second_location)
49
+ deltaLat = first_location.latitude - second_location.latitude;
50
+ deltaLon = first_location.longitude - second_location.longitude;
51
+ distance = Math.sqrt(deltaLat*deltaLat + deltaLon*deltaLon);
52
+
53
+ return distance;
54
+ end
55
+
56
+ def location_near_to_other_location(first_coordinate, second_coordinate, radius)
57
+ distance = distance_between(first_coordinate, second_coordinate, {units: :km})
58
+ (distance <= radius)
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module Kluster
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ class Pin
4
+
5
+ def initialize(attrs={})
6
+ @lat = attrs[:lat]
7
+ @lng = attrs[:lng]
8
+ end
9
+
10
+ def coordinate
11
+ [@lat, @lng]
12
+ end
13
+ end
14
+
15
+ describe Kluster::Clusterer do
16
+
17
+ it "returns nothing if there's nothing" do
18
+ clusterer = Kluster::Clusterer.new([])
19
+ clusterer.clusterize(20).should == []
20
+ end
21
+
22
+ it "returns one object if there's only one" do
23
+ pin = Pin.new
24
+ clusterer = Kluster::Clusterer.new([pin])
25
+ clusterer.clusterize(20).should == [pin]
26
+ end
27
+
28
+ it "returns a cluster of two if the pins are in the same place" do
29
+ pin = Pin.new(lat: 0.1, lng: 0.1)
30
+ other_pin = Pin.new(lat: 0.1, lng: 0.1)
31
+ clusterer = Kluster::Clusterer.new([pin, other_pin])
32
+ result = clusterer.clusterize(10)
33
+ result.length.should == 1
34
+ result[0].latitude.should == 0.1
35
+ result[0].longitude.should == 0.1
36
+ result[0].number_of_pins.should == 2
37
+ end
38
+
39
+ it "clusters two very close pins" do
40
+ pin = Pin.new(lng: -0.07949243, lat: 51.52628)
41
+ other_pin = Pin.new(lng: -0.0794824, lat: 51.52628)
42
+
43
+ clusterer = Kluster::Clusterer.new([pin, other_pin])
44
+ clusterer.clusterize(10).length.should == 1
45
+ end
46
+
47
+ it "clusters at right zoom level" do
48
+ lads_pin = Pin.new(lng: -0.0848282966763571, lat: 51.5262816148359)
49
+ vicks_pin = Pin.new(lng: -0.08467794, lat: 51.52645)
50
+ quick_pin = Pin.new(lng: -0.0832336395979703, lat: 51.5265399870011)
51
+
52
+ clusterer = Kluster::Clusterer.new([lads_pin, vicks_pin, quick_pin])
53
+ clusterer.clusterize(0.05).length.should == 2
54
+ clusterer.clusterize(1).length.should == 1
55
+ end
56
+
57
+ end
@@ -0,0 +1 @@
1
+ require 'kluster'
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kluster
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris O'Sullivan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: geocoder
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ description: Create geographic clusters
70
+ email:
71
+ - thechrisoshow@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - kluster.gemspec
83
+ - lib/kluster.rb
84
+ - lib/kluster/cluster.rb
85
+ - lib/kluster/clusterer.rb
86
+ - lib/kluster/version.rb
87
+ - spec/clusterer_spec.rb
88
+ - spec/spec_helper.rb
89
+ homepage: http://github.com/thechrisoshow/kluster
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.0.3
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Take a collection of geographic objects and figures out any clusters of them
113
+ test_files:
114
+ - spec/clusterer_spec.rb
115
+ - spec/spec_helper.rb