Problem with decoding the coordinates of the API response

Hi,

I’m having some issues decoding the response of the API, after decoding the protocol buffer using the schema offered in the API documentation.

This is one example of the response I’m receiving:

{'tags': [0, 0, 1, 3, 2, 2],
     'type': 'LINESTRING',
     'geometry': [9, 5344, 4468, 18, 7, 39, 7, 11,
      9, 252, 879, 10, 0, 8, 9, 388,844,18,23,23,0,3]}

I then use the zigzag decoding method mentioned in the docs to try getting the coordinates of the LineString. I use the following Python Code for this:

example = [9,5344,4468, 18,7,39,7,11,9,252,879,10,0,8,9,388,844,18,23,23,0,3]

c = 0
decoded_list = []

while c < len(example):
    
    command_and_count = example[c]
    command = command_and_count & 0x7
    count = command_and_count >> 0x3
    
    decode_x = 0
    decode_y = 0

    for _ in range(count):

        x = example[c+1] 
        y = example[c+2]
        decode_x += ((x >> command) ^ (-(x & command))) 
        decode_y += ((y >> command) ^ (-(y & command)))
        decoded_list.append([decode_x, decode_y])

        c += 2
    c += 1
       
print(decoded_list)
>> [[2672, 2234], [-1, -9], [-2, -13], [126, -440], [0, 2], [194, 422], [-5, -5], [-5, -7]]

The results are not making sense. Is the zigzag method correct?

Thanks!

I assume that you are using this page:

In the description of “Coordinates” section before the “Point” example there is missing information that the absolute coordinates are given only in x0 and y0, but only in first command in sequence. The rest of coordinates even in next command are relative to the ones in the previous command. So in your code you should move the declaration of decode_x0 and decode_y0 outside while loop and not reset them for every command decoding.

1 Like

I have been playing around with the above logic moving decode_x0 and decode_y0 out of the while loop.

My example encoded linestring geometry is:

example = [9, 235, 4148, 18, 52, 104, 72, 4, 9, 99, 55, 18, 23, 55, 3, 0, 9, 728, 1047, 26, 0, 115, 4, 39, 60, 0, 9, 900, 1427, 26, 16, 12, 12, 16, 8, 24, 9, 4, 4, 26, 4, 12, 8, 8, 0, 4, 9, 0, 3, 42, 7, 7, 7, 27, 11, 19, 15, 15, 7, 3, 9, 56, 51, 26, 19, 32, 3, 12, 19, 16, 9, 0, 0, 18, 16, 11, 28, 51, 9, 1228, 2227, 26, 19, 88, 15, 124, 3, 0, 9, 4, 0, 18, 16, 131, 20, 79, 9, 400, 2612, 10, 0, 35, 9, 0, 0, 10, 0, 36, 9, 119, 1207, 18, 12, 72, 4, 8, 9, 3, 0, 18, 11, 79, 11, 23, 9, 1292, 1584, 10, 11, 63, 9, 4, 12, 10, 8, 52, 9, 940, 6280, 10, 20, 32, 9, 3, 3, 10, 19, 31, 9, 1752, 6039, 18, 32, 35, 56, 19, 9, 0, 0, 34, 59, 24, 31, 36, 71, 4, 3, 4, 9, 1840, 923, 34, 8, 20, 12, 16, 12, 28, 40, 31, 9, 0, 0, 34, 43, 32, 7, 27, 11, 15, 51, 103]

My result looks like this:
LINESTRING (-118 2074,-105 2100,-87 2101,-137 2073,-142 2060,-144 2060,220 1536,220 1506,221 1497,236 1497,686 783,690 786,693 790,695 796,697 798,698 801,700 803,700 804,700 802,699 801,698 793,694 787,691 784,690 782,718 756,712 764,710 767,704 771,704 771,708 767,715 753,1329 -361,1323 -339,1320 -308,1318 -308,1320 -308,1324 -342,1329 -361,1529 945,1529 935,1529 935,1529 944,1469 340,1472 358,1473 360,1471 360,1467 341,1463 336,2109 1128,2105 1113,2107 1119,2109 1132,2579 4272,2584 4280,2582 4278,2576 4271,3452 1251,3460 1241,3474 1235,3474 1235,3458 1241,3451 1250,3434 1251,3432 1252,4352 790,4354 795,4357 799,4360 806,4370 799,4370 799,4358 807,4357 799,4353 796,4339 771)

Which looks like this which can’t be correct it seems.

@maloleps do you by any chance have an idea what I am doing wrong?

Are you sure that it is a one linestring?