Django vector tile generation
Project description
Generate MapBox VectorTiles from GeoDjango models
Directly with PostgreSQL/PostGIS 2.4+ or python native mapbox_vector_tile
Read full documentation
Installation
Basic
pip install django-vectortiles
- By default, postgis backend is enabled.
- Ensure you have psycopg2 set and installed
If you don't want to use Postgis and / or PostgreSQL
pip install django-vectortiles[python]
- This will incude mapbox_vector_tiles package and its dependencies
- Set VECTOR_TILES_BACKEND to "vectortiles.backends.python"
Examples
- 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)
Simple Example:
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):
layers = [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"),
...
]
Use TileJSON and multiple domains:
# in your view file
from django.urls import reverse
from vectortiles.views import TileJSONView
class FeatureTileJSONView(TileJSONView):
"""Simple model TileJSON View"""
name = "My features dataset"
attribution = "@JEC Data"
description = "My dataset"
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",
...
]
Usage without PostgreSQL / PostGIS
Just import and use vectortiles.mapbox.view.MVTView instead of vectortiles.postgis.view.MVTView
Usage with Django Rest Framework
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.
Development
With docker and docker-compose
docker-compose build
# docker-compose up
docker-compose run /code/venv/bin/python ./manage.py test
Local
- 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Close
Hashes for django-vectortiles-1.0.0b2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44071844e2a49e88f656c2473db9214b4fa73e42e2799f39b75976a51238ff4c |
|
MD5 | 38ad4c52ae3609815880fb740fa8dda6 |
|
BLAKE2b-256 | c7af70551fe52dc06b95be3f3d0c16c6485f26712a9324b281c1593daf1bd7bc |