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",
...
]
#### Development
##### With docker and docker-compose
```bash
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.0b1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b7a25b50c8e216c2776653f5621699f3a4c2db59ca2af3c755ea2f2c70faf87 |
|
MD5 | 5bfabf518363c4a7125815cd32e58030 |
|
BLAKE2b-256 | bb9a19b5cc51d421654b070e96f07c3934513df8bdc66f03e47a327543b1398a |