Welcome to djangorestframework-mvt’s documentation!

System Requirements

Installation

pip install djangorestframework-mvt

Getting Started

In a project’s models.py file add a MVTManager to a model to be served as Mapbox Vector Tiles. For example:

from django.contrib.gis.db import models
from rest_framework_mvt.managers import MVTManager

class Example(models.Model):
    geom = models.PointField()
    my_column = models.CharField()
    objects = models.Manager()
    vector_tiles = MVTManager()

The geo_col keyword argument specifies the name of the PostGIS geometry typed column.

In the project’s urls.py, create a view linked to a model using a MVTManager with the mvt_view_factory function. For example:

from rest_framework_mvt.views import mvt_view_factory

urlpatterns = [
    path("api/v1/data/example.mvt/", mvt_view_factory(Example)),
]

The following requests should now be enabled:

GET api/v1/data/example.mvt?tile=1/0/0 HTTP/1.1
GET api/v1/data/example.mvt?tile=1/0/0&my_column=foo HTTP/1.1
GET api/v1/data/example.mvt?tile=1/0/0&my_column=foo&limit=10 HTTP/1.1
GET api/v1/data/example.mvt?tile=1/0/0&my_column=foo&limit=10&offset=10 HTTP/1.1

References

Source Documentation

class rest_framework_mvt.managers.MVTManager(*args, geo_col='geom', source_name=None, **kwargs)
Parameters
  • geo_col (str) – Column name with the geometry. The default is “geom”.

  • source_name (str) – Connection source to use. If not provided the app’s default connection is used.

intersect(bbox='', limit=-1, offset=0, filters={})
Parameters
  • bbox (str) – A string representing a bounding box, e.g., ‘-90,29,-89,35’.

  • limit (int) – Number of entries to include in the result. The default is -1 (includes all results).

  • offset (int) – Index to start collecting entries from. Index size is the limit size. The default is 0.

  • filters (dict) – The keys represent column names and the values represent column values to filter on.

Returns

Bytes representing a Google Protobuf encoded Mapbox Vector Tile. The vector tile will store each applicable row from the database as a feature. Applicable rows fall within the passed in bbox.

Return type

bytes

Raises

ValidationError – If filters include keys or values not accepted by the manager’s model.

Note

The sql execution follows the guidelines from Django below. As suggested, the executed query string does NOT contain quoted parameters.

https://docs.djangoproject.com/en/2.2/topics/db/sql/#performing-raw-queries

class rest_framework_mvt.views.BaseMVTView(**kwargs)

Base view for serving a model as a Mapbox Vector Tile given X/Y/Z tile constraints.

get(request, *args, **kwargs)
Parameters

request (rest_framework.request.Request) – Standard DRF request object

Returns

Standard DRF response object

Return type

rest_framework.response.Response

rest_framework_mvt.views.mvt_view_factory(model_class, geom_col='geom')

Creates an MVTView that serves Mapbox Vector Tiles for the given model and geom column.

Parameters
  • model_class (django.contrib.gis.db.models.Model) – A GeoDjango model

  • geom_col (str) – A string representing the column name containing PostGIS geometry types.

Returns

A subclass of rest_framework_mvt.views.MVTView with its geom_col and model set to the function’s passed in values.

Return type

rest_framework_mvt.views.MVTView