Skip to content

Commit

Permalink
Add tests for optimise
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit9126 committed Jan 10, 2023
1 parent 2dfeb11 commit eda6b8e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
8 changes: 4 additions & 4 deletions mapbox_vector_tile/optimise.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import namedtuple

from mapbox_vector_tile.Mapbox.vector_tile_pb2 import tile
from mapbox_vector_tile.utils import CMD_LINE_TO, CMD_MOVE_TO, zig_zag_decode, zig_zag_encode
from mapbox_vector_tile.Mapbox import vector_tile_pb2 as vector_tile
from mapbox_vector_tile.utils import CMD_LINE_TO, CMD_MOVE_TO, LINESTRING, zig_zag_decode, zig_zag_encode


class StringTableOptimiser:
Expand Down Expand Up @@ -208,15 +208,15 @@ def optimise_tile(tile_bytes):
multilinestrings to save a few bytes.
"""

t = tile()
t = vector_tile.tile()
t.ParseFromString(tile_bytes)

for layer in t.layers:
sto = StringTableOptimiser()

for feature in layer.features:
# (multi)linestrings only
if feature.type == 2:
if feature.type == LINESTRING:
optimise_multilinestring(feature.geometry)

sto.add_tags(feature.tags)
Expand Down
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ line-length = 120
fast = true
extend-exclude = '''
(
/(
mapbox_vector_tile\/Mapbox\/vector_tile_pb2.py
)/
.*_pb2.py
)
'''
target-version = ["py37", "py38", "py39", "py310", "py311"]
Expand Down
65 changes: 65 additions & 0 deletions tests/test_optimise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import unittest

import mapbox_vector_tile
from mapbox_vector_tile.Mapbox import vector_tile_pb2 as vector_tile
from mapbox_vector_tile.optimise import optimise_tile


class BaseTestCase(unittest.TestCase):
def test_string_table_optimizer(self):
tile_data = mapbox_vector_tile.encode(
{
"name": "water",
"features": [
{"geometry": "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))", "properties": {"uid": 123, "foo": "bar"}},
{"geometry": "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))", "properties": {"uid": 124, "foo": "bar"}},
{
"geometry": "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))",
"properties": {"uid": 125, "cat": "flew", "foo": "bar"},
},
],
}
)
# Check the tags in the encoded tile
tile = vector_tile.tile()
tile.ParseFromString(tile_data)
tile_layer = tile.layers[0]
expected_tile_tags = ([0, 0, 1, 1], [0, 2, 1, 1], [0, 3, 2, 4, 1, 1])
for i, f in enumerate(tile_layer.features):
self.assertEqual(f.tags, expected_tile_tags[i])

# Optimise it
result = optimise_tile(tile_data)

# Check that the tags have been modified
result_tile = vector_tile.tile()
result_tile.ParseFromString(result)
result_layer = result_tile.layers[0]
expected_results = ([1, 4, 0, 0], [1, 3, 0, 0], [1, 2, 2, 1, 0, 0])
for i, f in enumerate(result_layer.features):
self.assertEqual(f.tags, expected_results[i])

def test_optimise_linestring(self):
# No particular optimization as only a linestring
tile_data = mapbox_vector_tile.encode(
{"name": "water", "features": [{"geometry": "LINESTRING (0 0, 0 1, 1 1, 1 0, 0 0)", "properties": {}}]}
)
result = optimise_tile(tile_data)
self.assertEqual(tile_data, result)

# Multilinestring to optimize
tile_data = mapbox_vector_tile.encode(
{
"name": "water",
"features": [
{"geometry": "MULTILINESTRING ((0 0, 0 1, 1 1), (2 2, 3 2), (1 1, 2 2))", "properties": {}}
],
}
)
result = optimise_tile(tile_data)
decoded_result = mapbox_vector_tile.decode(result)
decoded_geometry = decoded_result["water"]["features"][0]["geometry"]
self.assertEqual(decoded_geometry["type"], "MultiLineString")
self.assertEqual(
decoded_geometry["coordinates"], [[[0, 0], [0, 1], [1, 1]], [[1, 1], [2, 2]], [[2, 2], [3, 2]]]
)

0 comments on commit eda6b8e

Please sign in to comment.