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
Built Distribution
Close
Hashes for django-vectortiles-1.0.0b3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 867d3fe95143652ebc322dac2e0cbac1cde122cbc2bc6d673147c6712b6a6104 |
|
MD5 | 5ed1b201af561cbc14eae21b229533ca |
|
BLAKE2b-256 | e5ed3f0e5be386410812bccc443503de9a28ed5b97ba397918d76704b6b35bcd |
Close
Hashes for django_vectortiles-1.0.0b3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c62e79ad221a5020c152cf55eb46112eaaa13d458a18b2d84ab0175544081699 |
|
MD5 | fe1a60f2ae8bf932593ee3ed2b8f0afb |
|
BLAKE2b-256 | e28ec23b43b9cd943d01b8c15c636d325a54a57cda03553ca009f308ccf302ef |