From 83d4a5633533a014f85797aac4babbab0feab38c Mon Sep 17 00:00:00 2001 From: Rafael Santos Date: Fri, 25 Apr 2025 22:16:49 +1200 Subject: [PATCH 1/2] fix some namespaced calls --- lib/turf/length.rb | 2 +- lib/turf/meta.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/turf/length.rb b/lib/turf/length.rb index cb6ace6..e04a502 100644 --- a/lib/turf/length.rb +++ b/lib/turf/length.rb @@ -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 diff --git a/lib/turf/meta.rb b/lib/turf/meta.rb index 91258f6..5105466 100644 --- a/lib/turf/meta.rb +++ b/lib/turf/meta.rb @@ -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 From 135ad608cb954965483d788c86fe3c32f1454f49 Mon Sep 17 00:00:00 2001 From: Rafael Santos Date: Fri, 25 Apr 2025 22:21:42 +1200 Subject: [PATCH 2/2] port boolean_clockwise --- lib/turf/boolean_clockwise.rb | 24 +++++++++++++-- test/turf_boolean_clockwise_test.rb | 48 +++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/lib/turf/boolean_clockwise.rb b/lib/turf/boolean_clockwise.rb index cab9ba5..49e7081 100644 --- a/lib/turf/boolean_clockwise.rb +++ b/lib/turf/boolean_clockwise.rb @@ -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>] 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 diff --git a/test/turf_boolean_clockwise_test.rb b/test/turf_boolean_clockwise_test.rb index 75cd356..f34384b 100644 --- a/test/turf_boolean_clockwise_test.rb +++ b/test/turf_boolean_clockwise_test.rb @@ -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