simplify_rb 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -4
- data/lib/simplify_rb/version.rb +1 -1
- data/lib/simplify_rb.rb +12 -12
- data/simplify_rb.gemspec +1 -1
- data/spec/simplify_rb_spec.rb +14 -8
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f79646c8c01d7c69d1d64e96be6ef6035815879
|
4
|
+
data.tar.gz: 7d2c38e4bcdab4d114cd4c193539ed9f6a9a7a7c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac827d96699828338bbc0bf5826a592fb3f1a40ab16e7233aa407f9d7ba5520fa84cf00088b18551cb0176a8117a423cf1476d23c50b296604619f79f769a2bf
|
7
|
+
data.tar.gz: 6ecb8581a96851cc2e539428cec861a5763f1b964abdab02e2b709deec8d463849586fa317dc807c72eb3c9feeadbba34bd5d9360a7ca37f851c3e1f8e494647
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# SimplifyRb - Polyline simplification
|
2
2
|
|
3
|
-
|
3
|
+
[![Codeship Status for odlp/simplify_rb](https://www.codeship.io/projects/2a8ed250-47d5-0132-a14f-46b32a25e7b0/status)](https://www.codeship.io/projects/45709)
|
4
|
+
|
5
|
+
SimplifyRb is a Ruby port of [simplify.js](https://github.com/mourner/simplify-js) by Vladimir Agafonkin.
|
4
6
|
|
5
7
|
You can use this gem to reduce the number of points in a complex polyline / polygon, making use of an optimized Douglas-Peucker algorithm.
|
6
8
|
|
@@ -23,11 +25,15 @@ Or install it yourself as:
|
|
23
25
|
```ruby
|
24
26
|
require 'simplify_rb'
|
25
27
|
|
28
|
+
points = [{x: 51.5256, y: -0.0875}, {x: 51.7823, y: -0.0912}]
|
29
|
+
tolerance = 1
|
30
|
+
high_quality = true
|
31
|
+
|
26
32
|
SimplifyRb.simplify(points, tolerance, high_quality)
|
27
33
|
```
|
28
34
|
|
29
|
-
```points```: An array of hashes, containing x,y coordinates
|
35
|
+
```points```: An array of hashes, containing x,y coordinates.
|
30
36
|
|
31
|
-
```tolerance```: (optional, 1 by default): Affects the amount of simplification that occurs (the smaller, the less simplification)
|
37
|
+
```tolerance```: (optional, 1 by default): Affects the amount of simplification that occurs (the smaller, the less simplification).
|
32
38
|
|
33
|
-
```
|
39
|
+
```high_quality```: (optional, False by default): Flag to exclude the distance pre-processing. Produces higher quality results when true is passed, but runs slower.
|
data/lib/simplify_rb/version.rb
CHANGED
data/lib/simplify_rb.rb
CHANGED
@@ -3,15 +3,17 @@ require "simplify_rb/version"
|
|
3
3
|
class SimplifyRb
|
4
4
|
# Main method
|
5
5
|
def self.simplify (points, tolerance=1, highest_quality=false)
|
6
|
+
raise ArgumentError.new('Points must be an array') unless points.is_a? Array
|
7
|
+
|
6
8
|
return points if points.length <= 1
|
7
9
|
|
8
|
-
points = symbolize_keys(points) unless keys_are_symbols?(points.map
|
10
|
+
points = symbolize_keys(points) unless keys_are_symbols?(points.map(&:keys))
|
9
11
|
|
10
12
|
sq_tolerance = tolerance * tolerance
|
11
|
-
|
13
|
+
|
12
14
|
# Optimisation step 1
|
13
15
|
points = simplifyRadialDist(points, sq_tolerance) unless highest_quality
|
14
|
-
|
16
|
+
|
15
17
|
# Optimisation step 2
|
16
18
|
simplifyDouglasPeucker(points, sq_tolerance)
|
17
19
|
end
|
@@ -31,11 +33,10 @@ class SimplifyRb
|
|
31
33
|
|
32
34
|
# Simplification using optimized Douglas-Peucker algorithm with recursion elimination
|
33
35
|
def self.simplifyDouglasPeucker (points, sq_tolerance)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
stack = []
|
36
|
+
first = 0
|
37
|
+
last = points.length - 1
|
38
|
+
index = nil
|
39
|
+
stack = []
|
39
40
|
|
40
41
|
points.first[:keep] = true
|
41
42
|
points.last[:keep] = true
|
@@ -58,12 +59,10 @@ class SimplifyRb
|
|
58
59
|
stack.push(first, index, index, last)
|
59
60
|
end
|
60
61
|
|
61
|
-
last
|
62
|
-
first = stack.pop
|
63
|
-
|
62
|
+
first, last = stack.pop(2)
|
64
63
|
end # end while
|
65
64
|
|
66
|
-
points.
|
65
|
+
points.select { |p| p[:keep] && p.delete(:keep) }
|
67
66
|
end
|
68
67
|
|
69
68
|
# Square distance between two points
|
@@ -87,6 +86,7 @@ class SimplifyRb
|
|
87
86
|
if t > 1
|
88
87
|
x = point_2[:x]
|
89
88
|
y = point_2[:y]
|
89
|
+
|
90
90
|
elsif t > 0
|
91
91
|
x += dx * t
|
92
92
|
y += dy * t
|
data/simplify_rb.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = "simplify_rb"
|
8
8
|
spec.version = SimplifyRb::VERSION
|
9
9
|
spec.authors = ["odlp"]
|
10
|
-
spec.description = "You can use this gem to reduce the number of points in a complex polyline / polygon, making use of an optimized Douglas-Peucker algorithm."
|
10
|
+
spec.description = "You can use this gem to reduce the number of points in a complex polyline / polygon, making use of an optimized Douglas-Peucker algorithm. Ruby port of Simplify.js."
|
11
11
|
spec.summary = "Polyline simplification library. Ruby port of Simplify.js."
|
12
12
|
spec.homepage = "https://github.com/odlp/simplify_rb"
|
13
13
|
spec.license = "MIT"
|
data/spec/simplify_rb_spec.rb
CHANGED
@@ -4,11 +4,11 @@ describe SimplifyRb do
|
|
4
4
|
context "simplifies points correctly with the given tolerance" do
|
5
5
|
before(:each) {@test_data = SimplifyTestData::points}
|
6
6
|
|
7
|
-
it "
|
7
|
+
it "uses the fast strategy by default" do
|
8
8
|
expect(SimplifyRb.simplify(@test_data, 5)).to eq(SimplifyTestData::result_fast)
|
9
9
|
end
|
10
10
|
|
11
|
-
it "
|
11
|
+
it "uses the high quality strategy when the flag is passed" do
|
12
12
|
expect(SimplifyRb.simplify(@test_data, 5, true)).to eq(SimplifyTestData::result_high_quality)
|
13
13
|
end
|
14
14
|
end
|
@@ -23,21 +23,27 @@ describe SimplifyRb do
|
|
23
23
|
expect(SimplifyRb.simplify([])).to eq([])
|
24
24
|
end
|
25
25
|
|
26
|
-
|
27
|
-
|
26
|
+
it "raises an argument error if the points are passsed as an array" do
|
27
|
+
data = {x: 1, y: 2}
|
28
|
+
|
29
|
+
expect { SimplifyRb.simplify(data) }.to raise_error(ArgumentError, "Points must be an array")
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#keys_are_symbols?" do
|
33
|
+
it "returns false if any key is not a Symbol" do
|
28
34
|
expect(SimplifyRb.keys_are_symbols? [:a, 'b', :c]).to equal(false)
|
29
35
|
end
|
30
36
|
|
31
|
-
it "
|
37
|
+
it "returns return true if all the keys are Symbols" do
|
32
38
|
expect(SimplifyRb.keys_are_symbols? [:a, :b, :c]).to equal(true)
|
33
39
|
end
|
34
40
|
end
|
35
41
|
|
36
|
-
|
37
|
-
it "
|
42
|
+
describe "#symbolize_keys" do
|
43
|
+
it "converts all of the collection's keys to symbols" do
|
38
44
|
collection = [{'a' => 1, 'b' => 2}, {'c' => 3}]
|
39
45
|
|
40
46
|
expect(SimplifyRb.symbolize_keys(collection)).to eq([{a: 1, b: 2}, {c: 3}])
|
41
47
|
end
|
42
48
|
end
|
43
|
-
end
|
49
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simplify_rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- odlp
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -53,7 +53,7 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3'
|
55
55
|
description: You can use this gem to reduce the number of points in a complex polyline
|
56
|
-
/ polygon, making use of an optimized Douglas-Peucker algorithm.
|
56
|
+
/ polygon, making use of an optimized Douglas-Peucker algorithm. Ruby port of Simplify.js.
|
57
57
|
email:
|
58
58
|
executables: []
|
59
59
|
extensions: []
|