turf-ruby 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 989a5a140f46d9b673a83f71d3df89e6e6b2f9eaa23266a49db3ebeace62ce15
4
- data.tar.gz: 921c501f57d3446e76a505a0f6be9d9549a4f7cd09a57699b192ab2ae8ff7eff
3
+ metadata.gz: 4a1a13828f696140452584b283d12c6d8c9f365d97cbc0995ccbd275756a84e8
4
+ data.tar.gz: 8c9b413efdd827194c98535eb64dfe61bb5057c5aa0c61bc1e3899cf9b79a24c
5
5
  SHA512:
6
- metadata.gz: 41d5d74fffef50e570dd4171a5b3518a8b7a07b5ff697da60ef0fddd718919aee2affd646647f99176e5b13f1ee5264249845bf403930831cfe4849da4431127
7
- data.tar.gz: e50d3fdba23ffcb7d73e345bfde22c7fdb3793a0a2b9c923afe52f6bfe8b0893da6a617bbd3d258f7b2229fdca59c1a33a622b9a67dd11e831ec34db0b29666c
6
+ metadata.gz: b8ba31f189e103c965d96cf6e424e0e229221e3bf0391a0d45156f841b64d43cdbc690be5fe58555c41bd4d800313d177467b3f00b3fc599f87925f4261d3864
7
+ data.tar.gz: e56c866eef2ff30d099e459a4d0d095931de7ce1c72f5d5a91f569600fbb694c3ef410d6ab050ea614dc565a014d36643ab53b9910eeada430c9fc3bbb5da220
data/lib/turf.rb CHANGED
@@ -13,11 +13,18 @@ module Turf
13
13
  private
14
14
 
15
15
  # A non-intrusive implemenation of Rails' Hash#deep_symbolize_keys
16
- def deep_symbolize_keys(hash)
17
- return hash unless hash.is_a? Hash
18
-
19
- hash.transform_keys(&:to_sym).transform_values do |value|
20
- deep_symbolize_keys(value)
16
+ def deep_symbolize_keys(input)
17
+ case input
18
+ when Hash
19
+ input.transform_keys(&:to_sym).transform_values do |value|
20
+ deep_symbolize_keys(value)
21
+ end
22
+ when Array
23
+ input.map do |value|
24
+ deep_symbolize_keys value
25
+ end
26
+ else
27
+ input
21
28
  end
22
29
  end
23
30
  end
data/lib/turf/along.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ #:nodoc:
3
4
  module Turf
4
5
  # @!group Measurement
5
6
 
data/lib/turf/bearing.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ #:nodoc:
3
4
  module Turf
4
5
  # @!group Measurement
5
6
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ #:nodoc:
3
4
  module Turf
4
5
  # @!group Booleans
5
6
 
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ #:nodoc:
4
+ module Turf
5
+ # @!group Measurement
6
+
7
+ # Takes one or more features and calculates the centroid using the mean of all vertices. This lessens the effect of
8
+ # small islands and artifacts when calculating the centroid of a set of polygons.
9
+ # @see http://turfjs.org/docs/#centroid
10
+ # @param geojson [GeoJSON] GeoJSON to be centered
11
+ # @param properties [Hash] a [Hash] that is used as the Feature's properties
12
+ # @return [Feature<Point>] the centroid of the input features
13
+ def centroid(geojson, properties: {})
14
+ x_sum = 0
15
+ y_sum = 0
16
+ len = 0
17
+
18
+ coord_each geojson, exclude_wrap_coord: true do |coord|
19
+ x_sum += coord[0]
20
+ y_sum += coord[1]
21
+ len += 1
22
+ end
23
+
24
+ point(
25
+ [x_sum / len, y_sum / len],
26
+ properties: properties,
27
+ )
28
+ end
29
+ end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ #:nodoc:
3
4
  module Turf
4
5
  # @!group Measurement
5
6
 
@@ -28,7 +29,6 @@ module Turf
28
29
  lng = radians_to_degrees(longitude2)
29
30
  lat = radians_to_degrees(latitude2)
30
31
 
31
- # prevent ruby@2 converting `properties` as kwargs
32
- point([lng, lat], properties, **{})
32
+ point([lng, lat], properties: properties)
33
33
  end
34
34
  end
data/lib/turf/distance.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ #:nodoc:
3
4
  module Turf
4
5
  # @!group Measurement
5
6
 
data/lib/turf/helpers.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ #:nodoc:
3
4
  module Turf
4
5
  EARTH_RADIUS = 6_371_008.8
5
6
  private_constant :EARTH_RADIUS
@@ -31,11 +32,11 @@ module Turf
31
32
  # @param bbox [Array<number>] Bounding Box Array [west, south, east, north] associated with the Feature
32
33
  # @param id [string | number] Identifier associated with the Feature
33
34
  # @return [Feature] a GeoJSON Feature
34
- def feature(geom, properties = {}, bbox: nil, id: nil)
35
+ def feature(geom, properties: {}, bbox: nil, id: nil)
35
36
  feat = {
36
37
  type: "Feature",
37
- geometry: geom,
38
- properties: properties
38
+ properties: properties,
39
+ geometry: geom
39
40
  }
