turf-ruby 0.7.0 → 0.8.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1c2222302d50f2e6f28d1369f76d100b8fc31751dbda47083894a2dac065c84d
4
- data.tar.gz: 188b67a600244bc887794766e03b2107c819bc1ccfe559a8aab8c761920a2eb9
3
+ metadata.gz: 15efb6b14189380f9e59456b544fc7cb95436eeceefbf4772f4f6ee1ea2d8a4e
4
+ data.tar.gz: 16d8e44ef4eea15d03ed3059375d3df6c6c793f8c178878b6e1ebcbdb4f6d509
5
5
  SHA512:
6
- metadata.gz: c9c194a59ebf4c99d9390ff90262d297bfebf0dd58ef9ee973435a7c14118e73e60494dec2d1013568bb07a4172fda3fe1fb77dbe50b47e382c0c62d07cdf93e
7
- data.tar.gz: b3b425aa16b643ec0956f7a58b69a8562acf52791c674d12f044883c3445af8d088827abba0d576d5c4d5c251ace25709411e210d274b5c3e9a624c00f2947b6
6
+ metadata.gz: 4362da521aff1c2514668c011fd86348c2df8d6cb27510b17459303e046a3550552810bcc5ff42593ff758d4998d84de897bedf1ca851a71da0e44baec0e589f
7
+ data.tar.gz: ad865f140fab4344bdb0e05bdc31e4e505a8479d2e86e1cdf4dc986130f4033d540dbe4db6bfb2cf6c07bdbfadbb53ef6aeef784d656f75c205263b5fee0e9ac
data/lib/turf/along.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #:nodoc:
3
+ # :nodoc:
4
4
  module Turf
5
5
  # @!group Measurement
6
6
 
data/lib/turf/area.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #:nodoc:
3
+ # :nodoc:
4
4
  module Turf
5
5
  # Takes one or more features and returns their area in square meters.
6
6
  # @see https://turfjs.org/docs/#area
@@ -12,6 +12,8 @@ module Turf
12
12
  end
13
13
  end
14
14
 
15
+ AREA_RADIUS = 6_378_137.0
16
+
15
17
  private
16
18
 
17
19
  def area_calculate_area(geom)
@@ -36,8 +38,6 @@ module Turf
36
38
  total
37
39
  end
38
40
 
39
- AREA_RADIUS = 6_378_137
40
-
41
41
  def area_ring_area(coords)
42
42
  return 0 if coords.size <= 2
43
43
 
@@ -71,10 +71,10 @@ module Turf
71
71
  total += (area_rad(p3[0]) - area_rad(p1[0])) * Math.sin(area_rad(p2[1]))
72
72
  end
73
73
 
74
- total * AREA_RADIUS * AREA_RADIUS / 2
74
+ total * AREA_RADIUS * AREA_RADIUS / 2.0
75
75
  end
76
76
 
77
77
  def area_rad(num)
78
- num * Math::PI / 180
78
+ num * Math::PI / 180.0
79
79
  end
80
80
  end
data/lib/turf/bearing.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #:nodoc:
3
+ # :nodoc:
4
4
  module Turf
5
5
  # @!group Measurement
6
6
 
@@ -22,8 +22,8 @@ module Turf
22
22
  lat1 = degrees_to_radians(coordinates1[1])
23
23
  lat2 = degrees_to_radians(coordinates2[1])
24
24
  a = Math.sin(lon2 - lon1) * Math.cos(lat2)
25
- b = Math.cos(lat1) * Math.sin(lat2) -
26
- Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)
25
+ b = (Math.cos(lat1) * Math.sin(lat2)) -
26
+ (Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1))
27
27
 
28
28
  radians_to_degrees(Math.atan2(a, b))
29
29
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #:nodoc:
3
+ # :nodoc:
4
4
  module Turf
5
5
  # @!group Booleans
6
6
 
@@ -72,7 +72,7 @@ module Turf
72
72
  yj = ring_pt2[1]
73
73
 
74
74
  on_boundary = (
75
- point[1] * (xi - xj) + yi * (xj - point[0]) + yj * (point[0] - xi)
75
+ (point[1] * (xi - xj)) + (yi * (xj - point[0])) + (yj * (point[0] - xi))
76
76
  ).zero?
77
77
  on_boundary &&= ((xi - point[0]) * (xj - point[0]) <= 0)
78
78
  on_boundary &&= ((yi - point[1]) * (yj - point[1]) <= 0)
