Skip to content

Commit

Permalink
Merge pull request #97 from tilezen/separate-bool-vals-idx
Browse files Browse the repository at this point in the history
Use a separate values index for booleans
  • Loading branch information
rmarianski authored Oct 27, 2017
2 parents 5f54ef2 + 0f07437 commit 879569a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
16 changes: 11 additions & 5 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 @@ -206,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):
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 879569a

Please sign in to comment.