From e04415e897471221513bdf82bf5b6880bbd3db28 Mon Sep 17 00:00:00 2001 From: Robert Marianski Date: Thu, 26 Oct 2017 15:40:20 -0400 Subject: [PATCH 1/2] Use a separate values index for booleans 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. --- mapbox_vector_tile/encoder.py | 12 +++++++++--- tests/test_encoder.py | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/mapbox_vector_tile/encoder.py b/mapbox_vector_tile/encoder.py index f9697e7..f2058d3 100644 --- a/mapbox_vector_tile/encoder.py +++ b/mapbox_vector_tile/encoder.py @@ -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: @@ -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() @@ -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]) diff --git a/tests/test_encoder.py b/tests/test_encoder.py index b54a945..e2b39c7 100644 --- a/tests/test_encoder.py +++ b/tests/test_encoder.py @@ -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): From 0f074370dab5b75d8c3ada4314ac5927654e202f Mon Sep 17 00:00:00 2001 From: Robert Marianski Date: Thu, 26 Oct 2017 16:26:58 -0400 Subject: [PATCH 2/2] Fix flake8 errors --- mapbox_vector_tile/encoder.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mapbox_vector_tile/encoder.py b/mapbox_vector_tile/encoder.py index f2058d3..5f8c1ba 100644 --- a/mapbox_vector_tile/encoder.py +++ b/mapbox_vector_tile/encoder.py @@ -207,10 +207,10 @@ def _load_geometry(self, geometry_spec): try: return load_wkb(geometry_spec) - except: + except Exception: try: return load_wkt(geometry_spec) - except: + except Exception: return None def addFeature(self, feature, shape, y_coord_down):