Skip to content

Commit

Permalink
Use a separate values index for booleans
Browse files Browse the repository at this point in the history
This prevents collisions between boolean and integer values. In python:
1 == True and 0 == False. But, we don't want these to point to the same
value in the index table.
  • Loading branch information
rmarianski committed Oct 26, 2017
1 parent 5f54ef2 commit e04415e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
12 changes: 9 additions & 3 deletions mapbox_vector_tile/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def addFeatures(self, features, layer_name='',
self.val_idx = 0
self.seen_keys_idx = {}
self.seen_values_idx = {}
self.seen_values_bool_idx = {}

for feature in features:

Expand Down Expand Up @@ -284,8 +285,13 @@ def _handle_attr(self, layer, feature, props):

feature.tags.append(self.seen_keys_idx[k])

if v not in self.seen_values_idx:
self.seen_values_idx[v] = self.val_idx
if isinstance(v, bool):
values_idx = self.seen_values_bool_idx
else:
values_idx = self.seen_values_idx

if v not in values_idx:
values_idx[v] = self.val_idx
self.val_idx += 1

val = layer.values.add()
Expand All @@ -303,4 +309,4 @@ def _handle_attr(self, layer, feature, props):
elif isinstance(v, float):
val.double_value = v

feature.tags.append(self.seen_values_idx[v])
feature.tags.append(values_idx[v])
23 changes: 23 additions & 0 deletions tests/test_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,29 @@ def test_too_small_linestring(self):
features = result['foo']['features']
self.assertEqual(0, len(features))

def test_encode_1_True_values(self):
geometry = 'POINT(0 0)'
properties = {
'foo': True,
'bar': 1,
}
source = [{
'name': 'layer',
'features': [{
'geometry': geometry,
'properties': properties
}]
}]
encoded = encode(source)
decoded = decode(encoded)
layer = decoded['layer']
features = layer['features']
act_props = features[0]['properties']
self.assertEquals(act_props['foo'], True)
self.assertEquals(act_props['bar'], 1)
self.assertTrue(isinstance(act_props['foo'], bool))
self.assertFalse(isinstance(act_props['bar'], bool))


class TestDictGeometries(BaseTestCase):

Expand Down

0 comments on commit e04415e

Please sign in to comment.