googlestaticmap-clusters 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 055600ca638a83b634b5cb9372122e3f461a7ca0
4
+ data.tar.gz: 821569473a559894ba44fb1ba711aa2887e0147c
5
+ SHA512:
6
+ metadata.gz: 7263ad8ee8452479735bd0f3a5dd06a4d5bcc2744326f88b0c4db886a77668bc90dcf27cee62e3ce27ffa350825e28642d9501377ea79e653d1544b7a748b58c
7
+ data.tar.gz: 669cc676fd0a6662751dfe68081b908749f694872e0ea92eb7d4d3deed8181cd9b8948348f40a84ae54c53a2ea47d819a1f92e9dabd8c96f3ff474096d6426ea
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in googlestaticmap-clusters.gemspec
4
+ gemspec
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,68 @@
1
+ # GoogleStaticMap-Clusters
2
+
3
+ ![Google Static Map with clusters](https://cloud.githubusercontent.com/assets/6061036/7803404/9e4dd9e0-0324-11e5-8719-fedf9ccd9098.png)
4
+
5
+ Add points layers, cluster them and get the link to the static map. Each cluster is represented by a marker.
6
+
7
+ Based on [this post](http://www.appelsiini.net/2008/introduction-to-marker-clustering-with-google-maps).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'googlestaticmap-clusters'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install googlestaticmap-clusters
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ require 'googlestaticmap-clusters'
29
+
30
+ data = {:customers=>[{:lat=>-23.607581, :lon=>-46.630046,...},
31
+ {:lat=>-23.511634, :lon=>-46.71541,...},...],
32
+ :sellers => [{:lat=>-23.607551, :lon=>-46.630034,...},
33
+ {:lat=>-23.511644, :lon=>-46.71543,...},...]}
34
+
35
+ map = GoogleStaticMapClusters::Map.new({:zoom => 9})
36
+
37
+ map.add_layer(data[:customers])
38
+ map.add_layer(data[:sellers],{:style=>{:marker_color=>'red'}})
39
+
40
+ puts map.link
41
+ ```
42
+
43
+ ### Options with default values
44
+
45
+ ```ruby
46
+ defaults = {
47
+ :sensor => false,
48
+ :map_width =>600,
49
+ :map_height => 600,
50
+ :style => {:marker_size => 'mid', :marker_color => 'blue'},
51
+ :zoom => 12,
52
+ :max_distance => 50, # distance for clustering
53
+ :outliers_distance => 700 # distance for cleaning outliers
54
+ }
55
+ ```
56
+ ## Development
57
+
58
+ 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.
59
+
60
+ 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).
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it ( https://github.com/[my-github-username]/googlestaticmap-clusters/fork )
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 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 "googlestaticmap/clusters"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'googlestaticmap-clusters/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "googlestaticmap-clusters"
8
+ spec.version = GoogleStaticMapClusters::VERSION
9
+ spec.authors = ["Bruno Salerno"]
10
+ spec.email = ["br.salerno@gmail.com"]
11
+
12
+ spec.summary = 'Ruby gem to create Google Static Maps with clusters and markers'
13
+ spec.description = 'Add points layers, cluster them and get the link to the static map'
14
+ spec.homepage = 'https://github.com/BrunoSalerno/googlestaticmap-clusters'
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 "pixeldistance", "~> 0.2.1"
23
+ spec.add_runtime_dependency "rclusters", "~> 0.2"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.9"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
@@ -0,0 +1,77 @@
1
+ require 'pixeldistance'
2
+ require 'rclusters'
3
+ require 'json'
4
+ require 'pry'
5
+
6
+ require 'googlestaticmap-clusters/version'
7
+
8
+ module GoogleStaticMapClusters
9
+ class Map
10
+ def initialize(opts = {})
11
+ defaults = {
12
+ :sensor => false,
13
+ :map_width =>600,
14
+ :map_height => 600,
15
+ :style => {:marker_size => 'mid', :marker_color => 'blue'},
16
+ :zoom => 12,
17
+ :max_distance => 50, # distance for clustering
18
+ :outliers_distance => 700 # distance for cleaning outliers
19
+ }
20
+
21
+ @layers = []
22
+ @opts = defaults.merge(opts)
23
+ end
24
+
25
+ def add_layer(points,opts={})
26
+ options = @opts.merge(opts)
27
+ if options[:outliers_distance]
28
+ points = clean_outliers(points,options[:outliers_distance],options[:zoom])
29
+ end
30
+ c = RClusters::ScreenDistance.new({:max_distance=>options[:max_distance],
31
+ :zoom=>options[:zoom]})
32
+ @layers.push({:clusters => (c.calculate(points)),:style => options[:style]})
33
+ end
34
+
35
+ def link(opts = {})
36
+ options = @opts.merge(opts)
37
+
38
+ link_str =''
39
+ @layers.each {|layer|
40
+ layer[:clusters].each {|c|
41
+ centroid = centroid(c[:cluster])
42
+ str = "#{centroid[:lat]},#{centroid[:lon]}"
43
+
44
+ link_str += "&markers=color:#{layer[:style][:marker_color]}" <<
45
+ "%7Csize:#{layer[:style][:marker_size]}|" <<
46
+ "label:#{c[:size] > 1 ? c[:size] : ''}|#{str}|"
47
+ }
48
+ }
49
+
50
+ 'http://maps.googleapis.com/maps/api/staticmap?' <<
51
+ "size=#{options[:map_width]}x#{options[:map_height]}&sensor=#{options[:sensor]}" <<
52
+ link_str
53
+ end
54
+
55
+ private
56
+
57
+ def clean_outliers(points, max_distance, zoom)
58
+ centroid = centroid(points)
59
+ points.map{|point|
60
+ pixel_distance = PixelDistance::from_coords(point[:lat], point[:lon], centroid[:lat], centroid[:lon], zoom)
61
+ if pixel_distance > max_distance
62
+ nil
63
+ else
64
+ point
65
+ end
66
+
67
+ }.compact
68
+ end
69
+
70
+ def centroid(points)
71
+ return if points.empty?
72
+ lats = points.inject(0) {|sum, n| sum + n[:lat]}
73
+ lons = points.inject(0) {|sum, n| sum + n[:lon]}
74
+ {:lat => lats/points.count, :lon => lons/points.count}
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,3 @@
1
+ module GoogleStaticMapClusters
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: googlestaticmap-clusters
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-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pixeldistance
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rclusters
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: Add points layers, cluster them and get the link to the static map
70
+ email:
71
+ - br.salerno@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - googlestaticmap-clusters.gemspec
85
+ - lib/googlestaticmap-clusters.rb
86
+ - lib/googlestaticmap-clusters/version.rb
87
+ homepage: https://github.com/BrunoSalerno/googlestaticmap-clusters
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.2.2
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Ruby gem to create Google Static Maps with clusters and markers
111
+ test_files: []