Created
November 13, 2011 09:35
-
-
Save janv/1361880 to your computer and use it in GitHub Desktop.
Polyline Encoder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PolylineEncoder | |
# Encode an rgeo exterior ring/interior ring, | |
# anything with an attribute points, containing an array | |
# of object with x and y float attributes | |
def self.encode(ring) | |
encoded = "" | |
px = 0 | |
py = 0 | |
ring.points.each_with_index do |point, i| | |
dx,dy = point.x-px, point.y-py | |
px,py = point.x , point.y | |
dx = (dx * 1e5).round | |
dy = (dy * 1e5).round | |
chunks_x = encodeSignedNumber(dx) | |
chunks_y = encodeSignedNumber(dy) | |
encoded << chunks_y << chunks_x | |
end | |
return encoded | |
end | |
private | |
def self.encodeSignedNumber(num) | |
# Based on the official google example | |
sgn_num = num << 1 | |
sgn_num = ~(sgn_num) if( num < 0 ) | |
encodeNumber(sgn_num) | |
end | |
def self.encodeNumber(num) | |
# Based on the official google example | |
encoded = "" | |
while (num >= 0x20) do | |
encoded << (0x20 | (num & 0x1f)) + 63 | |
num = num >> 5 | |
end | |
encoded << num + 63 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment