Class: Polylines::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/polylines/base.rb

Direct Known Subclasses

Decoder, Encoder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_value) ⇒ Base

Returns a new instance of Base.


5
6
7
# File 'lib/polylines/base.rb', line 5

def initialize(current_value)
  @current_value = current_value
end

Instance Attribute Details

#current_valueObject (readonly)

Returns the value of attribute current_value


3
4
5
# File 'lib/polylines/base.rb', line 3

def current_value
  @current_value
end

#negativeObject (readonly)

Returns the value of attribute negative


3
4
5
# File 'lib/polylines/base.rb', line 3

def negative
  @negative
end

Class Method Details

.transform_to_array_of_lat_lng_and_deltas(value, precision = 1e5) ⇒ Object


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/polylines/base.rb', line 92

def self.transform_to_array_of_lat_lng_and_deltas(value, precision = 1e5)
  if self == Polylines::Encoder
    delta_latitude, delta_longitude = 0, 0

    shifted_values = value.map{|tuple| tuple.map{|val| (val * precision).round } }
    deltas = shifted_values.inject([]) do |polyline, (latitude, longitude)|
      polyline << latitude - delta_latitude
      polyline << longitude - delta_longitude
      delta_latitude, delta_longitude = latitude, longitude
      polyline
    end
    return deltas.map{|val| val.to_f/precision }
  end

  if self == Polylines::Decoder
    set = []
    return value.split(//).inject([]) do |charset, char|
      set << char

      if ((char.unpack("U").first - 63) & 0x20).zero?
        charset << set.join
        set = []
      end

      charset
    end.map {|charset| decode(charset, precision) }
  end
end

Instance Method Details

#decode!Object


78
79
80
81
82
# File 'lib/polylines/base.rb', line 78

def decode!
  if decoding?
    @current_value = yield
  end
end

#decoding?Boolean

Returns:

  • (Boolean)

88
89
90
# File 'lib/polylines/base.rb', line 88

def decoding?
  self.is_a?(Polylines::Decoder)
end

#encode!Object


72
73
74
75
76
# File 'lib/polylines/base.rb', line 72

def encode!
  if encoding?
    @current_value = yield
  end
end

#encoding?Boolean

Returns:

  • (Boolean)

84
85
86
# File 'lib/polylines/base.rb', line 84

def encoding?
  self.is_a?(Polylines::Encoder)
end

#step_10Object


62
63
64
65
# File 'lib/polylines/base.rb', line 62

def step_10
  encode! { current_value.map {|value| value + 63 } }
  decode! { current_value.map {|value| value - 63 } }
end

#step_11Object


67
68
69
70
# File 'lib/polylines/base.rb', line 67

def step_11
  encode! { current_value.map(&:chr).join }
  decode! { current_value.split(//).map {|char| char.unpack("U").first } }
end

#step_2(precision = 1e5) ⇒ Object


9
10
11
12
13
14
# File 'lib/polylines/base.rb', line 9

def step_2(precision = 1e5)
  @negative = current_value < 0 if encoding?

  encode! { (current_value * precision).round }
  decode! { current_value.to_f/precision }
end

#step_3Object


16
17
18
19
20
# File 'lib/polylines/base.rb', line 16

def step_3
  return unless negative
  encode! { ~(current_value * -1) + 1 }
  decode! { ~(current_value - 1) * -1 }
end

#step_4Object


22
23
24
25
# File 'lib/polylines/base.rb', line 22

def step_4
  encode! { current_value << 1 }
  decode! { current_value >> 1 }
end

#step_5Object


27
28
29
30
31
# File 'lib/polylines/base.rb', line 27

def step_5
  return unless negative
  encode! { ~current_value }
  decode! { ~current_value }
end

#step_6Object


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/polylines/base.rb', line 33

def step_6
  encode! do
    [].tap do |numbers|
      while current_value > 0 do
        numbers.unshift(current_value & 0x1f)
        @current_value >>= 5
      end

      numbers << 0 if numbers.empty?
    end
  end

  decode! do
    current_value.map {|chunk| "%05b" % chunk }.join.tap do |val|
      @negative = val[-1, 1] == "1"
    end.to_i(2)
  end
end

#step_7Object


52
53
54
55
# File 'lib/polylines/base.rb', line 52

def step_7
  encode! { current_value.reverse }
  decode! { current_value.reverse }
end

#step_8Object


57
58
59
60
# File 'lib/polylines/base.rb', line 57

def step_8
  encode! { current_value[0..-2].map {|item| item | 0x20 } << current_value.last }
  decode! { current_value[0..-2].map {|item| item ^ 0x20 } << current_value.last }
end