simplify_rb 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/.travis.yml +5 -0
- data/README.md +1 -1
- data/Rakefile +4 -0
- data/lib/simplify_rb/symbolizer.rb +13 -0
- data/lib/simplify_rb/version.rb +1 -1
- data/lib/simplify_rb.rb +17 -26
- data/simplify_rb.gemspec +10 -10
- data/spec/{simplify_test_data.rb → fixtures/simplify_test_data.rb} +0 -0
- data/spec/simplify_rb_spec.rb +32 -38
- data/spec/spec_helper.rb +2 -3
- data/spec/symbolizer_spec.rb +24 -0
- metadata +26 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 391f23576ae316977748323dbf295a0b11b5d99a
|
4
|
+
data.tar.gz: 21bfa76b9bdbee619f1ea406e729127edde09c44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6d2e5cc6e2038c8de7df917a91afd9d1eed41ce436dd95677a568426ff7d5234b9760f42056483b196a569c6196248f2cfad40b71f0557fab50886701642e01
|
7
|
+
data.tar.gz: 2646af352034c6633295eba793d0d8702029c0e2c5c3d07ced3d3db72d36154afc67e3bf055f85ae928daf3585d1545cfea59c921d8711b417d18f51f8dd4005
|
data/.rubocop.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# SimplifyRb - Polyline simplification
|
2
2
|
|
3
|
-
[](https://travis-ci.org/odlp/simplify_rb) [](https://badge.fury.io/rb/simplify_rb)
|
4
4
|
|
5
5
|
SimplifyRb is a Ruby port of [simplify.js](https://github.com/mourner/simplify-js) by Vladimir Agafonkin.
|
6
6
|
|
data/Rakefile
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
module SimplifyRbUtils
|
2
|
+
class Symbolizer
|
3
|
+
def keys_are_symbols?(keys)
|
4
|
+
keys.all? { |k| k.is_a? Symbol }
|
5
|
+
end
|
6
|
+
|
7
|
+
def symbolize_keys(collection)
|
8
|
+
collection.map do |item|
|
9
|
+
item.each_with_object({}) { |(k,v), memo| memo[k.to_sym] = v }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/simplify_rb/version.rb
CHANGED
data/lib/simplify_rb.rb
CHANGED
@@ -1,29 +1,32 @@
|
|
1
|
-
require
|
1
|
+
require 'simplify_rb/version'
|
2
|
+
require 'simplify_rb/symbolizer'
|
2
3
|
|
3
4
|
class SimplifyRb
|
4
|
-
|
5
|
-
def self.simplify
|
5
|
+
|
6
|
+
def self.simplify(points, tolerance = 1, highest_quality = false)
|
6
7
|
raise ArgumentError.new('Points must be an array') unless points.is_a? Array
|
7
8
|
|
8
9
|
return points if points.length <= 1
|
9
10
|
|
10
|
-
|
11
|
+
symbolizer = SimplifyRbUtils::Symbolizer.new
|
12
|
+
|
13
|
+
points = symbolizer.symbolize_keys(points) unless points.all? { |p| symbolizer.keys_are_symbols?(p.keys) }
|
11
14
|
|
12
15
|
sq_tolerance = tolerance * tolerance
|
13
16
|
|
14
17
|
# Optimisation step 1
|
15
|
-
points =
|
18
|
+
points = simplify_radial_dist(points, sq_tolerance) unless highest_quality
|
16
19
|
|
17
20
|
# Optimisation step 2
|
18
|
-
|
21
|
+
simplify_douglas_peucker(points, sq_tolerance)
|
19
22
|
end
|
20
23
|
|
21
24
|
# Basic distance-based simplification
|
22
|
-
def self.
|
25
|
+
def self.simplify_radial_dist(points, sq_tolerance)
|
23
26
|
new_points = [points.first]
|
24
27
|
|
25
28
|
points.each do |point|
|
26
|
-
new_points << point if (
|
29
|
+
new_points << point if (get_sq_dist(point, new_points.last) > sq_tolerance)
|
27
30
|
end
|
28
31
|
|
29
32
|
new_points << points.last unless new_points.last == points.last
|
@@ -32,7 +35,7 @@ class SimplifyRb
|
|
32
35
|
end
|
33
36
|
|
34
37
|
# Simplification using optimized Douglas-Peucker algorithm with recursion elimination
|
35
|
-
def self.
|
38
|
+
def self.simplify_douglas_peucker(points, sq_tolerance)
|
36
39
|
first = 0
|
37
40
|
last = points.length - 1
|
38
41
|
index = nil
|
@@ -41,11 +44,11 @@ class SimplifyRb
|
|
41
44
|
points.first[:keep] = true
|
42
45
|
points.last[:keep] = true
|
43
46
|
|
44
|
-
while last
|
47
|
+
while last
|
45
48
|
max_sq_dist = 0
|
46
49
|
|
47
50
|
((first + 1)...last).each do |i|
|
48
|
-
sq_dist =
|
51
|
+
sq_dist = get_sq_seg_dist(points[i], points[first], points[last])
|
49
52
|
|
50
53
|
if sq_dist > max_sq_dist
|
51
54
|
index = i
|
@@ -66,7 +69,7 @@ class SimplifyRb
|
|
66
69
|
end
|
67
70
|
|
68
71
|
# Square distance between two points
|
69
|
-
def self.
|
72
|
+
def self.get_sq_dist(point_1, point_2)
|
70
73
|
dx = point_1[:x] - point_2[:x]
|
71
74
|
dy = point_1[:y] - point_2[:y]
|
72
75
|
|
@@ -74,13 +77,13 @@ class SimplifyRb
|
|
74
77
|
end
|
75
78
|
|
76
79
|
# Square distance from a point to a segment
|
77
|
-
def self.
|
80
|
+
def self.get_sq_seg_dist(point, point_1, point_2)
|
78
81
|
x = point_1[:x]
|
79
82
|
y = point_1[:y]
|
80
83
|
dx = point_2[:x] - x
|
81
84
|
dy = point_2[:y] - y
|
82
85
|
|
83
|
-
if
|
86
|
+
if dx != 0 || dy != 0
|
84
87
|
t = ((point[:x] - x) * dx + (point[:y] - y) * dy) / (dx * dx + dy * dy)
|
85
88
|
|
86
89
|
if t > 1
|
@@ -98,16 +101,4 @@ class SimplifyRb
|
|
98
101
|
|
99
102
|
dx * dx + dy * dy
|
100
103
|
end
|
101
|
-
|
102
|
-
# Check if keys are symbols
|
103
|
-
def self.keys_are_symbols? (keys)
|
104
|
-
keys.all? {|k| k.is_a? Symbol}
|
105
|
-
end
|
106
|
-
|
107
|
-
# Symbolize all the hash keys in an array of hashes
|
108
|
-
def self.symbolize_keys (collection)
|
109
|
-
collection.map do |item|
|
110
|
-
item.each_with_object({}) { |(k,v), memo| memo[k.to_sym] = v }
|
111
|
-
end
|
112
|
-
end
|
113
104
|
end
|
data/simplify_rb.gemspec
CHANGED
@@ -4,20 +4,20 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'simplify_rb/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'simplify_rb'
|
8
8
|
spec.version = SimplifyRb::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.description =
|
11
|
-
spec.summary =
|
12
|
-
spec.homepage =
|
13
|
-
spec.license =
|
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. Ruby port of Simplify.js.'
|
11
|
+
spec.summary = 'Polyline simplification library. Ruby port of Simplify.js.'
|
12
|
+
spec.homepage = 'https://github.com/odlp/simplify_rb'
|
13
|
+
spec.license = 'MIT'
|
14
14
|
|
15
15
|
spec.files = `git ls-files`.split($/)
|
16
16
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
-
spec.require_paths = [
|
18
|
+
spec.require_paths = ['lib']
|
19
19
|
|
20
|
-
spec.add_development_dependency
|
21
|
-
spec.add_development_dependency
|
22
|
-
spec.add_development_dependency
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
21
|
+
spec.add_development_dependency 'rake', '~> 11'
|
22
|
+
spec.add_development_dependency 'rspec', '~> 3.5'
|
23
23
|
end
|
File without changes
|
data/spec/simplify_rb_spec.rb
CHANGED
@@ -1,49 +1,43 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'simplify_rb'
|
3
|
+
require File.expand_path('../fixtures/simplify_test_data.rb', __FILE__)
|
2
4
|
|
3
5
|
describe SimplifyRb do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
6
|
+
describe '#simplify' do
|
7
|
+
context 'simplifies points correctly with the given tolerance' do
|
8
|
+
let(:test_data) { SimplifyTestData::points }
|
9
|
+
let(:expected_fast_result) { SimplifyTestData::result_fast }
|
10
|
+
let(:expected_high_quality_result) { SimplifyTestData::result_high_quality }
|
11
|
+
|
12
|
+
it 'uses the fast strategy by default' do
|
13
|
+
result = SimplifyRb.simplify(test_data, 5)
|
14
|
+
expect(result).to eq(expected_fast_result)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'uses the high quality strategy when the flag is passed' do
|
18
|
+
result = SimplifyRb.simplify(test_data, 5, true)
|
19
|
+
expect(result).to eq(expected_high_quality_result)
|
20
|
+
end
|
13
21
|
end
|
14
|
-
end
|
15
|
-
|
16
|
-
it "returns the points if it has only one point" do
|
17
|
-
data = [{x: 1, y: 2}]
|
18
|
-
|
19
|
-
expect(SimplifyRb.simplify(data)).to eq(data)
|
20
|
-
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
34
|
-
expect(SimplifyRb.keys_are_symbols? [:a, 'b', :c]).to equal(false)
|
23
|
+
context 'only one point' do
|
24
|
+
it 'returns a list with one point' do
|
25
|
+
data = [{ x: 1, y: 2 }]
|
26
|
+
expect(SimplifyRb.simplify(data)).to eq(data)
|
27
|
+
end
|
35
28
|
end
|
36
29
|
|
37
|
-
|
38
|
-
|
30
|
+
context 'no points' do
|
31
|
+
it 'returns an empty list of points' do
|
32
|
+
expect(SimplifyRb.simplify([])).to be_empty
|
33
|
+
end
|
39
34
|
end
|
40
|
-
end
|
41
|
-
|
42
|
-
describe "#symbolize_keys" do
|
43
|
-
it "converts all of the collection's keys to symbols" do
|
44
|
-
collection = [{'a' => 1, 'b' => 2}, {'c' => 3}]
|
45
35
|
|
46
|
-
|
36
|
+
describe 'unexpected argument' do
|
37
|
+
it 'raises an error if the points are not passsed as an array' do
|
38
|
+
data = { x: 1, y: 2 }
|
39
|
+
expect { SimplifyRb.simplify(data) }.to raise_error(ArgumentError, 'Points must be an array')
|
40
|
+
end
|
47
41
|
end
|
48
42
|
end
|
49
43
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'simplify_rb/symbolizer'
|
3
|
+
|
4
|
+
describe SimplifyRbUtils::Symbolizer do
|
5
|
+
describe '#keys_are_symbols?' do
|
6
|
+
it 'returns false if any key is not a Symbol' do
|
7
|
+
expect(subject.keys_are_symbols?([:a, 'b', :c])).to equal(false)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'returns return true if all the keys are Symbols' do
|
11
|
+
expect(subject.keys_are_symbols?([:a, :b, :c])).to equal(true)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#symbolize_keys' do
|
16
|
+
it 'converts all of the collection\'s keys to symbols' do
|
17
|
+
collection = [{ 'a' => 1, 'b' => 2 }, { 'c' => 3 }]
|
18
|
+
symbolized_result = subject.symbolize_keys(collection)
|
19
|
+
expected_result = [{ a: 1, b: 2 }, { c: 3 }]
|
20
|
+
|
21
|
+
expect(symbolized_result).to eq(expected_result)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
CHANGED
@@ -1,57 +1,57 @@
|
|
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.3
|
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: 2016-07-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.7'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
26
|
+
version: '1.7'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '11'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '11'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '3'
|
47
|
+
version: '3.5'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - ~>
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '3'
|
54
|
+
version: '3.5'
|
55
55
|
description: You can use this gem to reduce the number of points in a complex polyline
|
56
56
|
/ polygon, making use of an optimized Douglas-Peucker algorithm. Ruby port of Simplify.js.
|
57
57
|
email:
|
@@ -59,18 +59,22 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
- .gitignore
|
63
|
-
- .rspec
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".rubocop.yml"
|
65
|
+
- ".travis.yml"
|
64
66
|
- Gemfile
|
65
67
|
- LICENSE.txt
|
66
68
|
- README.md
|
67
69
|
- Rakefile
|
68
70
|
- lib/simplify_rb.rb
|
71
|
+
- lib/simplify_rb/symbolizer.rb
|
69
72
|
- lib/simplify_rb/version.rb
|
70
73
|
- simplify_rb.gemspec
|
74
|
+
- spec/fixtures/simplify_test_data.rb
|
71
75
|
- spec/simplify_rb_spec.rb
|
72
|
-
- spec/simplify_test_data.rb
|
73
76
|
- spec/spec_helper.rb
|
77
|
+
- spec/symbolizer_spec.rb
|
74
78
|
homepage: https://github.com/odlp/simplify_rb
|
75
79
|
licenses:
|
76
80
|
- MIT
|
@@ -81,21 +85,22 @@ require_paths:
|
|
81
85
|
- lib
|
82
86
|
required_ruby_version: !ruby/object:Gem::Requirement
|
83
87
|
requirements:
|
84
|
-
- -
|
88
|
+
- - ">="
|
85
89
|
- !ruby/object:Gem::Version
|
86
90
|
version: '0'
|
87
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
92
|
requirements:
|
89
|
-
- -
|
93
|
+
- - ">="
|
90
94
|
- !ruby/object:Gem::Version
|
91
95
|
version: '0'
|
92
96
|
requirements: []
|
93
97
|
rubyforge_project:
|
94
|
-
rubygems_version: 2.
|
98
|
+
rubygems_version: 2.5.1
|
95
99
|
signing_key:
|
96
100
|
specification_version: 4
|
97
101
|
summary: Polyline simplification library. Ruby port of Simplify.js.
|
98
102
|
test_files:
|
103
|
+
- spec/fixtures/simplify_test_data.rb
|
99
104
|
- spec/simplify_rb_spec.rb
|
100
|
-
- spec/simplify_test_data.rb
|
101
105
|
- spec/spec_helper.rb
|
106
|
+
- spec/symbolizer_spec.rb
|