40
41
  feat[:id] = options[:id] if id
41
42
  feat[:bbox] = options[:bbox] if bbox
@@ -65,7 +66,7 @@ module Turf
65
66
  # @param bbox [Array<number>] Bounding Box Array [west, south, east, north] associated with the Feature
66
67
  # @param id [string | number] Identifier associated with the Feature
67
68
  # @return [Feature<LineString>] LineString Feature
68
- def line_string(coordinates, properties = {}, bbox: nil, id: nil)
69
+ def line_string(coordinates, properties: {}, bbox: nil, id: nil)
69
70
  if coordinates.size < 2
70
71
  raise Error, "coordinates must be an array of two or more positions"
71
72
  end
@@ -74,7 +75,7 @@ module Turf
74
75
  type: "LineString",
75
76
  coordinates: coordinates
76
77
  }
77
- feature(geom, properties, bbox: bbox, id: id)
78
+ feature(geom, properties: properties, bbox: bbox, id: id)
78
79
  end
79
80
 
80
81
  # Creates a Point Feature from a Position.
@@ -84,12 +85,12 @@ module Turf
84
85
  # @param bbox [Array<number>] Bounding Box Array [west, south, east, north] associated with the Feature
85
86
  # @param id [string | number] Identifier associated with the Feature
86
87
  # @return [Feature<Point>] a Point feature
87
- def point(coordinates, properties = {}, id: nil, bbox: nil)
88
+ def point(coordinates, properties: {}, id: nil, bbox: nil)
88
89
  geom = {
89
90
  type: "Point",
90
91
  coordinates: coordinates
91
92
  }
92
- feature(geom, properties, id: id, bbox: bbox)
93
+ feature(geom, properties: properties, id: id, bbox: bbox)
93
94
  end
94
95
 
95
96
  # Creates a Polygon Feature from an Array of LinearRings.
@@ -99,7 +100,7 @@ module Turf
99
100
  # @param bbox [Array<number>] Bounding Box Array [west, south, east, north] associated with the Feature
100
101
  # @param id [string | number] Identifier associated with the Feature
101
102
  # @return [Feature<Polygon>] Polygon feature
102
- def polygon(coordinates, properties = {}, bbox: nil, id: nil)
103
+ def polygon(coordinates, properties: {}, bbox: nil, id: nil)
103
104
  coordinates.each do |ring|
104
105
  if ring.size < 4
105
106
  raise(
@@ -118,7 +119,37 @@ module Turf
118
119
  type: "Polygon",
119
120
  coordinates: coordinates
120
121
  }
121
- feature(geom, properties, id: id, bbox: bbox)
122
+ feature(geom, properties: properties, id: id, bbox: bbox)
123
+ end
124
+
125
+ # Creates a Feature<MultiPolygon> based on a coordinate array. Properties can be added optionally.
126
+ # @see https://turfjs.org/docs/#multiPolygon
127
+ # @param coordinates [Array<Array<Array<Array<number>>>>] an array of Polygons
128
+ # @param properties [Hash] an Hash of key-value pairs to add as properties
129
+ # @param bbox [Array<number>] Bounding Box Array [west, south, east, north] associated with the Feature
130
+ # @param id [string|number] Identifier associated with the Feature
131
+ # @return [Feature<MultiPolygon>] a multipolygon feature
132
+ def multi_polygon(coordinates, properties: {}, bbox: nil, id: nil)
133
+ geom = {
134
+ type: "MultiPolygon",
135
+ coordinates: coordinates
136
+ }
137
+ feature(geom, properties: properties, id: id, bbox: bbox)
138
+ end
139
+
140
+ # Creates a Feature<MultiLineString> based on a coordinate array. Properties can be added optionally.
141
+ # @see https://turfjs.org/docs/#multiLineString
142
+ # @param coordinates [Array<Array<Array<number>>>] coordinates an array of LineStrings
143
+ # @param properties [Hash] a Hash of key-value pairs to add as properties
144
+ # @param bbox [Array<number>] Bounding Box Array [west, south, east, north] associated with the Feature
145
+ # @param id [string|number] Identifier associated with the Feature
146
+ # @return [Feature<MultiLineString>] a MultiLineString feature
147
+ def multi_line_string(coordinates, properties: {}, bbox: nil, id: nil)
148
+ geom = {
149
+ type: "MultiLineString",
150
+ coordinates: coordinates
151
+ }
152
+ feature(geom, properties: properties, id: id, bbox: bbox)
122
153
  end
123
154
 
124
155
  # @!group Unit Conversion
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ #:nodoc:
3
4
  module Turf
4
5
  # @!group Meta
5
6
 
