turf-ruby 0.2.0 → 0.3.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: f8ed0e8a70920277e4e8507100d1f14084e75d0904c03feea99dc8196530561a
4
- data.tar.gz: baba690e3110b85c5ece7eed4f5bd3229b5ecfbbeeca7ad4a7ca8e05d182763f
3
+ metadata.gz: b858fdec044cd9bcb71a29727849dee8ff75f74cf4bf2f97163fafb08be006ab
4
+ data.tar.gz: 718bd40cab7ece92b2f95d080d7596bfbac2de785b3ba129078c01a2085cc252
5
5
  SHA512:
6
- metadata.gz: 9e331328f338fff791262493ae0f63da0baea68b5ec457c911ce62711803fc5f91b989bb5a2abc5191ff354f25306c664cb1b00a75c857484da518da4367a475
7
- data.tar.gz: 7d1814173daf223ec7d8f9bbd4ae080c18c85bcfe4c0d307ada85abde67772aaa5d58bf87024fb5757825b5faa7544ab908ff12bc67c3cc5302ecf5e5281a0d6
6
+ metadata.gz: c115975844db3097b16c9ba05b601104a9f8de6ff4216797dab70bfd539b282493d9988835074ed2ba36f43ae806f8d101278e021d4615dcb7153c8cadec95fc
7
+ data.tar.gz: f155f788ea240f39d93603f6cd33e6712ccd7acfb71ca63d6052ad3f1df9d14d2b52d32ec8395f24ee6f922920dca57961d874d9fc41ebc566d4d3fc340c43fe
data/.gitignore CHANGED
@@ -6,3 +6,4 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ .ruby-version
data/Gemfile CHANGED
@@ -6,4 +6,5 @@ source "https://rubygems.org"
6
6
  gemspec
7
7
 
8
8
  gem "minitest", "~> 5.0"
9
+ gem "minitest-focus"
9
10
  gem "rake", "~> 12.0"
@@ -1,12 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- turf-ruby (0.2.0)
4
+ turf-ruby (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  minitest (5.14.0)
10
+ minitest-focus (1.2.1)
11
+ minitest (>= 4, < 6)
10
12
  rake (12.3.3)
11
13
 
12
14
  PLATFORMS
@@ -14,6 +16,7 @@ PLATFORMS
14
16
 
15
17
  DEPENDENCIES
16
18
  minitest (~> 5.0)
19
+ minitest-focus
17
20
  rake (~> 12.0)
18
21
  turf-ruby!
19
22
 
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Turf
4
+ def self.along(line, distance, **options)
5
+ travelled = 0
6
+
7
+ geom = get_geom line
8
+ coords = geom[:coordinates]
9
+
10
+ coords.each_with_index do |coord, i|
11
+ break if distance >= travelled && i == coords.length - 1
12
+
13
+ if travelled >= distance
14
+ overshot = distance - travelled
15
+ return point(coord) if overshot.zero?
16
+
17
+ direction = bearing(coord, coords[i - 1]) - 180
18
+ interpolated = destination(coord, overshot, direction, options)
19
+ return interpolated
20
+ else
21
+ travelled += distance(coords[i], coords[i + 1], options)
22
+ end
23
+ end
24
+
25
+ point(coords.last)
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Turf
4
+ def self.bearing(from, to, **options)
5
+ return calculate_final_bearing(from, to) if options[:final] == true
6
+
7
+ coordinates1 = get_coord from
8
+ coordinates2 = get_coord to
9
+
10
+ lon1 = degrees_to_radians(coordinates1[0])
11
+ lon2 = degrees_to_radians(coordinates2[0])
12
+ lat1 = degrees_to_radians(coordinates1[1])
13
+ lat2 = degrees_to_radians(coordinates2[1])
14
+ a = Math.sin(lon2 - lon1) * Math.cos(lat2)
15
+ b = Math.cos(lat1) * Math.sin(lat2) -
16
+ Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)
17
+
18
+ radians_to_degrees(Math.atan2(a, b))
19
+ end
20
+
21
+ def self.calculate_final_bearing(from, to)
22
+ bear = bearing(to, from)
23
+ (bear + 180) % 360
24
+ end
25
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ # https://github.com/Turfjs/turf/blob/master/packages/turf-boolean-point-in-polygon/index.ts
4
+ def in_bbox(point, bbox)
5
+ bbox[0] <= point[0] &&
6
+ bbox[1] <= point[1] &&
7
+ bbox[2] >= point[0] &&
8
+ bbox[3] >= point[1]
9
+ end
10
+
11
+ def in_ring(point, ring, ignore_boundary)
12
+ is_inside = false
13
+ is_ring_valid = ring[0][0] == ring[ring.size - 1][0]
14
+ is_ring_valid &&= ring[0][1] == ring[ring.size - 1][1]
15
+ if is_ring_valid
16
+ ring = ring.slice(0, ring.size - 1)
17
+ end
18
+ ring.each_with_index do |ring_pt, ring_pt_index|
19
+ ring_pt2 = ring[(ring_pt_index + 1) % ring.size]
20
+
21
+ xi = ring_pt[0]
22
+ yi = ring_pt[1]
23
+ xj = ring_pt2[0]
24
+ yj = ring_pt2[1]
25
+
26
+ on_boundary = (
27
+ point[1] * (xi - xj) + yi * (xj - point[0]) + yj * (point[0] - xi)
28
+ ).zero?
29
+ on_boundary &&= ((xi - point[0]) * (xj - point[0]) <= 0)
30
+ on_boundary &&= ((yi - point[1]) * (yj - point[1]) <= 0)
31
+ if on_boundary
32
+ return !ignore_boundary
33
+ end
34
+
35
+ intersect = ((yi > point[1]) != (yj > point[1])) &&
36
+ (point[0] < (xj - xi) * (point[1] - yi) / (yj - yi) + xi)
37
+ if intersect
38
+ is_inside = !is_inside
39
+ end
40
+ end
41
+ is_inside
42
+ end
43
+
44
+ module Turf
45
+ def self.boolean_point_in_polygon(point, polygon, ignore_boundary: false)
46
+ polygon = deep_symbolize_keys(polygon)
47
+ pt = get_coord(point)
48
+ geom = get_geom(polygon)
49
+ type = geom.fetch(:type)
50
+ bbox = polygon[:bbox]
51
+ polys = geom.fetch(:coordinates)
52
+
53
+ # Quick elimination if point is not inside bbox
54
+ return false if bbox && !in_bbox(pt, bbox)
55
+
56
+ # normalize to multipolygon
57
+ polys = [polys] if type == "Polygon"
58
+
59
+ inside_poly = false
60
+ polys.each do |poly|
61
+ # check if it is in the outer ring first
62
+ next unless in_ring(pt, poly[0], ignore_boundary)
63
+
64
+ in_hole = false
65
+
66
+ # check for the point in any of the holes
67
+ poly.slice(1, poly.size - 1).each do |hole|
68
+ if in_ring(pt, hole, !ignore_boundary)
69
+ in_hole = true
70
+ end
71
+ end
72
+ if !in_hole
73
+ inside_poly = true
74
+ end
75
+ end
76
+ inside_poly
77
+ end
78
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Turf
4
+ def self.destination(origin, distance, bearing, **options)
5
+ coordinates1 = get_coord origin
6
+ longitude1 = degrees_to_radians coordinates1[0]
7
+ latitude1 = degrees_to_radians coordinates1[1]
8
+ bearing_radians = degrees_to_radians bearing
9
+ radians = length_to_radians distance, options[:units]
10
+
11
+ latitude2 = Math.asin(Math.sin(latitude1) * Math.cos(radians) +
12
+ Math.cos(latitude1) * Math.sin(radians) * Math.cos(bearing_radians))
13
+ longitude2 = longitude1 + Math.atan2(
14
+ Math.sin(bearing_radians) * Math.sin(radians) * Math.cos(latitude1),
15
+ Math.cos(radians) - Math.sin(latitude1) * Math.sin(latitude2),
16
+ )
17
+ lng = radians_to_degrees(longitude2)
18
+ lat = radians_to_degrees(latitude2)
19
+
20
+ point([lng, lat], options[:properties])
21
+ end
22
+ end
@@ -33,6 +33,15 @@ module Turf
33
33
  feat
