Skip to content

Commit

Permalink
Add tests for optimise (#128)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Marianski <rob@marianski.com>
  • Loading branch information
benoit9126 and rmarianski authored Jan 11, 2023
1 parent f4ef1b0 commit 18232a4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
4 changes: 2 additions & 2 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 import vector_tile_pb2 as vector_tile
from mapbox_vector_tile.utils import CMD_LINE_TO, CMD_MOVE_TO, zig_zag_decode, zig_zag_encode
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 @@ -216,7 +216,7 @@ def optimise_tile(tile_bytes):

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 = ["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 18232a4

Please sign in to comment.