data/lib/turf/meta.rb ADDED
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ #:nodoc:
4
+ module Turf
5
+ # @!group Meta
6
+
7
+ # Iterate over coordinates in any GeoJSON object, similar to Array.forEach()
8
+ # @see https://turfjs.org/docs/#coordEach
9
+ # @param geojson [FeatureCollection|Feature|Geometry] any GeoJSON object
10
+ # @param exclude_wrap_coord [boolean] whether or not to include the final coordinate of LinearRings that wraps the
11
+ # ring in its iteration
12
+ # @yield [current_coord, coord_index] given any coordinate
13
+ # @yieldparam current_coord [Array<number>] The current coordinate being processed.
14
+ # @yieldparam coord_index [number] The current index of the coordinate being processed.
15
+ def coord_each(geojson, exclude_wrap_coord: false, &block)
16
+ return unless geojson
17
+
18
+ geojson = deep_symbolize_keys geojson
19
+ geometries = []
20
+ case geojson[:type]
21
+ when "FeatureCollection"
22
+ geojson[:features].each do |feature|
23
+ geometries.push feature[:geometry]
24
+ end
25
+ when "Feature"
26
+ geometries.push geojson[:geometry]
27
+ else
28
+ geometries.push geojson
29
+ end
30
+
31
+ # flatten GeometryCollection
32
+ geometries =
33
+ geometries.map do |geometry|
34
+ next if geometry.nil?
35
+ next geometry unless geometry[:type] == "GeometryCollection"
36
+
37
+ geometry[:geometries]
38
+ end.flatten
39
+
40
+ coords =
41
+ geometries.flat_map do |geometry|
42
+ next nil if geometry.nil?
43
+
44
+ case geometry[:type]
45
+ when "Point"
46
+ [geometry[:coordinates]]
47
+ when "LineString", "MultiPoint"
48
+ geometry[:coordinates]
49
+ when "Polygon", "MultiLineString"
50
+ geometry[:coordinates].flat_map do |line_coords|
51
+ (
52
+ exclude_wrap_coord ? line_coords.slice(0...-1) : line_coords
53
+ )
54
+ end
55
+ when "MultiPolygon"
56
+ geometry[:coordinates].flat_map do |polygon_coords|
57
+ polygon_coords.flat_map do |line_coords|
58
+ (
59
+ exclude_wrap_coord ? line_coords.slice(0...-1) : line_coords
60
+ )
61
+ end
62
+ end
63
+ when "Feature"
64
+ [].tap do |feature_coords|
65
+ coord_each geometry, exclude_wrap_coord: exclude_wrap_coord do |coord|
66
+ feature_coords.push coord
67
+ end
68
+ end
69
+ else
70
+ raise Error, "Unknown Geometry Type: #{geometry[:type]}"
71
+ end
72
+ end.compact
73
+
74
+ coords.each_with_index(&block)
75
+ end
76
+
77
+ # Iterate over features in any GeoJSON object, similar to Array.forEach.
78
+ # @see https://turfjs.org/docs/#featureEach
79
+ # @param geojson [FeatureCollection|Feature|Geometry] any GeoJSON object
80
+ # @yield [feature] given any coordinate
81
+ # @yieldparam feature [Feature<any>] currentFeature The current Feature being processed.
82
+ # @yieldparam feature_index [number] The current index of the Feature being processed.
83
+ def feature_each(geojson, &block)
84
+ return unless geojson
85
+
86
+ geojson = deep_symbolize_keys geojson
87
+ case geojson[:type]
88
+ when "Feature"
89
+ yield geojson, 0
90
+ when "FeatureCollection"
91
+ geojson[:features].each_with_index(&block)
92
+ end
93
+ end
94
+ 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.4.0"
5
+ VERSION = "0.5.0"
6
6
  end
data/lib/turf_ruby.rb CHANGED
@@ -11,5 +11,7 @@ require "turf/along"
11
11
  require "turf/distance"
12
12
  require "turf/invariant"
13
13
  require "turf/boolean_point_in_polygon"
14
+ require "turf/centroid"
15
+ require "turf/meta"
14
16
 
15
17
  require "turf"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turf-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafael Santos
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-14 00:00:00.000000000 Z
11
+ date: 2021-06-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Turf Ruby is a Ruby library for spatial analysis. It includes traditional
14
14
  spatial operations, helper functions for creating GeoJSON data, and data classification
@@ -23,10 +23,12 @@ files:
23
23
  - lib/turf/along.rb
24
24
  - lib/turf/bearing.rb
25
25
  - lib/turf/boolean_point_in_polygon.rb
26
+ - lib/turf/centroid.rb
26
27
  - lib/turf/destination.rb
27
28
  - lib/turf/distance.rb
28
29
  - lib/turf/helpers.rb
29
30
  - lib/turf/invariant.rb
31
+ - lib/turf/meta.rb
30
32
  - lib/turf/version.rb
31
33
  - lib/turf_ruby.rb
32
34
  homepage: http://github.com/formigarafa/turf-ruby
@@ -44,7 +46,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
44
46
  requirements:
45
47
  - - ">="
46
48
  - !ruby/object:Gem::Version
47
- version: '0'
49
+ version: 2.5.0
48
50
  required_rubygems_version: !ruby/object:Gem::Requirement
49
51
  requirements:
50
52
  - - ">="