• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tilezen / mapbox-vector-tile / 3981062280

pending completion
3981062280

push

github

GitHub
Slowly deprecate old encode and decode parameters (#129)

5 of 9 new or added lines in 1 file covered. (55.56%)

706 of 738 relevant lines covered (95.66%)

3.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.0
/mapbox_vector_tile/__init__.py
1
import warnings
4✔
2

3
from mapbox_vector_tile import decoder, encoder
4✔
4

5

6
def decode(tile, per_layer_options=None, default_options=None, **kwargs):
4✔
7
    """Decode the provided `tile`
8

9
    Args:
10
        tile:
11
            The tile to decode.
12

13
        per_layer_options:
14
            An optional dictionary containing per layer options. The keys are the layer names and the values are
15
            options as described further. If an option is missing for a layer or if a layer is missing, the values of
16
            `default_options` are taken first. Finally, the global default options are used.
17

18
        default_options:
19
            These options are taken for layers without entry in `per_layer_options`. For all missing options values,
20
            the global default values are taken.
21

22
    Returns:
23
        The decoded layers data.
24

25
    Notes:
26
        The possible options are:
27
            * `y_coord_down`: it suppresses flipping the y coordinate values during encoding when set to `True`.
28
            Default to `False`.
29
            * `transformer`: a function transforming the coordinates of geometry object. It takes two floats (`x`
30
            and `y`) as arguments and retrieves the transformed coordinates `x_transformed`, `y_transformed`. Default to
31
            `None`.
32
            * `geojson`: when set to `False`, the behaviour of mapbox-vector-tile version 1.* is used. When set
33
            to `False`, the retrieved dictionary is a valid geojson file. Default to `True`.
34
    """
35
    if kwargs:
4✔
NEW
36
        warnings.warn("`decode` signature has changed, use `default_options` instead", DeprecationWarning)
×
NEW
37
        default_options = {**kwargs, **(default_options or {})}
×
38
    vector_tile = decoder.TileData(pbf_data=tile, per_layer_options=per_layer_options, default_options=default_options)
4✔
39
    message = vector_tile.get_message()
4✔
40
    return message
4✔
41

42

43
def encode(layers, per_layer_options=None, default_options=None, **kwargs):
4✔
44
    """Encode the `layers` into a MVT tile.
45

46
    Args:
47
        layers:
48
            The layer data to encode.
49

50
        per_layer_options:
51
            An optional dictionary containing per layer options. The keys are the layer names and the values are
52
            options as described further. If an option is missing for a layer or if a layer is missing, the values of
53
            `default_options` are taken first. Finally, the global default options are used.
54

55
        default_options:
56
            These options are taken for layers without entry in `per_layer_options`. For all missing options values,
57
            the global default values are taken.
58

59
    Returns:
60
        The encoded tile.
61

62
    Notes:
63
        The possible options are:
64
            * `y_coord_down`: it suppresses flipping the y coordinate values during encoding when set to `True`.
65
            Default to `False`.
66
            * `transformer`: a function transforming the coordinates of geometry object. It takes two floats (`x`
67
            and `y`) as arguments and retrieves the transformed coordinates `x_transformed`, `y_transformed`. Default to
68
            `None`.
69
            * `quantize_bounds`: bounds in the form (minx, miny, maxx, maxy) used to scale coordinates during
70
            encoding. Default to `None`.
71
            * `extents`: extents of the tile which is passed through to the layer in the pbf, and honored during any
72
            quantization or y coordinate flipping. Default to 4096.
73
            * `on_invalid_geometry`: a function taking a shapely shape as argument and retrieving an optional
74
            valid geometry. Default to None. In the file `encoder.py`, three possible functions are defined:
75
                * `on_invalid_geometry_raise`: it raises an error if an invalid geometry exists.
76
                * `on_invalid_geometry_ignore`: it ignores the invalid geometry and replaces it with a `None`.
77
                * `on_invalid_geometry_make_valid`: it tries to make the geometry valid. If it fails, retrieves `None`.
78
            * `check_winding_order`: it forces the check of the winding order for polygons. Default to True.
79
            * `max_geometry_validate_tries`: the number of tries when trying to enforce the good winding order. Default
80
            to 5.
81
    """
82
    if kwargs:
4✔
NEW
83
        warnings.warn("`encode` signature has changed, use `default_options` instead", DeprecationWarning)
×
NEW
84
        default_options = {**kwargs, **(default_options or {})}
×
85
    vector_tile = encoder.VectorTile(default_options=default_options)
4✔
86
    if per_layer_options is None:
4✔
87
        per_layer_options = dict()
4✔
88
    if isinstance(layers, list):
4✔
89
        for layer in layers:
4✔
90
            layer_name = layer["name"]
4✔
91
            layer_options = per_layer_options.get(layer_name, None)
4✔
92
            vector_tile.add_layer(features=layer["features"], name=layer_name, options=layer_options)
4✔
93
    else:
94
        layer_name = layers["name"]
4✔
95
        layer_options = per_layer_options.get(layer_name, None)
4✔
96
        vector_tile.add_layer(features=layers["features"], name=layer_name, options=layer_options)
4✔
97

98
    return vector_tile.tile.SerializeToString()
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc