Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ jobs:
bundler-cache: true
- name: RSpec
run: bundle exec rspec
- name: Standard
run: bundle exec standardrb --no-fix
3 changes: 0 additions & 3 deletions .rubocop.yml

This file was deleted.

2 changes: 2 additions & 0 deletions .standard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix: true
parallel: true
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'
source "https://rubygems.org"

# Specify your gem's dependencies in simplify_rb.gemspec
gemspec
47 changes: 47 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,20 @@ PATH
GEM
remote: https://rubygems.org/
specs:
ast (2.4.3)
diff-lcs (1.6.2)
json (2.15.1)
language_server-protocol (3.17.0.5)
lint_roller (1.1.0)
parallel (1.27.0)
parser (3.3.9.0)
ast (~> 2.4.1)
racc
prism (1.6.0)
racc (1.8.1)
rainbow (3.1.1)
rake (13.3.0)
regexp_parser (2.11.3)
rspec (3.13.2)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
Expand All @@ -21,6 +33,40 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.6)
rubocop (1.80.2)
json (~> 2.3)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.1.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.46.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.47.1)
parser (>= 3.3.7.2)
prism (~> 1.4)
rubocop-performance (1.25.0)
lint_roller (~> 1.1)
rubocop (>= 1.75.0, < 2.0)
rubocop-ast (>= 1.38.0, < 2.0)
ruby-progressbar (1.13.0)
standard (1.51.1)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.80.2)
standard-custom (~> 1.0.0)
standard-performance (~> 1.8)
standard-custom (1.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.50)
standard-performance (1.8.0)
lint_roller (~> 1.1)
rubocop-performance (~> 1.25.0)
unicode-display_width (3.2.0)
unicode-emoji (~> 4.1)
unicode-emoji (4.1.0)

PLATFORMS
arm64-darwin-24
Expand All @@ -30,6 +76,7 @@ DEPENDENCIES
rake
rspec
simplify_rb!
standard

BUNDLED WITH
2.7.2
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)
task default: [:spec]
6 changes: 3 additions & 3 deletions example/example.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'simplify_rb'
require "simplify_rb"

points = [
{ x: 51.5256, y: -0.0875 },
{ x: 51.7823, y: -0.0912 }
{x: 51.5256, y: -0.0875},
{x: 51.7823, y: -0.0912}
]
tolerance = 1
high_quality = true
Expand Down
10 changes: 5 additions & 5 deletions lib/simplify_rb.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require 'simplify_rb/version'
require 'simplify_rb/point'
require 'simplify_rb/radial_distance_simplifier'
require 'simplify_rb/douglas_peucker_simplifier'
require "simplify_rb/version"
require "simplify_rb/point"
require "simplify_rb/radial_distance_simplifier"
require "simplify_rb/douglas_peucker_simplifier"

module SimplifyRb
class Simplifier
def process(raw_points, tolerance = 1, highest_quality = false)
raise ArgumentError.new('raw_points must be enumerable') unless raw_points.is_a? Enumerable
raise ArgumentError.new("raw_points must be enumerable") unless raw_points.is_a? Enumerable

return raw_points if raw_points.length <= 1

Expand Down
8 changes: 4 additions & 4 deletions lib/simplify_rb/douglas_peucker_simplifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module SimplifyRb
class DouglasPeuckerSimplifier
def process(points, sq_tolerance)
points.first.keep = true
points.last.keep = true
points.last.keep = true

simplify_douglas_peucker(points, sq_tolerance)
.select(&:keep)
Expand All @@ -16,7 +16,7 @@ def process(points, sq_tolerance)

def simplify_douglas_peucker(points, sq_tolerance)
first_i = 0
last_i = points.length - 1
last_i = points.length - 1
index = nil
stack = []

Expand Down Expand Up @@ -55,8 +55,8 @@ def calc_max_sq_dist(first_i, last_i, points)

# Square distance from a point to a segment
def get_sq_seg_dist(point, point_1, point_2)
x = point_1.x
y = point_1.y
x = point_1.x
y = point_1.y
dx = point_2.x - x
dy = point_2.y - y

Expand Down
8 changes: 4 additions & 4 deletions lib/simplify_rb/point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ def parse_x_y(raw_point)
x = nil
y = nil

if raw_point.kind_of? Hash
x = raw_point[:x] || raw_point['x']
y = raw_point[:y] || raw_point['y']
if raw_point.is_a? Hash
x = raw_point[:x] || raw_point["x"]
y = raw_point[:y] || raw_point["y"]
elsif raw_point.respond_to?(:x) && raw_point.respond_to?(:y)
x = raw_point.x
y = raw_point.y
end

