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
24 changes: 21 additions & 3 deletions lib/turf/boolean_clockwise.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
# frozen_string_literal: true

# :nodoc:
# Takes a ring and returns true or false whether or not the ring is clockwise or counter-clockwise.
#
# @param [Array<Array<Number>>] line to be evaluated
# @return [Boolean] true/false
# @example
# clockwise_ring = [[0, 0], [1, 1], [1, 0], [0, 0]]
# counter_clockwise_ring = [[0, 0], [1, 0], [1, 1], [0, 0]]
#
# boolean_clockwise(clockwise_ring)
# # => true
# boolean_clockwise(counter_clockwise_ring)
# # => false
module Turf
def boolean_clockwise(*args)
raise NotImplementedError
def boolean_clockwise(line)
ring = get_coords(line)
sum = 0

ring.each_cons(2) do |prev, cur|
sum += (cur[0] - prev[0]) * (cur[1] + prev[1])
end

sum > 0
end
end
2 changes: 1 addition & 1 deletion lib/turf/length.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def length(geojson, options = {})
segment_reduce(geojson, 0) do |previous_value, segment|
previous_value ||= 0
coords = segment.dig(:geometry, :coordinates)
previous_value + Turf.distance(coords[0], coords[1], options)
previous_value + distance(coords[0], coords[1], options)
end
end
end
2 changes: 1 addition & 1 deletion lib/turf/meta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def segment_each(geojson)
next
end

segment = Turf.line_string([previous_coords, current_coord], feature[:properties])
segment = line_string([previous_coords, current_coord], feature[:properties])
next unless yield(segment, feature_index, multi_feature_index, geometry_index, segment_index)

segment_index += 1
Expand Down
48 changes: 48 additions & 0 deletions test/turf_boolean_clockwise_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,59 @@

class TurfBooleanClockwiseTest < Minitest::Test
def test_is_clockwise_fixtures
# True Fixtures
Dir.glob(File.join(__dir__, "boolean_clockwise/true/*.geojson")).each do |filepath|
name = File.basename(filepath, ".geojson")
geojson = JSON.parse(File.read(filepath))
feature = geojson["features"].first
assert(Turf.boolean_clockwise(feature), "[true] #{name}")
end

# False Fixtures
Dir.glob(File.join(__dir__, "boolean_clockwise/false/*.geojson")).each do |filepath|
name = File.basename(filepath, ".geojson")
geojson = JSON.parse(File.read(filepath))
feature = geojson["features"].first
refute(Turf.boolean_clockwise(feature), "[false] #{name}")
end
end

def test_is_clockwise
cw_array = [
[0, 0],
[1, 1],
[1, 0],
[0, 0],
]
ccw_array = [
[0, 0],
[1, 0],
[1, 1],
[0, 0],
]

assert_equal(true, Turf.boolean_clockwise(cw_array), "[true] clockwise array input")
assert_equal(false, Turf.boolean_clockwise(ccw_array), "[false] counter-clockwise array input")
end

def test_is_clockwise_geometry_types
line = Turf.line_string([
[0, 0],
[1, 1],
[1, 0],
[0, 0],
])

assert_equal(true, Turf.boolean_clockwise(line), "Feature")
assert_equal(true, Turf.boolean_clockwise(line[:geometry]), "Geometry Object")
end

# Uncomment the following test if exception handling for unsupported geometry types is implemented
#
# def test_is_clockwise_throws
# pt = Turf.point([-10, -33])
# assert_raises do
# Turf.boolean_clockwise(pt)
# end
# end
end