Skip to content

Commit

Permalink
Add support for lon-lat/GeoJSON tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Seelye committed May 28, 2019
1 parent 0764302 commit 5e43466
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ This should return ``_p~iF~ps|U_ulL~ugC_hgN~eq`@``.

You can set the required precision with the optional ``precision`` parameter. The default value is 5.

You can encode lon-lat tuples by setting ``geojson=True``.

Decoding
--------

Expand All @@ -44,3 +46,5 @@ To get a set of coordinates represented by a given encoded polyline string::
polyline.decode('u{~vFvyys@fS]')

This should return ``[(40.63179, -8.65708), (40.62855, -8.65693)]``.

You can decode into lon-lat tuples by setting ``geojson=True``.
10 changes: 6 additions & 4 deletions polyline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@
__version__ = '1.3.2'


def decode(expression, precision=5):
def decode(expression, precision=5, geojson=False):
"""
Decode a polyline string into a set of coordinates.
:param expression: Polyline string, e.g. 'u{~vFvyys@fS]'.
:param precision: Precision of the encoded coordinates. Google Maps uses 5, OpenStreetMap uses 6.
The default value is 5.
:param geojson: Set output of tuples to (lon, lat), as per https://tools.ietf.org/html/rfc7946#section-3.1.1
:return: List of coordinate tuples
"""
return PolylineCodec().decode(expression, precision)
return PolylineCodec().decode(expression, precision, geojson)


def encode(coordinates, precision=5):
def encode(coordinates, precision=5, geojson=False):
"""
Encode a set of coordinates in a polyline string.
:param coordinates: List of coordinate tuples, e.g. [(0, 0), (1, 0)].
:param precision: Precision of the coordinates to encode. Google Maps uses 5, OpenStreetMap uses 6.
The default value is 5.
:param geojson: Set to True in order to encode lon-lat tuples.
:return: The encoded polyline string.
"""
return PolylineCodec().encode(coordinates, precision)
return PolylineCodec().encode(coordinates, precision, geojson)


__all__ = ['decode', 'encode']
10 changes: 8 additions & 2 deletions polyline/codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _trans(self, value, index):

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

def decode(self, expression, precision=5):
def decode(self, expression, precision=5, geojson=False):
coordinates, index, lat, lng, length, factor = [], 0, 0, 0, len(expression), float(10 ** precision)

while index < length:
Expand All @@ -46,9 +46,15 @@ def decode(self, expression, precision=5):
lng += lng_change
coordinates.append((lat / factor, lng / factor))

if geojson is True:
coordinates = [t[::-1] for t in coordinates]

return coordinates

def encode(self, coordinates, precision=5):
def encode(self, coordinates, precision=5, geojson=False):
if geojson is True:
coordinates = [t[::-1] for t in coordinates]

output, factor = six.StringIO(), int(10 ** precision)

self._write(output, coordinates[0][0], 0, factor)
Expand Down

0 comments on commit 5e43466

Please sign in to comment.