if x.nil? || y.nil?
raise ArgumentError.new('Points must have :x and :y values')
raise ArgumentError.new("Points must have :x and :y values")
end

[x, y]
Expand Down
31 changes: 15 additions & 16 deletions simplify_rb.gemspec
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'simplify_rb/version'
require "simplify_rb/version"

Gem::Specification.new do |spec|
spec.name = 'simplify_rb'
spec.version = SimplifyRb::VERSION
spec.authors = ['odlp']
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.'
spec.summary = 'Polyline simplification library. Ruby port of Simplify.js.'
spec.homepage = 'https://github.com/odlp/simplify_rb'
spec.license = 'MIT'
spec.name = "simplify_rb"
spec.version = SimplifyRb::VERSION
spec.authors = ["odlp"]
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."
spec.summary = "Polyline simplification library. Ruby port of Simplify.js."
spec.homepage = "https://github.com/odlp/simplify_rb"
spec.license = "MIT"

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']
spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "standard"
end
52 changes: 30 additions & 22 deletions spec/simplify_rb/point_spec.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
require 'simplify_rb/point'
require "simplify_rb/point"

RSpec.describe SimplifyRb::Point do
describe 'parsing hashes with string keys' do
it 'determines the :x, :y value' do
raw_point = { "x" => 51.5256, "y" => -0.0875 }
describe "parsing hashes with string keys" do
it "determines the :x, :y value" do
raw_point = {"x" => 51.5256, "y" => -0.0875}
point = described_class.new(raw_point)

expect(point.x).to eq(51.5256)
expect(point.y).to eq(-0.0875)
end
end

describe 'parsing structs' do
it 'determines the :x, :y value' do
CustomPointStruct = Struct.new(:x, :y)
raw_point = CustomPointStruct.new(51.5256, -0.0875)
describe "parsing structs" do
it "determines the :x, :y value" do
custom_point_struct = Struct.new(:x, :y)
raw_point = custom_point_struct.new(51.5256, -0.0875)

point = described_class.new(raw_point)

Expand All @@ -23,9 +23,9 @@
end
end

describe 'handling raw points which are objects' do
it 'determines the :x, :y value' do
class MyCustomPoint
describe "handling raw points which are objects" do
it "determines the :x, :y value" do
my_custom_point = define_class("MyCustomPoint") do
attr_reader :x, :y

def initialize(x, y)
Expand All @@ -34,7 +34,7 @@ def initialize(x, y)
end
end

raw_point = MyCustomPoint.new(51.5256, -0.0875)
raw_point = my_custom_point.new(51.5256, -0.0875)

point = described_class.new(raw_point)

Expand All @@ -43,17 +43,17 @@ def initialize(x, y)
end
end

describe 'missing x/y values' do
it 'raises an error if the points are missing keys' do
invalid_point = { Z: 51.5256, y: -0.0875 }
expect { described_class.new(invalid_point) }.to raise_error(ArgumentError, 'Points must have :x and :y values')
describe "missing x/y values" do
it "raises an error if the points are missing keys" do
invalid_point = {Z: 51.5256, y: -0.0875}
expect { described_class.new(invalid_point) }.to raise_error(ArgumentError, "Points must have :x and :y values")

invalid_point = { x: 51.5256, Z: -0.0875 }
expect { described_class.new(invalid_point) }.to raise_error(ArgumentError, 'Points must have :x and :y values')
invalid_point = {x: 51.5256, Z: -0.0875}
expect { described_class.new(invalid_point) }.to raise_error(ArgumentError, "Points must have :x and :y values")
end

it 'raises an error if points don\'t respond to x / y' do
class UnconventialPoint
it "raises an error if points don't respond to x / y" do
unconvention_point = define_class("UnconventionPoint") do
attr_reader :a, :b

def initialize(a, b)
Expand All @@ -62,8 +62,16 @@ def initialize(a, b)
end
end

invalid_point = UnconventialPoint.new(51.5256, -0.0875)
expect { described_class.new(invalid_point) }.to raise_error(ArgumentError, 'Points must have :x and :y values')
invalid_point = unconvention_point.new(51.5256, -0.0875)
expect { described_class.new(invalid_point) }.to raise_error(ArgumentError, "Points must have :x and :y values")
end
end

private

def define_class(name, &block)
const = stub_const(name, Class.new)
const.class_eval(&block) if block_given?
const
end
end
Loading