turf-ruby 0.8.1 → 1.1.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.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +21 -0
  3. data/README.md +163 -140
  4. data/lib/turf/along.rb +10 -3
  5. data/lib/turf/angle.rb +8 -0
  6. data/lib/turf/area.rb +4 -1
  7. data/lib/turf/bbox.rb +36 -0
  8. data/lib/turf/bbox_clip.rb +74 -0
  9. data/lib/turf/bbox_polygon.rb +41 -0
  10. data/lib/turf/bearing.rb +5 -2
  11. data/lib/turf/bezier_spline.rb +8 -0
  12. data/lib/turf/boolean_clockwise.rb +26 -0
  13. data/lib/turf/boolean_concave.rb +40 -0
  14. data/lib/turf/boolean_contains.rb +52 -0
  15. data/lib/turf/boolean_crosses.rb +8 -0
  16. data/lib/turf/boolean_disjoint.rb +8 -0
  17. data/lib/turf/boolean_equal.rb +8 -0
  18. data/lib/turf/boolean_intersects.rb +8 -0
  19. data/lib/turf/boolean_overlap.rb +8 -0
  20. data/lib/turf/boolean_parallel.rb +8 -0
  21. data/lib/turf/boolean_point_in_polygon.rb +5 -3
  22. data/lib/turf/boolean_point_on_line.rb +128 -0
  23. data/lib/turf/boolean_touches.rb +8 -0
  24. data/lib/turf/boolean_valid.rb +8 -0
  25. data/lib/turf/boolean_within.rb +8 -0
  26. data/lib/turf/buffer.rb +8 -0
  27. data/lib/turf/center.rb +32 -0
  28. data/lib/turf/center_mean.rb +8 -0
  29. data/lib/turf/center_median.rb +8 -0
  30. data/lib/turf/center_of_mass.rb +8 -0
  31. data/lib/turf/centroid.rb +5 -2
  32. data/lib/turf/circle.rb +5 -2
  33. data/lib/turf/clean_coords.rb +8 -0
  34. data/lib/turf/clone.rb +8 -0
  35. data/lib/turf/clusters.rb +32 -0
  36. data/lib/turf/clusters_dbscan.rb +8 -0
  37. data/lib/turf/clusters_kmeans.rb +8 -0
  38. data/lib/turf/collect.rb +8 -0
  39. data/lib/turf/combine.rb +8 -0
  40. data/lib/turf/concave.rb +8 -0
  41. data/lib/turf/convex.rb +8 -0
  42. data/lib/turf/destination.rb +6 -3
  43. data/lib/turf/difference.rb +8 -0
  44. data/lib/turf/directional_mean.rb +8 -0
  45. data/lib/turf/dissolve.rb +8 -0
  46. data/lib/turf/distance.rb +5 -2
  47. data/lib/turf/distance_weight.rb +12 -0
  48. data/lib/turf/ellipse.rb +8 -0
  49. data/lib/turf/envelope.rb +8 -0
  50. data/lib/turf/explode.rb +29 -0
  51. data/lib/turf/flatten.rb +8 -0
  52. data/lib/turf/flip.rb +8 -0
  53. data/lib/turf/geojson_rbush.rb +8 -0
  54. data/lib/turf/great_circle.rb +8 -0
  55. data/lib/turf/helpers.rb +166 -26
  56. data/lib/turf/hex_grid.rb +8 -0
  57. data/lib/turf/interpolate.rb +8 -0
  58. data/lib/turf/intersect.rb +8 -0
  59. data/lib/turf/invariant.rb +127 -1
  60. data/lib/turf/isobands.rb +8 -0
  61. data/lib/turf/isolines.rb +8 -0
  62. data/lib/turf/kinks.rb +8 -0
  63. data/lib/turf/length.rb +6 -3
  64. data/lib/turf/lib/lineclip.rb +118 -0
  65. data/lib/turf/line_arc.rb +8 -0
  66. data/lib/turf/line_chunk.rb +8 -0
  67. data/lib/turf/line_intersect.rb +8 -0
  68. data/lib/turf/line_offset.rb +8 -0
  69. data/lib/turf/line_overlap.rb +8 -0
  70. data/lib/turf/line_segment.rb +8 -0
  71. data/lib/turf/line_slice.rb +8 -0
  72. data/lib/turf/line_slice_along.rb +8 -0
  73. data/lib/turf/line_split.rb +8 -0
  74. data/lib/turf/line_to_polygon.rb +8 -0
  75. data/lib/turf/mask.rb +8 -0
  76. data/lib/turf/meta.rb +445 -79
  77. data/lib/turf/midpoint.rb +8 -0
  78. data/lib/turf/moran_index.rb +8 -0
  79. data/lib/turf/nearest_neighbor_analysis.rb +8 -0
  80. data/lib/turf/nearest_point.rb +8 -0
  81. data/lib/turf/nearest_point_on_line.rb +8 -0
  82. data/lib/turf/nearest_point_to_line.rb +8 -0
  83. data/lib/turf/planepoint.rb +8 -0
  84. data/lib/turf/point_grid.rb +8 -0
  85. data/lib/turf/point_on_feature.rb +8 -0
  86. data/lib/turf/point_to_line_distance.rb +8 -0
  87. data/lib/turf/point_to_polygon_distance.rb +8 -0
  88. data/lib/turf/points_within_polygon.rb +8 -0
  89. data/lib/turf/polygon_smooth.rb +8 -0
  90. data/lib/turf/polygon_tangents.rb +8 -0
  91. data/lib/turf/polygon_to_line.rb +20 -0
  92. data/lib/turf/polygonize.rb +8 -0
  93. data/lib/turf/projection.rb +12 -0
  94. data/lib/turf/quadrat_analysis.rb +8 -0
  95. data/lib/turf/random.rb +20 -0
  96. data/lib/turf/rectangle_grid.rb +8 -0
  97. data/lib/turf/rewind.rb +8 -0
  98. data/lib/turf/rhumb_bearing.rb +8 -0
  99. data/lib/turf/rhumb_destination.rb +8 -0
  100. data/lib/turf/rhumb_distance.rb +8 -0
  101. data/lib/turf/sample.rb +8 -0
  102. data/lib/turf/sector.rb +8 -0
  103. data/lib/turf/shortest_path.rb +8 -0
  104. data/lib/turf/simplify.rb +8 -0
  105. data/lib/turf/square.rb +42 -0
  106. data/lib/turf/square_grid.rb +8 -0
  107. data/lib/turf/standard_deviational_ellipse.rb +8 -0
  108. data/lib/turf/tag.rb +8 -0
  109. data/lib/turf/tesselate.rb +8 -0
  110. data/lib/turf/tin.rb +8 -0
  111. data/lib/turf/transform_rotate.rb +8 -0
  112. data/lib/turf/transform_scale.rb +8 -0
  113. data/lib/turf/transform_translate.rb +8 -0
  114. data/lib/turf/triangle_grid.rb +8 -0
  115. data/lib/turf/truncate.rb +3 -0
  116. data/lib/turf/union.rb +8 -0
  117. data/lib/turf/unkink_polygon.rb +8 -0
  118. data/lib/turf/version.rb +2 -1
  119. data/lib/turf/voronoi.rb +8 -0
  120. data/lib/turf.rb +2 -0
  121. data/lib/turf_ruby.rb +107 -3
  122. metadata +106 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a1759c68a13dd02fb326e6bf140b6a04cfc7c41cdb8094d444382438c74c639b