34
34
  end
35
35
 
36
+ def self.feature_collection(features, options = {})
37
+ fc = { type: "FeatureCollection" }
38
+ fc[:id] = options[:id] if options[:id]
39
+ fc[:bbox] = options[:bbox] if options[:bbox]
40
+ fc[:features] = features
41
+
42
+ fc
43
+ end
44
+
36
45
  def self.line_string(coordinates, properties = nil, options = {})
37
46
  if coordinates.size < 2
38
47
  raise Error, "coordinates must be an array of two or more positions"
@@ -75,6 +84,11 @@ module Turf
75
84
  feature(geom, properties, options)
76
85
  end
77
86
 
87
+ def self.radians_to_degrees(radians)
88
+ degrees = radians.remainder(2 * Math::PI)
89
+ degrees * 180 / Math::PI
90
+ end
91
+
78
92
  def self.degrees_to_radians(degrees)
79
93
  radians = degrees.remainder(360)
80
94
  radians * Math::PI / 180
@@ -26,4 +26,10 @@ module Turf
26
26
 
27
27
  raise Error, "coord must be GeoJSON Point or an Array of numbers"
28
28
  end
29
+
30
+ def self.get_geom(geojson)
31
+ return geojson[:geometry] if geojson[:type] == "Feature"
32
+
33
+ geojson
34
+ end
29
35
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Turf
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -1,6 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "turf"
4
+ require "turf/along"
5
+ require "turf/bearing"
6
+ require "turf/destination"
4
7
  require "turf/helper"
5
8
  require "turf/measurement"
6
9
  require "turf/invariant"
10
+ require "turf/boolean_point_in_polygon"
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.2.0
4
+ version: 0.3.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: 2020-03-10 00:00:00.000000000 Z
11
+ date: 2020-10-05 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
@@ -32,6 +32,10 @@ files:
32
32
  - bin/console
33
33
  - bin/setup
34
34
  - lib/turf.rb
35
+ - lib/turf/along.rb
36
+ - lib/turf/bearing.rb
37
+ - lib/turf/boolean_point_in_polygon.rb
38
+ - lib/turf/destination.rb
35
39
  - lib/turf/helper.rb
36
40
  - lib/turf/invariant.rb
37
41
  - lib/turf/measurement.rb