@@ -81,7 +81,7 @@ module Turf
81
81
  end
82
82
 
83
83
  intersect = ((yi > point[1]) != (yj > point[1])) &&
84
- (point[0] < (xj - xi) * (point[1] - yi) / (yj - yi) + xi)
84
+ (point[0] < ((xj - xi) * (point[1] - yi).to_f / (yj - yi)) + xi)
85
85
  if intersect
86
86
  is_inside = !is_inside
87
87
  end
data/lib/turf/centroid.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #:nodoc:
3
+ # :nodoc:
4
4
  module Turf
5
5
  # @!group Measurement
6
6
 
@@ -11,9 +11,9 @@ module Turf
11
11
  # @param properties [Hash] a [Hash] that is used as the Feature's properties
12
12
  # @return [Feature<Point>] the centroid of the input features
13
13
  def centroid(geojson, properties: {})
14
- x_sum = 0
15
- y_sum = 0
16
- len = 0
14
+ x_sum = 0.0
15
+ y_sum = 0.0
16
+ len = 0.0
17
17
 
18
18
  coord_each geojson, exclude_wrap_coord: true do |coord|
19
19
  x_sum += coord[0]
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc:
4
+ module Turf
5
+ # Takes a Point and calculates the circle polygon given a radius in degrees, radians, miles, or kilometers;
6
+ # and steps for precision.
7
+ # @param {Feature<Point>|number[]} center center point
8
+ # @param {number} radius radius of the circle
9
+ # @param {hash} [options={}] Optional parameters
10
+ # @param {number} [options.steps=64] number of steps
11
+ # @param {string} [options.units='kilometers'] miles, kilometers, degrees, or radians
12
+ # @param {hash} [options.properties={}] properties
13
+ # @returns {Feature<Polygon>} circle polygon
14
+ def circle(center, radius, options = {}, units: nil, steps: 64)
15
+ # default params
16
+ center = deep_symbolize_keys(center)
17
+ properties = options[:properties] || (
18
+ !center.is_a?(Array) && center[:type] == "Feature" && center[:properties] ? center[:properties] : {}
19
+ )
20
+
21
+ # main
22
+ coordinates = []
23
+ destination_options = { properties: properties }
24
+ if units
25
+ destination_options[:units] = units
26
+ end
27
+ steps.times do |i|
28
+ coordinates.push(destination(center, radius, (i * -360.0) / steps, **options).dig(:geometry, :coordinates))
29
+ end
30
+ coordinates.push(coordinates[0])
31
+
32
+ polygon([coordinates], properties: properties)
33
+ end
34
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #:nodoc:
3
+ # :nodoc:
4
4
  module Turf
5
5
  # @!group Measurement
6
6
 
@@ -20,11 +20,11 @@ module Turf
20
20
  bearing_radians = degrees_to_radians bearing
21
21
  radians = length_to_radians distance, units
22
22
 
23
- latitude2 = Math.asin(Math.sin(latitude1) * Math.cos(radians) +
24
- Math.cos(latitude1) * Math.sin(radians) * Math.cos(bearing_radians))
23
+ latitude2 = Math.asin((Math.sin(latitude1) * Math.cos(radians)) +
24
+ (Math.cos(latitude1) * Math.sin(radians) * Math.cos(bearing_radians)))
25
25
  longitude2 = longitude1 + Math.atan2(
26
26
  Math.sin(bearing_radians) * Math.sin(radians) * Math.cos(latitude1),
27
- Math.cos(radians) - Math.sin(latitude1) * Math.sin(latitude2),
27
+ Math.cos(radians) - (Math.sin(latitude1) * Math.sin(latitude2)),
28
28
  )
29
29
  lng = radians_to_degrees(longitude2)
30
30
  lat = radians_to_degrees(latitude2)
data/lib/turf/distance.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #:nodoc:
3
+ # :nodoc:
4
4
  module Turf
5
5
  # @!group Measurement
6
6
 
@@ -22,8 +22,8 @@ module Turf
22
22
 
23
23
  a =
24
24
  (
25
- (Math.sin(d_lat / 2)**2) +
26
- (Math.sin(d_lon / 2)**2) * Math.cos(lat1) * Math.cos(lat2)
25
+ (Math.sin(d_lat / 2.0)**2) +
26
+ ((Math.sin(d_lon / 2.0)**2) * Math.cos(lat1) * Math.cos(lat2))
27
27
  )
28
28
 
