Skip to content

Instantly share code, notes, and snippets.

@vehrka
Created October 10, 2019 08:25
Show Gist options
  • Save vehrka/bc43cf835f315405ff8b95ed119716b9 to your computer and use it in GitHub Desktop.
Save vehrka/bc43cf835f315405ff8b95ed119716b9 to your computer and use it in GitHub Desktop.
def __poly_decode_trans(value, index):
"""Transforms a GMaps Encoded polyline chunk back into numbers
Args:
value: chunk of the GMaps polyline
index: position in the chunk
Returns:
chunk as a number
"""
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 __poly_decode(expression, precision=5, geojson=False):
"""Transforms a GMaps Encoded polyline into a set of points
Args:
expression: the GMaps polyline
precision: number of decimal places to calculate
geojson: Return the sets in geojson compatible mode
Returns:
coordinates: list of points composing the polyline
"""
coordinates, index, lat, lng, length, factor = (
[],
0,
0,
0,
len(expression),
float(10 ** precision),
)
while index < length:
lat_change, index = __poly_decode_trans(expression, index)
lng_change, index = __poly_decode_trans(expression, index)
lat += lat_change
lng += lng_change
coordinates.append((lat / factor, lng / factor))
if geojson is True:
coordinates = [t[::-1] for t in coordinates]
return coordinates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment