Polyline decoder

This commit is contained in:
Mario de Frutos
2016-03-01 23:05:24 +01:00
parent 2b084ee2ab
commit 4d04882c18
4 changed files with 95 additions and 1 deletions

View File

@@ -1,2 +1,3 @@
from redis_tools import RedisConnection
from coordinates import Coordinate
from coordinates import Coordinate
from polyline import PolyLine

View File

@@ -0,0 +1,51 @@
from itertools import tee, izip
from math import trunc
class PolyLine:
""" Polyline decoder https://developers.google.com/maps/documentation/utilities/polylinealgorithm?csw=1 """
def decode(self, data):
coordinates = []
chunks = self._extract_chunks(data)
for chunk in chunks:
coordinate = self._process_chunk(chunk)
coordinate /= 1e5
print coordinate
if len(coordinates) > 1:
# We have to sum the previous with the offset in this chunk
coordinate += coordinates[-2]
coordinates.append(round(coordinate, 5))
print coordinates
return zip(coordinates, coordinates[1:])[::2]
def _extract_chunks(self, data):
chunks, chunk = [], []
for character in data:
byte = ord(character) - 63
if byte & 0x20 > 0:
byte &= 0x1F
chunk.append(byte)
else:
chunk.append(byte)
chunks.append(chunk)
chunk = []
return chunks
def _process_chunk(self, chunk):
coordinate = self._get_coordinate(chunk)
# Check if the coordinate is negative
if coordinate & 0x1:
return ~(coordinate >> 1)
else:
return coordinate >> 1
def _get_coordinate(self, chunk):
coordinate = 0
for i, c in enumerate(chunk):
coordinate |= c << (i * 5)
return coordinate