Create polyline.py
polyline decoder
This commit is contained in:
parent
0c4685e251
commit
3eeedade52
1 changed files with 35 additions and 0 deletions
35
polyline.py
Normal file
35
polyline.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
# This function is free of any dependencies.
|
||||
# source: https://github.com/mgd722/decode-google-maps-polyline
|
||||
def decode_polyline(polyline_str):
|
||||
'''Pass a Google Maps encoded polyline string; returns list of lat/lon pairs'''
|
||||
index, lat, lng = 0, 0, 0
|
||||
coordinates = []
|
||||
changes = {'latitude': 0, 'longitude': 0}
|
||||
|
||||
# Coordinates have variable length when encoded, so just keep
|
||||
# track of whether we've hit the end of the string. In each
|
||||
# while loop iteration, a single coordinate is decoded.
|
||||
while index < len(polyline_str):
|
||||
# Gather lat/lon changes, store them in a dictionary to apply them later
|
||||
for unit in ['latitude', 'longitude']:
|
||||
shift, result = 0, 0
|
||||
|
||||
while True:
|
||||
byte = ord(polyline_str[index]) - 63
|
||||
index+=1
|
||||
result |= (byte & 0x1f) << shift
|
||||
shift += 5
|
||||
if not byte >= 0x20:
|
||||
break
|
||||
|
||||
if (result & 1):
|
||||
changes[unit] = ~(result >> 1)
|
||||
else:
|
||||
changes[unit] = (result >> 1)
|
||||
|
||||
lat += changes['latitude']
|
||||
lng += changes['longitude']
|
||||
|
||||
coordinates.append((lat / 100000.0, lng / 100000.0))
|
||||
|
||||
return coordinates
|
Loading…
Add table
Add a link
Reference in a new issue