{"id":13784362,"url":"https://github.com/tilezen/mapbox-vector-tile","last_synced_at":"2025-05-16T05:08:14.133Z","repository":{"id":23463200,"uuid":"26827282","full_name":"tilezen/mapbox-vector-tile","owner":"tilezen","description":"Python package for encoding \u0026 decoding Mapbox Vector Tiles","archived":false,"fork":false,"pushed_at":"2024-05-31T16:13:57.000Z","size":553,"stargazers_count":255,"open_issues_count":22,"forks_count":48,"subscribers_count":33,"default_branch":"master","last_synced_at":"2025-05-10T01:06:21.938Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tilezen.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-11-18T19:57:49.000Z","updated_at":"2025-04-22T19:58:17.000Z","dependencies_parsed_at":"2024-10-12T14:01:31.979Z","dependency_job_id":"182c411f-577f-4078-b384-9f1ef166dc34","html_url":"https://github.com/tilezen/mapbox-vector-tile","commit_stats":{"total_commits":247,"total_committers":27,"mean_commits":9.148148148148149,"dds":0.7004048582995952,"last_synced_commit":"196582de0baec43d0b4bdaed53d9280a1b1afba7"},"previous_names":["mapzen/mapbox-vector-tile"],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tilezen%2Fmapbox-vector-tile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tilezen%2Fmapbox-vector-tile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tilezen%2Fmapbox-vector-tile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tilezen%2Fmapbox-vector-tile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tilezen","download_url":"https://codeload.github.com/tilezen/mapbox-vector-tile/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254471058,"owners_count":22076585,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T19:00:41.129Z","updated_at":"2025-05-16T05:08:09.116Z","avatar_url":"https://github.com/tilezen.png","language":"Python","funding_links":[],"categories":["Parsers \u0026 Generators"],"sub_categories":[],"readme":"# Mapbox Vector Tile\n\n[![CI](https://github.com/tilezen/mapbox-vector-tile/actions/workflows/ci.yml/badge.svg)](https://github.com/tilezen/mapbox-vector-tile/actions/workflows/ci.yml)\n[![pre-commit](https://github.com/tilezen/mapbox-vector-tile/actions/workflows/pre-commit.yml/badge.svg)](https://github.com/tilezen/mapbox-vector-tile/actions/workflows/pre-commit.yml)\n[![Coverage Status](https://coveralls.io/repos/github/tilezen/mapbox-vector-tile/badge.svg?branch=master)](https://coveralls.io/github/tilezen/mapbox-vector-tile?branch=master)\n\n## Installation\n\nmapbox-vector-tile is compatible with Python 3.9 or newer. It is listed on PyPi as `mapbox-vector-tile`. The\nrecommended way to install is via `pip`:\n\n```shell\npip install mapbox-vector-tile\n```\n\nAn extra dependency has been defined to install [`pyproj`](https://pyproj4.github.io/pyproj/stable/). This is useful\nwhen changing the Coordinate Reference System when encoding or decoding tiles.\n\n```shell\npip install mapbox-vector-tile[proj]\n```\n\n## Encoding\n\nEncode method expects an array of layers or at least a single valid layer. A valid layer is a dictionary with the\nfollowing keys\n\n- `name`: layer name\n- `features`: an array of features. A feature is a dictionary with the following keys:\n\n  - `geometry`: representation of the feature geometry in WKT, WKB, or a shapely geometry. Coordinates are relative to the tile, scaled in the range `[0, 4096)`. See below for example code to perform the necessary transformation. _Note_ that `GeometryCollection` types are not supported, and will trigger a `ValueError`.\n  - `properties`: a dictionary with a few keys and their corresponding values.\n\nThe encoding operation accepts options which can be defined per layer using the `per_layer_options` argument. If\nthere is missing layer or missing options values in the `per_layer_options`, the options of `default_options` are\ntaken into account. Finally, global default values are used. See the docstring of the `encode` method for more\ndetails about the available options and their global default values.\n\n```python\n\n  \u003e\u003e\u003e import mapbox_vector_tile\n\n  # Using WKT\n  \u003e\u003e\u003e mapbox_vector_tile.encode([\n      {\n        \"name\": \"water\",\n        \"features\": [\n          {\n            \"geometry\":\"POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))\",\n            \"properties\":{\n              \"uid\":123,\n              \"foo\":\"bar\",\n              \"cat\":\"flew\"\n            }\n          }\n        ]\n      },\n      {\n        \"name\": \"air\",\n        \"features\": [\n          {\n            \"geometry\":\"LINESTRING(159 3877, -1570 3877)\",\n            \"properties\":{\n              \"uid\":1234,\n              \"foo\":\"bar\",\n              \"cat\":\"flew\"\n            }\n          }\n        ]\n      }\n    ])\n\n  b'\\x1aH\\n\\x05water\\x12\\x18\\x12\\x06\\x00\\x00\\x01\\x01\\x02\\x02\\x18\\x03\"\\x0c\\t\\x00\\x80@\\x1a\\x00\\x01\\x02\\x00\\x00\\x02\\x0f\\x1a\\x03uid\\x1a\\x03foo\\x1a\\x03cat\"\\x02 {\"\\x05\\n\\x03bar\"\\x06\\n\\x04flew(\\x80 x\\x02\\x1aD\\n\\x03air\\x12\\x15\\x12\\x06\\x00\\x00\\x01\\x01\\x02\\x02\\x18\\x02\"\\t\\t\\xbe\\x02\\xb6\\x03\\n\\x81\\x1b\\x00\\x1a\\x03uid\\x1a\\x03foo\\x1a\\x03cat\"\\x03 \\xd2\\t\"\\x05\\n\\x03bar\"\\x06\\n\\x04flew(\\x80 x\\x02'\n\n\n  # Using WKB\n  \u003e\u003e\u003e mapbox_vector_tile.encode([\n      {\n        \"name\": \"water\",\n        \"features\": [\n          {\n            \"geometry\":b\"\\x01\\x03\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x05\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\",\n            \"properties\":{\n              \"uid\":123,\n              \"foo\":\"bar\",\n              \"cat\":\"flew\"\n            }\n          }\n        ]\n      },\n      {\n        \"name\": \"air\",\n        \"features\": [\n          {\n            \"geometry\":b\"\\x01\\x03\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x05\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\",\n            \"properties\":{\n              \"uid\":1234,\n              \"foo\":\"bar\",\n              \"cat\":\"flew\"\n            }\n          }\n        ]\n      }\n      ])\n\n    b'\\x1aH\\n\\x05water\\x12\\x18\\x12\\x06\\x00\\x00\\x01\\x01\\x02\\x02\\x18\\x03\"\\x0c\\t\\x00\\x80@\\x1a\\x00\\x01\\x02\\x00\\x00\\x02\\x0f\\x1a\\x03uid\\x1a\\x03foo\\x1a\\x03cat\"\\x02 {\"\\x05\\n\\x03bar\"\\x06\\n\\x04flew(\\x80 x\\x02\\x1aG\\n\\x03air\\x12\\x18\\x12\\x06\\x00\\x00\\x01\\x01\\x02\\x02\\x18\\x03\"\\x0c\\t\\x00\\x80@\\x1a\\x00\\x01\\x02\\x00\\x00\\x02\\x0f\\x1a\\x03uid\\x1a\\x03foo\\x1a\\x03cat\"\\x03 \\xd2\\t\"\\x05\\n\\x03bar\"\\x06\\n\\x04flew(\\x80 x\\x02'\n```\n\n### Coordinate transformations for encoding\n\nThe encoder expects geometries either:\n\n1. In tile-relative coordinates, where the lower left corner is origin and values grow up and to the right, and the tile is 4096 pixels square. For example, `POINT(0 0)` is the lower left corner of the tile and `POINT(4096, 4096)` is the upper right corner of the tile. In this case, the library does no projection, and coordinates are encoded as-is.\n2. In another coordinate system, with the tile bounds given by the `quantize_bounds` parameter. In this case, the library will scale coordinates so that the `quantize_bounds` fit within the range (0, 4096) in both `x` and `y` directions. Aside than the affine transformation, the library does no other projection.\n\nIt is possible to control whether the tile is in a \"y down\" coordinate system by setting the parameter `y_coord_down=True` on the call to `encode()`. The default is \"y up\".\n\nIt is possible to control the tile extents (by default, 4096 as used in the examples above), by setting the `extents` parameter on the call to `encode()`. The default is 4096.\n\nIf you have geometries in longitude and latitude (EPSG:4326), you can convert to tile-based coordinates by first projecting to Spherical Mercator (EPSG:3857) and then computing the pixel location within the tile. This example code uses Django's included GEOS library to do the transformation for `LineString` objects:\n\n```python\n  SRID_SPHERICAL_MERCATOR = 3857\n\n  def linestring_in_tile(tile_bounds, line):\n      # `mapbox-vector-tile` has a hardcoded tile extent of 4096 units.\n      MVT_EXTENT = 4096\n      from django.contrib.gis.geos import LineString\n\n      # We need tile bounds in spherical mercator\n      assert tile_bounds.srid == SRID_SPHERICAL_MERCATOR\n\n      # And we need the line to be in a known projection so we can re-project\n      assert line.srid is not None\n      line.transform(SRID_SPHERICAL_MERCATOR)\n\n      (x0, y0, x_max, y_max) = tile_bounds.extent\n      x_span = x_max - x0\n      y_span = y_max - y0\n\n      tile_based_coords = []\n      for x_merc, y_merc in line:\n          tile_based_coord = (int((x_merc - x0) * MVT_EXTENT / x_span),\n                              int((y_merc - y0) * MVT_EXTENT / y_span))\n          tile_based_coords.append(tile_based_coord)\n      return LineString(*tile_based_coords)\n```\n\nThe tile bounds can be found with `mercantile`, so a complete usage example might look like this:\n\n```python\n  from django.contrib.gis.geos import LineString, Polygon\n  import mercantile\n  import mapbox_vector_tile\n\n  SRID_LNGLAT = 4326\n  SRID_SPHERICAL_MERCATOR = 3857\n\n  tile_xyz = (2452, 3422, 18)\n  tile_bounds = Polygon.from_bbox(mercantile.bounds(*tile_xyz))\n  tile_bounds.srid = SRID_LNGLAT\n  tile_bounds.transform(SRID_SPHERICAL_MERCATOR)\n\n  lnglat_line = LineString(((-122.1, 45.1), (-122.2, 45.2)), srid=SRID_LNGLAT)\n  tile_line = linestring_in_tile(tile_bounds, lnglat_line)\n  tile_pbf = mapbox_vector_tile.encode({\n    \"name\": \"my-layer\",\n    \"features\": [ {\n      \"geometry\": tile_line.wkt,\n      \"properties\": { \"stuff\": \"things\" },\n    } ]\n  })\n```\n\nNote that this example may not have anything visible within the tile when rendered. It's up to you to make sure you put the right data in the tile!\n\nAlso note that the spec allows the extents to be modified, even though they are often set to 4096 by convention. `mapbox-vector-tile` assumes an extent of 4096.\n\n```python\n  import mapbox_vector_tile\n  from pyproj import Transformer\n  from shapely.geometry import LineString\n\n  SRID_LNGLAT = 4326\n  SRID_SPHERICAL_MERCATOR = 3857\n  direct_transformer = Transformer.from_crs(crs_from=SRID_LNGLAT, crs_to=SRID_SPHERICAL_MERCATOR, always_xy=True)\n\n  lnglat_line = LineString(((-122.1, 45.1), (-122.2, 45.2)))\n\n  # Encode the tile\n  tile_pbf = mapbox_vector_tile.encode({\n    \"name\": \"my-layer\",\n    \"features\": [{\n      \"geometry\": lnglat_line.wkt,\n      \"properties\": {\"stuff\": \"things\"},\n    }]\n  },\n    default_options={\"transformer\": direct_transformer.transform})\n\n  # Decode the tile\n  reverse_transformer = Transformer.from_crs(crs_from=SRID_SPHERICAL_MERCATOR, crs_to=SRID_LNGLAT, always_xy=True)\n  mapbox_vector_tile.decode(tile=tile_pbf, default_options={\"transformer\": reverse_transformer.transform})\n\n  {\n      \"my-layer\": {\n          \"extent\": 4096,\n          \"version\": 1,\n          \"features\": [\n              {\n                  \"geometry\": {\n                      \"type\": \"LineString\",\n                      \"coordinates\": [\n                          [-122.10000156433787, 45.09999871982179],\n                          [-122.20000202176608, 45.20000292038091]\n                      ]\n                  },\n                  \"properties\": {\n                      \"stuff\": \"things\"\n                  },\n                  \"id\": 0,\n                  \"type\": \"Feature\"\n              }\n          ],\n          \"type\": \"FeatureCollection\"\n      }\n  }\n```\n\n### Quantization\n\nThe encoder also has options to quantize the data for you via the `quantize_bounds` option. When encoding, pass in the bounds in the form (minx, miny, maxx, maxy) and the coordinates will be scaled appropriately during encoding.\n\n```python\nmapbox_vector_tile.encode([\n      {\n        \"name\": \"water\",\n        \"features\": [\n          {\n            \"geometry\":\"POINT(15 15)\",\n            \"properties\":{\n              \"foo\":\"bar\",\n            }\n          }\n        ]\n      }\n    ], default_options={\"quantize_bounds\": (10.0, 10.0, 20.0, 20.0)})\n```\n\nIn this example, the coordinate that would get encoded would be (2048, 2048)\n\nAdditionally, if the data is already in a coordinate system with y values going down, the encoder supports an\noption, `y_coord_down`, that can be set to True. This will suppress flipping the y coordinate values during encoding.\n\n### Custom extents\n\nThe encoder also supports passing in custom extents. These will be passed through to the layer in the pbf, and honored during any quantization or y coordinate flipping.\n\n```python\nmapbox_vector_tile.encode([\n      {\n        \"name\": \"water\",\n        \"features\": [\n          {\n            \"geometry\":\"POINT(15 15)\",\n            \"properties\":{\n              \"foo\":\"bar\",\n            }\n          }\n        ]\n      }\n    ], default_options={\"quantize_bounds\": (0.0, 0.0, 10.0, 10.0), \"extents\":50})\n```\n\n## Decoding\n\nDecode method takes in a valid google.protobuf.message Tile and returns decoded string in the following format:\n\n```python\n  {\n    layername: {\n        'extent': 'integer layer extent'\n        'version': 'integer'\n        'features': [{\n          'geometry': 'list of points',\n          'properties': 'dictionary of key/value pairs',\n          'id': 'unique id for the given feature within the layer '\n          }, ...\n        ]\n    },\n    layername2: {\n      # ...\n    }\n  }\n```\n\nThe decoding operation accepts options which can be defined per layer using the `per_layer_options` argument. If\nthere is missing layer or missing options values in the `per_layer_options`, the options of `default_options` are\ntaken into account. Finally, global default values are used. See the docstring of the `decode` method for more\ndetails about the available options and their global default values.\n\n```python\n  \u003e\u003e\u003e import mapbox_vector_tile\n\n  \u003e\u003e\u003e mapbox_vector_tile.decode(b'\\x1aJ\\n\\x05water\\x12\\x1a\\x08\\x01\\x12\\x06\\x00\\x00\\x01\\x01\\x02\\x02\\x18\\x03\"\\x0c\\t\\x00\\x80@\\x1a\\x00\\x01\\x02\\x00\\x00\\x02\\x0f\\x1a\\x03foo\\x1a\\x03uid\\x1a\\x03cat\"\\x05\\n\\x03bar\"\\x02 {\"\\x06\\n\\x04flew(\\x80 x\\x02\\x1aY\\n\\x03air\\x12\\x1c\\x08\\x01\\x12\\x08\\x00\\x00\\x01\\x01\\x02\\x02\\x03\\x03\\x18\\x03\"\\x0c\\t\\x00\\x80@\\x1a\\x00\\x01\\x02\\x00\\x00\\x02\\x0f\\x1a\\x03foo\\x1a\\x03uid\\x1a\\x05balls\\x1a\\x03cat\"\\x05\\n\\x03bar\"\\x03 \\xd2\\t\"\\x05\\n\\x03foo\"\\x06\\n\\x04flew(\\x80 x\\x02')\n\n  {\n      \"water\": {\n          \"extent\": 4096,\n          \"version\": 2,\n          \"features\": [\n              {\n                  \"geometry\": {\n                      \"type\": \"Polygon\",\n                      \"coordinates\": [[[0,0],[0,1],[1,1],[1,0],[0,0]]]\n                  },\n                  \"properties\": {\n                      \"foo\": \"bar\",\n                      \"uid\": 123,\n                      \"cat\": \"flew\"\n                  },\n                  \"id\": 1,\n                  \"type\": \"Feature\"\n              }\n          ],\n          \"type\": \"FeatureCollection\"\n      },\n      \"air\": {\n          \"extent\": 4096,\n          \"version\": 2,\n          \"features\": [\n              {\n                  \"geometry\": {\n                      \"type\": \"Polygon\",\n                      \"coordinates\": [[[0,0],[0,1],[1,1],[1,0],[0,0]]]\n                  },\n                  \"properties\": {\n                      \"foo\": \"bar\",\n                      \"uid\": 1234,\n                      \"balls\": \"foo\",\n                      \"cat\": \"flew\"\n                  },\n                  \"id\": 1,\n                  \"type\": \"Feature\"\n              }\n          ],\n          \"type\": \"FeatureCollection\"\n      }\n  }\n\n```\n\nHere's how you might decode a tile from a file.\n\n```python\n  \u003e\u003e\u003e import mapbox_vector_tile\n  \u003e\u003e\u003e with open('tile.mvt', 'rb') as f:\n  \u003e\u003e\u003e     data = f.read()\n  \u003e\u003e\u003e decoded_data = mapbox_vector_tile.decode(data)\n  \u003e\u003e\u003e with open('out.txt', 'w') as f:\n  \u003e\u003e\u003e     f.write(repr(decoded_data))\n```\n\nThe `decode` function has a `geojson` option which enforces a GeoJson RFC7946 compatible result. Its default value\nis `True`. To enforce the behaviour of versions \u003c2.0.0, please use `geojson=False`.\n\n## Use native protobuf library for performance\n\nThe c++ implementation of the underlying protobuf library is more performant than the pure python one. Depending on your operating system, you might need to [compile the C++ library](https://github.com/google/protobuf/tree/master/python#c-implementation) or install it.\n\nSince May 6, 2022, the Python `profobuf` library is based on the udp library and thus, the generated Python code\nrequires `protoc` 3.19.0 or newer. Cf. [here](https://developers.google.com/protocol-buffers/docs/news/2022-05-06). On\ndebian Bullseye, the version of `protoc` available in the package registry is too old. Please install it from [protobuf\n+GitHub repository](https://github.com/protocolbuffers/protobuf/releases).\n\nTo compile the `proto` file, you have to enable two environnement variables BEFORE running your python program :\n\n    $  sudo apt-get install libprotoc9 libprotobuf9 protobuf-compiler python-protobuf\n\nThen, you'll have to enable two environnement variable BEFORE runing your python program :\n\n     $ export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp\n     $ export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION=2\n\nand then:\n\n    $ protoc -I=mapbox_vector_tile/Mapbox/ --python_out=mapbox_vector_tile/Mapbox/ mapbox_vector_tile/Mapbox/vector_tile.proto\n\n## Changelog\n\nClick [here](https://github.com/tilezen/mapbox-vector-tile/blob/master/CHANGELOG.md) to see what changed over time in various versions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftilezen%2Fmapbox-vector-tile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftilezen%2Fmapbox-vector-tile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftilezen%2Fmapbox-vector-tile/lists"}