Module: GitFit::Geo::Polyline
- Defined in:
- lib/git_fit/geo/polyline.rb
Class Method Summary collapse
Class Method Details
.decode(str) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/git_fit/geo/polyline.rb', line 6 def decode(str) return nil unless str && str.length > 0 points = [] index = 0 lat = 0 lng = 0 while index < str.length shift = 0 result = 0 begin byte = str[index].ord - 63 index += 1 result |= (byte & 0x1f) << shift shift += 5 end while byte >= 0x20 dlat = (result & 1) != 0 ? ~(result >> 1) : (result >> 1) lat += dlat shift = 0 result = 0 begin byte = str[index].ord - 63 index += 1 result |= (byte & 0x1f) << shift shift += 5 end while byte >= 0x20 dlng = (result & 1) != 0 ? ~(result >> 1) : (result >> 1) lng += dlng points << [lat * 1e-5, lng * 1e-5] end points end |
.encode(points) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/git_fit/geo/polyline.rb', line 45 def encode(points) result = "" plat = 0 plng = 0 points.each do |lat, lng| dlat = ((lat * 1e5).round - plat) dlng = ((lng * 1e5).round - plng) plat += dlat plng += dlng result += encode_number(dlat) result += encode_number(dlng) end result end |
.encode_number(num) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/git_fit/geo/polyline.rb', line 64 def encode_number(num) num = num < 0 ? ~(num << 1) : (num << 1) result = "" while num >= 0x20 result += ((0x20 | (num & 0x1f)) + 63).chr num >>= 5 end result += (num + 63).chr result end |