<div dir="ltr">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!<br>
<br>-------------------------------------------------------------------------------------------<br># Decode Geometry that was in reply from OSRM<br>def decode_geometry(encoded_string, precision):<br> decode_precision = math.pow(10, precision * -1)<br>
encoded_length = len(encoded_string)<br> index = 0<br> lat = 0<br> lon = 0<br> output_array = []<br><br> while index < encoded_length:<br> b = 0<br> shift = 0<br> result = 0<br>
<br>
while True:<br> b = ord(encoded_string[index]) - 63<br> index += 1<br> result |= (b & 0x1f) << shift<br> shift += 5<br> if b < 0x20:<br> break<br>
if result & 1:<br> dlat = ~(result >> 1)<br> else:<br> dlat = result >> 1<br> lat += dlat<br><br> shift = 0<br> result = 0<br><br> while True:<br>
b = ord(encoded_string[index]) - 63<br> index += 1<br> result |= (b & 0x1f) << shift<br> shift += 5<br> if b < 0x20:<br> break<br> if result & 1:<br>
dlon = ~(result >> 1)<br> else:<br> dlon = result >> 1<br> lon += dlon<br> output_array.append([lat*decode_precision, lon*decode_precision])<br><br> return output_array<br>
<div>-------------------------------------------------------------------------------------------<br></div><div><br></div><div><br></div></div>