Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes non-default (4096) layer extent #24

Merged
merged 6 commits into from
Dec 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 43 additions & 37 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,19 @@ Decode method takes in a valid google.protobuf.message Tile and returns decoded
::

{
layername: [
{
'geometry': 'list of points',
'properties': 'dictionary of key/value pairs',
'id': 'unique id for the given feature within the layer '
},
{
# ...
}
],
layername2: [
layername: {
'extent': 'integer layer extent'
'version': 'integer'
'features': [{
'geometry': 'list of points',
'properties': 'dictionary of key/value pairs',
'id': 'unique id for the given feature within the layer '
}, ...
]
},
layername2: {
# ...
]
}
}

.. code-block:: python
Expand All @@ -188,31 +188,37 @@ Decode method takes in a valid google.protobuf.message Tile and returns decoded
>>> mapbox_vector_tile.decode('\x1aJ\n\x05water\x12\x1a\x08\x01\x12\x06\x00\x00\x01\x01\x02\x02\x18\x03"\x0c\t\x00\x80@\x1a\x00\x01\x02\x00\x00\x02\x0f\x1a\x03foo\x1a\x03uid\x1a\x03cat"\x05\n\x03bar"\x02 {"\x06\n\x04flew(\x80 x\x02\x1aY\n\x03air\x12\x1c\x08\x01\x12\x08\x00\x00\x01\x01\x02\x02\x03\x03\x18\x03"\x0c\t\x00\x80@\x1a\x00\x01\x02\x00\x00\x02\x0f\x1a\x03foo\x1a\x03uid\x1a\x05balls\x1a\x03cat"\x05\n\x03bar"\x03 \xd2\t"\x05\n\x03foo"\x06\n\x04flew(\x80 x\x02')

{
'water': [
{
'geometry': [[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]],
'properties': {
'foo': 'bar',
'uid': 123,
'cat': 'flew'
},
'type': 3,
'id': 1
}
],
'air': [
{
'geometry': [[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]],
'properties': {
'foo': 'bar',
'uid': 1234,
'balls': 'foo',
'cat': 'flew'
},
'type': 3,
'id': 1
}
]
'water': {
'extent': 4096,
'version': 2,
'features': [{
'geometry': [[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]],
'properties': {
'foo': 'bar',
'uid': 123,
'cat': 'flew'
},
'type': 3,
'id': 1
}
]
},
'air': {
'extent': 4096,
'version': 2,
'features': [{
'geometry': [[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]],
'properties': {
'foo': 'bar',
'uid': 1234,
'balls': 'foo',
'cat': 'flew'
},
'type': 3,
'id': 1
}
]
}
}

Here's how you might decode a tile from a file.
Expand Down
2 changes: 1 addition & 1 deletion mapbox_vector_tile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def decode(tile):
vector_tile = decoder.TileData(extents)
vector_tile = decoder.TileData()
message = vector_tile.getMessage(tile)
return message

Expand Down
25 changes: 16 additions & 9 deletions mapbox_vector_tile/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
class TileData:
"""
"""
def __init__(self, extents=4096):
def __init__(self):
self.tile = vector_tile.tile()
self.extents = extents

def getMessage(self, pbf_data):
self.tile.ParseFromString(pbf_data)

features_by_layer = {}
tile = {}
for layer in self.tile.layers:
features_for_layer = features_by_layer.setdefault(layer.name, [])
keys = layer.keys
vals = layer.values

features = []
for feature in layer.features:
tags = feature.tags
props = {}
Expand All @@ -45,15 +45,22 @@ def getMessage(self, pbf_data):
value = self.parse_value(val)
props[key] = value

geometry = self.parse_geometry(feature.geometry, feature.type)
geometry = self.parse_geometry(feature.geometry, feature.type,
layer.extent)
new_feature = {
"geometry": geometry,
"properties": props,
"id": feature.id,
"type": feature.type
}
features_for_layer.append(new_feature)
return features_by_layer
features.append(new_feature)

tile[layer.name] = {
"extent": layer.extent,
"version": layer.version,
"features": features,
}
return tile

def zero_pad(self, val):
return '0' + val if val[0] == 'b' else val
Expand All @@ -73,7 +80,7 @@ def parse_value(self, val):
def zig_zag_decode(self, n):
return (n >> 1) ^ (-(n & 1))

