[OSRM-talk] Python Decode Geometry Function
Jay Patel
patelj27b at gmail.com
Mon Jan 13 20:11:08 UTC 2014
I converted the javascript function for decoding the compressed geometry to
python, but the lat-lon values are not coming out correctly. Can someone
look at the function I wrote and tell me if there is anything wrong with
it? Thanks!
-------------------------------------------------------------------------------------------
# Decode Geometry that was in reply from OSRM
def decode_geometry(encoded_string, precision):
decode_precision = math.pow(10, precision * -1)
encoded_length = len(encoded_string)
index = 0
lat = 0
lon = 0
output_array = []
while index < encoded_length:
b = 0
shift = 0
result = 0
while True:
b = ord(encoded_string[index]) - 63
index += 1
result |= (b & 0x1f) << shift
shift += 5
if b < 0x20:
break
if result & 1:
dlat = ~(result >> 1)
else:
dlat = result >> 1
lat += dlat
shift = 0
result = 0
while True:
b = ord(encoded_string[index]) - 63
index += 1
result |= (b & 0x1f) << shift
shift += 5
if b < 0x20:
break
if result & 1:
dlon = ~(result >> 1)
else:
dlon = result >> 1
lon += dlon
output_array.append([lat*decode_precision, lon*decode_precision])
return output_array
-------------------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/osrm-talk/attachments/20140113/8807d85e/attachment.html>
More information about the OSRM-talk
mailing list