border_patrol 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +9 -0
- data/border_patrol.gemspec +6 -3
- data/lib/border_patrol.rb +3 -2
- data/lib/border_patrol/version.rb +3 -0
- data/script/benchmark.rb +21 -0
- data/spec/lib/border_patrol/region_spec.rb +18 -14
- data/spec/spec_helper.rb +0 -3
- metadata +7 -6
- data/lib/VERSION +0 -1
data/README.markdown
CHANGED
@@ -21,6 +21,15 @@ To test if a point is in the region you can either pass a class that responds to
|
|
21
21
|
|
22
22
|
If you want to use your own point class, just define `x` and `y` as methods that correspond to `longitude` and `latitude`.
|
23
23
|
|
24
|
+
## Performance
|
25
|
+
It's definitely not going to beat a specialized system like PostGIS or SOLR, but it also doesn't have to go across the network to get results.
|
26
|
+
We've been using it successfully in critical paths in production with zero impact. Here's a benchmark checking 10,000 random points against the sample files included in the specs.
|
27
|
+
|
28
|
+
user system total real
|
29
|
+
colorado region 0.240000 0.010000 0.250000 ( 0.249663)
|
30
|
+
multi polygon region 0.610000 0.020000 0.630000 ( 0.631532)
|
31
|
+
|
32
|
+
|
24
33
|
## Pro Tip
|
25
34
|
|
26
35
|
You can make KML files easily on Google Maps by clicking "My Maps", drawing shapes and saving the map. Just copy the share link and add "&output=kml" to download the file.g
|
data/border_patrol.gemspec
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
+
$:.push File.expand_path("../lib", __FILE__)
|
4
|
+
require 'border_patrol/version'
|
5
|
+
|
3
6
|
Gem::Specification.new do |s|
|
4
7
|
s.name = "border_patrol"
|
5
|
-
s.version =
|
8
|
+
s.version = BorderPatrol::VERSION
|
6
9
|
s.authors = ["Zach Brock", "Matt Wilson"]
|
7
10
|
s.email = "eng@squareup.com"
|
8
|
-
s.date = "
|
9
|
-
s.description = "Lets you import a KML file and then check if points are inside or outside
|
11
|
+
s.date = "2011-07-06"
|
12
|
+
s.description = "Lets you import a KML file and then check if points are inside or outside region polygons defined by the file."
|
10
13
|
s.summary = "Import and query KML regions"
|
11
14
|
s.homepage = "http://github.com/square/border_patrol"
|
12
15
|
|
data/lib/border_patrol.rb
CHANGED
data/script/benchmark.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby -w
|
2
|
+
require "spec/spec_helper"
|
3
|
+
require 'benchmark'
|
4
|
+
|
5
|
+
colorado_region = BorderPatrol.parse_kml(File.read('spec/support/colorado-test.kml'))
|
6
|
+
multi_polygon_region = BorderPatrol.parse_kml(File.read('spec/support/multi-polygon-test.kml'))
|
7
|
+
Benchmark.bm(20) do |x|
|
8
|
+
x.report("colorado region") do
|
9
|
+
10000.times do |i|
|
10
|
+
multiple = (rand(2) == 1) ? -1 : 1
|
11
|
+
colorado_region.contains_point?(rand * 180 * multiple, rand * 180 * multiple)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
x.report("multi polygon region") do
|
16
|
+
10000.times do |i|
|
17
|
+
multiple = (rand(2) == 1) ? -1 : 1
|
18
|
+
multi_polygon_region.contains_point?(rand * 180 * multiple, rand * 180 * multiple)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -5,42 +5,46 @@ describe BorderPatrol::Region do
|
|
5
5
|
BorderPatrol::Region.new.should be_a Set
|
6
6
|
end
|
7
7
|
|
8
|
+
it "stores the polygons provided at initialization" do
|
9
|
+
region = BorderPatrol::Region.new([create_polygon, create_polygon(1), create_polygon(2)])
|
10
|
+
region.length.should == 3
|
11
|
+
end
|
12
|
+
|
8
13
|
describe "#contains_point?" do
|
9
|
-
def point
|
10
|
-
|
11
|
-
end
|
12
|
-
def polygon(start=0)
|
13
|
-
BorderPatrol::Polygon.new(
|
14
|
-
BorderPatrol::Point.new(start,start),
|
15
|
-
BorderPatrol::Point.new(start + 10, start),
|
16
|
-
BorderPatrol::Point.new(start + 10, start + 10),
|
17
|
-
BorderPatrol::Point.new(start, start + 10))
|
18
|
-
end
|
19
14
|
subject { BorderPatrol::Region.new(@polygons) }
|
20
15
|
|
21
16
|
it "raises an argument error if contains_point? takes more than 3 arguments" do
|
22
17
|
expect { subject.contains_point? }.to raise_exception ArgumentError
|
23
18
|
expect { subject.contains_point?(1,2,3) }.to raise_exception ArgumentError
|
24
19
|
end
|
25
|
-
|
20
|
+
|
26
21
|
it "returns true if any polygon contains the point" do
|
27
22
|
point = BorderPatrol::Point.new(1,2)
|
28
|
-
@polygons = [
|
23
|
+
@polygons = [create_polygon, create_polygon(30)]
|
29
24
|
|
30
25
|
subject.contains_point?(point).should be_true
|
31
26
|
end
|
32
27
|
|
33
28
|
it "returns false if no polygons contain the point" do
|
34
29
|
point = BorderPatrol::Point.new(-1,-2)
|
35
|
-
@polygons = [
|
30
|
+
@polygons = [create_polygon, create_polygon(30)]
|
36
31
|
|
37
32
|
subject.contains_point?(point).should be_false
|
38
33
|
end
|
39
34
|
|
40
35
|
it "transforms (x,y) coordinates passed in into a point" do
|
41
|
-
@polygons = [
|
36
|
+
@polygons = [create_polygon, create_polygon(30)]
|
42
37
|
|
43
38
|
subject.contains_point?(1,2).should be_true
|
44
39
|
end
|
45
40
|
end
|
41
|
+
|
42
|
+
def create_polygon(start=0)
|
43
|
+
BorderPatrol::Polygon.new(
|
44
|
+
BorderPatrol::Point.new(start,start),
|
45
|
+
BorderPatrol::Point.new(start + 10, start),
|
46
|
+
BorderPatrol::Point.new(start + 10, start + 10),
|
47
|
+
BorderPatrol::Point.new(start, start + 10))
|
48
|
+
end
|
49
|
+
|
46
50
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: border_patrol
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Zach Brock
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date:
|
19
|
+
date: 2011-07-06 00:00:00 -07:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -66,7 +66,7 @@ dependencies:
|
|
66
66
|
version: 2.6.0
|
67
67
|
type: :development
|
68
68
|
version_requirements: *id003
|
69
|
-
description: Lets you import a KML file and then check if points are inside or outside
|
69
|
+
description: Lets you import a KML file and then check if points are inside or outside region polygons defined by the file.
|
70
70
|
email: eng@squareup.com
|
71
71
|
executables: []
|
72
72
|
|
@@ -82,10 +82,11 @@ files:
|
|
82
82
|
- README.markdown
|
83
83
|
- Rakefile
|
84
84
|
- border_patrol.gemspec
|
85
|
-
- lib/VERSION
|
86
85
|
- lib/border_patrol.rb
|
87
86
|
- lib/border_patrol/polygon.rb
|
88
87
|
- lib/border_patrol/region.rb
|
88
|
+
- lib/border_patrol/version.rb
|
89
|
+
- script/benchmark.rb
|
89
90
|
- script/ci
|
90
91
|
- spec/lib/border_patrol/polygon_spec.rb
|
91
92
|
- spec/lib/border_patrol/region_spec.rb
|
data/lib/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.1.1
|