pip install django-vectortiles
- By default, postgis backend is enabled.
- Ensure you have psycopg2 set and installed
pip install django-vectortiles[python]
- This will incude mapbox_vector_tiles package and its dependencies
- Set VECTOR_TILES_BACKEND to "vectortiles.backends.python"
- assuming you have
django.contrib.gis
in yourINSTALLED_APPS
and a gis compatible database backend
# in your app models.py
from django.contrib.gis.db import models
class Feature(models.Model):
geom = models.GeometryField(srid=4326)
name = models.CharField(max_length=250)
from yourapp.models import Feature
# in a vector_layers.py file
from vectortiles import VectorLayer
class FeatureVectorLayer(VectorLayer):
model = Feature
vector_tile_layer_name = "features"
vector_tile_fields = ("name",)
# in your view file
from yourapp.vector_layers import FeatureVectorLayer
from vectortiles.views import MVTView
class FeatureTileView(MVTView):
layer_classes = [FeatureVectorLayer]
# in your urls file
from django.urls import path
from yourapp import views
urlpatterns = [
...
path('tiles/<int:z>/<int:x>/<int:y>', views.FeatureTileView.as_view(), name="feature-tile"),
...
]
# in your view file
from django.urls import reverse
from vectortiles.views import TileJSONView
from yourapp.vector_layers import FeatureVectorLayer
class FeatureTileJSONView(TileJSONView):
"""Simple model TileJSON View"""
name = "My features dataset"
attribution = "@JEC Data"
description = "My dataset"
layer_classes = [FeatureVectorLayer]
def get_tile_url(self):
""" Base MVTView Url used to generates urls in TileJSON in a.tiles.xxxx/{z}/{x}/{y} format """
return str(reverse("feature-tile", args=(0, 0, 0))).replace("0/0/0", "{z}/{x}/{y}")
# in your urls file
from django.urls import path
from yourapp import views
urlpatterns = [
...
path('tiles/<int:z>/<int:x>/<int:y>', views.FeatureTileView.as_view(), name="feature-tile"),
path("feature/tiles.json", views.FeatureTileJSONView.as_view(), name="feature-tilejson"),
...
]
]
# in your settings file
ALLOWED_HOSTS = [
"a.tiles.xxxx",
"b.tiles.xxxx",
"c.tiles.xxxx",
...
]
VECTOR_TILES_URLS = [
"https://a.tiles.xxxx",
"https://b.tiles.xxxx",
"https://c.tiles.xxxx",
]
Just import and use vectortiles.mapbox.view.MVTView instead of vectortiles.postgis.view.MVTView
django-vectortiles can be used with DRF if renderer_classes
of the view is overridden (see DRF docs). Simply use the right BaseMixin and action on viewsets, or directly a GET method in an APIView. See documentation for more details.
Copy .env.dist
to .env
and fill SECRET_KEY
and POSTGRES_PASSWORD
docker-compose build
# docker-compose up
docker-compose run /code/venv/bin/python ./manage.py test
- Install python and django requirements (python 3.6+, django 2.2+)
- Install geodjango requirements
- Have a postgresql / postgis 2.4+ enabled database
- Use a virtualenv
pip install .[dev] -U