polylines 0.2.0 → 0.3.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
  SHA1:
3
- metadata.gz: cdb3e4a0ccf6490c83d0cbf0e04d5ad247b833cc
4
- data.tar.gz: f35af512711415d4f831f694a92c213cc9c6626c
3
+ metadata.gz: 0237e7cc9473d7cf8379bd047914dd7e28b044bf
4
+ data.tar.gz: a1a78b14f27af757e05841e4ee14ace6c8d368e1
5
5
  SHA512:
6
- metadata.gz: ba22ff2de2fe0f3a9e714bbf74303bdfc08acac814e04a33afeaef4f619828b4033fd3e48c0790e2fbad1f0c865210d11c1ae52a4b613ab6a975053c3600e5a4
7
- data.tar.gz: 2f1383080d0217870a5ccf57363490eca251d64e1ca2b2c550f434a52c566c4eb7f0692d00804760949fd5e292e54d9090b232790c7c0ffe8dd1fb5699435019
6
+ metadata.gz: 6fc953efe71e70921b55a2dea01a25f450e13602a0647b0b5ac6519ac92e449246bd40bbdccef086bb028f41dccab9cdc5b2d3f7fc19f8d84cc8a885863f4ae7
7
+ data.tar.gz: 5d5f51ee3a2f3d1b6d14716184887607789e139520b6d83f804147d3a4058115e7828e9bd8ea77134c69cbd8d1c822c40d14547a348ebfedf3eabb1db0d71b8a
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- polylines (0.2.0)
4
+ polylines (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -6,11 +6,11 @@ module Polylines
6
6
  @current_value = current_value
7
7
  end
8
8
 
9
- def step_2
9
+ def step_2(precision = 1e5)
10
10
  @negative = current_value < 0 if encoding?
11
11
 
12
- encode! { (current_value * 1e5).round }
13
- decode! { current_value.to_f/1e5 }
12
+ encode! { (current_value * precision).round }
13
+ decode! { current_value.to_f/precision }
14
14
  end
15
15
 
16
16
  def step_3
@@ -89,18 +89,18 @@ module Polylines
89
89
  self.is_a?(Polylines::Decoder)
90
90
  end
91
91
 
92
- def self.transform_to_array_of_lat_lng_and_deltas(value)
92
+ def self.transform_to_array_of_lat_lng_and_deltas(value, precision = 1e5)
93
93
  if self == Polylines::Encoder
94
94
  delta_latitude, delta_longitude = 0, 0
95
95
 
96
- e5_values = value.map{|tuple| tuple.map{|val| (val * 1e5).round } }
97
- deltas = e5_values.inject([]) do |polyline, (latitude, longitude)|
96
+ shifted_values = value.map{|tuple| tuple.map{|val| (val * precision).round } }
97
+ deltas = shifted_values.inject([]) do |polyline, (latitude, longitude)|
98
98
  polyline << latitude - delta_latitude
99
99
  polyline << longitude - delta_longitude
100
100
  delta_latitude, delta_longitude = latitude, longitude
101
101
  polyline
102
102
  end
103
- return deltas.map{|val| val.to_f/1e5 }
103
+ return deltas.map{|val| val.to_f/precision }
104
104
  end
105
105
 
106
106
  if self == Polylines::Decoder
@@ -114,7 +114,7 @@ module Polylines
114
114
  end
115
115
 
116
116
  charset
117
- end.map {|charset| decode(charset) }
117
+ end.map {|charset| decode(charset, precision) }
118
118
  end
119
119
  end
120
120
  end
@@ -1,7 +1,8 @@
1
1
  module Polylines
2
2
  class Decoder < Base
3
- def self.decode_polyline(polyline)
4
- points_with_deltas = transform_to_array_of_lat_lng_and_deltas(polyline)
3
+ def self.decode_polyline(polyline, precision = 1e5)
4
+ points_with_deltas = transform_to_array_of_lat_lng_and_deltas(polyline,
5
+ precision)
5
6
 
6
7
  [].tap do |points|
7
8
  points << [points_with_deltas.shift, points_with_deltas.shift]
@@ -15,7 +16,7 @@ module Polylines
15
16
  end
16
17
  end
17
18
 
18
- def self.decode(string)
19
+ def self.decode(string, precision = 1e5)
19
20
  self.new(string).tap do |decoding|
20
21
  decoding.step_11
21
22
  decoding.step_10
@@ -25,7 +26,7 @@ module Polylines
25
26
  decoding.step_5
26
27
  decoding.step_4
27
28
  decoding.step_3
28
- decoding.step_2
29
+ decoding.step_2 precision
29
30
  end.current_value
30
31
  end
31
32
  end
@@ -1,13 +1,13 @@
1
1
  module Polylines
2
2
  class Encoder < Base
3
- def self.encode_points(points)
4
- points_with_deltas = transform_to_array_of_lat_lng_and_deltas(points)
5
- points_with_deltas.map {|point| encode(point) }.join
3
+ def self.encode_points(points, precision = 1e5)
4
+ points_with_deltas = transform_to_array_of_lat_lng_and_deltas(points, precision)
5
+ points_with_deltas.map {|point| encode(point, precision) }.join
6
6
  end
7
7
 
8
- def self.encode(number)
8
+ def self.encode(number, precision = 1e5)
9
9
  self.new(number).tap do |encoding|
10
- encoding.step_2
10
+ encoding.step_2 precision
11
11
  encoding.step_3
12
12
  encoding.step_4
13
13
  encoding.step_5
@@ -1,3 +1,3 @@
1
1
  module Polylines
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -4,14 +4,27 @@ describe Polylines::Decoder, ".decode" do
4
4
  it "decodes a single point" do
5
5
  Polylines::Decoder.decode("`~oia@").should be_within(0.00001).of(-179.9832104)
6
6
  end
7
+
8
+ it "decodes a single point with 1e6 precision" do
9
+ Polylines::Decoder.decode("ruhhvI", 1e6).should be_within(0.000001).of(-179.9832104)
10
+ end
7
11
  end
8
12
 
9
13
  describe Polylines::Decoder, ".decode_polyline" do
10
- let(:polyline) { "_p~iF~ps|U_ulLnnqC_mqNvxq`@" }
11
14
  let(:points) { [[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]] }
12
15
 
13
- it "decodes a polyline correctly" do
14
- Polylines::Decoder.decode_polyline(polyline).should == points
16
+ context "with default precision" do
17
+ let(:polyline) { "_p~iF~ps|U_ulLnnqC_mqNvxq`@" }
18
+ it "decodes a polyline correctly" do
19
+ Polylines::Decoder.decode_polyline(polyline).should == points
20
+ end
21
+ end
22
+
23
+ context "with 1e6 precision" do
24
+ let(:polyline) { "_izlhA~rlgdF_{geC~ywl@_kwzCn`{nI" }
25
+ it "decodes a polyline correctly" do
26
+ Polylines::Decoder.decode_polyline(polyline, 1e6).should == points
27
+ end
15
28
  end
16
29
  end
17
30
 
@@ -19,4 +32,8 @@ describe Polylines::Decoder, ".decode_polyline with points that were close toget
19
32
  it "decodes a polyline correctly" do
20
33
  Polylines::Decoder.decode_polyline("krk{FdxdlO?e@").should == [[41.35222, -86.04563],[41.35222, -86.04544]]
21
34
  end
35
+
36
+ it "decodes a polyline correctly with 1e6 precision" do
37
+ Polylines::Decoder.decode_polyline("q`}zmAzzxbcD?aK", 1e6).should == [[41.352217, -86.045630],[41.352217, -86.045437]]
38
+ end
22
39
  end
@@ -4,14 +4,27 @@ describe Polylines::Encoder, ".encode" do
4
4
  it "encodes a single point" do
5
5
  Polylines::Encoder.encode(-179.9832104).should == "`~oia@"
6
6
  end
7
+
8
+ it "encodes a single point with 1e6 precision" do
9
+ Polylines::Encoder.encode(-179.9832104, 1e6).should == "ruhhvI"
10
+ end
7
11
  end
8
12
 
9
13
  describe Polylines::Encoder, ".encode_points" do
10
- let(:polyline) { "_p~iF~ps|U_ulLnnqC_mqNvxq`@" }
11
14
  let(:points) { [[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]] }
12
15
 
13
- it "encodes points correctly" do
14
- Polylines::Encoder.encode_points(points).should == polyline
16
+ context "with default precision" do
17
+ let(:polyline) { "_p~iF~ps|U_ulLnnqC_mqNvxq`@" }
18
+ it "encodes points correctly" do
19
+ Polylines::Encoder.encode_points(points).should == polyline
20
+ end
21
+ end
22
+
23
+ context "with 1e6 precsion" do
24
+ let(:polyline) { "_izlhA~rlgdF_{geC~ywl@_kwzCn`{nI" }
25
+ it "encodes points correctly" do
26
+ Polylines::Encoder.encode_points(points, 1e6).should == polyline
27
+ end
15
28
  end
16
29
  end
17
30
 
@@ -19,6 +32,10 @@ describe Polylines::Encoder, ".encode_points that are very close together" do
19
32
  it "encodes points correctly" do
20
33
  Polylines::Encoder.encode_points([[41.3522171071184, -86.0456299662023],[41.3522171071183, -86.0454368471533]]).should == "krk{FdxdlO?e@"
21
34
  end
35
+
36
+ it "encodes points correctly with 1e6 precision" do
37
+ Polylines::Encoder.encode_points([[41.3522171071184, -86.0456299662023],[41.3522171071183, -86.0454368471533]], 1e6).should == "q`}zmAzzxbcD?aK"
38
+ end
22
39
  end
23
40
 
24
41
  describe Polylines::Encoder, ".encode_points with same results as google's api" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polylines
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
  - Josh Clayton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-22 00:00:00.000000000 Z
11
+ date: 2015-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  version: '0'
80
80
  requirements: []
81
81
  rubyforge_project:
82
- rubygems_version: 2.2.2
82
+ rubygems_version: 2.4.5
83
83
  signing_key:
84
84
  specification_version: 4
85
85
  summary: Easily handle Google polylines
@@ -87,3 +87,4 @@ test_files:
87
87
  - spec/polylines/decoder_spec.rb
88
88
  - spec/polylines/encoder_spec.rb
89
89
  - spec/spec_helper.rb
90
+ has_rdoc: