Class: ActiveRecordPolyline::Compaction::VectorCompressor

Inherits:
SimpleCompressor show all
Defined in:
lib/active_record_polyline/compaction/vector_compressor.rb

Instance Method Summary collapse

Methods inherited from SimpleCompressor

#distant?

Methods inherited from Base

#apply, compress_after_push, compress_before_push, #push

Constructor Details

#initialize(distance: 100) ⇒ VectorCompressor

Returns a new instance of VectorCompressor.



10
11
12
# File 'lib/active_record_polyline/compaction/vector_compressor.rb', line 10

def initialize(distance: 100)
  super
end

Instance Method Details

#addable?(location) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
# File 'lib/active_record_polyline/compaction/vector_compressor.rb', line 14

def addable?(location)
  return true if @polyline.points.empty?
  return distant?(@polyline.points.last, location) if @polyline.points.size == 1

  true
end

#compress_after_pushObject



21
22
23
24
25
# File 'lib/active_record_polyline/compaction/vector_compressor.rb', line 21

def compress_after_push
  return if @polyline.points.size < 3

  @polyline.points.delete_at(-2) if nearly_same_direction?(second_from_last_vec, last_vec)
end

#last_vecObject



27
28
29
# File 'lib/active_record_polyline/compaction/vector_compressor.rb', line 27

def last_vec
  two_point_to_vec(@polyline.points.last(2).first, @polyline.points.last)
end

#nearly_same_direction?(vec1, vec2) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/active_record_polyline/compaction/vector_compressor.rb', line 40

def nearly_same_direction?(vec1, vec2)
  vec1.normalize.dot(vec2.normalize) > 0.93
end

#second_from_last_vecObject



31
32
33
# File 'lib/active_record_polyline/compaction/vector_compressor.rb', line 31

def second_from_last_vec
  two_point_to_vec(@polyline.points.last(3).first, @polyline.points.last(2).first)
end

#two_point_to_vec(from, to) ⇒ Object



35
36
37
38
# File 'lib/active_record_polyline/compaction/vector_compressor.rb', line 35

def two_point_to_vec(from, to)
  each_norm = %i[longitude latitude].map { |key| to[key] - from[key] }
  Vector[*each_norm]
end