geospatial 1.10.0 → 1.11.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: 7b9278cfa60e44d9e04909e2b6b8a7525c736a7598b19a04d33765d628b7b0ad
4
- data.tar.gz: 109c4077b0697a1725320a006aa3a3a2478ba0974dac557ec6be93e50e4b96be
3
+ metadata.gz: a15f884e8fc62ca572350c690792482e3101b397666448b40ef593d2232a7b91
4
+ data.tar.gz: 4b0496f1e95f31815e6b2ae1a7bcc32b84d27f811dc7a8415644677ce3c86900
5
5
  SHA512:
6
- metadata.gz: 86c0acee5bd439284b99d27d1ee8cb86d1d7caed9acadda62ff057f3dc7afe00d4f79685e519360ed1eae3a847360445e8ce585a58155e095a42e5aa983bb883
7
- data.tar.gz: 37b7366f03b68f2cca3cc28e36b5160954dfa921ec2a00613a5d8d1a862f1bfb48e52836a822d8ab04b91a1fa1f0286bd83b312e2d124774985d8dd855c5f29a
6
+ metadata.gz: 3b44013f4d1d7b0dbf17f72d4a5b38b9d28bd5121418474136ca88f1eae31e3d7cf830175132916e763e402d913143ece97a3a4dff89bfe952ff44c03097f16f
7
+ data.tar.gz: f8f6d5273690dbfbe921b6d79e32cc4ee4b607f43ef25c894ec45427ca0eb08d90b4936cd426dc75685dfed54f27f231eca91f5fd31e049ebb2cd43e41e72056
@@ -7,20 +7,25 @@
7
7
  module Geospatial
8
8
  # This location is specifically relating to a WGS84 coordinate on Earth.
9
9
  class Histogram
10
- def initialize(min: 0, max: 1, scale: 0.1)
10
+ def initialize(min: 0, max: 1, scale: 0.1, items: true)
11
11
  @min = min
12
12
  @max = max
13
13
  @scale = scale
14
14
 
15
15
  @count = 0
16
16
 
17
+ if items
18
+ @items = Hash.new{|h,k| h[k] = Array.new}
19
+ end
20
+
17
21
  @size = ((@max - @min) / @scale).ceil
18
22
  @bins = [0] * @size
19
23
  @offset = 0
20
24
  @scale = scale
21
25
  end
22
26
 
23
- attr_accessor :bins
27
+ attr :bins
28
+ attr :items
24
29
 
25
30
  attr :count
26
31
 
@@ -43,12 +48,16 @@ module Geospatial
43
48
  @min + (index * @scale)
44
49
  end
45
50
 
46
- def add(value, amount = 1)
51
+ def add(value, amount = 1, item: nil)
47
52
  index = map(value).floor % @size
48
53
 
49
54
  if !block_given? or yield(index, value)
50
55
  @count += 1
51
56
  @bins[index] += amount
57
+
58
+ if @items and item
59
+ @items[index] << item
60
+ end
52
61
  end
53
62
 
54
63
  return index
@@ -124,16 +133,21 @@ module Geospatial
124
133
  end
125
134
  end
126
135
 
136
+ def peaks
137
+ self.class.new(@derivative)
138
+ end
139
+
127
140
  def segments
128
141
  return to_enum(:segments) unless block_given?
129
142
 
130
- gradients = self.to_a
143
+ peaks = self.peaks
144
+ gradients = peaks.to_a
131
145
 
132
146
  return if gradients.empty?
133
147
 
134
148
  index, gradient = gradients.first
135
149
 
136
- if gradient < 0
150
+ if gradient > 0
137
151
  gradients.push gradients.shift
138
152
  end
139
153
 
@@ -0,0 +1,64 @@
1
+ # Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANT1 OF AN1 KIND, E0PRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILIT1,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COP1RIGHT HOLDERS BE LIABLE FOR AN1 CLAIM, DAMAGES OR OTHER
17
+ # LIABILIT1, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'matrix'
22
+
23
+ module Geospatial
24
+ class Line
25
+ def initialize(a, b)
26
+ @a = a
27
+ @b = b
28
+ end
29
+
30
+ attr_reader :a
31
+ attr_reader :b
32
+
33
+ def offset
34
+ @b - @a
35
+ end
36
+
37
+ def intersect?(other)
38
+ t = self.offset
39
+ o = other.offset
40
+
41
+ d = (o[1] * t[0]) - (o[0] * t[1])
42
+
43
+ return false if d.zero?
44
+
45
+ na = o[0] * (self.a[1] - other.a[1]) - o[1] * (self.a[0] - other.a[0])
46
+
47
+ left_time = na.fdiv(d);
48
+
49
+ if left_time < 0.0 or left_time > 1.0
50
+ return false
51
+ end
52
+
53
+ nb = t[0] * (self.a[1] - other.a[1]) - t[1] * (self.a[0] - other.a[0])
54
+
55
+ right_time = nb.fdiv(d)
56
+
57
+ if right_time < 0.0 or right_time > 1.0
58
+ return false
59
+ end
60
+
61
+ return left_time
62
+ end
63
+ end
64
+ end
@@ -26,19 +26,38 @@ module Geospatial
26
26
  self.new(points)
