turf-ruby 0.6.0 → 0.7.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: 1449d9f733898e30a4773786b5bf8af4484e90387b2eabea1514673675808bf5
4
- data.tar.gz: a90f6e2fa677344a1ab82f855dd288dd1ebd7067e8a96bbc36c14c23088aa740
3
+ metadata.gz: 1c2222302d50f2e6f28d1369f76d100b8fc31751dbda47083894a2dac065c84d
4
+ data.tar.gz: 188b67a600244bc887794766e03b2107c819bc1ccfe559a8aab8c761920a2eb9
5
5
  SHA512:
6
- metadata.gz: 57668c76b16fba5796e74b217a2f5e9dcd9b00c5f3898e79ed55ac6b81f9788ec70a74f21639e91129a8930d277c791bae1e62028901079ab17467b2329089c2
7
- data.tar.gz: 8878e5cc910cf52781b349cc7ac2a3051abed0e46fc7a94bde316185b59930098d0fe2a02014a3cb6b1de0adaf82aade91b7767cd98d098cc57ffe6db91bb81d
6
+ metadata.gz: c9c194a59ebf4c99d9390ff90262d297bfebf0dd58ef9ee973435a7c14118e73e60494dec2d1013568bb07a4172fda3fe1fb77dbe50b47e382c0c62d07cdf93e
7
+ data.tar.gz: b3b425aa16b643ec0956f7a58b69a8562acf52791c674d12f044883c3445af8d088827abba0d576d5c4d5c251ace25709411e210d274b5c3e9a624c00f2947b6
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ #:nodoc:
4
+ module Turf
5
+ # @!group Measurement
6
+
7
+ # Takes a GeoJSON and measures its length in the specified units, (Multi)Point 's distance are ignored.
8
+ # @see http://turfjs.org/docs/#length
9
+ # @param geojson [Feature<LineString|MultiLinestring>] GeoJSON to measure
10
+ # @param units [string] can be degrees, radians, miles, or kilometers (optional, default "kilometers")
11
+ # @return [number] length of GeoJSON
12
+ def length(geojson, units: "kilometers")
13
+ geojson = deep_symbolize_keys(geojson)
14
+ geojson = feature(geojson) if geojson[:geometry].nil?
15
+ segment_reduce(geojson, initial_value: 0) do |previous_value, segment|
16
+ previous_value ||= 0
17
+ coords = segment.dig(:geometry, :coordinates)
18
+ previous_value + Turf.distance(coords[0], coords[1], units: units)
19
+ end
20
+ end
21
+ end
data/lib/turf/meta.rb CHANGED
@@ -298,4 +298,64 @@ module Turf
298
298
 
299
299
  previous_value
300
300
  end
301
+
302
+ # Iterate over 2-vertex line segment in any GeoJSON object, similar to Array.forEach()
303
+ # (Multi)Point geometries do not contain segments therefore they are ignored during this operation.
304
+ # @see https://turfjs.org/docs/#segmentEach
305
+ # @param geojson [FeatureCollection|Feature|Geometry] any GeoJSON object
306
+ def segment_each(geojson)
307
+ flatten_each(geojson) do |feature, feature_index|
308
+ # Exclude null Geometries
309
+ return if feature[:geometry].nil?
310
+
311
+ # (Multi)Point geometries do not contain segments therefore they are ignored during this operation.
312
+ type = feature[:geometry][:type]
313
+ return if %w[Point MultiPoint].include?(type)
314
+
315
+ segment_index = 0
316
+
317
+ # Generate 2-vertex line segments
318
+ previous_coords = nil
319
+ previous_feature_index = 0
320
+ coord_each(feature) do |current_coord|
321
+ # Simulating a meta.coord_reduce() since `reduce` operations cannot be stopped by returning `false`
322
+ if previous_coords.nil? || feature_index > previous_feature_index
323
+ previous_coords = current_coord
324
+ previous_feature_index = feature_index
325
+ segment_index = 0
326
+ next
327
+ end
328
+
329
+ segment = Turf.line_string([previous_coords, current_coord], properties: feature[:properties])
330
+ next unless yield(segment, feature_index)
331
+
332
+ segment_index += 1
333
+ previous_coords = current_coord
334
+ end
335
+ end
336
+ end
337
+
338
+ def segment_reduce(geojson, initial_value: nil)
339
+ previous_value = initial_value
340
+ started = false
341
+
342
+ segment_each(geojson) do |segment, feature_index, multifeature_index, geometry_index, segment_index|
343
+ previous_value =
344
+ if !started && initial_value.nil?
345
+ segment
346
+ else
347
+ yield(
348
+ previous_value,
349
+ segment,
350
+ feature_index,
351
+ multifeature_index,
352
+ geometry_index,
353
+ segment_index
354
+ )
355
+ end
356
+ started = true
357
+ end
358
+
359
+ previous_value
360
+ end
301
361
  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.6.0"
5
+ VERSION = "0.7.0"
6
6
  end
data/lib/turf_ruby.rb CHANGED
@@ -4,15 +4,16 @@
4
4
  #
5
5
  # https://github.com/Turfjs/turf/tree/master/packages
6
6
  #
7
- require "turf/helpers"
7
+ require "turf/along"
8
+ require "turf/area"
8
9
  require "turf/bearing"
10
+ require "turf/boolean_point_in_polygon"
11
+ require "turf/centroid"
9
12
  require "turf/destination"
10
- require "turf/along"
11
13
  require "turf/distance"
14
+ require "turf/helpers"
12
15
  require "turf/invariant"
13
- require "turf/boolean_point_in_polygon"
14
- require "turf/centroid"
16
+ require "turf/length"
15
17
  require "turf/meta"
16
- require "turf/area"
17
18
 
18
19
  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.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafael Santos
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-06 00:00:00.000000000 Z
11
+ date: 2023-04-29 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
@@ -29,6 +29,7 @@ files:
29
29
  - lib/turf/distance.rb
30
30
  - lib/turf/helpers.rb
31
31
  - lib/turf/invariant.rb
32
+ - lib/turf/length.rb
32
33
  - lib/turf/meta.rb
33
34
  - lib/turf/version.rb
34
35
  - lib/turf_ruby.rb
@@ -39,7 +40,7 @@ metadata:
39
40
  homepage_uri: http://github.com/formigarafa/turf-ruby
40
41
  source_code_uri: http://github.com/formigarafa/turf-ruby
41
42
  documentation_uri: https://formigarafa.github.io/turf-ruby/
42
- post_install_message:
43
+ post_install_message:
43
44
  rdoc_options: []
44
45
  require_paths:
45
46
  - lib
@@ -54,9 +55,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
55
  - !ruby/object:Gem::Version
55
56
  version: '0'
56
57
  requirements: []
57
- rubyforge_project:
58
- rubygems_version: 2.7.6.3
59
- signing_key:
58
+ rubygems_version: 3.1.6
59
+ signing_key:
60
60
  specification_version: 4
61
61
  summary: A modular geospatial engine. Ruby port of TurfJS.
62
62
  test_files: []