29
29
  radians_to_length(
data/lib/turf/helpers.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #:nodoc:
3
+ # :nodoc:
4
4
  module Turf
5
5
  EARTH_RADIUS = 6_371_008.8
6
6
  private_constant :EARTH_RADIUS
@@ -18,7 +18,7 @@ module Turf
18
18
  "millimeters" => EARTH_RADIUS * 1000,
19
19
  "millimetres" => EARTH_RADIUS * 1000,
20
20
  "nauticalmiles" => EARTH_RADIUS / 1852,
21
- "radians" => 1,
21
+ "radians" => 1.0,
22
22
  "yards" => EARTH_RADIUS / 1.0936
23
23
  }.freeze
24
24
  private_constant :FACTORS
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #:nodoc:
3
+ # :nodoc:
4
4
  module Turf
5
5
  # @!group Meta
6
6
 
data/lib/turf/length.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #:nodoc:
3
+ # :nodoc:
4
4
  module Turf
5
5
  # @!group Measurement
6
6
 
data/lib/turf/meta.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #:nodoc:
3
+ # :nodoc:
4
4
  module Turf
5
5
  # @!group Meta
6
6
 
@@ -13,7 +13,7 @@ module Turf
13
13
  # @yieldparam current_coord [Array<number>] The current coordinate being processed.
14
14
  # @yieldparam coord_index [number] The current index of the coordinate being processed.
15
15
  def coord_each(geojson, exclude_wrap_coord: false, &block)
16
- coord_all(geojson, exclude_wrap_coord: exclude_wrap_coord).each_with_index(&block)
16
+ coord_all(geojson, exclude_wrap_coord: exclude_wrap_coord, &block)
17
17
  end
18
18
 
19
19
  # Get all coordinates from any GeoJSON object.
@@ -22,40 +22,51 @@ module Turf
22
22
  # @param exclude_wrap_coord [boolean] whether or not to include the final coordinate of LinearRings that wraps the
23
23
  # ring in its iteration
24
24
  # @return [Array<Array<number>>] coordinate position array
25
- def coord_all(geojson, exclude_wrap_coord: false)
26
- geometries = self.geometries(geojson)
27
- geometries.flat_map do |geometry|
28
- next [] if geometry.nil?
25
+ def coord_all(geojson, exclude_wrap_coord: false, &block)
26
+ geometry_index = -1
27
+ geom_each(geojson) do |geometry, _idx, _properties|
28
+ if geometry.nil?
29
+ next
30
+ end
29
31
 
30
32
  case geometry[:type]
31
33
  when "Point"
32
- [geometry[:coordinates]]
34
+ geometry_index += 1
35
+ block.call(geometry[:coordinates], geometry_index)
33
36
  when "LineString", "MultiPoint"
34
- geometry[:coordinates]
37
+ geometry[:coordinates].each do |coords|
38
+ geometry_index += 1
39
+ block.call(coords, geometry_index)
40
+ end
35
41
  when "Polygon", "MultiLineString"
36
- geometry[:coordinates].flat_map do |line_coords|
37
- (
38
- exclude_wrap_coord ? line_coords.slice(0...-1) : line_coords
39
- )
42
+ geometry[:coordinates].each do |line_coords|
43
+ if exclude_wrap_coord
44
+ line_coords = line_coords[0...-1]
45
+ end
46
+ line_coords.each do |coords|
47
+ geometry_index += 1
48
+ block.call(coords, geometry_index)
49
+ end
40
50
  end
41
51
  when "MultiPolygon"
42
- geometry[:coordinates].flat_map do |polygon_coords|
43
- polygon_coords.flat_map do |line_coords|
44
- (
45
- exclude_wrap_coord ? line_coords.slice(0...-1) : line_coords
46
- )
52
+ geometry[:coordinates].each do |polygon_coords|
53
+ polygon_coords.each do |line_coords|
54
+ if exclude_wrap_coord
55
+ line_coords = line_coords[0...-1]
56
+ end
57
+ line_coords.each do |coords|
58
+ geometry_index += 1
59
+ block.call(coords, geometry_index)
60
+ end
47
61
  end
48
62
  end
49
63
  when "Feature"
50
- [].tap do |feature_coords|
51
- coord_each geometry, exclude_wrap_coord: exclude_wrap_coord do |coord|
52
- feature_coords.push coord
53
- end
54
- end
64
+ coord_each(geometry, exclude_wrap_coord: exclude_wrap_coord, &block)
55
65
  else
56
66
  raise Error, "Unknown Geometry Type: #{geometry[:type]}"
