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
-------------------------------------------------------------------------------------------
_______________________________________________
OSRM-talk mailing list
OSRM-talk@openstreetmap.org
https://lists.openstreetmap.org/listinfo/osrm-talk

Reply via email to