rclusters 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +117 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/rclusters/base.rb +51 -0
- data/lib/rclusters/pixel_distance.rb +37 -0
- data/lib/rclusters/surface_distance.rb +13 -0
- data/lib/rclusters/version.rb +3 -0
- data/lib/rclusters.rb +4 -0
- data/rclusters.gemspec +26 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1746690c3d66ea1c1addace4ced13de169e837f6
|
4
|
+
data.tar.gz: fd7e9628708141a43da2dba11f311e1677169622
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3df198e536e2f89e86fa732979ef3e1cf48d3b10d078aa914d0145165245f16fa95e193ca7ae798c442cb72105d95d8f8edf435f5122be7f894a961060584f01
|
7
|
+
data.tar.gz: 92a991153f4d407b78b870ffd232e225418d0e9e4fdece0f8867a382b058118673010b48776964f1575168ac04d1b5cc5deafad420a482b25b307c279e5e9500
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Bruno Salerno
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# Rclusters
|
2
|
+
|
3
|
+
RClusters is a Ruby gem that creates clusters from a points hash using either pixel or surface distance calculations.
|
4
|
+
The input is a hash and the output is another hash. Right now, **a max_distance value has to be provided**.
|
5
|
+
|
6
|
+
Inspired by [this post](http://www.appelsiini.net/2008/introduction-to-marker-clustering-with-google-maps).
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'rclusters'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install rclusters
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
Calculating clusters based on pixel distances:
|
27
|
+
```ruby
|
28
|
+
require 'rclusters'
|
29
|
+
|
30
|
+
opts = {}
|
31
|
+
|
32
|
+
rcluster = RClusters::PixelDistance.new(opts)
|
33
|
+
|
34
|
+
data = [{:lat=>-23.607581, :lon=>-46.630046,...},{:lat=>-23.511634, :lon=>-46.71541,...},...]
|
35
|
+
|
36
|
+
clusters = rcluster.calculate(data,2000) # 2000px as max_distance
|
37
|
+
```
|
38
|
+
|
39
|
+
Calculating clusters based on surface distances:
|
40
|
+
```ruby
|
41
|
+
require 'rclusters'
|
42
|
+
|
43
|
+
opts = {}
|
44
|
+
|
45
|
+
rcluster = RClusters::SurfaceDistance.new(opts)
|
46
|
+
|
47
|
+
data = [{:lat=>-23.607581, :lon=>-46.630046,...},{:lat=>-23.511634, :lon=>-46.71541,...},...]
|
48
|
+
|
49
|
+
clusters = rcluster.calculate(data,200) # 200 mts as max_distance
|
50
|
+
```
|
51
|
+
|
52
|
+
|
53
|
+
Result example:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
[ {:size=>9,
|
57
|
+
:cluster=>
|
58
|
+
[{:lat=>-23.607581, :lon=>-46.630046,...},
|
59
|
+
{:lat=>-23.511634, :lon=>-46.71541,...},
|
60
|
+
{:lat=>-23.56957, :lon=>-46.628092,...},
|
61
|
+
{:lat=>-23.611887, :lon=>-46.67767,...}]},
|
62
|
+
{:size=>1,
|
63
|
+
:cluster=>[{:lat=>-22.4356084066, :lon=>-46.97118445}]},
|
64
|
+
{:size=>1,
|
65
|
+
:cluster=>[{:lat=>-23.472742, :lon=>-46.600197}]}
|
66
|
+
]
|
67
|
+
|
68
|
+
```
|
69
|
+
|
70
|
+
## Public Methods
|
71
|
+
|
72
|
+
```
|
73
|
+
calculate(Array of hashes points, Integer max_distance, opts)
|
74
|
+
```
|
75
|
+
Args are optional if passed in instantiation.
|
76
|
+
|
77
|
+
### Options
|
78
|
+
Either in instantiation or in the `calculate` method.
|
79
|
+
|
80
|
+
#### Common
|
81
|
+
|
82
|
+
(Optional if passed as args in the `calculate` method)
|
83
|
+
|
84
|
+
| Option | Info | Values |
|
85
|
+
---------|-------|------
|
86
|
+
| `points`| Points to be clustered |Array of points [hashes]|
|
87
|
+
| `max_distance` | Maximum distance between points in order to cluster them | Integer. Unit `px` using de `PixelDistance` class or the one matching `default_units` option if using `SurfaceDistance` |
|
88
|
+
|
89
|
+
#### PixelDistance
|
90
|
+
|
91
|
+
| Option | Info | Values |
|
92
|
+
---------|-------|------
|
93
|
+
| `zoom` | Distance is processed by a Google Maps type zoom scale | Integer . Default: `12` |
|
94
|
+
| `offset` | Used for translation to projected coordinates | Default: `268435456`|
|
95
|
+
| `radius` | Earth radius | Default: `85445659.4471`|
|
96
|
+
|
97
|
+
#### SurfaceDistance
|
98
|
+
|
99
|
+
| Option | Info | Values |
|
100
|
+
---------|-------|------
|
101
|
+
| `default_units` | Geokit option| Symbol . Default: `:meters`. Others: `:miles`, `:kms`, `:nms` |
|
102
|
+
| `default_formula` | Geokit option | Symbol. Default: `:sphere`.|
|
103
|
+
|
104
|
+
|
105
|
+
## Development
|
106
|
+
|
107
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
108
|
+
|
109
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
110
|
+
|
111
|
+
## Contributing
|
112
|
+
|
113
|
+
1. Fork it ( https://github.com/[my-github-username]/rclusters/fork )
|
114
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
115
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
116
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
117
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rclusters"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
module RClusters
|
2
|
+
class Base
|
3
|
+
def initialize(opts = {})
|
4
|
+
@points = opts[:points]
|
5
|
+
@max_distance = opts[:max_distance]
|
6
|
+
end
|
7
|
+
|
8
|
+
def calculate(points = nil,max_distance = nil, opts={})
|
9
|
+
points ||= @points
|
10
|
+
max_distance ||= @max_distance
|
11
|
+
cluster(points,max_distance,opts)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def distance(lat1,lon1,lat2,lon2,opts={})
|
17
|
+
# To be implemented by specific classes
|
18
|
+
end
|
19
|
+
|
20
|
+
def cluster(points, max_distance,opts)
|
21
|
+
return unless points and max_distance
|
22
|
+
|
23
|
+
clusters = []
|
24
|
+
points = points.clone
|
25
|
+
points.count.times do
|
26
|
+
point = points.pop
|
27
|
+
|
28
|
+
next if point.nil?
|
29
|
+
|
30
|
+
cluster = []
|
31
|
+
points.each_with_index {|target, index|
|
32
|
+
next if target.nil? or point.nil?
|
33
|
+
|
34
|
+
distance = distance(point[:lat], point[:lon], target[:lat], target[:lon],opts)
|
35
|
+
|
36
|
+
if max_distance > distance
|
37
|
+
cluster.push(target)
|
38
|
+
points[index]= nil
|
39
|
+
end
|
40
|
+
}
|
41
|
+
|
42
|
+
cluster.push(point)
|
43
|
+
clusters.push(cluster)
|
44
|
+
end
|
45
|
+
|
46
|
+
clusters.map {|cl|
|
47
|
+
{:size =>cl.count, :cluster=>cl}
|
48
|
+
}
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module RClusters
|
2
|
+
class PixelDistance < Base
|
3
|
+
OFFSET = 268435456
|
4
|
+
RADIUS = 85445659.4471
|
5
|
+
ZOOM = 12
|
6
|
+
|
7
|
+
def initialize(opts = {})
|
8
|
+
super
|
9
|
+
@offset = opts[:offset] || OFFSET
|
10
|
+
@radius = opts[:radius] || RADIUS
|
11
|
+
@zoom = opts[:zoom] || ZOOM
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def distance(lat1,lon1,lat2,lon2,opts={})
|
17
|
+
zoom = opts[:zoom] || @zoom
|
18
|
+
|
19
|
+
x1 = lon_to_x(lon1)
|
20
|
+
y1 = lat_to_y(lat1)
|
21
|
+
x2 = lon_to_x(lon2)
|
22
|
+
y2 = lat_to_y(lat2)
|
23
|
+
|
24
|
+
Math.sqrt((x1-x2)**2 + (y1-y2)**2).to_i >> (21 - zoom)
|
25
|
+
end
|
26
|
+
|
27
|
+
def lon_to_x(lon)
|
28
|
+
(@offset + @radius * lon * Math::PI / 180).to_i
|
29
|
+
end
|
30
|
+
|
31
|
+
def lat_to_y(lat)
|
32
|
+
(@offset + @radius *
|
33
|
+
Math.log((1 + Math.sin(lat * Math::PI / 180)) /
|
34
|
+
(1 - Math.sin(lat * Math::PI / 180))) / 2)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
require 'geokit'
|
3
|
+
|
4
|
+
module RClusters
|
5
|
+
class SurfaceDistance < Base
|
6
|
+
def distance(lat1,lon1,lat2,lon2,opts={})
|
7
|
+
Geokit::default_units = opts[:default_units] || :meters
|
8
|
+
Geokit::default_formula = opts[:default_formula] || :sphere
|
9
|
+
|
10
|
+
Geokit::LatLng.new(lat1,lon1).distance_to(Geokit::LatLng.new(lat2,lon2))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/rclusters.rb
ADDED
data/rclusters.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rclusters/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rclusters"
|
8
|
+
spec.version = RClusters::VERSION
|
9
|
+
spec.authors = ["Bruno Salerno"]
|
10
|
+
spec.email = ["br.salerno@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = 'Points clustering for Ruby based on distance.'
|
13
|
+
spec.description = 'RClusters creates clusters from a points hash using either pixel or surface distance calculations.'
|
14
|
+
spec.homepage = 'https://github.com/BrunoSalerno/rclusters'
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_runtime_dependency "geokit", "~> 1.9"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rclusters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bruno Salerno
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: geokit
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: RClusters creates clusters from a points hash using either pixel or surface
|
56
|
+
distance calculations.
|
57
|
+
email:
|
58
|
+
- br.salerno@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/rclusters.rb
|
72
|
+
- lib/rclusters/base.rb
|
73
|
+
- lib/rclusters/pixel_distance.rb
|
74
|
+
- lib/rclusters/surface_distance.rb
|
75
|
+
- lib/rclusters/version.rb
|
76
|
+
- rclusters.gemspec
|
77
|
+
homepage: https://github.com/BrunoSalerno/rclusters
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.2.2
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Points clustering for Ruby based on distance.
|
101
|
+
test_files: []
|