57
67
  end
58
68
  end
69
+ geojson
59
70
  end
60
71
 
61
72
  # Reduce coordinates in any GeoJSON object, similar to Array.reduce()
@@ -98,9 +109,8 @@ module Turf
98
109
  def geom_each(geojson)
99
110
  return unless geojson
100
111
 
101
- geojson = deep_symbolize_keys geojson
112
+ geojson = deep_symbolize_keys! geojson
102
113
 
103
- # [geometry, properties, bbox, id]
104
114
  entries = []
105
115
 
106
116
  case geojson[:type]
@@ -114,21 +124,18 @@ module Turf
114
124
  entries.push [geojson, {}, nil, nil]
115
125
  end
116
126
 
127
+ entry_index = -1
128
+
117
129
  # flatten GeometryCollection
118
- entries =
119
- entries.flat_map do |entry|
120
- geometry, properties, bbox, id = entry
121
- next [entry] if geometry.nil?
122
- next [entry] unless geometry[:type] == "GeometryCollection"
123
-
124
- geometry[:geometries].map do |sub_geometry|
125
- [sub_geometry, properties, bbox, id]
130
+ entries.each do |entry|
131
+ geometry, properties, bbox, id = entry
132
+ if geometry.nil? || geometry[:type] != "GeometryCollection"
133
+ yield(geometry, (entry_index += 1), properties, bbox, id)
134
+ else
135
+ geometry[:geometries].each do |sub_geometry|
136
+ yield(sub_geometry, (entry_index += 1), properties, bbox, id)
126
137
  end
127
138
  end
128
-
129
- entries.each_with_index do |entry, entry_index|
130
- geometry, properties, bbox, id = entry
131
- yield geometry, entry_index, properties, bbox, id
132
139
  end
133
140
  end
134
141
 
@@ -171,11 +178,11 @@ module Turf
171
178
  # @param geojson [FeatureCollection|Feature|Geometry] any GeoJSON object
172
179
  # @return [Array<Geometry>] list of Geometry
173
180
  def geometries(geojson)
174
- [].tap do |geometries|
175
- geom_each(geojson) do |geometry|
176
- geometries.push(geometry)
177
- end
181
+ geometries = []
182
+ geom_each(geojson) do |geometry|
183
+ geometries.push(geometry)
178
184
  end
185
+ geometries
179
186
  end
180
187
 
181
188
  # Iterate over features in any GeoJSON object, similar to Array.forEach.
@@ -188,7 +195,7 @@ module Turf
188
195
  return unless geojson
189
196
 
190
197
  features = []
191
- geojson = deep_symbolize_keys geojson
198
+ geojson = deep_symbolize_keys! geojson
192
199
  case geojson[:type]
193
200
  when "Feature"
194
201
  features.push geojson
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc:
4
+ module Turf
5
+ # Takes a GeoJSON Feature or FeatureCollection and truncates the precision of the geometry.
6
+ # @param {GeoJSON} geojson any GeoJSON Feature, FeatureCollection, Geometry or GeometryCollection.
7
+ # @param {hash} [options={}] Optional parameters
8
+ # @param {number} [options.precision=6] coordinate decimal precision
9
+ # @param {number} [options.coordinates=3] maximum number of coordinates (primarly used to remove z coordinates)
10
+ # @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated
11
+ # (significant performance increase if true (note statement from js port. not checked in ruby))
12
+ # @returns {GeoJSON} layer with truncated geometry
13
+ # @example
14
+ # var point = turf.point([
15
+ # 70.46923055566859,
16
+ # 58.11088890802906,
17
+ # 1508
18
+ # ])
19
+ # var options = {precision: 3, coordinates: 2}
20
+ # var truncated = turf.truncate(point, options)
21
+ # //=truncated.geometry.coordinates => [70.469, 58.111]
22
+ #
23
+ # //addToMap
24
+ # var addToMap = [truncated]
25
+ # /
26
+ def truncate(geojson, options = {})
27
+ precision = options[:precision] || 6
28
+ coordinates = options[:coordinates] || 3
29
+ mutate = options[:mutate]
30
+
31
+ if !geojson
32
+ raise("geojson is required")
33
+ end
34
+
35
+ if !mutate
36
+ geojson = deep_dup(geojson)
37
+ end
38
+
39
+ # factor = Math.pow(10, precision)
40
+ # geojson[:properties][:truncate] = "done"
41
+ # Truncate Coordinates
42
+ coord_each(geojson) do |coords|
43
+ truncate_coords(coords, precision, coordinates)
44
+ end
45
+ geojson
46
+ end
47
+
48
+ def truncate_coords(coords, precision, coordinates)
49
+ # Remove extra coordinates (usually elevation coordinates and more)
50
+ if coords.length > coordinates
51
+ coords.slice!(coordinates..-1)
52
+ end
53
+
54
+ # coords.map do |coord|
55
+ # coord.round(precision)
56
+ # end
57
+
58
+ # Truncate coordinate decimals
59
+ (0...coords.length).each do |idx|
60
+ coords[idx] = coords[idx].round(precision)
61
+ end
62
+ coords
63
+ end
64
+ end
data/lib/turf/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Turf
4
4
  # Version of turf-ruby