4
- data.tar.gz: aead3f8b19b118f5a5be8a5e9d45800e6888a85b0833ddd60289a7bef723cfdb
3
+ metadata.gz: aace9437eb9fd63a23fb1cae270444dffa0dbf58f371ce88abf1ff7dc483fb5b
4
+ data.tar.gz: 15ef3f0c1cc5dd1d9ebfaf298512f0a509bb524a432b002cccfe48a4b5d2d011
5
5
  SHA512:
6
- metadata.gz: 7729eb457352857ab86aa6a8e2f18c0801395ac7adb81623e07f2ea4164f378ee0eade368d0933eb6e66b5264667f3899f40eed8372e799f1084a96a8f65f46c
7
- data.tar.gz: f105cc22e100bc24a73e6150b530a42a0b1f33dcc0326598da3fe18baed5e5674b3dc19d8f6c4b5b57a4329f224eed4a65adffffd4bab57b2ecb071c658dbb73
6
+ metadata.gz: e56d69dd9c440438b5de0734b7c8068e745a111a27c17fa7c6fc5883eed7c42fcdc1765234605f533fb9becfc22d7009c95199111665a147738e38abe0c5c30c
7
+ data.tar.gz: c5cf79e1af361fb5010c4f997445ddc7d6378422e07252b01f105464863d2253a0d8de89ed889ec872f958a81b0cfccd6c983076d80b92aaf236f75b05c6e6c4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Rafael Santos
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,4 +1,7 @@
1
- # turf-ruby [![Build Status](https://travis-ci.com/formigarafa/turf-ruby.svg?branch=master)](https://travis-ci.com/formigarafa/turf-ruby)
1
+ # turf-ruby
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/turf-ruby.svg)](https://badge.fury.io/rb/turf-ruby)
4
+ [![Build Status](https://github.com/formigarafa/turf-ruby/actions/workflows/tests.yml/badge.svg)](https://github.com/formigarafa/turf-ruby)
2
5
 
3
6
  Ruby port of [Turf.js](https://turfjs.org/), an advance geospatial analysis library.
4
7
 
@@ -39,196 +42,216 @@ Currently not all functions are available, feel free to fork the repo and port m
39
42
 
40
43
  This list should be updated from [https://github.com/Turfjs/turf/blob/master/documentation.yml](https://github.com/Turfjs/turf/blob/master/documentation.yml).
41
44
 
42
- Measurement
43
-
45
+ ### Measurement
44
46
  - [x] along
45
47
  - [x] area
46
- - [ ] bbox
47
- - [ ] bboxPolygon
48
+ - [x] bbox
49
+ - [x] bbox_polygon
48
50
  - [x] bearing
49
- - [ ] center
50
- - [ ] centerOfMass
51
+ - [x] center
52
+ - [ ] center_of_mass
51
53
  - [x] centroid
52
54
  - [x] destination
53
55
  - [x] distance
54
56
  - [ ] envelope
57
+ - [ ] great_circle
55
58
  - [x] length
56
59
  - [ ] midpoint
57
- - [ ] pointOnFeature
58
- - [ ] polygonTangents
59
- - [ ] pointToLineDistance
60
- - [ ] rhumbBearing
61
- - [ ] rhumbDestination
62
- - [ ] rhumbDistance
63
- - [ ] square
64
- - [ ] greatCircle
65
-
66
- Coordinate Mutation
67
-
68
- - [ ] cleanCoords
60
+ - [ ] point_on_feature
61
+ - [ ] point_to_line_distance
62
+ - [ ] point_to_polygon_distance
63
+ - [ ] polygon_tangents
64
+ - [ ] rhumb_bearing
65
+ - [ ] rhumb_destination
66
+ - [ ] rhumb_distance
67
+ - [x] square
68
+
69
+ ### Coordinate Mutation
70
+ - [ ] clean_coords
69
71
  - [ ] flip
70
72
  - [ ] rewind
71
- - [ ] round
72
- - [ ] truncate
73
+ - [x] round
74
+ - [x] truncate
73
75
 
74
- Transformation
75
-
76
- - [ ] bboxClip
77
- - [ ] bezierSpline
76
+ ### Transformation
77
+ - [x] bbox_clip
78
+ - [ ] bezier_spline
78
79
  - [ ] buffer
79
- - [ ] circle
80
+ - [x] circle
80
81
  - [ ] clone
81
82
  - [ ] concave
82
83
  - [ ] convex
83
84
  - [ ] difference
84
85
  - [ ] dissolve
85
86
  - [ ] intersect
86
- - [ ] lineOffset
87
- - [ ] polygonSmooth
87
+ - [ ] line_offset
88
+ - [ ] polygon_smooth
88
89
  - [ ] simplify
89
90
  - [ ] tesselate
90
- - [ ] transformRotate
91
- - [ ] transformTranslate
92
- - [ ] transformScale
91
+ - [ ] transform_rotate
92
+ - [ ] transform_scale
93
+ - [ ] transform_translate
93
94
  - [ ] union
94
95
  - [ ] voronoi
95
96
 
96
- Feature Conversion
97
-
97
+ ### Feature Conversion
98
98
  - [ ] combine
99
- - [ ] explode
99
+ - [x] explode
100
100
  - [ ] flatten
101
- - [ ] lineToPolygon
101
+ - [ ] line_to_polygon
102
+ - [ ] polygon_to_line
102
103
  - [ ] polygonize
103
- - [ ] polygonToLine
104
-
105
- Misc
106
104
 
105
+ ### Misc
107
106
  - [ ] kinks
108
- - [ ] lineArc
109
- - [ ] lineChunk
110
- - [ ] lineIntersect
111
- - [ ] lineOverlap
112
- - [ ] lineSegment
113
- - [ ] lineSlice
114
- - [ ] lineSliceAlong
115
- - [ ] lineSplit
107
+ - [ ] line_arc
108
+ - [ ] line_chunk
109
+ - [ ] line_intersect
110
+ - [ ] line_overlap
111
+ - [ ] line_segment
112
+ - [ ] line_slice
113
+ - [ ] line_slice_along
114
+ - [ ] line_split
116
115
  - [ ] mask
117
- - [ ] nearestPointOnLine
116
+ - [ ] nearest_point_on_line
118
117
  - [ ] sector
119
- - [ ] shortestPath
120
- - [ ] unkinkPolygon
121
-
122
- Helper
118
+ - [ ] shortest_path
119
+ - [ ] unkink_polygon
123
120
 
124
- - [x] featureCollection
121
+ ### Helper
125
122
  - [x] feature
126
- - [x] geometryCollection
127
- - [x] lineString
128
- - [x] multiLineString
129
- - [x] multiPoint
130
- - [x] multiPolygon
123
+ - [x] feature_collection
124
+ - [x] geometry_collection
125
+ - [x] line_string
126
+ - [x] multi_line_string
127
+ - [x] multi_point
128
+ - [x] multi_polygon
131
129
  - [x] point
132
130
  - [x] polygon
133
131
 
134
- Random
135
-
136
- - [ ] randomPosition
137
- - [ ] randomPoint
138
- - [ ] randomLineString
139
- - [ ] randomPolygon
140
-
141
- Data
132
+ ### Random
133
+ - [ ] random_line_string
134
+ - [ ] random_point
135
+ - [ ] random_polygon
136
+ - [ ] random_position
142
137
 
138
+ ### Data
143
139
  - [ ] sample
144
140
 
145
- Interpolation
146
-
141
+ ### Interpolation
147
142
  - [ ] interpolate
148
143
  - [ ] isobands
149
144
  - [ ] isolines
150
145
  - [ ] planepoint
151
146
  - [ ] tin
152
147
 
153
- Joins
154
-
155
- - [ ] pointsWithinPolygon
148
+ ### Joins
149
+ - [ ] points_within_polygon
156
150
  - [ ] tag
157
151
 
158
- Grids
152
+ ### Grids
153
+ - [ ] hex_grid
154
+ - [ ] point_grid
155
+ - [ ] square_grid
156
+ - [ ] triangle_grid
159
157
 
160
- - [ ] hexGrid
161
- - [ ] pointGrid
162
- - [ ] squareGrid
163
- - [ ] triangleGrid
158
+ ### Classification
159
+ - [ ] nearest_point
164
160
 
165
- Classification
166
-
167
- - [ ] nearestPoint
161
+ ### Aggregation
162
+ - [ ] clusters_dbscan
163
+ - [ ] clusters_kmeans
164
+ - [ ] collect
168
165
 
169
- Aggregation
166
+ ### Meta
167
+ - [ ] cluster_each
168
+ - [ ] cluster_reduce
169
+ - [x] coord_all
170
+ - [x] coord_each
171
+ - [x] coord_reduce
172
+ - [x] feature_each
173
+ - [x] feature_reduce
174
+ - [x] flatten_each
175
+ - [x] flatten_reduce
176
+ - [x] geom_each
177
+ - [x] geom_reduce
178
+ - [ ] get_cluster
179
+ - [x] get_coord
180
+ - [x] get_coords
181
+ - [x] get_geom
182
+ - [x] get_type
183
+ - [x] prop_each
184
+ - [x] prop_reduce
185
+ - [x] segment_each
186
+ - [x] segment_reduce
187
+
188
+ ### Assertions
189
+ - [x] collection_of
190
+ - [x] contains_number
191
+ - [x] feature_of
192
+ - [x] geojson_type
193
+
194
+ ### Booleans
195
+ - [ ] boolean_clockwise
196
+ - [ ] boolean_concave
197
+ - [ ] boolean_contains
198
+ - [ ] boolean_crosses
199
+ - [ ] boolean_disjoint
200
+ - [ ] boolean_equal
201
+ - [ ] boolean_intersects
202
+ - [ ] boolean_overlap
203
+ - [ ] boolean_parallel
204
+ - [x] boolean_point_in_polygon
205
+ - [ ] boolean_point_on_line
206
+ - [ ] boolean_touches
207
+ - [ ] boolean_within
208
+
209
+ ### Unit Conversion
210
+ - [x] azimuth_to_bearing
211
+ - [x] bearing_to_azimuth
212
+ - [x] convert_area
213
+ - [x] convert_length
214
+ - [x] degrees_to_radians
215
+ - [x] length_to_degrees
216
+ - [x] length_to_radians
217
+ - [x] radians_to_degrees
218
+ - [x] radians_to_length
219
+ - [ ] to_mercator
220
+ - [ ] to_wgs84
221
+
222
+ ### Other
223
+ - [ ] angle
224
+ - [ ] boolean_valid
225
+ - [ ] center_mean
226
+ - [ ] center_median
227
+ - [ ] directional_mean
228
+ - [ ] distance_weight
229
+ - [ ] ellipse
230
+ - [x] find_point
231
+ - [x] find_segment
232
+ - [x] geometry
233
+ - [x] is_number
234
+ - [x] is_object
235
+ - [x] line_each
236
+ - [x] line_reduce
237
+ - [x] line_strings
238
+ - [ ] moran_index
239
+ - [ ] nearest_neighbor_analysis
240
+ - [ ] nearest_point_to_line
241
+ - [ ] p_norm_distance
242
+ - [x] points
243
+ - [x] polygons
244
+ - [ ] quadrat_analysis
245
+ - [x] rbush
246
+ - [ ] rectangle_grid
247
+ - [ ] standard_deviational_ellipse
248
+
249
+ ### Constants
250
+ - [ ] K_TABLE
251
+ - [x] AREA_FACTORS
252
+ - [x] EARTH_RADIUS
253
+ - [x] FACTORS
170
254
 
171
- - [ ] collect
172
- - [ ] clustersDbscan
173
- - [ ] clustersKmeans
174
-
175
- Meta
176
-
177
- - [ ] coordAll
178
- - [x] coordEach
179
- - [x] coordReduce
180
- - [x] featureEach
181
- - [x] featureReduce
182
- - [x] flattenEach
183
- - [x] flattenReduce
184
- - [x] getCoord
185
- - [ ] getCoords
186
- - [x] getGeom
187
- - [ ] getType
188
- - [x] geomEach
189
- - [x] geomReduce
190
- - [ ] propEach
191
- - [ ] propReduce
192
- - [ ] segmentEach
193
- - [ ] segmentReduce
194
- - [ ] getCluster
195
- - [ ] clusterEach
196
- - [ ] clusterReduce
197
-
198
- Assertions
199
-
200
- - [ ] collectionOf
201
- - [ ] containsNumber
202
- - [ ] geojsonType
203
- - [ ] featureOf
204
-
205
- Booleans
206
-
207
- - [ ] booleanClockwise
208
- - [ ] booleanConcave
209
- - [ ] booleanContains
210
- - [ ] booleanCrosses
211
- - [ ] booleanDisjoint
212
- - [ ] booleanEqual
213
- - [ ] booleanIntersects
214
- - [ ] booleanOverlap
215
- - [ ] booleanParallel
216
- - [x] booleanPointInPolygon
217
- - [ ] booleanPointOnLine
218
- - [ ] booleanWithin
219
-
220
- Unit Conversion
221
-
222
- - [ ] bearingToAzimuth
223
- - [ ] convertArea
224
- - [ ] convertLength
225
- - [ ] degreesToRadians
226
- - [x] lengthToRadians
227
- - [x] lengthToDegrees
228
- - [x] radiansToLength
229
- - [x] radiansToDegrees
230
- - [ ] toMercator
231
- - [ ] toWgs84
232
255
 
233
256
  ## Development
234
257
 
data/lib/turf/along.rb CHANGED
@@ -1,5 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "bearing"
4
+ require_relative "destination"
5
+ require_relative "distance"
6
+ require_relative "helpers"
7
+ require_relative "invariant"
8
+
3
9
  # :nodoc:
4
10
  module Turf
5
11
  # @!group Measurement
@@ -10,7 +16,8 @@ module Turf
10
16
  # @param distance [number] distance along the line
11
17
  # @param units [string] can be degrees, radians, miles, or kilometers (optional, default "kilometers")
12
18
  # @return [Feature<Point>] Point distance units along the line
13
- def along(line, distance, units: "kilometers")
19
+ def along(line, distance, options = {})
20
+ units = options[:units] || "kilometers"
14
21
  line = deep_symbolize_keys line
15
22
  travelled = 0
16
23
 
@@ -25,10 +32,10 @@ module Turf
25
32
  return point(coord) if overshot.zero?
26
33
 
27
34
  direction = bearing(coord, coords[i - 1]) - 180
28
- interpolated = destination(coord, overshot, direction, units: units)
35
+ interpolated = destination(coord, overshot, direction, { units: units })
29
36
  return interpolated
30
37
  else
31
- travelled += distance(coords[i], coords[i + 1], units: units)
38
+ travelled += distance(coords[i], coords[i + 1], { units: units })
32
39
  end
33
40
  end
34
41
 
data/lib/turf/angle.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc:
4
+ module Turf
5
+ def angle(*args)
6
+ raise NotImplementedError
7
+ end
8
+ end
data/lib/turf/area.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "helpers"
4
+ require_relative "meta"
5
+
3
6
  # :nodoc:
4
7
  module Turf
5
8
  # Takes one or more features and returns their area in square meters.
@@ -7,7 +10,7 @@ module Turf
7
10
  # @param geojson [GeoJSON] input GeoJSON feature(s)
8
11
  # @return [number] aria in square meters
9
12
  def area(geojson)
10
- geom_reduce(geojson, initial_value: 0) do |value, geom|
13
+ geom_reduce(geojson, 0) do |value, geom|
11
14
  value + area_calculate_area(geom)
12
15
  end
13
16
  end
data/lib/turf/bbox.rb ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc:
4
+ module Turf
5
+ # Calculates the bounding box for any GeoJSON object, including FeatureCollection.
6
+ # Uses geojson[:bbox] if available and options[:recompute] is not set.
7
+ #
8
+ # @param [Hash] geojson any GeoJSON object
9
+ # @param [Hash] options optional parameters
10
+ # @option options [Boolean] :recompute whether to ignore an existing bbox property on geojson
11
+ # @return [Array<Float>] bbox extent in [minX, minY, maxX, maxY] order
12
+ # @example
13
+ # line = {
14
+ # type: "LineString",
15
+ # coordinates: [[-74, 40], [-78, 42], [-82, 35]]
16
+ # }
17
+ # bbox = bbox(line)
18
+ # puts bbox.inspect # => [-82, 35, -74, 42]
19
+ def bbox(geojson, options = {})
20
+ # If geojson has a bbox property and options[:recompute] is not true, return the existing bbox
21
+ return geojson[:bbox] if geojson[:bbox] && options[:recompute] != true
22
+
23
+ # Initialize the result array with infinity values
24
+ result = [Float::INFINITY, Float::INFINITY, -Float::INFINITY, -Float::INFINITY]
25
+
26
+ # Iterate through each coordinate in the GeoJSON object using coord_each
27
+ coord_each(geojson) do |coord|
28
+ result[0] = coord[0] if result[0] > coord[0] # minX
29
+ result[1] = coord[1] if result[1] > coord[1] # minY
30
+ result[2] = coord[0] if result[2] < coord[0] # maxX
31
+ result[3] = coord[1] if result[3] < coord[1] # maxY
32
+ end
33
+
34
+ result
35
+ end
36
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc:
4
+ module Turf
5
+ # Takes a Feature and a bbox and clips the feature to the bbox using
6
+ # [lineclip](https://github.com/mapbox/lineclip).
7
+ # May result in degenerate edges when clipping Polygons.
8
+ #
9
+ # @param [Feature<LineString, MultiLineString, Polygon, MultiPolygon>] feature Feature to clip to the bbox
10
+ # @param [BBox] bbox Extent in [minX, minY, maxX, maxY] order
11
+ # @return [Feature<LineString, MultiLineString, Polygon, MultiPolygon>] Clipped Feature
12
+ # @example
13
+ # bbox = [0, 0, 10, 10]
14
+ # poly = polygon([[[2, 2], [8, 4], [12, 8], [3, 7], [2, 2]]])
15
+ #
16
+ # clipped = bbox_clip(poly, bbox)
17
+ #
18
+ # # add_to_map
19
+ # add_to_map = [bbox, poly, clipped]
20
+ def bbox_clip(feature, bbox)
21
+ geom = get_geom(feature)
22
+ type = geom[:type]
23
+ properties = feature[:type] == "Feature" ? feature[:properties] : {}
24
+ coords = geom[:coordinates]
25
+
26
+ case type
27
+ when "LineString", "MultiLineString"
28
+ lines = []
29
+ coords = [coords] if type == "LineString"
30
+
31
+ coords.each do |line|
32
+ Lineclip.lineclip(line, bbox, lines)
33
+ end
34
+
35
+ if lines.length == 1
36
+ return line_string(lines[0], properties)
37
+ end
38
+
39
+ multi_line_string(lines, properties)
40
+ when "Polygon"
41
+ polygon(clip_polygon(coords, bbox), properties)
42
+ when "MultiPolygon"
43
+ multi_polygon(
44
+ coords.map { |poly| clip_polygon(poly, bbox) },
45
+ properties,
46
+ )
47
+ else
48
+ raise Error, "geometry #{type} not supported"
49
+ end
50
+ end
51
+
52
+ # Clips the rings of a Polygon or MultiPolygon to the bbox.
53
+ #
54
+ # @param [Array<Array<Array<Number>>>] rings The coordinates of the polygon rings
55
+ # @param [BBox] bbox Extent in [minX, minY, maxX, maxY] order
56
+ # @return [Array<Array<Array<Number>>>] Clipped polygon rings
57
+ def clip_polygon(rings, bbox)
58
+ out_rings = []
59
+
60
+ rings.each do |ring|
61
+ clipped = Lineclip.polygonclip(ring, bbox)
62
+
63
+ next unless clipped.any?
64
+
65
+ if clipped[0][0] != clipped[-1][0] || clipped[0][1] != clipped[-1][1]
66
+ clipped.push(clipped[0])
67
+ end
68
+
69
+ out_rings.push(clipped) if clipped.length >= 4
70
+ end
71
+
72
+ out_rings
73
+ end
74
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc:
4
+ module Turf
5
+ # Takes a bbox and returns an equivalent polygon.
6
+ #
7
+ # @param bbox [Array<Numeric>] extent in [minX, minY, maxX, maxY] order
8
+ # @param options [Hash] Optional parameters
9
+ # @option options [Hash] :properties Translate properties to Polygon
10
+ # @option options [String, Numeric] :id Translate Id to Polygon
11
+ # @return [Feature<Polygon>] a Polygon representation of the bounding box
12
+ # @example
13
+ # bbox = [0, 0, 10, 10]
14
+ # poly = bbox_polygon(bbox)
15
+ # # addToMap
16
+ # add_to_map = [poly]
17
+ def bbox_polygon(bbox, options = {})
18
+ # Convert BBox positions to Numbers
19
+ # No performance loss for including to_f
20
+ # https://github.com/Turfjs/turf/issues/1119
21
+ west = Float(bbox[0], exception: false) || Float::NAN
22
+ south = Float(bbox[1], exception: false) || Float::NAN
23
+ east = Float(bbox[2], exception: false) || Float::NAN
24
+ north = Float(bbox[3], exception: false) || Float::NAN
25
+
26
+ if bbox.length == 6
27
+ raise Error, "@turf/bbox-polygon does not support BBox with 6 positions"
28
+ end
29
+
30
+ low_left = [west, south]
31
+ top_left = [west, north]
32
+ top_right = [east, north]
33
+ low_right = [east, south]
34
+
35
+ polygon(
36
+ [[low_left, low_right, top_right, top_left, low_left]],
37
+ options[:properties] || {},
38
+ { bbox: bbox, id: options[:id] },
39
+ )
40
+ end
41
+ end
data/lib/turf/bearing.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "helpers"
4
+ require_relative "invariant"
5
+
3
6
  # :nodoc:
4
7
  module Turf
5
8
  # @!group Measurement
@@ -11,8 +14,8 @@ module Turf
11
14
  # @param to [Coord] ending Point
12
15
  # @param final boolean calculates the final bearing if true
13
16
  # @return [number] bearing in decimal degrees, between -180 and 180 degrees (positive clockwise)
14
- def bearing(from, to, final: false)
15
- return calculate_final_bearing(from, to) if final
17
+ def bearing(from, to, options = {})
18
+ return calculate_final_bearing(from, to) if options[:final]
16
19
 
17
20
  coordinates1 = get_coord from
18
21
  coordinates2 = get_coord to
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc:
4
+ module Turf
5
+ def bezier_spline(*args)
6
+ raise NotImplementedError
7
+ end
8
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Takes a ring and returns true or false whether or not the ring is clockwise or counter-clockwise.
4
+ #
5
+ # @param [Array<Array<Number>>] line to be evaluated
6
+ # @return [Boolean] true/false
7
+ # @example
8
+ # clockwise_ring = [[0, 0], [1, 1], [1, 0], [0, 0]]
9
+ # counter_clockwise_ring = [[0, 0], [1, 0], [1, 1], [0, 0]]
10
+ #
11
+ # boolean_clockwise(clockwise_ring)
12
+ # # => true
13
+ # boolean_clockwise(counter_clockwise_ring)
14
+ # # => false
15
+ module Turf
16
+ def boolean_clockwise(line)
17
+ ring = get_coords(line)
18
+ sum = 0
19
+
20
+ ring.each_cons(2) do |prev, cur|
21
+ sum += (cur[0] - prev[0]) * (cur[1] + prev[1])
22
+ end
23
+
24
+ sum > 0
25
+ end
26
+ end