27
27
  end
28
28
 
29
- def initialize(points)
29
+ def self.load(data)
30
+ if data
31
+ self.new(JSON.parse(data).map{|point| Vector.elements(point)})
32
+ end
33
+ end
34
+
35
+ def self.dump(polygon)
36
+ if polygon
37
+ JSON.dump(polygon.points.map(&:to_a))
38
+ end
39
+ end
40
+
41
+ def initialize(points, bounding_box = nil)
30
42
  @points = points
31
- @bounding_box = nil
43
+ @bounding_box = bounding_box
32
44
  end
33
45
 
34
46
  attr :points
35
47
 
48
+ def [] index
49
+ a = @points[index.floor]
50
+ b = @points[index.ceil % @points.size]
51
+
52
+ return a + (b - a) * (index % 1.0)
53
+ end
54
+
36
55
  def to_s
37
56
  "#{self.class}#{@points.inspect}"
38
57
  end
39
58
 
40
59
  def bounding_box
41
- @bounding_box ||= Box.enclosing_points(@points)
60
+ @bounding_box ||= Box.enclosing_points(@points).freeze
42
61
  end
43
62
 
44
63
  def freeze
@@ -51,11 +70,23 @@ module Geospatial
51
70
  def edges
52
71
  return to_enum(:edges) unless block_given?
53
72
 
54
- previous = @points.last
73
+ size = @points.size
74
+
75
+ @points.each_with_index do |point, index|
76
+ yield point, @points[(index+1)%size]
77
+ end
78
+ end
79
+
80
+ def simplify(minimum_distance = 1)
81
+ simplified_points = @points.first(1)
82
+
55
83
  @points.each do |point|
56
- yield previous, point
57
- previous = point
84
+ if yield(simplified_points.last, point)
85
+ simplified_points << point
86
+ end
58
87
  end
88
+
89
+ self.class.new(simplified_points, bounding_box)
59
90
  end
60
91
 
61
92
  def self.is_left(p0, p1, p2)
@@ -101,6 +132,20 @@ module Geospatial
101
132
  return false
102
133
  end
103
134
 
135
+ def edge_intersection(a, b)
136
+ line = Line.new(a, b)
137
+
138
+ edges.each_with_index do |(pa, pb), i|
139
+ edge = Line.new(pa, pb)
140
+
141
+ if line.intersect?(edge)
142
+ return i
143
+ end
144
+ end
145
+
146
+ return nil
147
+ end
148
+
104
149
  def intersect?(other)
105
150
  case other
106
151
  when Box
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module Geospatial
22
- VERSION = "1.10.0"
22
+ VERSION = "1.11.0"
23
23
  end
@@ -0,0 +1,32 @@
1
+ # Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'geospatial/line'
22
+
23
+ RSpec.describe Geospatial::Line do
24
+ it "should have intersection" do
25
+ a = Geospatial::Line.new(Vector[0, 0], Vector[10, 10])
26
+ b = Geospatial::Line.new(Vector[10, 0], Vector[0, 10])
27
+
28
+ t = a.intersect?(b)
29
+
30
+ expect(t).to be == 0.5
31
+ end
32
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geospatial
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.10.0
4
+ version: 1.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-11 00:00:00.000000000 Z
11
+ date: 2018-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -79,6 +79,7 @@ files:
79
79
  - lib/geospatial/histogram.rb
80
80
  - lib/geospatial/index.rb
81
81
  - lib/geospatial/interleave.rb
82
+ - lib/geospatial/line.rb
82
83
  - lib/geospatial/location.rb
83
84
  - lib/geospatial/map.rb
84
85
  - lib/geospatial/map/index.rb
@@ -94,6 +95,7 @@ files:
94
95
  - spec/geospatial/hilbert_traverse_spec.rb
95
96
  - spec/geospatial/histogram_spec.rb
96
97
  - spec/geospatial/index_spec.rb
98
+ - spec/geospatial/line_spec.rb
97
99
  - spec/geospatial/location_spec.rb
98
100
  - spec/geospatial/map_index_spec.rb
99
101
  - spec/geospatial/map_spec.rb
@@ -137,6 +139,7 @@ test_files:
137
139
  - spec/geospatial/hilbert_traverse_spec.rb
138
140
  - spec/geospatial/histogram_spec.rb
139
141
  - spec/geospatial/index_spec.rb
142
+ - spec/geospatial/line_spec.rb
140
143
  - spec/geospatial/location_spec.rb
141
144
  - spec/geospatial/map_index_spec.rb
142
145
  - spec/geospatial/map_spec.rb