is 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +5 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +70 -0
- data/README.md +42 -0
- data/Rakefile +9 -0
- data/is.gemspec +23 -0
- data/lib/is/area.rb +74 -0
- data/lib/is/point.rb +17 -0
- data/lib/is/version.rb +5 -0
- data/lib/is.rb +49 -0
- data/spec/is/basic_spec.rb +97 -0
- metadata +73 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
gon (4.0.0)
|
5
|
+
actionpack (>= 2.3.0)
|
6
|
+
json
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionpack (3.2.3)
|
12
|
+
activemodel (= 3.2.3)
|
13
|
+
activesupport (= 3.2.3)
|
14
|
+
builder (~> 3.0.0)
|
15
|
+
erubis (~> 2.7.0)
|
16
|
+
journey (~> 1.0.1)
|
17
|
+
rack (~> 1.4.0)
|
18
|
+
rack-cache (~> 1.2)
|
19
|
+
rack-test (~> 0.6.1)
|
20
|
+
sprockets (~> 2.1.2)
|
21
|
+
activemodel (3.2.3)
|
22
|
+
activesupport (= 3.2.3)
|
23
|
+
builder (~> 3.0.0)
|
24
|
+
activesupport (3.2.3)
|
25
|
+
i18n (~> 0.6)
|
26
|
+
multi_json (~> 1.0)
|
27
|
+
blankslate (2.1.2.4)
|
28
|
+
builder (3.0.0)
|
29
|
+
diff-lcs (1.1.3)
|
30
|
+
erubis (2.7.0)
|
31
|
+
hike (1.2.1)
|
32
|
+
i18n (0.6.0)
|
33
|
+
jbuilder (0.4.0)
|
34
|
+
activesupport (>= 3.0.0)
|
35
|
+
blankslate (>= 2.1.2.4)
|
36
|
+
journey (1.0.4)
|
37
|
+
json (1.7.3)
|
38
|
+
multi_json (1.2.0)
|
39
|
+
rabl (0.6.9)
|
40
|
+
activesupport (>= 2.3.14)
|
41
|
+
multi_json (~> 1.0)
|
42
|
+
rack (1.4.1)
|
43
|
+
rack-cache (1.2)
|
44
|
+
rack (>= 0.4)
|
45
|
+
rack-test (0.6.1)
|
46
|
+
rack (>= 1.0)
|
47
|
+
rake (0.9.2.2)
|
48
|
+
rspec (2.10.0)
|
49
|
+
rspec-core (~> 2.10.0)
|
50
|
+
rspec-expectations (~> 2.10.0)
|
51
|
+
rspec-mocks (~> 2.10.0)
|
52
|
+
rspec-core (2.10.1)
|
53
|
+
rspec-expectations (2.10.0)
|
54
|
+
diff-lcs (~> 1.1.3)
|
55
|
+
rspec-mocks (2.10.1)
|
56
|
+
sprockets (2.1.3)
|
57
|
+
hike (~> 1.2)
|
58
|
+
rack (~> 1.0)
|
59
|
+
tilt (~> 1.1, != 1.3.0)
|
60
|
+
tilt (1.3.3)
|
61
|
+
|
62
|
+
PLATFORMS
|
63
|
+
ruby
|
64
|
+
|
65
|
+
DEPENDENCIES
|
66
|
+
gon!
|
67
|
+
jbuilder
|
68
|
+
rabl
|
69
|
+
rake
|
70
|
+
rspec
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Simple tool to find points in polygon
|
2
|
+
|
3
|
+
### Build Status ![http://travis-ci.org/gazay/is](https://secure.travis-ci.org/gazay/is.png)
|
4
|
+
|
5
|
+
Sponsored by Evil Martians <http://evilmartians.com>
|
6
|
+
|
7
|
+
``` ruby
|
8
|
+
pt = [2, 2]
|
9
|
+
all_pts = [[2, 2], [2, 2.5]]
|
10
|
+
any_pts = [[2, 0], [2, 2]]
|
11
|
+
no_pts = [[0, 0], [1, 0]]
|
12
|
+
area = \
|
13
|
+
[
|
14
|
+
[1, 1],
|
15
|
+
[3, 1],
|
16
|
+
[3, 3],
|
17
|
+
[1, 3]
|
18
|
+
]
|
19
|
+
|
20
|
+
Is.point(pt).in?(area) # => true
|
21
|
+
Is.all_points(all_pts).in?(area) # => true
|
22
|
+
Is.any_points(any_pts).in?(area) # => true
|
23
|
+
Is.any_points(no_pts).in?(area) # => false
|
24
|
+
|
25
|
+
# or
|
26
|
+
Is.point_in_area?(pt, area) # => true
|
27
|
+
Is.all_points_in_area?(all_pts, area) # => true
|
28
|
+
Is.any_points_in_area?(any_pts, area) # => true
|
29
|
+
Is.any_points_in_area?(no_pts, area) # => false
|
30
|
+
```
|
31
|
+
|
32
|
+
## License
|
33
|
+
|
34
|
+
The MIT License
|
35
|
+
|
36
|
+
Copyright (c) 2011-2012 gazay
|
37
|
+
|
38
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
39
|
+
|
40
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
41
|
+
|
42
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/is.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "is/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "is"
|
7
|
+
s.version = Is::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['gazay']
|
10
|
+
s.email = ['alex.gaziev@gmail.com']
|
11
|
+
s.homepage = "https://github.com/gazay/is"
|
12
|
+
s.summary = %q{Is this point in polygon?}
|
13
|
+
s.description = %q{Simple small tool for finding points in polygon}
|
14
|
+
|
15
|
+
s.rubyforge_project = "is"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
end
|
data/lib/is/area.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
class Is
|
2
|
+
class Area
|
3
|
+
|
4
|
+
attr_reader :area
|
5
|
+
|
6
|
+
def initialize(points)
|
7
|
+
@area = points.map { |it| Is::Point.new *it }
|
8
|
+
end
|
9
|
+
|
10
|
+
def contains?(point)
|
11
|
+
point = Is::Point.new(*point) unless point.is_a?(Is::Point)
|
12
|
+
return false if outside_box? point
|
13
|
+
|
14
|
+
contains_point = false
|
15
|
+
area_size = area.size
|
16
|
+
i = -1
|
17
|
+
j = area_size - 1
|
18
|
+
|
19
|
+
while (i += 1) < area_size
|
20
|
+
vertex1, vertex2 = area.values_at i, j
|
21
|
+
if conditions_passed?(point, vertex1, vertex2)
|
22
|
+
contains_point = !contains_point
|
23
|
+
end
|
24
|
+
j = i
|
25
|
+
end
|
26
|
+
|
27
|
+
contains_point
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def conditions_passed?(*args)
|
33
|
+
orig_lat_between_vertices_lats?(*args.map(&:latitude)) and ray_crosses_segment?(*args)
|
34
|
+
end
|
35
|
+
|
36
|
+
def orig_lat_between_vertices_lats?(orig_lat, vertex1_lat, vertex2_lat)
|
37
|
+
(vertex1_lat..vertex2_lat).include?(orig_lat) or
|
38
|
+
(vertex2_lat..vertex1_lat).include?(orig_lat)
|
39
|
+
end
|
40
|
+
|
41
|
+
def ray_crosses_segment?(orig, vertex1, vertex2)
|
42
|
+
olt = orig.latitude
|
43
|
+
olg = orig.longitude
|
44
|
+
v1lt = vertex1.latitude
|
45
|
+
v1lg = vertex1.longitude
|
46
|
+
v2lt = vertex2.latitude
|
47
|
+
v2lg = vertex2.longitude
|
48
|
+
|
49
|
+
olg < (v2lg - v1lg) * (olt - v1lt) / (v2lt - v1lt) + v1lg
|
50
|
+
end
|
51
|
+
|
52
|
+
def bounding_box
|
53
|
+
{
|
54
|
+
'left' => area.min{|a, b| a.longitude <=> b.longitude}.longitude,
|
55
|
+
'right' => area.max{|a, b| a.longitude <=> b.longitude}.longitude,
|
56
|
+
'top' => area.max{|a, b| a.latitude <=> b.latitude}.latitude,
|
57
|
+
'bottom' => area.min{|a, b| a.latitude <=> b.latitude}.latitude
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
def outside_box?(point)
|
62
|
+
if point.nil? || point.longitude.nil? || point.latitude.nil?
|
63
|
+
true
|
64
|
+
else
|
65
|
+
box = bounding_box
|
66
|
+
point.longitude < box['left'] ||
|
67
|
+
point.longitude > box['right'] ||
|
68
|
+
point.latitude < box['bottom'] ||
|
69
|
+
point.latitude > box['top']
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
data/lib/is/point.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
class Is
|
2
|
+
class Point
|
3
|
+
|
4
|
+
attr_reader :longitude, :latitude
|
5
|
+
|
6
|
+
def initialize(lat, long)
|
7
|
+
@latitude = lat.to_f
|
8
|
+
@longitude = long.to_f
|
9
|
+
end
|
10
|
+
|
11
|
+
def in?(area)
|
12
|
+
area = Is::Area.new(area) unless area.is_a?(Is::Area) # :)
|
13
|
+
area.contains? self
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
data/lib/is.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'is/area'
|
2
|
+
require 'is/point'
|
3
|
+
|
4
|
+
class Is
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def point(point)
|
9
|
+
Point.new *point
|
10
|
+
end
|
11
|
+
|
12
|
+
def all_points(points)
|
13
|
+
pts = points.map { |it| Point.new *it }
|
14
|
+
|
15
|
+
pts.define_singleton_method :in?, ->(area) do
|
16
|
+
area = Area.new area
|
17
|
+
all? { |point| point.in? area }
|
18
|
+
end
|
19
|
+
|
20
|
+
pts
|
21
|
+
end
|
22
|
+
alias :points :all_points
|
23
|
+
|
24
|
+
def any_points(points)
|
25
|
+
pts = points.map { |it| Point.new *it }
|
26
|
+
|
27
|
+
pts.define_singleton_method :in?, ->(area) do
|
28
|
+
area = Area.new area
|
29
|
+
any? { |point| point.in? area }
|
30
|
+
end
|
31
|
+
|
32
|
+
pts
|
33
|
+
end
|
34
|
+
|
35
|
+
def point_in_area?(point, area)
|
36
|
+
point(point).in? area
|
37
|
+
end
|
38
|
+
|
39
|
+
def all_points_in_area?(points, area)
|
40
|
+
all_points(points).in?(area)
|
41
|
+
end
|
42
|
+
|
43
|
+
def any_points_in_area?(points, area)
|
44
|
+
any_points(points).in?(area)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'is'
|
2
|
+
|
3
|
+
describe Is do
|
4
|
+
|
5
|
+
AREA = \
|
6
|
+
[
|
7
|
+
[1, 1],
|
8
|
+
[3, 1],
|
9
|
+
[3, 3],
|
10
|
+
[1, 3]
|
11
|
+
]
|
12
|
+
|
13
|
+
describe '#point' do
|
14
|
+
|
15
|
+
it 'is inside area' do
|
16
|
+
point = [2, 2]
|
17
|
+
Is.point(point).in?(AREA).should be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "isn't inside area" do
|
21
|
+
point = [0, 2]
|
22
|
+
Is.point(point).in?(AREA).should_not be_true
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '#all_points' do
|
28
|
+
|
29
|
+
it 'are inside area' do
|
30
|
+
points = [[2, 2], [2, 2.5]]
|
31
|
+
Is.all_points(points).in?(AREA).should be_true
|
32
|
+
end
|
33
|
+
|
34
|
+
it "aren't inside area" do
|
35
|
+
points = [[0, 2], [0, 1]]
|
36
|
+
Is.all_points(points).in?(AREA).should_not be_true
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#any_points' do
|
42
|
+
|
43
|
+
it 'are inside area' do
|
44
|
+
points = [[2, 0], [2, 2.5]]
|
45
|
+
Is.any_points(points).in?(AREA).should be_true
|
46
|
+
end
|
47
|
+
|
48
|
+
it "aren't inside area" do
|
49
|
+
points = [[0, 2], [0, 1]]
|
50
|
+
Is.any_points(points).in?(AREA).should_not be_true
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '#point_in_area?' do
|
56
|
+
|
57
|
+
it 'is inside area' do
|
58
|
+
point = [2, 2]
|
59
|
+
Is.point_in_area?(point, AREA).should be_true
|
60
|
+
end
|
61
|
+
|
62
|
+
it "isn't inside area" do
|
63
|
+
point = [0, 2]
|
64
|
+
Is.point_in_area?(point, AREA).should_not be_true
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
describe '#all_points_in_area?' do
|
70
|
+
|
71
|
+
it 'are inside area' do
|
72
|
+
points = [[2, 2], [2, 2.5]]
|
73
|
+
Is.all_points_in_area?(points, AREA).should be_true
|
74
|
+
end
|
75
|
+
|
76
|
+
it "aren't inside area" do
|
77
|
+
points = [[0, 2], [0, 1]]
|
78
|
+
Is.all_points_in_area?(points, AREA).should_not be_true
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
describe '#any_points_in_area?' do
|
84
|
+
|
85
|
+
it 'are inside area' do
|
86
|
+
points = [[2, 0], [2, 2.5]]
|
87
|
+
Is.any_points_in_area?(points, AREA).should be_true
|
88
|
+
end
|
89
|
+
|
90
|
+
it "aren't inside area" do
|
91
|
+
points = [[0, 2], [0, 1]]
|
92
|
+
Is.any_points_in_area?(points, AREA).should_not be_true
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: is
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- gazay
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Simple small tool for finding points in polygon
|
31
|
+
email:
|
32
|
+
- alex.gaziev@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- CHANGELOG.md
|
38
|
+
- Gemfile
|
39
|
+
- Gemfile.lock
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- is.gemspec
|
43
|
+
- lib/is.rb
|
44
|
+
- lib/is/area.rb
|
45
|
+
- lib/is/point.rb
|
46
|
+
- lib/is/version.rb
|
47
|
+
- spec/is/basic_spec.rb
|
48
|
+
homepage: https://github.com/gazay/is
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project: is
|
68
|
+
rubygems_version: 1.8.24
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Is this point in polygon?
|
72
|
+
test_files:
|
73
|
+
- spec/is/basic_spec.rb
|