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

Use a separate values index for booleans #97

Merged
merged 2 commits into from
Oct 27, 2017
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
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