Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
bmcustodio committed Oct 10, 2014
0 parents commit c2243c7
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
40 changes: 40 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
*.egg
*.egg-info/
*.log
*.manifest
*.mo
*.pot
*.py[cod]
*.so
*.spec
*.un~
*~
.cache
.coverage
.directory
.installed.cfg
.netrwhist
.Python
.tox/
build/
coverage.xml
develop-eggs/
dist/
docs/_build/
downloads/
eggs/
env/
htmlcov/
lib/
lib64/
nosetests.xml
parts/
pip-delete-this-directory.txt
pip-log.txt
sdist/
Session.vim
target/
var/
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
__pycache__/
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2014 Bruno M. Custódio

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
polyline
========

A Python implementation of Google's Encoded Polyline Algorithm Format
(http://goo.gl/PvXf8Y). This is essentially a port of
https://github.com/mapbox/polyline built with Python 2 and 3 support in mind.

License
-------

MIT © `Bruno M. Custódio <mailto:[email protected]>`_
1 change: 1 addition & 0 deletions polyline/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '1.0.0-dev'
53 changes: 53 additions & 0 deletions polyline/codec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import itertools
import six


class PolylineCodec(object):
def _pcitr(self, iterable):
return six.moves.zip(iterable, itertools.islice(iterable, 1, None))

def _write(self, output, value):
coord = int(round(value * 1e5, 0))
coord <<= 1
coord = coord if coord >= 0 else ~coord

while coord >= 0x20:
output.write(six.unichr((0x20 | (coord & 0x1f)) + 63))
coord >>= 5

output.write(six.unichr(coord + 63))

def _trans(self, value, index):
byte, result, shift = None, 0, 0

while (byte is None or byte >= 0x20):
byte = ord(value[index]) - 63
index += 1
result |= (byte & 0x1f) << shift
shift += 5
comp = result & 1

return ~(result >> 1) if comp else (result >> 1), index

def decode(self, expression):
coordinates, index, lat, lng, length = [], 0, 0, 0, len(expression)

while (index < length):
lat_change, index = self._trans(expression, index)
lng_change, index = self._trans(expression, index)
lat += lat_change
lng += lng_change
coordinates.append((lat / 1e5, lng / 1e5))

return coordinates

def encode(self, coordinates):
output = six.StringIO()
self._write(output, coordinates[0][0])
self._write(output, coordinates[0][1])

for prev, curr in self._pcitr(coordinates):
self._write(output, curr[0] - prev[0])
self._write(output, curr[1] - prev[1])

return output.getvalue()
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
six==1.8.0
Empty file added test/__init__.py
Empty file.
70 changes: 70 additions & 0 deletions test/test_codec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import unittest

from polyline.codec import PolylineCodec


class PolylineCodecTestCase(unittest.TestCase):
def setUp(self):
self.codec = PolylineCodec()

def test_decode_multiple_points(self):
d = self.codec.decode('gu`wFnfys@???nKgE??gE?????oK????fE??fE')
self.assertSequenceEqual(d, [
(40.641, -8.654),
(40.641, -8.654),
(40.641, -8.656),
(40.642, -8.656),
(40.642, -8.655),
(40.642, -8.655),
(40.642, -8.655),
(40.642, -8.653),
(40.642, -8.653),
(40.642, -8.653),
(40.641, -8.653),
(40.641, -8.654)
])

def test_decode_official_example(self):
d = self.codec.decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@')
self.assertSequenceEqual(d, [
(38.500, -120.200),
(40.700, -120.950),
(43.252, -126.453)
])

def test_decode_single_point(self):
d = self.codec.decode('gu`wFf`ys@')
self.assertSequenceEqual(d, [
(40.641, -8.653)
])

def test_encode_multiple_points(self):
e = self.codec.encode([
(40.641, -8.654),
(40.641, -8.654),
(40.641, -8.656),
(40.642, -8.656),
(40.642, -8.655),
(40.642, -8.655),
(40.642, -8.655),
(40.642, -8.653),
(40.642, -8.653),
(40.642, -8.653),
(40.641, -8.653),
(40.641, -8.654)
])
self.assertEqual(e, 'gu`wFnfys@???nKgE??gE?????oK????fE??fE')

def test_encode_official_example(self):
e = self.codec.encode([
(38.500, -120.200),
(40.700, -120.950),
(43.252, -126.453)
])
self.assertEqual(e, '_p~iF~ps|U_ulLnnqC_mqNvxq`@')

def test_encode_single_point(self):
e = self.codec.encode([
(40.641, -8.653)
])
self.assertEqual(e, 'gu`wFf`ys@')

0 comments on commit c2243c7

Please sign in to comment.