Django vector tile generation
Project description
Generate MapBox VectorTiles from GeoDjango models
With mapbox_vector_tile or directly with PostgreSQL/PostGIS 2.4+
Installation
Basic
pip install django-vectortiles
- Without any other option, use only vectortiles.postgis
- Ensure you have psycopg2 set and installed
If you don't want to use Postgis
pip install django-vectortiles[mapbox]
- This will incude mapbox_vector_tiles package and its dependencies
- Use only vectortiles.mapbox
Examples
- assuming you have django.contrib.gis in your INSTALLED_APPS and a gis compatible database backend
# in your app models.py
from django.contrib.gis.db import models
class Layer(models.Model):
name = models.CharField(max_length=250)
class Feature(models.Model):
geom = models.GeometryField(srid=4326)
name = models.CharField(max_length=250)
layer = models.ForeignKey(Layer, on_delete=models.CASCADE, related_name='features')
Simple model:
# in your view file
from django.views.generic import ListView
from vectortiles.postgis.views import MVTView
from yourapp.models import Feature
class FeatureTileView(MVTView, ListView):
model = Feature
vector_tile_layer_name = "features"
vector_tile_fields = ('other_field_to_include', )
# 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"),
...
]
Related model:
# in your view file
from django.views.generic import DetailView
from vectortiles.mixins import BaseVectorTileView
from vectortiles.postgis.views import MVTView
from yourapp.models import Layer
class LayerTileView(MVTView, DetailView):
model = Layer
vector_tile_fields = ('other_field_to_include', )
def get_vector_tile_layer_name(self):
return self.get_object().name
def get_vector_tile_queryset(self):
return self.get_object().features.all()
def get(self, request, *args, **kwargs):
self.object = self.get_object()
return BaseVectorTileView.get(self,request=request, z=kwargs.get('z'), x=kwargs.get('x'), y=kwargs.get('y'))
# in your urls file
from django.urls import path
from yourapp import views
urlpatterns = [
...
path('layer/<int:pk>/tile/<int:z>/<int:x>/<int:y>', views.LayerTileView.as_view(), name="layer-tile"),
...
]
Usage without PostgreSQL / PostGIS
Just import and use vectortiles.mapbox.view.MVTView instead of vectortiles.postgis.view.MVTView
Usage with DRF
django-vectortiles can be used with DRF, use right BaseMixin and action on viewsets, or directly a GET method in an APIView.
-> vectortiles.mapbox.mixins.MapboxBaseVectorTile and vectortiles.postgis.mixins.PostgisBaseVectorTile
Development
With docker and docker-compose
docker pull makinacorpus/geodjango:bionic-3.6
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
CHANGELOG
0.0.3 (2021-02-18)
- Delete useless Envelope transformation because django implicitly transform on intersects lookup (thanks to StefanBrand)
- Avoid useless queryset evaluation in some cases (thanks to StefanBrand)
0.0.2 (2021-02-12)
- Fix required 'fields' key in tilejson. Will be filled later
- Fix generated subquery to deal with DateField (thanks to StefanBrand)
0.0.1 (2020-10-22)
- First Release
- Generate Vector Tiles from django models
- in python
- with PostGIS
- Generate associated TileJSON
- Default views to handle Vector tiles and tilejson
- Generate Vector Tiles from django models
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
File details
Details for the file django-vectortiles-0.0.3.tar.gz.
File metadata
- Download URL: django-vectortiles-0.0.3.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.6.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ebe9c6876ea95540b6ab3729144d023dc77f4260bcb7ece5346e4bf8ad3744b3
|
|
| MD5 |
2241ae48c40552ad9219165a93635021
|
|
| BLAKE2b-256 |
9c256d528aba1fdd9b463804fa0678a0f6fc8127de490beede6155f399356f9a
|