tilezen / mapbox-vector-tile

Python package for encoding & decoding Mapbox Vector Tiles
MIT License
244 stars 47 forks source link

'name' disappeared after using mapbox_vector_tile.decode, causing error when using mapbox_vector_tile.encode #136

Closed thangqd closed 2 months ago

thangqd commented 3 months ago

Please help:

When I tried:

mapbox_vector_tile.encode([ {"name": "water","features": [{"geometry":"POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))","properties":{ "uid":123, "foo":"bar", "cat":"flew"} }] }, {"name": "air","features": [ {"geometry":"LINESTRING(159 3877, -1570 3877)","properties":{ "uid":1234, "foo":"bar", "cat":"flew"}}]}])

it returned:

b'\x1aH\n\x05water\x12\x18\x12\x06\x00\x00\x01\x01\x02\x02\x18\x03"\x0c\t\x00\x80@\x1a\x00\x01\x02\x00\x00\x02\x0f\x1a\x03uid\x1a\x03foo\x1a\x03cat"\x02 {"\x05\n\x03bar"\x06\n\x04flew(\x80 x\x02\x1aD\n\x03air\x12\x15\x12\x06\x00\x00\x01\x01\x02\x02\x18\x02"\t\t\xbe\x02\xb6\x03\n\x81\x1b\x00\x1a\x03uid\x1a\x03foo\x1a\x03cat"\x03 \xd2\t"\x05\n\x03bar"\x06\n\x04flew(\x80 x\x02'

Then I tried: >>>mapbox_vector_tile.decode(b'\x1aH\n\x05water\x12\x18\x12\x06\x00\x00\x01\x01\x02\x02\x18\x03"\x0c\t\x00\x80@\x1a\x00\x01\x02\x00\x00\x02\x0f\x1a\x03uid\x1a\x03foo\x1a\x03cat"\x02 {"\x05\n\x03bar"\x06\n\x04flew(\x80 x\x02\x1aD\n\x03air\x12\x15\x12\x06\x00\x00\x01\x01\x02\x02\x18\x02"\t\t\xbe\x02\xb6\x03\n\x81\x1b\x00\x1a\x03uid\x1a\x03foo\x1a\x03cat"\x03 \xd2\t"\x05\n\x03bar"\x06\n\x04flew(\x80 x\x02')

It returned: {'water': {'extent': 4096, 'version': 2, 'features': [{'geometry': {'type': 'Polygon', 'coordinates': [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]]}, 'properties': {'uid': 123, 'foo': 'bar', 'cat': 'flew'}, 'id': 0, 'type': 'Feature'}], 'type': 'FeatureCollection'}, 'air': {'extent': 4096, 'version': 2, 'features': [{'geometry': {'type': 'LineString', 'coordinates': [[159, 3877], [-1570, 3877]]}, 'properties': {'uid': 1234, 'foo': 'bar', 'cat': 'flew'}, 'id': 0, 'type': 'Feature'}], 'type': 'FeatureCollection'}}

Then I tried to decode it back:

mapbox_vector_tile.encode([{'water': {'extent': 4096, 'version': 2, 'features': [{'geometry': {'type': 'Polygon', 'coordinates': [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]]}, 'properties': {'uid': 123, 'foo': 'bar', 'cat': 'flew'}, 'id': 0, 'type': 'Feature'}], 'type': 'FeatureCollection'}, 'air': {'extent': 4096, 'version': 2, 'features': [{'geometry': {'type': 'LineString', 'coordinates': [[159, 3877], [-1570, 3877]]}, 'properties': {'uid': 1234, 'foo': 'bar', 'cat': 'flew'}, 'id': 0, 'type': 'Feature'}], 'type': 'FeatureCollection'}}])

It returned error: File "C:\Users\Me\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\mapbox_vector_tile__init__.py", line 90, in encode layer_name = layer["name"]


KeyError: 'name'

I realized that after encode: the 'name' before 'water' disappeared --> that caused error when trying to decode to the original input

Thanks!
rmarianski commented 3 months ago

the encode and decode api's are not symmetric, ie round tripping does not yield the original result.

thangqd commented 2 months ago

Thank you @rmarianski