def parse_geometry(self, geom, ftype):
def parse_geometry(self, geom, ftype, extent):
# [9 0 8192 26 0 10 2 0 0 2 15]
i = 0
coords = []
Expand Down Expand Up @@ -133,7 +140,7 @@ def _ensure_polygon_closed(coords):
dx = x
dy = y

coords.append([x, self.extents-y])
coords.append([x, extent-y])

if ftype == POINT:
return coords
Expand Down
65 changes: 46 additions & 19 deletions tests/test_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@ def test_decoder(self):
else:
vector_tile = '\x1aI\n\x05water\x12\x1a\x08\x01\x12\x06\x00\x00\x01\x01\x02\x02\x18\x03"\x0c\t\x00\x80@\x1a\x00\x01\x02\x00\x00\x02\x0f\x1a\x03foo\x1a\x03baz\x1a\x03uid"\x05\n\x03bar"\x05\n\x03foo"\x02 {(\x80 x\x02' # noqa
self.assertEqual(mapbox_vector_tile.decode(vector_tile), {
'water': [{
'geometry': [[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]],
'properties': {
'foo': 'bar',
'baz': 'foo',
'uid': 123
},
'id': 1,
'type': 3
}]
'water': {
'version': 2,
'extent': 4096,
'features': [{
'geometry': [[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]],
'properties': {
'foo': 'bar',
'baz': 'foo',
'uid': 123
},
'id': 1,
'type': 3
}],
},
})

def test_decode_polygon_no_cmd_seg_end(self):
Expand All @@ -40,13 +44,36 @@ def test_decode_polygon_no_cmd_seg_end(self):
else:
vector_tile = '\x1a+\n\x05water\x12\x1d\x18\x03"\x19\t\x00\x80@"\x08\x00\x00\x07\x07\x00\x00\x08\t\x02\x01"\x00\x03\x04\x00\x00\x04\x03\x00(\x80 x\x02' # noqa
self.assertEqual(mapbox_vector_tile.decode(vector_tile), {
'water': [{
'geometry': [
[[0, 0], [4, 0], [4, 4], [0, 4], [0, 0]],
[[1, 1], [1, 3], [3, 3], [3, 1], [1, 1]],
],
'properties': {},
'id': 0,
'type': 3
}]
'water': {
'version': 2,
'extent': 4096,
'features': [{
'geometry': [
[[0, 0], [4, 0], [4, 4], [0, 4], [0, 0]],
[[1, 1], [1, 3], [3, 3], [3, 1], [1, 1]],
],
'properties': {},
'id': 0,
'type': 3
}],
},
})

def test_nondefault_extent(self):
if PY3:
vector_tile = b'\x1aK\n\x05water\x12\x1c\x08\x01\x12\x06\x00\x00\x01\x01\x02\x02\x18\x02"\x0e\t\x80}\xd0\x12\x12\xbf>\xd86\xbf>\xd86\x1a\x03foo\x1a\x03baz\x1a\x03uid"\x05\n\x03bar"\x05\n\x03foo"\x02 {(\x80@x\x02' # noqa
else:
vector_tile = '\x1aK\n\x05water\x12\x1c\x08\x01\x12\x06\x00\x00\x01\x01\x02\x02\x18\x02"\x0e\t\x80}\xd0\x12\x12\xbf>\xd86\xbf>\xd86\x1a\x03foo\x1a\x03baz\x1a\x03uid"\x05\n\x03bar"\x05\n\x03foo"\x02 {(\x80@x\x02' # noqa

self.assertEqual(mapbox_vector_tile.decode(vector_tile), {
'water': {
'version': 2,
'extent': 8192,
'features': [{
'geometry': [[8000, 7000], [4000, 3500], [0, 0]],
'id': 1,
'properties': {'baz': 'foo', 'foo': 'bar', 'uid': 123},
'type': 2
}],
}
})
5 changes: 3 additions & 2 deletions tests/test_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ def assertRoundTrip(self, input_geometry, expected_geometry, name=None,
encoded = encode(source)
decoded = decode(encoded)
self.assertIn(name, decoded)
features = decoded[name]
layer = decoded[name]
features = layer['features']
self.assertEqual(expected_len, len(features))
self.assertEqual(features[0]['properties'], expected_properties)
self.assertEqual(features[0]['geometry'], expected_geometry)
if id:
self.assertEqual(decoded[name][0]['id'], id)
self.assertEqual(features[0]['id'], id)


class TestDifferentGeomFormats(BaseTestCase):
Expand Down