5
- VERSION = "0.7.0"
5
+ VERSION = "0.8.0"
6
6
  end
data/lib/turf.rb CHANGED
@@ -27,4 +27,37 @@ module Turf
27
27
  input
28
28
  end
29
29
  end
30
+
31
+ def deep_symbolize_keys!(input)
32
+ case input
33
+ when Hash
34
+ input.transform_keys!(&:to_sym).transform_values do |value|
35
+ deep_symbolize_keys!(value)
36
+ end
37
+ when Array
38
+ input.map do |value|
39
+ deep_symbolize_keys! value
40
+ end
41
+ end
42
+ input
43
+ end
44
+
45
+ def deep_dup(input)
46
+ if input.is_a?(Hash)
47
+ duppe = {}
48
+ input.each_pair do |key, value|
49
+ if key.is_a?(::String) || key.is_a?(::Symbol)
50
+ duppe[key] = deep_dup(value)
51
+ else
52
+ duppe.delete(key)
53
+ duppe[deep_dup(key)] = deep_dup(value)
54
+ end
55
+ end
56
+ duppe
57
+ elsif input.is_a?(Array)
58
+ input.map { |i| deep_dup(i) }
59
+ else
60
+ input.dup
61
+ end
62
+ end
30
63
  end
data/lib/turf_ruby.rb CHANGED
@@ -9,11 +9,13 @@ require "turf/area"
9
9
  require "turf/bearing"
10
10
  require "turf/boolean_point_in_polygon"
11
11
  require "turf/centroid"
12
+ require "turf/circle"
12
13
  require "turf/destination"
13
14
  require "turf/distance"
14
15
  require "turf/helpers"
15
16
  require "turf/invariant"
16
17
  require "turf/length"
18
+ require "turf/truncate"
17
19
  require "turf/meta"
18
20
 
19
21
  require "turf"
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turf-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafael Santos
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2023-04-29 00:00:00.000000000 Z
10
+ date: 2025-03-11 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Turf Ruby is a Ruby library for spatial analysis. It includes traditional
14
13
  spatial operations, helper functions for creating GeoJSON data, and data classification
@@ -25,12 +24,14 @@ files:
25
24
  - lib/turf/bearing.rb
26
25
  - lib/turf/boolean_point_in_polygon.rb
27
26
  - lib/turf/centroid.rb
27
+ - lib/turf/circle.rb
28
28
  - lib/turf/destination.rb
29
29
  - lib/turf/distance.rb
30
30
  - lib/turf/helpers.rb
31
31
  - lib/turf/invariant.rb
32
32
  - lib/turf/length.rb
33
33
  - lib/turf/meta.rb
34
+ - lib/turf/truncate.rb
34
35
  - lib/turf/version.rb
35
36
  - lib/turf_ruby.rb
36
37
  homepage: http://github.com/formigarafa/turf-ruby
@@ -40,7 +41,7 @@ metadata:
40
41
  homepage_uri: http://github.com/formigarafa/turf-ruby
41
42
  source_code_uri: http://github.com/formigarafa/turf-ruby
42
43
  documentation_uri: https://formigarafa.github.io/turf-ruby/
43
- post_install_message:
44
+ rubygems_mfa_required: 'true'
44
45
  rdoc_options: []
45
46
  require_paths:
46
47
  - lib
@@ -55,8 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
56
  - !ruby/object:Gem::Version
56
57
  version: '0'
57
58
  requirements: []
58
- rubygems_version: 3.1.6
59
- signing_key:
59
+ rubygems_version: 3.6.2
60
60
  specification_version: 4
61
61
  summary: A modular geospatial engine. Ruby port of TurfJS.
62
62
  test_files: []