cartesian_for_geo 1.0.1

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
+ SHA256:
3
+ metadata.gz: 272671cc18c0c61b8e12b1e9982e3f8fb7ec4f7302f0f814693159da3fc3b369
4
+ data.tar.gz: 65ebf521a4692a1ab34d187965852f109c0821a96b6010af051bb76308c852aa
5
+ SHA512:
6
+ metadata.gz: 60236ed2e4c4a384a0b497dbdeeaef3baebaa330475e41a3dccb4b7cc5a3bf37bf0706185ba3070878b77245506a4434e81c3a609ccecea12ca6d3b77639b73a
7
+ data.tar.gz: 7bcb4157bad4881a906858380f38b2ce765d00803b63c2bab9d1a68dc52fe43ed3720b4a77e520570997fc539184937f62a272fbbe7536aedf768e7d18865bf7
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,4 @@
1
+ Style/Tab:
2
+ Enabled: false
3
+ Style/IndentationWidth:
4
+ Width: 1
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.1
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at worstofany@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in cartesian_for_geo.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ivan Tyurin
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,21 @@
1
+ # CartesianForGeo
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'cartesian_for_geo'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cartesian_for_geo
18
+
19
+ ## License
20
+
21
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'cartesian_for_geo/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'cartesian_for_geo'
10
+ spec.version = CartesianForGeo::VERSION
11
+ spec.authors = ['Ivan Tyurin']
12
+ spec.email = ['worstofany@gmail.com']
13
+
14
+ spec.summary = %(
15
+ Helper for transform Polygons from GoogleMaps to PSQL DB correctly
16
+ )
17
+ spec.homepage = 'https://github.com/WorstOfAny/cartesian_for_geo'
18
+ spec.license = 'MIT'
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
21
+ f.match(%r{^(test|spec|features)/})
22
+ end
23
+
24
+ spec.add_runtime_dependency 'gorilla_patch', '~> 3.0'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.14'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ end
@@ -0,0 +1,190 @@
1
+ require 'cartesian_for_geo/version'
2
+ require 'gorilla_patch/cover'
3
+
4
+ module CartesianForGeo
5
+ ## Collection (Array) of Polygons
6
+ class PolygonsCollection < Array
7
+ def <<(new_polygon)
8
+ each_with_index do |polygon, ind|
9
+ if new_polygon.include?(polygon)
10
+ insert(ind, new_polygon) unless include?(new_polygon)
11
+ next new_polygon.concat delete(polygon)
12
+ elsif polygon.include?(new_polygon)
13
+ break new_polygon = polygon.concat(new_polygon)
14
+ end
15
+ end
16
+ return self if include?(new_polygon)
17
+ super new_polygon
18
+ end
19
+ end
20
+
21
+ ## Class for one Point
22
+ class Point
23
+ include Comparable
24
+
25
+ attr_accessor :lat, :lng
26
+ attr_reader :coords
27
+
28
+ alias to_a coords
29
+
30
+ class << self
31
+ alias [] new
32
+ end
33
+
34
+ def initialize(*coords)
35
+ @coords = coords.flatten
36
+ @lat, @lng = @coords
37
+ end
38
+
39
+ def side
40
+ lng.negative? ? -1 : 1
41
+ end
42
+
43
+ def empty?
44
+ (lat && lng).nil?
45
+ end
46
+
47
+ def lng_from_edge
48
+ 180 - lng.abs
49
+ end
50
+
51
+ def to_s
52
+ empty? ? '' : "(#{lat},#{lng})"
53
+ end
54
+
55
+ def to_json(*)
56
+ JSON.generate(lat: lat, lng: lng)
57
+ end
58
+
59
+ def ==(other)
60
+ super if other.is_a?(Point)
61
+ end
62
+
63
+ def <=>(other)
64
+ [to_a, other.to_a].map { |cord| cord.map { |f| f.round 9 } }.reduce(:<=>)
65
+ end
66
+ end
67
+
68
+ ## Class for Vector (of two Points)
69
+ class Vector
70
+ attr_accessor :from, :to
71
+ attr_reader :points
72
+ alias to_a points
73
+
74
+ class << self
75
+ alias [] new
76
+ end
77
+
78
+ def initialize(*points)
79
+ @points = points.flatten
80
+ @from, @to = @points
81
+ end
82
+
83
+ def split
84
+ @split ||= [
85
+ [from, point_at_edge(from.side)], [point_at_edge(to.side), to]
86
+ ].map(&Vector.method(:[]))
87
+ end
88
+
89
+ def crossing?
90
+ delta = points.map(&:lng).reduce(:-).abs
91
+ delta > 180 && delta < 360
92
+ end
93
+
94
+ private
95
+
96
+ def point_at_edge(side)
97
+ @lat_at_edge ||= lat_at_edge
98
+ Point[@lat_at_edge, side * 180]
99
+ end
100
+
101
+ def lat_at_edge
102
+ points_by_lat = points.sort_by!(&:lat).reverse!
103
+ lng_dists = points_by_lat.map(&:lng_from_edge)
104
+ lat_diff = points_by_lat.map(&:lat).reduce(:-)
105
+ points_by_lat.first.lat -
106
+ lat_diff * lng_dists.first / lng_dists.reduce(:+)
107
+ end
108
+ end
109
+
110
+ ## Class for Polygon (has many Vectors)
111
+ class Polygon
112
+ attr_reader :vectors
113
+
114
+ def initialize(*vectors)
115
+ @vectors = vectors.flatten
116
+ raise VectorsCountError if @vectors.size < 2
117
+ first_crossing = @vectors.index(&:crossing?)
118
+ @vectors.rotate!(first_crossing).push(@vectors.first) if first_crossing
119
+ end
120
+
121
+ def self.parse(text)
122
+ points = text.tr('() ', '').split(',').each_slice(2).to_a.map! do |point|
123
+ Point[point.map(&:to_f)]
124
+ end
125
+ new points.map.with_index(-1) { |point, ind| Vector[points[ind], point] }
126
+ end
127
+
128
+ def self.parse!(text)
129
+ parse(text)
130
+ rescue VectorsCountError
131
+ nil
132
+ end
133
+
134
+ def split
135
+ @polygons = PolygonsCollection.new
136
+ @vectors.each_with_object([]) do |vector, vectors_arr|
137
+ next vectors_arr << vector unless vector.crossing?
138
+ @polygons << Polygon.new(vectors_arr.push(*vector.split).slice!(0..-2))
139
+ rescue VectorsCountError
140
+ next
141
+ end
142
+ @polygons.empty? ? @polygons << Polygon.new(@vectors) : @polygons
143
+ end
144
+
145
+ def concat(other)
146
+ vectors.concat(other.vectors)
147
+ self
148
+ end
149
+
150
+ using GorillaPatch::Cover
151
+
152
+ def include?(other)
153
+ side == other.side && lat_range.cover?(other.lat_range)
154
+ end
155
+
156
+ def lat_range
157
+ @lat_range ||= Range.new(*lat_edges)
158
+ end
159
+
160
+ def side
161
+ vectors.first.from.side
162
+ end
163
+
164
+ def to_s
165
+ points = vectors.map(&:to_a).flatten!.uniq
166
+ "(#{points.map(&:to_s).join(',')})"
167
+ end
168
+
169
+ private
170
+
171
+ def center
172
+ coords =
173
+ vectors.each_with_object({ lat: 0, lng: 0 }.to_a) do |vector, arr|
174
+ arr.each { |el| el[-1] += vector.to.send(el.first) }
175
+ end
176
+ Point.new(coords.map! { |el| el.last / vectors.size })
177
+ end
178
+
179
+ def lat_edges
180
+ [vectors.first.from, vectors.last.to].map!(&:lat).sort!
181
+ end
182
+ end
183
+
184
+ ## Custom error for Polygon initialization
185
+ class VectorsCountError < ArgumentError
186
+ def message
187
+ 'You need at least two vectors to initialize the polygon'
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,3 @@
1
+ module CartesianForGeo
2
+ VERSION = '1.0.1'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cartesian_for_geo
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Tyurin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gorilla_patch
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
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.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
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
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - worstofany@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".ruby-version"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - cartesian_for_geo.gemspec
87
+ - lib/cartesian_for_geo.rb
88
+ - lib/cartesian_for_geo/version.rb
89
+ homepage: https://github.com/WorstOfAny/cartesian_for_geo
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.7.6
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Helper for transform Polygons from GoogleMaps to PSQL DB correctly
113
+ test_files: []